• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "aemu/base/Optional.h"
16 
17 
18 #include <gtest/gtest.h>
19 
20 #include <memory>
21 #include <vector>
22 
23 namespace android {
24 namespace base {
25 
TEST(Optional,TypeProperties)26 TEST(Optional, TypeProperties) {
27     // Making sure optional has the correct alignment and doesn't waste too much
28     // space
29 
30     static_assert(sizeof(Optional<bool>) == 2, "bad Optional<bool> size");
31     static_assert(std::alignment_of<Optional<bool>>::value ==
32                   std::alignment_of<bool>::value,
33                   "bad Optional<bool> alignment");
34 
35     static_assert(sizeof(Optional<char>) == 2, "bad Optional<char> size");
36     static_assert(std::alignment_of<Optional<char>>::value ==
37                   std::alignment_of<char>::value,
38                   "bad Optional<char> alignment");
39 
40     static_assert(sizeof(Optional<int16_t>) == 4, "bad Optional<int16_t> size");
41     static_assert(std::alignment_of<Optional<int16_t>>::value ==
42                   std::alignment_of<int16_t>::value,
43                   "bad Optional<int16_t> alignment");
44 
45     static_assert(sizeof(Optional<int32_t>) == 8, "bad Optional<int32_t> size");
46     static_assert(std::alignment_of<Optional<int32_t>>::value ==
47                   std::alignment_of<int32_t>::value,
48                   "bad Optional<int32_t> alignment");
49 
50     static_assert(sizeof(Optional<int64_t>) == 16,
51                   "bad Optional<int64_t> size");
52     static_assert(std::alignment_of<Optional<int64_t>>::value ==
53                   std::alignment_of<int64_t>::value,
54                   "bad Optional<int64_t> alignment");
55 
56     struct S128 {
57         int64_t data[2];
58     };
59 
60     static_assert(sizeof(Optional<S128>) == 3*sizeof(int64_t),
61                   "bad Optional<S128> size");
62     static_assert(std::alignment_of<Optional<S128>>::value ==
63                   std::alignment_of<S128>::value,
64                   "bad Optional<S128> alignment");
65 }
66 
TEST(Optional,ConstructFromValue)67 TEST(Optional, ConstructFromValue) {
68     {
69         Optional<int> o;
70         EXPECT_FALSE(o);
71     }
72     {
73         Optional<int> o = {};
74         EXPECT_FALSE(o);
75     }
76     {
77         Optional<int> o = kNullopt;
78         EXPECT_FALSE(o);
79     }
80     {
81         Optional<int> o(1);
82         EXPECT_TRUE(o);
83         EXPECT_EQ(1, *o);
84     }
85     {
86         // check the std::decay<> constructor
87         Optional<int> o = static_cast<const short&>(1);
88         EXPECT_TRUE(o);
89         EXPECT_EQ(1, *o);
90     }
91     {
92         Optional<int> o = 1;
93         EXPECT_TRUE(o);
94         EXPECT_EQ(1, *o);
95     }
96     {
97         Optional<int> o { 1 };
98         EXPECT_TRUE(o);
99         EXPECT_EQ(1, *o);
100     }
101     {
102         short val = 10;
103         Optional<int> o = val;
104         EXPECT_TRUE(o);
105         EXPECT_EQ(10, *o);
106     }
107     {
108         Optional<std::vector<int>> o(kInplace, 10);
109         EXPECT_TRUE(o);
110         EXPECT_EQ((std::vector<int>(10)), *o);
111     }
112     {
113         Optional<std::vector<int>> o(kInplace, {1,2,3,4});
114         EXPECT_TRUE(o);
115         EXPECT_EQ((std::vector<int>{1,2,3,4}), *o);
116     }
117 }
118 
TEST(Optional,ConstructFromOptional)119 TEST(Optional, ConstructFromOptional) {
120     {
121         Optional<int> o = Optional<int>();
122         EXPECT_FALSE(o);
123     }
124     {
125         Optional<short> o2;
126         Optional<int> o(o2);
127         EXPECT_FALSE(o);
128     }
129     {
130         Optional<short> o2 = 42;
131         Optional<int> o(o2);
132         EXPECT_TRUE(o);
133         EXPECT_EQ(42, *o);
134     }
135     {
136         Optional<int> o(Optional<int>(1));
137         EXPECT_TRUE(o);
138         EXPECT_EQ(1, *o);
139     }
140     {
141         Optional<int> o2 = 2;
142         Optional<int> o = o2;
143         EXPECT_TRUE(o);
144         EXPECT_EQ(2, *o);
145     }
146     {
147         Optional<std::vector<int>> o2 = std::vector<int>{20, 30, 40};
148         Optional<std::vector<int>> o = o2;
149         EXPECT_TRUE(o);
150         EXPECT_EQ((std::vector<int>{20, 30, 40}), *o);
151     }
152 }
153 
TEST(Optional,Assign)154 TEST(Optional, Assign) {
155     {
156         Optional<int> o;
157         o = 1;
158         EXPECT_TRUE(o);
159         EXPECT_EQ(1, *o);
160 
161         o = 2;
162         EXPECT_TRUE(o);
163         EXPECT_EQ(2, *o);
164 
165         o = kNullopt;
166         EXPECT_FALSE(o);
167 
168         o = Optional<int>(10);
169         EXPECT_TRUE(o);
170         EXPECT_EQ(10, *o);
171 
172         Optional<int> o2;
173         o = o2;
174         EXPECT_FALSE(o);
175 
176         o = 2u;
177         EXPECT_TRUE(o);
178         EXPECT_EQ(2, *o);
179 
180         o = Optional<short>();
181         EXPECT_FALSE(o);
182 
183         o = Optional<short>(20);
184         EXPECT_TRUE(o);
185         EXPECT_EQ(20, *o);
186 
187         Optional<short> o3(200);
188         o = o3;
189         EXPECT_TRUE(o);
190         EXPECT_EQ(200, *o);
191 
192         o = {};
193         EXPECT_FALSE(o);
194 
195         // check the std::decay<> assignment
196         o = static_cast<const short&>(1);
197         EXPECT_TRUE(o);
198         EXPECT_EQ(1, *o);
199     }
200 }
201 
TEST(Optional,MakeOptional)202 TEST(Optional, MakeOptional) {
203     {
204         auto o = makeOptional(1);
205         static_assert(std::is_same<decltype(o), Optional<int>>::value,
206                       "Bad type deduction in makeOptional()");
207         EXPECT_TRUE(o);
208         EXPECT_EQ(1, *o);
209     }
210     {
211         auto o = makeOptional(std::vector<char>{'1', '2'});
212         static_assert(std::is_same<decltype(o), Optional<std::vector<char>>>::value,
213                       "Bad type deduction in makeOptional()");
214         EXPECT_TRUE(o);
215         EXPECT_EQ((std::vector<char>{'1', '2'}), *o);
216     }
217     {
218         // check std::decay<> in the factory function
219         auto o = makeOptional("String");
220         static_assert(std::is_same<decltype(o), Optional<const char*>>::value,
221                       "Bad type deduction in makeOptional()");
222         EXPECT_TRUE(o);
223         EXPECT_STREQ("String", *o);
224     }
225     {
226         auto o = makeOptional<std::string>("String");
227         static_assert(std::is_same<decltype(o), Optional<std::string>>::value,
228                       "Bad type deduction in makeOptional()");
229         EXPECT_TRUE(o);
230         EXPECT_STREQ("String", o->c_str());
231     }
232     {
233         auto o = makeOptional<std::string>(5, 'b');
234         static_assert(std::is_same<decltype(o), Optional<std::string>>::value,
235                       "Bad type deduction in makeOptional()");
236         EXPECT_TRUE(o);
237         EXPECT_STREQ("bbbbb", o->c_str());
238     }
239     {
240         auto o = makeOptional<std::string>();
241         static_assert(std::is_same<decltype(o), Optional<std::string>>::value,
242                       "Bad type deduction in makeOptional()");
243         EXPECT_TRUE(o);
244         EXPECT_STREQ("", o->c_str());
245     }
246 }
247 
TEST(Optional,Move)248 TEST(Optional, Move) {
249     auto o = makeOptional(std::unique_ptr<int>(new int(10)));
250     {
251         decltype(o) o2 = std::move(o);
252         EXPECT_TRUE(o);
253         EXPECT_TRUE(o2);
254         EXPECT_FALSE(bool(*o));
255         EXPECT_TRUE(bool(*o2));
256         EXPECT_EQ(10, **o2);
257 
258         decltype(o) o3;
259         o3 = std::move(o2);
260         EXPECT_TRUE(o2);
261         EXPECT_TRUE(o3);
262         EXPECT_FALSE(bool(*o2));
263         EXPECT_TRUE(bool(*o3));
264         EXPECT_EQ(10, **o3);
265 
266         o3 = std::move(o2);
267         EXPECT_TRUE(o2);
268         EXPECT_TRUE(o3);
269         EXPECT_FALSE(bool(*o2));
270         EXPECT_FALSE(bool(*o3));
271     }
272 
273     {
274         decltype(o) o1;
275         decltype(o) o2 = std::move(o1);
276         EXPECT_FALSE(o1);
277         EXPECT_FALSE(o2);
278 
279         o2 = std::move(o1);
280         EXPECT_FALSE(o1);
281         EXPECT_FALSE(o2);
282 
283         decltype(o) o3 {kInplace, new int(20)};
284         o3 = std::move(o1);
285         EXPECT_FALSE(o1);
286         EXPECT_FALSE(o3);
287     }
288 }
289 
TEST(Optional,Value)290 TEST(Optional, Value) {
291     auto o = makeOptional(1);
292     EXPECT_EQ(1, o.value());
293     EXPECT_EQ(1, o.valueOr(2));
294 
295     o = kNullopt;
296     EXPECT_EQ(2, o.valueOr(2));
297 }
298 
TEST(Optional,Clear)299 TEST(Optional, Clear) {
300     auto o = makeOptional(1);
301     o.clear();
302     EXPECT_FALSE(o);
303 
304     o.clear();
305     EXPECT_FALSE(o);
306 }
307 
TEST(Optional,Emplace)308 TEST(Optional, Emplace) {
309     auto o = makeOptional(std::vector<int>{1,2,3,4});
310     o.emplace(3, 1);
311     EXPECT_TRUE(o);
312     EXPECT_EQ((std::vector<int>{1,1,1}), *o);
313     EXPECT_EQ(3U, o->capacity());
314 
315     o.clear();
316     o.emplace({1,2});
317     EXPECT_TRUE(o);
318     EXPECT_EQ((std::vector<int>{1,2}), *o);
319     EXPECT_EQ(2U, o->capacity());
320 }
321 
TEST(Optional,Reset)322 TEST(Optional, Reset) {
323     auto o = makeOptional(std::vector<int>{1,2,3,4});
324     o.reset(std::vector<int>{4,3});
325     EXPECT_TRUE(o);
326     EXPECT_EQ((std::vector<int>{4,3}), *o);
327     EXPECT_EQ(2U, o->capacity());
328 
329     o.clear();
330     o.reset(std::vector<int>{1});
331     EXPECT_EQ((std::vector<int>{1}), *o);
332     EXPECT_EQ(1U, o->capacity());
333 }
334 
TEST(Optional,CompareEqual)335 TEST(Optional, CompareEqual) {
336     EXPECT_TRUE(makeOptional(1) == makeOptional(1));
337     EXPECT_TRUE(makeOptional(1) == 1);
338     EXPECT_TRUE(1 == makeOptional(1));
339     EXPECT_FALSE(makeOptional(1) == makeOptional(2));
340     EXPECT_FALSE(makeOptional(2) == 1);
341     EXPECT_FALSE(2 == makeOptional(1));
342     EXPECT_TRUE(makeOptional(1) != makeOptional(2));
343     EXPECT_TRUE(makeOptional(1) != 2);
344     EXPECT_TRUE(1 != makeOptional(2));
345 
346     EXPECT_FALSE(makeOptional(1) == kNullopt);
347     EXPECT_FALSE(makeOptional(1) == Optional<int>());
348     EXPECT_FALSE(kNullopt == makeOptional(1));
349     EXPECT_FALSE(Optional<int>() == makeOptional(1));
350     EXPECT_TRUE(makeOptional(1) != kNullopt);
351     EXPECT_TRUE(makeOptional(1) != Optional<int>());
352     EXPECT_TRUE(kNullopt != makeOptional(1));
353     EXPECT_TRUE(Optional<int>() != makeOptional(1));
354 
355     EXPECT_TRUE(kNullopt == Optional<int>());
356     EXPECT_TRUE(kNullopt == Optional<char*>());
357     EXPECT_FALSE(kNullopt != Optional<int>());
358     EXPECT_FALSE(kNullopt != Optional<char*>());
359     EXPECT_TRUE(Optional<int>() == Optional<int>());
360     EXPECT_FALSE(Optional<int>() != Optional<int>());
361 }
362 
TEST(Optional,CompareLess)363 TEST(Optional, CompareLess) {
364     EXPECT_TRUE(makeOptional(1) < makeOptional(2));
365     EXPECT_TRUE(1 < makeOptional(2));
366     EXPECT_TRUE(makeOptional(1) < 2);
367 
368     EXPECT_FALSE(makeOptional(1) < makeOptional(1));
369     EXPECT_FALSE(1 < makeOptional(1));
370     EXPECT_FALSE(makeOptional(1) < 1);
371     EXPECT_FALSE(makeOptional(2) < makeOptional(1));
372     EXPECT_FALSE(2 < makeOptional(1));
373     EXPECT_FALSE(makeOptional(2) < 1);
374 
375     EXPECT_TRUE(kNullopt < makeOptional(2));
376     EXPECT_TRUE(Optional<int>() < makeOptional(2));
377     EXPECT_TRUE(Optional<int>() < 2);
378     EXPECT_FALSE(makeOptional(2) < kNullopt);
379     EXPECT_FALSE(makeOptional(2) < Optional<int>());
380     EXPECT_FALSE(2 < Optional<int>());
381 
382     EXPECT_FALSE(kNullopt < Optional<int>());
383     EXPECT_FALSE(Optional<int>() < kNullopt);
384 }
385 
TEST(Optional,Destruction)386 TEST(Optional, Destruction) {
387     // create a reference counting class to check if we delete everything
388     // we've created
389     struct Track {
390         Track(int& val) : mVal(val) {
391             ++mVal.get();
392         }
393         Track(std::initializer_list<int*> vals) : mVal(**vals.begin()) {
394             ++mVal.get();
395         }
396         Track(const Track& other) : mVal(other.mVal) {
397             ++mVal.get();
398         }
399         Track(Track&& other) : mVal(other.mVal) {
400             ++mVal.get();
401         }
402         Track& operator=(const Track& other) {
403             --mVal.get();
404             mVal = other.mVal;
405             ++mVal.get();
406             return *this;
407         }
408         Track& operator=(Track&& other) {
409             --mVal.get();
410             mVal = other.mVal;
411             ++mVal.get();
412             return *this;
413         }
414 
415         ~Track() {
416             --mVal.get();
417         }
418 
419         std::reference_wrapper<int> mVal;
420     };
421 
422     int counter = 0;
423     {
424         auto o = makeOptional(Track(counter));
425         EXPECT_EQ(1, counter);
426     }
427     EXPECT_EQ(0, counter);
428 
429     {
430         auto o = makeOptional(Track(counter));
431         EXPECT_EQ(1, counter);
432         o.clear();
433         EXPECT_EQ(0, counter);
434     }
435     EXPECT_EQ(0, counter);
436 
437     {
438         auto o = makeOptional(Track(counter));
439         EXPECT_EQ(1, counter);
440         int counter2 = 0;
441         o.emplace(counter2);
442         EXPECT_EQ(0, counter);
443         EXPECT_EQ(1, counter2);
444         o = Track(counter);
445         EXPECT_EQ(1, counter);
446         EXPECT_EQ(0, counter2);
447 
448         auto o2 = o;
449         EXPECT_EQ(2, counter);
450         EXPECT_EQ(0, counter2);
451     }
452     EXPECT_EQ(0, counter);
453 
454     {
455         auto o = makeOptional(Track(counter));
456         auto o2 = std::move(o);
457         EXPECT_EQ(2, counter);
458         o = o2;
459         EXPECT_EQ(2, counter);
460     }
461     EXPECT_EQ(0, counter);
462 
463     int counter2 = 0;
464     {
465         Optional<Track> o;
466         o.emplace(counter);
467         EXPECT_EQ(1, counter);
468 
469         o.emplace(counter2);
470         EXPECT_EQ(0, counter);
471         EXPECT_EQ(1, counter2);
472     }
473     EXPECT_EQ(0, counter);
474     EXPECT_EQ(0, counter2);
475 
476     {
477         Optional<Track> o;
478         o.emplace({&counter});
479         EXPECT_EQ(1, counter);
480 
481         counter2 = 0;
482         o.emplace({&counter2});
483         EXPECT_EQ(0, counter);
484         EXPECT_EQ(1, counter2);
485     }
486     EXPECT_EQ(0, counter);
487     EXPECT_EQ(0, counter2);
488 }
489 
490 }  // namespace base
491 }  // namespace android
492