-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariantTests.cpp
109 lines (88 loc) · 2.27 KB
/
VariantTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**************************************************************************
* @file: VariantTests.cpp
* @brief:
*
* Copyright (c) 2022 O-Net Communications Inc.
* All rights reserved.
*************************************************************************/
#include <gtest/gtest.h>
#include <common/Variant.h>
using namespace cppbase;
struct simple_type
{
simple_type() {}
simple_type(const simple_type& other)
: moved(other.moved),
moved_from(other.moved_from)
{
}
simple_type(simple_type&& other)
: moved(true)
{
other.moved_from = true;
}
bool moved = false;
bool moved_from = false;
};
struct big_type : simple_type
{
big_type() {}
big_type(const big_type& other) : simple_type(other) {}
big_type(big_type&& other) : simple_type(std::forward<simple_type>(other)) {}
double value;
double value_2;
};
TEST(Variant, Constructors)
{
Variant a;
EXPECT_FALSE(a.IsValid());
EXPECT_FALSE((bool)a);
EXPECT_FALSE(a.GetType().is_valid());
Variant b = 42;
EXPECT_TRUE(b.IsValid());
EXPECT_TRUE((bool)b);
EXPECT_TRUE(b.GetType().is_valid());
EXPECT_TRUE(b.IsType<int>());
Variant c = b;
EXPECT_TRUE(c.IsValid());
EXPECT_TRUE((bool)c);
EXPECT_TRUE(c.GetType().is_valid());
EXPECT_TRUE(c.IsType<int>());
Variant d = std::move(b);
EXPECT_FALSE(b.IsValid());
EXPECT_TRUE(d.IsValid());
EXPECT_TRUE((bool)d);
EXPECT_TRUE(d.GetType().is_valid());
EXPECT_TRUE(d.IsType<int>());
}
TEST(Variant, BasicAssignment)
{
Variant a;
Variant b;
a = b;
EXPECT_FALSE(a.IsValid());
EXPECT_FALSE(a.GetType().is_valid());
a = std::move(b);
EXPECT_FALSE(a.IsValid());
EXPECT_FALSE(a.GetType().is_valid());
EXPECT_FALSE(b.IsValid());
EXPECT_FALSE(b.GetType().is_valid());
}
TEST(Variant, SimpleType)
{
simple_type obj;
Variant var(obj);
EXPECT_FALSE(obj.moved_from);
Variant var_2(std::move(obj));
EXPECT_TRUE(obj.moved_from);
EXPECT_TRUE(var_2.GetValue<simple_type>().moved);
}
TEST(Variant, BigType)
{
big_type obj;
Variant var(obj);
EXPECT_FALSE(obj.moved_from);
Variant var_2(std::move(obj));
EXPECT_TRUE(obj.moved_from);
EXPECT_TRUE(var_2.GetValue<simple_type>().moved);
}