1 #include <cstdint>
2 #include <functional>
3 #include <memory>
4 #include <string>
5 #include <type_traits>
6 
7 #include <gtest/gtest.h>
8 #include <pdx/rpc/variant.h>
9 
10 using namespace android::pdx;
11 using namespace android::pdx::rpc;
12 
13 namespace {
14 
15 struct BaseType {
BaseType__anon1fde347f0111::BaseType16   BaseType(int value) : value(value) {}
17   int value;
18 };
19 
20 struct DerivedType : BaseType {
DerivedType__anon1fde347f0111::DerivedType21   DerivedType(int value) : BaseType{value} {};
22 };
23 
24 template <typename T>
25 class TestType {
26  public:
TestType(const T & value)27   TestType(const T& value) : value_(value) {}
TestType(T && value)28   TestType(T&& value) : value_(std::move(value)) {}
29   TestType(const TestType&) = default;
30   TestType(TestType&&) = default;
31 
32   TestType& operator=(const TestType&) = default;
33   TestType& operator=(TestType&&) = default;
34 
get() const35   const T& get() const { return value_; }
take()36   T&& take() { return std::move(value_); }
37 
38  private:
39   T value_;
40 };
41 
42 template <typename T>
43 class InstrumentType {
44  public:
InstrumentType(const T & value)45   InstrumentType(const T& value) : value_(value) { constructor_count_++; }
InstrumentType(T && value)46   InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; }
InstrumentType(const InstrumentType & other)47   InstrumentType(const InstrumentType& other) : value_(other.value_) {
48     constructor_count_++;
49   }
InstrumentType(InstrumentType && other)50   InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) {
51     constructor_count_++;
52   }
InstrumentType(const TestType<T> & other)53   InstrumentType(const TestType<T>& other) : value_(other.get()) {
54     constructor_count_++;
55   }
InstrumentType(TestType<T> && other)56   InstrumentType(TestType<T>&& other) : value_(other.take()) {
57     constructor_count_++;
58   }
~InstrumentType()59   ~InstrumentType() { destructor_count_++; }
60 
operator =(const InstrumentType & other)61   InstrumentType& operator=(const InstrumentType& other) {
62     copy_assignment_count_++;
63     value_ = other.value_;
64     return *this;
65   }
operator =(InstrumentType && other)66   InstrumentType& operator=(InstrumentType&& other) {
67     move_assignment_count_++;
68     value_ = std::move(other.value_);
69     return *this;
70   }
71 
operator =(const TestType<T> & other)72   InstrumentType& operator=(const TestType<T>& other) {
73     copy_assignment_count_++;
74     value_ = other.get();
75     return *this;
76   }
operator =(TestType<T> && other)77   InstrumentType& operator=(TestType<T>&& other) {
78     move_assignment_count_++;
79     value_ = other.take();
80     return *this;
81   }
82 
constructor_count()83   static std::size_t constructor_count() { return constructor_count_; }
destructor_count()84   static std::size_t destructor_count() { return destructor_count_; }
move_assignment_count()85   static std::size_t move_assignment_count() { return move_assignment_count_; }
copy_assignment_count()86   static std::size_t copy_assignment_count() { return copy_assignment_count_; }
87 
get() const88   const T& get() const { return value_; }
take()89   T&& take() { return std::move(value_); }
90 
clear()91   static void clear() {
92     constructor_count_ = 0;
93     destructor_count_ = 0;
94     move_assignment_count_ = 0;
95     copy_assignment_count_ = 0;
96   }
97 
98  private:
99   T value_;
100 
101   static std::size_t constructor_count_;
102   static std::size_t destructor_count_;
103   static std::size_t move_assignment_count_;
104   static std::size_t copy_assignment_count_;
105 };
106 
107 template <typename T>
108 std::size_t InstrumentType<T>::constructor_count_ = 0;
109 template <typename T>
110 std::size_t InstrumentType<T>::destructor_count_ = 0;
111 template <typename T>
112 std::size_t InstrumentType<T>::move_assignment_count_ = 0;
113 template <typename T>
114 std::size_t InstrumentType<T>::copy_assignment_count_ = 0;
115 
116 }  // anonymous namespace
117 
TEST(Variant,Assignment)118 TEST(Variant, Assignment) {
119   // Assert basic type properties.
120   {
121     Variant<int, bool, float> v;
122     ASSERT_EQ(-1, v.index());
123     ASSERT_FALSE(v.is<int>());
124     ASSERT_FALSE(v.is<bool>());
125     ASSERT_FALSE(v.is<float>());
126   }
127 
128   {
129     Variant<int, bool, float> v;
130     v = 10;
131     ASSERT_EQ(0, v.index());
132     ASSERT_TRUE(v.is<int>());
133     ASSERT_FALSE(v.is<bool>());
134     ASSERT_FALSE(v.is<float>());
135     EXPECT_EQ(10, std::get<int>(v));
136   }
137 
138   {
139     Variant<int, bool, float> v;
140     v = false;
141     ASSERT_EQ(1, v.index());
142     ASSERT_FALSE(v.is<int>());
143     ASSERT_TRUE(v.is<bool>());
144     ASSERT_FALSE(v.is<float>());
145     EXPECT_EQ(false, std::get<bool>(v));
146   }
147 
148   {
149     Variant<int, bool, float> v;
150     v = 1.0f;
151     ASSERT_EQ(2, v.index());
152     ASSERT_FALSE(v.is<int>());
153     ASSERT_FALSE(v.is<bool>());
154     ASSERT_TRUE(v.is<float>());
155     EXPECT_FLOAT_EQ(1.0f, std::get<float>(v));
156   }
157 
158   {
159     Variant<int, bool, float> v;
160     // ERROR: More than one type is implicitly convertible from double.
161     // v = 1.0;
162     v = static_cast<float>(1.0);
163   }
164 
165   {
166     Variant<int, bool, float> v;
167 
168     double x = 1.1;
169     v = static_cast<float>(x);
170     ASSERT_EQ(2, v.index());
171     ASSERT_FALSE(v.is<int>());
172     ASSERT_FALSE(v.is<bool>());
173     ASSERT_TRUE(v.is<float>());
174     EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
175   }
176 
177   {
178     Variant<int, std::string> v;
179     ASSERT_EQ(-1, v.index());
180     ASSERT_FALSE(v.is<int>());
181     ASSERT_FALSE(v.is<std::string>());
182   }
183 
184   {
185     Variant<int, std::string> v;
186     v = 20;
187     ASSERT_EQ(0, v.index());
188     ASSERT_TRUE(v.is<int>());
189     ASSERT_FALSE(v.is<std::string>());
190     EXPECT_EQ(20, std::get<int>(v));
191   }
192 
193   {
194     Variant<int, std::string> v;
195     v = std::string("test");
196     ASSERT_EQ(1, v.index());
197     ASSERT_FALSE(v.is<int>());
198     ASSERT_TRUE(v.is<std::string>());
199     EXPECT_EQ("test", std::get<std::string>(v));
200   }
201 
202   {
203     Variant<int, std::string> v;
204     v = "test";
205     ASSERT_EQ(1, v.index());
206     ASSERT_FALSE(v.is<int>());
207     ASSERT_TRUE(v.is<std::string>());
208     EXPECT_EQ("test", std::get<std::string>(v));
209   }
210 
211   {
212     Variant<const char*> v1;
213     Variant<std::string> v2;
214 
215     v1 = "test";
216     ASSERT_TRUE(v1.is<const char*>());
217     v2 = v1;
218     ASSERT_TRUE(v2.is<std::string>());
219     EXPECT_EQ("test", std::get<std::string>(v2));
220   }
221 
222   {
223     Variant<int> a(1);
224     Variant<int> b;
225     ASSERT_TRUE(!a.empty());
226     ASSERT_TRUE(b.empty());
227 
228     a = b;
229     ASSERT_TRUE(a.empty());
230     ASSERT_TRUE(b.empty());
231   }
232 
233   {
234     Variant<int*, char*> v;
235 
236     // ERROR: More than one type is implicitly convertible from nullptr.
237     // v = nullptr;
238 
239     v = static_cast<int*>(nullptr);
240     EXPECT_TRUE(v.is<int*>());
241 
242     v = static_cast<char*>(nullptr);
243     EXPECT_TRUE(v.is<char*>());
244   }
245 
246   {
247     Variant<int*, char*> v;
248     int a = 10;
249     char b = 20;
250 
251     v = &b;
252     ASSERT_TRUE(v.is<char*>());
253     EXPECT_EQ(&b, std::get<char*>(v));
254     EXPECT_EQ(b, *std::get<char*>(v));
255 
256     v = &a;
257     ASSERT_TRUE(v.is<int*>());
258     EXPECT_EQ(&a, std::get<int*>(v));
259     EXPECT_EQ(a, *std::get<int*>(v));
260   }
261 
262   {
263     using IntRef = std::reference_wrapper<int>;
264     Variant<IntRef> v;
265     int a = 10;
266 
267     v = a;
268     ASSERT_TRUE(v.is<IntRef>());
269     EXPECT_EQ(a, std::get<IntRef>(v));
270 
271     a = 20;
272     EXPECT_EQ(a, std::get<IntRef>(v));
273   }
274 }
275 
TEST(Variant,MoveAssignment)276 TEST(Variant, MoveAssignment) {
277   {
278     Variant<std::string> v;
279     std::string s = "test";
280     v = std::move(s);
281 
282     EXPECT_TRUE(s.empty());
283     ASSERT_TRUE(v.is<std::string>());
284     EXPECT_EQ("test", std::get<std::string>(v));
285   }
286 
287   {
288     Variant<std::string> v("test");
289     std::string s = "fizz";
290     s = std::move(std::get<std::string>(v));
291 
292     ASSERT_TRUE(v.is<std::string>());
293     EXPECT_TRUE(std::get<std::string>(v).empty());
294     EXPECT_EQ("test", s);
295   }
296 
297   {
298     Variant<std::string> a("test");
299     Variant<std::string> b;
300 
301     b = std::move(a);
302     ASSERT_TRUE(a.is<std::string>());
303     ASSERT_TRUE(b.is<std::string>());
304     EXPECT_TRUE(std::get<std::string>(a).empty());
305     EXPECT_EQ("test", std::get<std::string>(b));
306   }
307 
308   {
309     Variant<std::string> a("test");
310     Variant<std::string> b("fizz");
311 
312     b = std::move(a);
313     ASSERT_TRUE(a.is<std::string>());
314     ASSERT_TRUE(b.is<std::string>());
315     EXPECT_TRUE(std::get<std::string>(a).empty());
316     EXPECT_EQ("test", std::get<std::string>(b));
317   }
318 
319   {
320     Variant<int, std::string> a("test");
321     Variant<int, std::string> b(10);
322 
323     b = std::move(a);
324     ASSERT_TRUE(a.is<std::string>());
325     ASSERT_TRUE(b.is<std::string>());
326     EXPECT_TRUE(std::get<std::string>(a).empty());
327     EXPECT_EQ("test", std::get<std::string>(b));
328   }
329 
330   {
331     Variant<int, std::string> a(10);
332     Variant<int, std::string> b("test");
333 
334     b = std::move(a);
335     ASSERT_TRUE(a.is<int>());
336     ASSERT_TRUE(b.is<int>());
337     EXPECT_EQ(10, std::get<int>(a));
338     EXPECT_EQ(10, std::get<int>(b));
339   }
340 }
341 
TEST(Variant,Constructor)342 TEST(Variant, Constructor) {
343   {
344     Variant<int, bool, float> v(true);
345     EXPECT_TRUE(v.is<bool>());
346   }
347 
348   {
349     Variant<int, bool, float> v(10);
350     EXPECT_TRUE(v.is<int>());
351   }
352 
353   {
354     Variant<int, bool, float> v(10.1f);
355     EXPECT_TRUE(v.is<float>());
356   }
357 
358   {
359     Variant<float, std::string> v(10.);
360     EXPECT_TRUE(v.is<float>());
361   }
362 
363   {
364     TestType<int> i(1);
365     Variant<int, bool, float> v(i.take());
366     ASSERT_TRUE(v.is<int>());
367     EXPECT_EQ(1, std::get<int>(v));
368   }
369 
370   {
371     TestType<int> i(1);
372     Variant<int, bool, float> v(i.get());
373     ASSERT_TRUE(v.is<int>());
374     EXPECT_EQ(1, std::get<int>(v));
375   }
376 
377   {
378     TestType<bool> b(true);
379     Variant<int, bool, float> v(b.take());
380     ASSERT_TRUE(v.is<bool>());
381     EXPECT_EQ(true, std::get<bool>(v));
382   }
383 
384   {
385     TestType<bool> b(true);
386     Variant<int, bool, float> v(b.get());
387     ASSERT_TRUE(v.is<bool>());
388     EXPECT_EQ(true, std::get<bool>(v));
389   }
390 
391   {
392     Variant<const char*> c("test");
393     Variant<std::string> s(c);
394     ASSERT_TRUE(s.is<std::string>());
395     EXPECT_EQ("test", std::get<std::string>(s));
396   }
397 
398   {
399     Variant<int, bool, float> a(true);
400     Variant<int, bool, float> b(a);
401 
402     ASSERT_TRUE(b.is<bool>());
403   }
404 
405   {
406     using IntRef = std::reference_wrapper<int>;
407     int a = 10;
408     Variant<IntRef> v(a);
409     TestType<IntRef> t(a);
410 
411     ASSERT_TRUE(v.is<IntRef>());
412     EXPECT_EQ(a, std::get<IntRef>(v));
413     EXPECT_EQ(a, t.get());
414 
415     a = 20;
416     EXPECT_EQ(a, std::get<IntRef>(v));
417     EXPECT_EQ(a, t.get());
418   }
419 }
420 
421 // Verify correct ctor/dtor and assignment behavior used an instrumented type.
TEST(Variant,CopyMoveConstructAssign)422 TEST(Variant, CopyMoveConstructAssign) {
423   {
424     InstrumentType<int>::clear();
425 
426     // Default construct to empty, no InstrumentType activity.
427     Variant<int, InstrumentType<int>> v;
428     ASSERT_EQ(0u, InstrumentType<int>::constructor_count());
429     ASSERT_EQ(0u, InstrumentType<int>::destructor_count());
430     ASSERT_EQ(0u, InstrumentType<int>::move_assignment_count());
431     ASSERT_EQ(0u, InstrumentType<int>::copy_assignment_count());
432   }
433 
434   {
435     InstrumentType<int>::clear();
436 
437     // Construct from int type, no InstrumentType activity.
438     Variant<int, InstrumentType<int>> v;
439     v = 10;
440     EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
441     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
442     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
443     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
444   }
445 
446   {
447     InstrumentType<int>::clear();
448 
449     // Construct from int type, no InstrumentType activity.
450     Variant<int, InstrumentType<int>> v(10);
451     EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
452     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
453     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
454     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
455   }
456 
457   {
458     InstrumentType<int>::clear();
459 
460     // Construct from temporary, temporary ctor/dtor.
461     Variant<int, InstrumentType<int>> v;
462     v = InstrumentType<int>(25);
463     EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
464     EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
465     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
466     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
467   }
468 
469   {
470     InstrumentType<int>::clear();
471 
472     // Construct from temporary, temporary ctor/dtor.
473     Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
474     EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
475     EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
476     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
477     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
478   }
479 
480   {
481     InstrumentType<int>::clear();
482 
483     // Construct from temporary, temporary ctor/dtor.
484     Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
485 
486     // Assign from temporary, temporary ctor/dtor.
487     v = InstrumentType<int>(35);
488     EXPECT_EQ(3u, InstrumentType<int>::constructor_count());
489     EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
490     EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
491     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
492   }
493 
494   {
495     InstrumentType<int>::clear();
496 
497     // Construct from temporary, temporary ctor/dtor.
498     Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
499 
500     // dtor.
501     v = 10;
502     EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
503     EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
504     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
505     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
506   }
507 
508   {
509     InstrumentType<int>::clear();
510 
511     // Construct from temporary, temporary ctor/dtor.
512     Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
513 
514     EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
515     EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
516     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
517     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
518   }
519   EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
520   EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
521   EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
522   EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
523 
524   {
525     InstrumentType<int>::clear();
526 
527     // Construct from other temporary.
528     Variant<int, InstrumentType<int>> v(TestType<int>(10));
529     EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
530     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
531     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
532     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
533   }
534 
535   {
536     InstrumentType<int>::clear();
537 
538     // Construct from other temporary.
539     Variant<int, InstrumentType<int>> v(TestType<int>(10));
540     // Assign from other temporary.
541     v = TestType<int>(11);
542     EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
543     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
544     EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
545     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
546   }
547 
548   {
549     InstrumentType<int>::clear();
550 
551     // Construct from other temporary.
552     Variant<int, InstrumentType<int>> v(TestType<int>(10));
553     // Assign from empty Variant.
554     v = Variant<int, InstrumentType<int>>();
555     EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
556     EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
557     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
558     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
559   }
560 
561   {
562     InstrumentType<int>::clear();
563 
564     TestType<int> other(10);
565     // Construct from other.
566     Variant<int, InstrumentType<int>> v(other);
567 
568     EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
569     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
570     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
571     EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
572   }
573 
574   {
575     InstrumentType<int>::clear();
576 
577     // Construct from other temporary.
578     Variant<int, InstrumentType<int>> v(TestType<int>(0));
579     TestType<int> other(10);
580     // Assign from other.
581     v = other;
582     EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
583     EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
584     EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
585     EXPECT_EQ(1u, InstrumentType<int>::copy_assignment_count());
586   }
587 }
588 
TEST(Variant,MoveConstructor)589 TEST(Variant, MoveConstructor) {
590   {
591     std::unique_ptr<int> pointer = std::make_unique<int>(10);
592     Variant<std::unique_ptr<int>> v(std::move(pointer));
593     ASSERT_TRUE(v.is<std::unique_ptr<int>>());
594     EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) != nullptr);
595     EXPECT_TRUE(pointer == nullptr);
596   }
597 
598   {
599     Variant<std::unique_ptr<int>> a(std::make_unique<int>(10));
600     Variant<std::unique_ptr<int>> b(std::move(a));
601 
602     ASSERT_TRUE(a.is<std::unique_ptr<int>>());
603     ASSERT_TRUE(b.is<std::unique_ptr<int>>());
604     EXPECT_TRUE(std::get<std::unique_ptr<int>>(a) == nullptr);
605     EXPECT_TRUE(std::get<std::unique_ptr<int>>(b) != nullptr);
606   }
607 }
608 
TEST(Variant,IndexOf)609 TEST(Variant, IndexOf) {
610   Variant<int, bool, float> v1;
611 
612   EXPECT_EQ(0, v1.index_of<int>());
613   EXPECT_EQ(1, v1.index_of<bool>());
614   EXPECT_EQ(2, v1.index_of<float>());
615 
616   Variant<int, bool, float, int> v2;
617 
618   EXPECT_EQ(0, v2.index_of<int>());
619   EXPECT_EQ(1, v2.index_of<bool>());
620   EXPECT_EQ(2, v2.index_of<float>());
621 }
622 
623 struct Visitor {
624   int int_value = 0;
625   bool bool_value = false;
626   float float_value = 0.0;
627   bool empty_value = false;
628 
VisitVisitor629   void Visit(int value) { int_value = value; }
VisitVisitor630   void Visit(bool value) { bool_value = value; }
VisitVisitor631   void Visit(float value) { float_value = value; }
VisitVisitor632   void Visit(EmptyVariant) { empty_value = true; }
633 };
634 
TEST(Variant,Visit)635 TEST(Variant, Visit) {
636   {
637     Variant<int, bool, float> v(10);
638     EXPECT_TRUE(v.is<int>());
639 
640     Visitor visitor;
641     v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
642     EXPECT_EQ(10, visitor.int_value);
643 
644     visitor = {};
645     v = true;
646     v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
647     EXPECT_EQ(true, visitor.bool_value);
648   }
649 
650   {
651     Variant<int, bool, float> v;
652     EXPECT_EQ(-1, v.index());
653 
654     Visitor visitor;
655     v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
656     EXPECT_TRUE(visitor.empty_value);
657   }
658 
659   {
660     Variant<std::string> v("test");
661     ASSERT_TRUE(v.is<std::string>());
662     EXPECT_FALSE(std::get<std::string>(v).empty());
663 
664     v.Visit([](auto&& value) {
665       std::remove_reference_t<decltype(value)> empty;
666       std::swap(empty, value);
667     });
668     ASSERT_TRUE(v.is<std::string>());
669     EXPECT_TRUE(std::get<std::string>(v).empty());
670   }
671 }
672 
TEST(Variant,Become)673 TEST(Variant, Become) {
674   {
675     Variant<int, bool, float> v;
676 
677     v.Become(0);
678     EXPECT_TRUE(v.is<int>());
679 
680     v.Become(1);
681     EXPECT_TRUE(v.is<bool>());
682 
683     v.Become(2);
684     EXPECT_TRUE(v.is<float>());
685 
686     v.Become(3);
687     EXPECT_TRUE(v.empty());
688 
689     v.Become(-1);
690     EXPECT_TRUE(v.empty());
691 
692     v.Become(-2);
693     EXPECT_TRUE(v.empty());
694   }
695 
696   {
697     Variant<int, bool, float> v;
698 
699     v.Become(0, 10);
700     ASSERT_TRUE(v.is<int>());
701     EXPECT_EQ(10, std::get<int>(v));
702 
703     v.Become(1, true);
704     ASSERT_TRUE(v.is<bool>());
705     EXPECT_EQ(true, std::get<bool>(v));
706 
707     v.Become(2, 2.0f);
708     ASSERT_TRUE(v.is<float>());
709     EXPECT_FLOAT_EQ(2.0f, std::get<float>(v));
710 
711     v.Become(3, 10);
712     EXPECT_TRUE(v.empty());
713 
714     v.Become(-1, 10);
715     EXPECT_TRUE(v.empty());
716 
717     v.Become(-2, 20);
718     EXPECT_TRUE(v.empty());
719   }
720 
721   {
722     Variant<std::string> v;
723 
724     v.Become(0);
725     ASSERT_TRUE(v.is<std::string>());
726     EXPECT_TRUE(std::get<std::string>(v).empty());
727   }
728 
729   {
730     Variant<std::string> v;
731 
732     v.Become(0, "test");
733     ASSERT_TRUE(v.is<std::string>());
734     EXPECT_EQ("test", std::get<std::string>(v));
735   }
736 
737   {
738     Variant<std::string> v("foo");
739 
740     v.Become(0, "bar");
741     ASSERT_TRUE(v.is<std::string>());
742     EXPECT_EQ("foo", std::get<std::string>(v));
743   }
744 }
745 
TEST(Variant,Swap)746 TEST(Variant, Swap) {
747   {
748     Variant<std::string> a;
749     Variant<std::string> b;
750 
751     std::swap(a, b);
752     EXPECT_TRUE(a.empty());
753     EXPECT_TRUE(b.empty());
754   }
755 
756   {
757     Variant<std::string> a("1");
758     Variant<std::string> b;
759 
760     std::swap(a, b);
761     EXPECT_TRUE(!a.empty());
762     EXPECT_TRUE(!b.empty());
763     ASSERT_TRUE(b.is<std::string>());
764     EXPECT_EQ("1", std::get<std::string>(b));
765   }
766 
767   {
768     Variant<std::string> a;
769     Variant<std::string> b("1");
770 
771     std::swap(a, b);
772     EXPECT_TRUE(!a.empty());
773     EXPECT_TRUE(!b.empty());
774     ASSERT_TRUE(a.is<std::string>());
775     EXPECT_EQ("1", std::get<std::string>(a));
776   }
777 
778   {
779     Variant<std::string> a("1");
780     Variant<std::string> b("2");
781 
782     std::swap(a, b);
783     ASSERT_TRUE(a.is<std::string>());
784     ASSERT_TRUE(b.is<std::string>());
785     EXPECT_EQ("2", std::get<std::string>(a));
786     EXPECT_EQ("1", std::get<std::string>(b));
787   }
788 
789   {
790     Variant<int, std::string> a(10);
791     Variant<int, std::string> b("1");
792 
793     std::swap(a, b);
794     ASSERT_TRUE(a.is<std::string>());
795     ASSERT_TRUE(b.is<int>());
796     EXPECT_EQ("1", std::get<std::string>(a));
797     EXPECT_EQ(10, std::get<int>(b));
798   }
799 
800   {
801     Variant<int, std::string> a("1");
802     Variant<int, std::string> b(10);
803 
804     std::swap(a, b);
805     ASSERT_TRUE(a.is<int>());
806     ASSERT_TRUE(b.is<std::string>());
807     EXPECT_EQ(10, std::get<int>(a));
808     EXPECT_EQ("1", std::get<std::string>(b));
809   }
810 }
811 
TEST(Variant,Get)812 TEST(Variant, Get) {
813   {
814     Variant<int, bool, float, int> v;
815 
816     EXPECT_EQ(nullptr, &std::get<int>(v));
817     EXPECT_EQ(nullptr, &std::get<bool>(v));
818     EXPECT_EQ(nullptr, &std::get<float>(v));
819     EXPECT_EQ(nullptr, &std::get<0>(v));
820     EXPECT_EQ(nullptr, &std::get<1>(v));
821     EXPECT_EQ(nullptr, &std::get<2>(v));
822     EXPECT_EQ(nullptr, &std::get<3>(v));
823   }
824 
825   {
826     Variant<int, bool, float, int> v;
827     v = 9;
828     ASSERT_TRUE(v.is<int>())
829         << "Expected type " << v.index_of<int>() << " got type " << v.index();
830     EXPECT_EQ(9, std::get<int>(v));
831     EXPECT_EQ(9, std::get<0>(v));
832 
833     std::get<int>(v) = 10;
834     EXPECT_EQ(10, std::get<int>(v));
835     EXPECT_EQ(10, std::get<0>(v));
836 
837     std::get<0>(v) = 11;
838     EXPECT_EQ(11, std::get<int>(v));
839     EXPECT_EQ(11, std::get<0>(v));
840 
841     std::get<3>(v) = 12;
842     EXPECT_EQ(12, std::get<int>(v));
843     EXPECT_EQ(12, std::get<3>(v));
844   }
845 
846   {
847     Variant<int, bool, float, int> v;
848     v = false;
849     ASSERT_TRUE(v.is<bool>())
850         << "Expected type " << v.index_of<bool>() << " got type " << v.index();
851     EXPECT_EQ(false, std::get<bool>(v));
852     EXPECT_EQ(false, std::get<1>(v));
853 
854     std::get<bool>(v) = true;
855     EXPECT_EQ(true, std::get<bool>(v));
856     EXPECT_EQ(true, std::get<1>(v));
857 
858     std::get<bool>(v) = false;
859     EXPECT_EQ(false, std::get<bool>(v));
860     EXPECT_EQ(false, std::get<1>(v));
861 
862     std::get<1>(v) = true;
863     EXPECT_EQ(true, std::get<bool>(v));
864     EXPECT_EQ(true, std::get<1>(v));
865 
866     std::get<1>(v) = false;
867     EXPECT_EQ(false, std::get<bool>(v));
868     EXPECT_EQ(false, std::get<1>(v));
869   }
870 
871   {
872     Variant<int, bool, float, int> v;
873     v = 1.0f;
874     ASSERT_TRUE(v.is<float>())
875         << "Expected type " << v.index_of<float>() << " got type " << v.index();
876     EXPECT_EQ(2, v.index());
877     EXPECT_FLOAT_EQ(1.0, std::get<float>(v));
878     EXPECT_FLOAT_EQ(1.0, std::get<2>(v));
879 
880     std::get<float>(v) = 1.1;
881     EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
882     EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
883 
884     std::get<float>(v) = -3.0;
885     EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
886     EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
887 
888     std::get<2>(v) = 1.1;
889     EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
890     EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
891 
892     std::get<2>(v) = -3.0;
893     EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
894     EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
895   }
896 
897   {
898     Variant<std::unique_ptr<int>> v(std::make_unique<int>(10));
899     std::unique_ptr<int> pointer = std::move(std::get<std::unique_ptr<int>>(v));
900     ASSERT_FALSE(v.empty());
901     EXPECT_TRUE(pointer != nullptr);
902     EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
903   }
904 
905   {
906     Variant<std::string> v("test");
907     std::string s = std::get<std::string>(std::move(v));
908     EXPECT_EQ("test", s);
909   }
910 }
911 
TEST(Variant,IfAnyOf)912 TEST(Variant, IfAnyOf) {
913   {
914     Variant<int, float> v(10);
915     ASSERT_TRUE(v.is<int>());
916 
917     bool b = false;
918     EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
919     EXPECT_TRUE(b);
920 
921     float f = 0.0f;
922     EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
923     EXPECT_FLOAT_EQ(10.f, f);
924   }
925 
926   {
927     const Variant<int, float> v(10);
928     ASSERT_TRUE(v.is<int>());
929 
930     bool b = false;
931     EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
932     EXPECT_TRUE(b);
933 
934     float f = 0.0f;
935     EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
936     EXPECT_FLOAT_EQ(10.f, f);
937   }
938 
939   {
940     Variant<int, float> v(10);
941     ASSERT_TRUE(v.is<int>());
942 
943     bool b = false;
944     EXPECT_TRUE(IfAnyOf<int>::Call(&v, [&b](const auto& value) { b = value; }));
945     EXPECT_TRUE(b);
946 
947     float f = 0.0f;
948     EXPECT_TRUE((
949         IfAnyOf<int, float>::Call(&v, [&f](const auto& value) { f = value; })));
950     EXPECT_FLOAT_EQ(10.f, f);
951   }
952 
953   {
954     Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
955     ASSERT_TRUE(v.is<std::unique_ptr<int>>());
956     const int* original_v = std::get<std::unique_ptr<int>>(v).get();
957 
958     std::unique_ptr<int> u(std::make_unique<int>(20));
959 
960     EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Take(&v, &u));
961     ASSERT_TRUE(v.is<std::unique_ptr<int>>());
962     EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
963     EXPECT_EQ(u.get(), original_v);
964   }
965 
966   {
967     Variant<std::unique_ptr<DerivedType>, int> v(
968         std::make_unique<DerivedType>(10));
969     ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
970     const DerivedType* original_v =
971         std::get<std::unique_ptr<DerivedType>>(v).get();
972 
973     std::unique_ptr<BaseType> u(std::make_unique<BaseType>(20));
974 
975     EXPECT_TRUE(IfAnyOf<std::unique_ptr<DerivedType>>::Take(&v, &u));
976     ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
977     EXPECT_TRUE(std::get<std::unique_ptr<DerivedType>>(v) == nullptr);
978     EXPECT_EQ(u.get(), original_v);
979   }
980 
981   {
982     Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
983     ASSERT_TRUE(v.is<std::unique_ptr<int>>());
984     const int* original_v = std::get<std::unique_ptr<int>>(v).get();
985 
986     std::unique_ptr<int> u(std::make_unique<int>(20));
987 
988     EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Call(
989         &v, [&u](auto&& value) { u = std::move(value); }));
990     ASSERT_TRUE(v.is<std::unique_ptr<int>>());
991     EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
992     EXPECT_EQ(u.get(), original_v);
993   }
994 
995   {
996     Variant<int, bool, float> v(true);
997     ASSERT_TRUE(v.is<bool>());
998 
999     float f = 0.f;
1000     EXPECT_FALSE((IfAnyOf<int, float>::Get(&v, &f)));
1001     EXPECT_FLOAT_EQ(0.f, f);
1002   }
1003 
1004   {
1005     Variant<std::string, int> v("foo");
1006     ASSERT_TRUE(v.is<std::string>());
1007 
1008     std::string s = "bar";
1009     EXPECT_TRUE(IfAnyOf<std::string>::Swap(&v, &s));
1010     ASSERT_TRUE(v.is<std::string>());
1011     EXPECT_EQ("bar", std::get<std::string>(v));
1012     EXPECT_EQ("foo", s);
1013   }
1014 
1015   {
1016     Variant<std::string, const char*> v(static_cast<const char*>("foo"));
1017     ASSERT_TRUE(v.is<const char*>());
1018 
1019     std::string s = "bar";
1020     EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1021     ASSERT_TRUE(v.is<const char*>());
1022     EXPECT_EQ("foo", std::get<const char*>(v));
1023     EXPECT_EQ("foo", s);
1024 
1025     v = std::string("bar");
1026     ASSERT_TRUE(v.is<std::string>());
1027 
1028     EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1029     ASSERT_TRUE(v.is<std::string>());
1030     EXPECT_EQ("bar", s);
1031   }
1032 
1033   {
1034     Variant<std::string, const char*> v;
1035     ASSERT_TRUE(v.empty());
1036 
1037     std::string s = "bar";
1038     EXPECT_FALSE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1039     EXPECT_EQ("bar", s);
1040   }
1041 
1042   {
1043     Variant<std::string, const char*> v(static_cast<const char*>("test"));
1044     ASSERT_TRUE(v.is<const char*>());
1045 
1046     std::string s;
1047     EXPECT_FALSE(IfAnyOf<>::Take(&v, &s));
1048     EXPECT_TRUE(s.empty());
1049   }
1050 }
1051 
TEST(Variant,ConstVolatile)1052 TEST(Variant, ConstVolatile) {
1053   {
1054     Variant<const int> v(10);
1055     ASSERT_TRUE(v.is<const int>());
1056     EXPECT_EQ(10, std::get<const int>(v));
1057   }
1058 
1059   {
1060     Variant<const std::string> v("test");
1061     ASSERT_TRUE(v.is<const std::string>());
1062     EXPECT_EQ("test", std::get<const std::string>(v));
1063   }
1064 
1065   {
1066     Variant<volatile int, std::string> v(10);
1067     ASSERT_TRUE(v.is<volatile int>());
1068     EXPECT_EQ(10, std::get<volatile int>(v));
1069   }
1070 }
1071 
TEST(Variant,HasType)1072 TEST(Variant, HasType) {
1073   EXPECT_TRUE((detail::HasType<int, int, float, bool>::value));
1074   EXPECT_FALSE((detail::HasType<char, int, float, bool>::value));
1075   EXPECT_FALSE(detail::HasType<>::value);
1076 
1077   EXPECT_TRUE((detail::HasType<int&, int, float, bool>::value));
1078   EXPECT_FALSE((detail::HasType<char&, int, float, bool>::value));
1079 }
1080 
TEST(Variant,Set)1081 TEST(Variant, Set) {
1082   EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<int, bool,
1083                                                                 float>::value));
1084   EXPECT_TRUE(
1085       (detail::Set<int, bool, float>::template IsSubset<bool, float>::value));
1086   EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<float>::value));
1087   EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<>::value));
1088 
1089   EXPECT_FALSE(
1090       (detail::Set<int, bool, float>::template IsSubset<int, bool, float,
1091                                                         char>::value));
1092   EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<bool, float,
1093                                                                  char>::value));
1094   EXPECT_FALSE(
1095       (detail::Set<int, bool, float>::template IsSubset<float, char>::value));
1096   EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<char>::value));
1097 
1098   EXPECT_TRUE(detail::Set<>::template IsSubset<>::value);
1099   EXPECT_FALSE(detail::Set<>::template IsSubset<int>::value);
1100   EXPECT_FALSE((detail::Set<>::template IsSubset<int, float>::value));
1101 }
1102