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