-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_optional.cpp
243 lines (206 loc) · 5.84 KB
/
test_optional.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "optional.hpp"
#include <algorithm>
#include <gtest/gtest.h>
using namespace extalloc;
namespace {
class value {
public:
explicit value (int v)
: v_{new int(v)} {}
~value () noexcept = default;
value (value const & other);
value (value && other) noexcept = default;
value & operator= (value const & other);
value & operator= (value && other) noexcept = default;
bool operator== (value const & rhs) const { return this->get () == rhs.get (); }
int get () const { return *v_; }
private:
std::unique_ptr<int> v_;
};
value::value (value const & other)
: v_{new int (other.get ())} {}
value & value::operator= (value const & other) {
if (this != &other) {
v_.reset (new int (other.get ()));
}
return *this;
}
} // end anonymous namespace
TEST (Optional, NoValue) {
optional<value> m;
EXPECT_FALSE (m.operator bool ());
EXPECT_FALSE (m.has_value ());
EXPECT_FALSE (m);
}
TEST (Optional, Value) {
optional<value> m (42);
EXPECT_TRUE (m.has_value ());
EXPECT_TRUE (m);
EXPECT_EQ (m.value (), value (42));
m.reset ();
EXPECT_FALSE (m.has_value ());
}
TEST (Optional, CtorWithOptionalHoldingAValue) {
optional<value> m1 (42);
optional<value> m2 = m1;
EXPECT_TRUE (m2.has_value ());
EXPECT_TRUE (m2);
EXPECT_EQ (m2.value (), value (42));
}
TEST (Optional, ValueOr) {
optional<value> m1;
EXPECT_EQ (m1.value_or (value{37}), value{37});
optional<value> m2 (5);
EXPECT_EQ (m2.value_or (value{37}), value{5});
optional<value> m3 (7);
EXPECT_EQ (m2.value_or (value{37}), value{5});
}
TEST (Optional, AssignValue) {
optional<value> m;
// First assignment, m has no value
m.operator=(value{43});
EXPECT_TRUE (m.has_value ());
EXPECT_TRUE (m);
EXPECT_EQ (m.value (), value{43});
// Second assignment, m holds a value
m.operator=(value{44});
EXPECT_TRUE (m.has_value ());
EXPECT_TRUE (m);
EXPECT_EQ (m.value (), value{44});
// Third assignment, m holds a value, assigning nothing.
m.operator= (optional<value> ());
EXPECT_FALSE (m.has_value ());
}
TEST (Optional, AssignZero) {
optional<char> m;
m = char{0};
EXPECT_TRUE (m.operator bool ());
EXPECT_TRUE (m.has_value ());
EXPECT_EQ (m.value (), 0);
EXPECT_EQ (*m, 0);
optional<char> m2 = m;
EXPECT_TRUE (m2.has_value ());
EXPECT_EQ (m2.value (), 0);
EXPECT_EQ (*m2, 0);
}
TEST (Optional, MoveCtor) {
{
optional<std::string> m1{std::string{"test"}};
EXPECT_TRUE (m1.has_value ());
EXPECT_EQ (m1.value (), "test");
optional<std::string> m2 = std::move (m1);
EXPECT_TRUE (m2.has_value ());
EXPECT_EQ (*m2, "test");
}
{
// Now move to a optional<> with no initial value.
optional<std::string> m3;
optional<std::string> m4 = std::move (m3);
EXPECT_FALSE (m4.has_value ());
}
}
TEST (Optional, MoveAssign) {
// No initial value
{
optional<std::string> m1;
EXPECT_FALSE (m1);
m1 = std::string{"test"};
EXPECT_TRUE (m1.has_value ());
EXPECT_EQ (m1.value (), std::string{"test"});
}
// With an initial value
{
optional<std::string> m2{"before"};
EXPECT_TRUE (m2);
optional<std::string> m3{"after"};
EXPECT_TRUE (m2);
m2 = std::move (m3);
EXPECT_TRUE (m2.has_value ());
EXPECT_TRUE (m3.has_value ()); // a moved-from optional still contains a value.
EXPECT_EQ (m2.value (), std::string{"after"});
}
}
TEST (Optional, CopyAssign) {
// both lhs and rhs have no value.
{
optional<std::string> m1;
optional<std::string> const m2{};
m1.operator= (m2);
EXPECT_FALSE (m1.has_value ());
}
// lhs with no value, rhs with value.
{
optional<std::string> m1;
optional<std::string> const m2 ("test");
m1.operator= (m2);
ASSERT_TRUE (m1.has_value ());
EXPECT_EQ (m1.value (), std::string{"test"});
}
// lhs with value, rhs with no value.
{
optional<std::string> m1 ("test");
optional<std::string> m2;
m1.operator= (m2);
EXPECT_FALSE (m1.has_value ());
}
// both lhs and rhs have a value.
{
optional<std::string> m1 ("original");
optional<std::string> m2 ("new");
m1.operator= (m2);
ASSERT_TRUE (m1.has_value ());
EXPECT_EQ (m1.value (), std::string{"new"});
}
}
TEST (Optional, SelfAssign) {
// self-assign with no value
{
optional<std::string> m1;
m1.operator= (m1);
EXPECT_FALSE (m1.has_value ());
}
// self-assign with a value
{
optional<std::string> m1 ("test");
m1.operator= (m1);
ASSERT_TRUE (m1.has_value ());
EXPECT_EQ (m1.value (), std::string{"test"});
}
}
TEST (Optional, Equal) {
{
// neither lhs nor rhs have a value
optional<int> m1;
optional<int> m2;
EXPECT_TRUE (m1 == m2);
EXPECT_FALSE (m1 != m2);
}
{
// lhs has a value, rhs not.
optional<int> m1 (3);
optional<int> m2;
EXPECT_FALSE (m1 == m2);
EXPECT_TRUE (m1 != m2);
}
{
// lhs has no value, rhs does.
optional<int> m1;
optional<int> m2 (5);
EXPECT_FALSE (m1 == m2);
EXPECT_TRUE (m1 != m2);
}
{
// both lhs and rhs have values but they are different.
optional<int> m1 (7);
optional<int> m2 (11);
EXPECT_FALSE (m1 == m2);
EXPECT_TRUE (m1 != m2);
}
{
// lhs both have the same value.
optional<int> m1 (13);
optional<int> m2 (13);
EXPECT_TRUE (m1 == m2);
EXPECT_FALSE (m1 != m2);
}
}