blob: c30c055a7fbc0c9be7beb392fc7c6f6f5b781a89 [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#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
10using namespace android::pdx;
11using namespace android::pdx::rpc;
12
13namespace {
14
15struct BaseType {
16 BaseType(int value) : value(value) {}
17 int value;
18};
19
20struct DerivedType : BaseType {
21 DerivedType(int value) : BaseType{value} {};
22};
23
24template <typename T>
25class TestType {
26 public:
27 TestType(const T& value) : value_(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
35 const T& get() const { return value_; }
36 T&& take() { return std::move(value_); }
37
38 private:
39 T value_;
40};
41
42template <typename T>
43class InstrumentType {
44 public:
45 InstrumentType(const T& value) : value_(value) { constructor_count_++; }
46 InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; }
47 InstrumentType(const InstrumentType& other) : value_(other.value_) {
48 constructor_count_++;
49 }
50 InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) {
51 constructor_count_++;
52 }
53 InstrumentType(const TestType<T>& other) : value_(other.get()) {
54 constructor_count_++;
55 }
56 InstrumentType(TestType<T>&& other) : value_(other.take()) {
57 constructor_count_++;
58 }
59 ~InstrumentType() { destructor_count_++; }
60
61 InstrumentType& operator=(const InstrumentType& other) {
62 copy_assignment_count_++;
63 value_ = other.value_;
64 return *this;
65 }
66 InstrumentType& operator=(InstrumentType&& other) {
67 move_assignment_count_++;
68 value_ = std::move(other.value_);
69 return *this;
70 }
71
72 InstrumentType& operator=(const TestType<T>& other) {
73 copy_assignment_count_++;
74 value_ = other.get();
75 return *this;
76 }
77 InstrumentType& operator=(TestType<T>&& other) {
78 move_assignment_count_++;
79 value_ = other.take();
80 return *this;
81 }
82
83 static std::size_t constructor_count() { return constructor_count_; }
84 static std::size_t destructor_count() { return destructor_count_; }
85 static std::size_t move_assignment_count() { return move_assignment_count_; }
86 static std::size_t copy_assignment_count() { return copy_assignment_count_; }
87
88 const T& get() const { return value_; }
89 T&& take() { return std::move(value_); }
90
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
107template <typename T>
108std::size_t InstrumentType<T>::constructor_count_ = 0;
109template <typename T>
110std::size_t InstrumentType<T>::destructor_count_ = 0;
111template <typename T>
112std::size_t InstrumentType<T>::move_assignment_count_ = 0;
113template <typename T>
114std::size_t InstrumentType<T>::copy_assignment_count_ = 0;
115
116} // anonymous namespace
117
118TEST(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
276TEST(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
342TEST(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<bool> b(true);
372 Variant<int, bool, float> v(b.take());
373 ASSERT_TRUE(v.is<bool>());
374 EXPECT_EQ(true, std::get<bool>(v));
375 }
376
377 {
378 Variant<const char*> c("test");
379 Variant<std::string> s(c);
380 ASSERT_TRUE(s.is<std::string>());
381 EXPECT_EQ("test", std::get<std::string>(s));
382 }
383
384 {
385 Variant<int, bool, float> a(true);
386 Variant<int, bool, float> b(a);
387
388 ASSERT_TRUE(b.is<bool>());
389 }
390
391 {
392 using IntRef = std::reference_wrapper<int>;
393 int a = 10;
394 Variant<IntRef> v(a);
395 TestType<IntRef> t(a);
396
397 ASSERT_TRUE(v.is<IntRef>());
398 EXPECT_EQ(a, std::get<IntRef>(v));
399 EXPECT_EQ(a, t.get());
400
401 a = 20;
402 EXPECT_EQ(a, std::get<IntRef>(v));
403 EXPECT_EQ(a, t.get());
404 }
405}
406
407// Verify correct ctor/dtor and assignment behavior used an instrumented type.
408TEST(Variant, CopyMoveConstructAssign) {
409 {
410 InstrumentType<int>::clear();
411
412 // Default construct to empty, no InstrumentType activity.
413 Variant<int, InstrumentType<int>> v;
414 ASSERT_EQ(0u, InstrumentType<int>::constructor_count());
415 ASSERT_EQ(0u, InstrumentType<int>::destructor_count());
416 ASSERT_EQ(0u, InstrumentType<int>::move_assignment_count());
417 ASSERT_EQ(0u, InstrumentType<int>::copy_assignment_count());
418 }
419
420 {
421 InstrumentType<int>::clear();
422
423 // Construct from int type, no InstrumentType activity.
424 Variant<int, InstrumentType<int>> v;
425 v = 10;
426 EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
427 EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
428 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
429 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
430 }
431
432 {
433 InstrumentType<int>::clear();
434
435 // Construct from int type, no InstrumentType activity.
436 Variant<int, InstrumentType<int>> v(10);
437 EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
438 EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
439 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
440 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
441 }
442
443 {
444 InstrumentType<int>::clear();
445
446 // Construct from temporary, temporary ctor/dtor.
447 Variant<int, InstrumentType<int>> v;
448 v = InstrumentType<int>(25);
449 EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
450 EXPECT_EQ(1u, 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 temporary, temporary ctor/dtor.
459 Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
460 EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
461 EXPECT_EQ(1u, 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(InstrumentType<int>(25));
471
472 // Assign from temporary, temporary ctor/dtor.
473 v = InstrumentType<int>(35);
474 EXPECT_EQ(3u, InstrumentType<int>::constructor_count());
475 EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
476 EXPECT_EQ(1u, 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 // dtor.
487 v = 10;
488 EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
489 EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
490 EXPECT_EQ(0u, 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 EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
501 EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
502 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
503 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
504 }
505 EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
506 EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
507 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
508 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
509
510 {
511 InstrumentType<int>::clear();
512
513 // Construct from other temporary.
514 Variant<int, InstrumentType<int>> v(TestType<int>(10));
515 EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
516 EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
517 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
518 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
519 }
520
521 {
522 InstrumentType<int>::clear();
523
524 // Construct from other temporary.
525 Variant<int, InstrumentType<int>> v(TestType<int>(10));
526 // Assign from other temporary.
527 v = TestType<int>(11);
528 EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
529 EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
530 EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
531 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
532 }
533
534 {
535 InstrumentType<int>::clear();
536
537 // Construct from other temporary.
538 Variant<int, InstrumentType<int>> v(TestType<int>(10));
539 // Assign from empty Variant.
540 v = Variant<int, InstrumentType<int>>();
541 EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
542 EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
543 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
544 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
545 }
546
547 {
548 InstrumentType<int>::clear();
549
550 TestType<int> other(10);
551 // Construct from other.
552 Variant<int, InstrumentType<int>> v(other);
553
554 EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
555 EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
556 EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
557 EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
558 }
559
560 {
561 InstrumentType<int>::clear();
562
563 // Construct from other temporary.
564 Variant<int, InstrumentType<int>> v(TestType<int>(0));
565 TestType<int> other(10);
566 // Assign from other.
567 v = other;
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(1u, InstrumentType<int>::copy_assignment_count());
572 }
573}
574
575TEST(Variant, MoveConstructor) {
576 {
577 std::unique_ptr<int> pointer = std::make_unique<int>(10);
578 Variant<std::unique_ptr<int>> v(std::move(pointer));
579 ASSERT_TRUE(v.is<std::unique_ptr<int>>());
580 EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) != nullptr);
581 EXPECT_TRUE(pointer == nullptr);
582 }
583
584 {
585 Variant<std::unique_ptr<int>> a(std::make_unique<int>(10));
586 Variant<std::unique_ptr<int>> b(std::move(a));
587
588 ASSERT_TRUE(a.is<std::unique_ptr<int>>());
589 ASSERT_TRUE(b.is<std::unique_ptr<int>>());
590 EXPECT_TRUE(std::get<std::unique_ptr<int>>(a) == nullptr);
591 EXPECT_TRUE(std::get<std::unique_ptr<int>>(b) != nullptr);
592 }
593}
594
595TEST(Variant, IndexOf) {
596 Variant<int, bool, float> v1;
597
598 EXPECT_EQ(0, v1.index_of<int>());
599 EXPECT_EQ(1, v1.index_of<bool>());
600 EXPECT_EQ(2, v1.index_of<float>());
601
602 Variant<int, bool, float, int> v2;
603
604 EXPECT_EQ(0, v2.index_of<int>());
605 EXPECT_EQ(1, v2.index_of<bool>());
606 EXPECT_EQ(2, v2.index_of<float>());
607}
608
609struct Visitor {
610 int int_value = 0;
611 bool bool_value = false;
612 float float_value = 0.0;
613 bool empty_value = false;
614
615 void Visit(int value) { int_value = value; }
616 void Visit(bool value) { bool_value = value; }
617 void Visit(float value) { float_value = value; }
618 void Visit(EmptyVariant) { empty_value = true; }
619};
620
621TEST(Variant, Visit) {
622 {
623 Variant<int, bool, float> v(10);
624 EXPECT_TRUE(v.is<int>());
625
626 Visitor visitor;
627 v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
628 EXPECT_EQ(10, visitor.int_value);
629
630 visitor = {};
631 v = true;
632 v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
633 EXPECT_EQ(true, visitor.bool_value);
634 }
635
636 {
637 Variant<int, bool, float> v;
638 EXPECT_EQ(-1, v.index());
639
640 Visitor visitor;
641 v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
642 EXPECT_TRUE(visitor.empty_value);
643 }
644
645 {
646 Variant<std::string> v("test");
647 ASSERT_TRUE(v.is<std::string>());
648 EXPECT_FALSE(std::get<std::string>(v).empty());
649
650 v.Visit([](auto&& value) {
651 std::remove_reference_t<decltype(value)> empty;
652 std::swap(empty, value);
653 });
654 ASSERT_TRUE(v.is<std::string>());
655 EXPECT_TRUE(std::get<std::string>(v).empty());
656 }
657}
658
659TEST(Variant, Become) {
660 {
661 Variant<int, bool, float> v;
662
663 v.Become(0);
664 EXPECT_TRUE(v.is<int>());
665
666 v.Become(1);
667 EXPECT_TRUE(v.is<bool>());
668
669 v.Become(2);
670 EXPECT_TRUE(v.is<float>());
671
672 v.Become(3);
673 EXPECT_TRUE(v.empty());
674
675 v.Become(-1);
676 EXPECT_TRUE(v.empty());
677
678 v.Become(-2);
679 EXPECT_TRUE(v.empty());
680 }
681
682 {
683 Variant<int, bool, float> v;
684
685 v.Become(0, 10);
686 ASSERT_TRUE(v.is<int>());
687 EXPECT_EQ(10, std::get<int>(v));
688
689 v.Become(1, true);
690 ASSERT_TRUE(v.is<bool>());
691 EXPECT_EQ(true, std::get<bool>(v));
692
693 v.Become(2, 2.0f);
694 ASSERT_TRUE(v.is<float>());
695 EXPECT_FLOAT_EQ(2.0f, std::get<float>(v));
696
697 v.Become(3, 10);
698 EXPECT_TRUE(v.empty());
699
700 v.Become(-1, 10);
701 EXPECT_TRUE(v.empty());
702
703 v.Become(-2, 20);
704 EXPECT_TRUE(v.empty());
705 }
706
707 {
708 Variant<std::string> v;
709
710 v.Become(0);
711 ASSERT_TRUE(v.is<std::string>());
712 EXPECT_TRUE(std::get<std::string>(v).empty());
713 }
714
715 {
716 Variant<std::string> v;
717
718 v.Become(0, "test");
719 ASSERT_TRUE(v.is<std::string>());
720 EXPECT_EQ("test", std::get<std::string>(v));
721 }
722
723 {
724 Variant<std::string> v("foo");
725
726 v.Become(0, "bar");
727 ASSERT_TRUE(v.is<std::string>());
728 EXPECT_EQ("foo", std::get<std::string>(v));
729 }
730}
731
732TEST(Variant, Swap) {
733 {
734 Variant<std::string> a;
735 Variant<std::string> b;
736
737 std::swap(a, b);
738 EXPECT_TRUE(a.empty());
739 EXPECT_TRUE(b.empty());
740 }
741
742 {
743 Variant<std::string> a("1");
744 Variant<std::string> b;
745
746 std::swap(a, b);
747 EXPECT_TRUE(!a.empty());
748 EXPECT_TRUE(!b.empty());
749 ASSERT_TRUE(b.is<std::string>());
750 EXPECT_EQ("1", std::get<std::string>(b));
751 }
752
753 {
754 Variant<std::string> a;
755 Variant<std::string> b("1");
756
757 std::swap(a, b);
758 EXPECT_TRUE(!a.empty());
759 EXPECT_TRUE(!b.empty());
760 ASSERT_TRUE(a.is<std::string>());
761 EXPECT_EQ("1", std::get<std::string>(a));
762 }
763
764 {
765 Variant<std::string> a("1");
766 Variant<std::string> b("2");
767
768 std::swap(a, b);
769 ASSERT_TRUE(a.is<std::string>());
770 ASSERT_TRUE(b.is<std::string>());
771 EXPECT_EQ("2", std::get<std::string>(a));
772 EXPECT_EQ("1", std::get<std::string>(b));
773 }
774
775 {
776 Variant<int, std::string> a(10);
777 Variant<int, std::string> b("1");
778
779 std::swap(a, b);
780 ASSERT_TRUE(a.is<std::string>());
781 ASSERT_TRUE(b.is<int>());
782 EXPECT_EQ("1", std::get<std::string>(a));
783 EXPECT_EQ(10, std::get<int>(b));
784 }
785
786 {
787 Variant<int, std::string> a("1");
788 Variant<int, std::string> b(10);
789
790 std::swap(a, b);
791 ASSERT_TRUE(a.is<int>());
792 ASSERT_TRUE(b.is<std::string>());
793 EXPECT_EQ(10, std::get<int>(a));
794 EXPECT_EQ("1", std::get<std::string>(b));
795 }
796}
797
798TEST(Variant, Get) {
799 {
800 Variant<int, bool, float, int> v;
801
802 EXPECT_EQ(nullptr, &std::get<int>(v));
803 EXPECT_EQ(nullptr, &std::get<bool>(v));
804 EXPECT_EQ(nullptr, &std::get<float>(v));
805 EXPECT_EQ(nullptr, &std::get<0>(v));
806 EXPECT_EQ(nullptr, &std::get<1>(v));
807 EXPECT_EQ(nullptr, &std::get<2>(v));
808 EXPECT_EQ(nullptr, &std::get<3>(v));
809 }
810
811 {
812 Variant<int, bool, float, int> v;
813 v = 9;
814 ASSERT_TRUE(v.is<int>())
815 << "Expected type " << v.index_of<int>() << " got type " << v.index();
816 EXPECT_EQ(9, std::get<int>(v));
817 EXPECT_EQ(9, std::get<0>(v));
818
819 std::get<int>(v) = 10;
820 EXPECT_EQ(10, std::get<int>(v));
821 EXPECT_EQ(10, std::get<0>(v));
822
823 std::get<0>(v) = 11;
824 EXPECT_EQ(11, std::get<int>(v));
825 EXPECT_EQ(11, std::get<0>(v));
826
827 std::get<3>(v) = 12;
828 EXPECT_EQ(12, std::get<int>(v));
829 EXPECT_EQ(12, std::get<3>(v));
830 }
831
832 {
833 Variant<int, bool, float, int> v;
834 v = false;
835 ASSERT_TRUE(v.is<bool>())
836 << "Expected type " << v.index_of<bool>() << " got type " << v.index();
837 EXPECT_EQ(false, std::get<bool>(v));
838 EXPECT_EQ(false, std::get<1>(v));
839
840 std::get<bool>(v) = true;
841 EXPECT_EQ(true, std::get<bool>(v));
842 EXPECT_EQ(true, std::get<1>(v));
843
844 std::get<bool>(v) = false;
845 EXPECT_EQ(false, std::get<bool>(v));
846 EXPECT_EQ(false, std::get<1>(v));
847
848 std::get<1>(v) = true;
849 EXPECT_EQ(true, std::get<bool>(v));
850 EXPECT_EQ(true, std::get<1>(v));
851
852 std::get<1>(v) = false;
853 EXPECT_EQ(false, std::get<bool>(v));
854 EXPECT_EQ(false, std::get<1>(v));
855 }
856
857 {
858 Variant<int, bool, float, int> v;
859 v = 1.0f;
860 ASSERT_TRUE(v.is<float>())
861 << "Expected type " << v.index_of<float>() << " got type " << v.index();
862 EXPECT_EQ(2, v.index());
863 EXPECT_FLOAT_EQ(1.0, std::get<float>(v));
864 EXPECT_FLOAT_EQ(1.0, std::get<2>(v));
865
866 std::get<float>(v) = 1.1;
867 EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
868 EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
869
870 std::get<float>(v) = -3.0;
871 EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
872 EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
873
874 std::get<2>(v) = 1.1;
875 EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
876 EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
877
878 std::get<2>(v) = -3.0;
879 EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
880 EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
881 }
882
883 {
884 Variant<std::unique_ptr<int>> v(std::make_unique<int>(10));
885 std::unique_ptr<int> pointer = std::move(std::get<std::unique_ptr<int>>(v));
886 ASSERT_FALSE(v.empty());
887 EXPECT_TRUE(pointer != nullptr);
888 EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
889 }
890
891 {
892 Variant<std::string> v("test");
893 std::string s = std::get<std::string>(std::move(v));
894 EXPECT_EQ("test", s);
895 }
896}
897
898TEST(Variant, IfAnyOf) {
899 {
900 Variant<int, float> v(10);
901 ASSERT_TRUE(v.is<int>());
902
903 bool b = false;
904 EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
905 EXPECT_TRUE(b);
906
907 float f = 0.0f;
908 EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
909 EXPECT_FLOAT_EQ(10.f, f);
910 }
911
912 {
913 const Variant<int, float> v(10);
914 ASSERT_TRUE(v.is<int>());
915
916 bool b = false;
917 EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
918 EXPECT_TRUE(b);
919
920 float f = 0.0f;
921 EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
922 EXPECT_FLOAT_EQ(10.f, f);
923 }
924
925 {
926 Variant<int, float> v(10);
927 ASSERT_TRUE(v.is<int>());
928
929 bool b = false;
930 EXPECT_TRUE(IfAnyOf<int>::Call(&v, [&b](const auto& value) { b = value; }));
931 EXPECT_TRUE(b);
932
933 float f = 0.0f;
934 EXPECT_TRUE((
935 IfAnyOf<int, float>::Call(&v, [&f](const auto& value) { f = value; })));
936 EXPECT_FLOAT_EQ(10.f, f);
937 }
938
939 {
940 Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
941 ASSERT_TRUE(v.is<std::unique_ptr<int>>());
942 const int* original_v = std::get<std::unique_ptr<int>>(v).get();
943
944 std::unique_ptr<int> u(std::make_unique<int>(20));
945
946 EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Take(&v, &u));
947 ASSERT_TRUE(v.is<std::unique_ptr<int>>());
948 EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
949 EXPECT_EQ(u.get(), original_v);
950 }
951
952 {
953 Variant<std::unique_ptr<DerivedType>, int> v(
954 std::make_unique<DerivedType>(10));
955 ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
956 const DerivedType* original_v =
957 std::get<std::unique_ptr<DerivedType>>(v).get();
958
959 std::unique_ptr<BaseType> u(std::make_unique<BaseType>(20));
960
961 EXPECT_TRUE(IfAnyOf<std::unique_ptr<DerivedType>>::Take(&v, &u));
962 ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
963 EXPECT_TRUE(std::get<std::unique_ptr<DerivedType>>(v) == nullptr);
964 EXPECT_EQ(u.get(), original_v);
965 }
966
967 {
968 Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
969 ASSERT_TRUE(v.is<std::unique_ptr<int>>());
970 const int* original_v = std::get<std::unique_ptr<int>>(v).get();
971
972 std::unique_ptr<int> u(std::make_unique<int>(20));
973
974 EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Call(
975 &v, [&u](auto&& value) { u = std::move(value); }));
976 ASSERT_TRUE(v.is<std::unique_ptr<int>>());
977 EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
978 EXPECT_EQ(u.get(), original_v);
979 }
980
981 {
982 Variant<int, bool, float> v(true);
983 ASSERT_TRUE(v.is<bool>());
984
985 float f = 0.f;
986 EXPECT_FALSE((IfAnyOf<int, float>::Get(&v, &f)));
987 EXPECT_FLOAT_EQ(0.f, f);
988 }
989
990 {
991 Variant<std::string, int> v("foo");
992 ASSERT_TRUE(v.is<std::string>());
993
994 std::string s = "bar";
995 EXPECT_TRUE(IfAnyOf<std::string>::Swap(&v, &s));
996 ASSERT_TRUE(v.is<std::string>());
997 EXPECT_EQ("bar", std::get<std::string>(v));
998 EXPECT_EQ("foo", s);
999 }
1000
1001 {
1002 Variant<std::string, const char*> v(static_cast<const char*>("foo"));
1003 ASSERT_TRUE(v.is<const char*>());
1004
1005 std::string s = "bar";
1006 EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1007 ASSERT_TRUE(v.is<const char*>());
1008 EXPECT_EQ("foo", std::get<const char*>(v));
1009 EXPECT_EQ("foo", s);
1010
1011 v = std::string("bar");
1012 ASSERT_TRUE(v.is<std::string>());
1013
1014 EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1015 ASSERT_TRUE(v.is<std::string>());
1016 EXPECT_EQ("bar", s);
1017 }
1018
1019 {
1020 Variant<std::string, const char*> v;
1021 ASSERT_TRUE(v.empty());
1022
1023 std::string s = "bar";
1024 EXPECT_FALSE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
1025 EXPECT_EQ("bar", s);
1026 }
1027
1028 {
1029 Variant<std::string, const char*> v(static_cast<const char*>("test"));
1030 ASSERT_TRUE(v.is<const char*>());
1031
1032 std::string s;
1033 EXPECT_FALSE(IfAnyOf<>::Take(&v, &s));
1034 EXPECT_TRUE(s.empty());
1035 }
1036}
1037
1038TEST(Variant, ConstVolatile) {
1039 {
1040 Variant<const int> v(10);
1041 ASSERT_TRUE(v.is<const int>());
1042 EXPECT_EQ(10, std::get<const int>(v));
1043 }
1044
1045 {
1046 Variant<const std::string> v("test");
1047 ASSERT_TRUE(v.is<const std::string>());
1048 EXPECT_EQ("test", std::get<const std::string>(v));
1049 }
1050
1051 {
1052 Variant<volatile int, std::string> v(10);
1053 ASSERT_TRUE(v.is<volatile int>());
1054 EXPECT_EQ(10, std::get<volatile int>(v));
1055 }
1056}
1057
1058TEST(Variant, HasType) {
1059 EXPECT_TRUE((detail::HasType<int, int, float, bool>::value));
1060 EXPECT_FALSE((detail::HasType<char, int, float, bool>::value));
1061 EXPECT_FALSE(detail::HasType<>::value);
1062
1063 EXPECT_TRUE((detail::HasTypeIgnoreRef<int&, int, float, bool>::value));
1064 EXPECT_FALSE((detail::HasTypeIgnoreRef<char&, int, float, bool>::value));
1065}
1066
1067TEST(Variant, Set) {
1068 EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<int, bool,
1069 float>::value));
1070 EXPECT_TRUE(
1071 (detail::Set<int, bool, float>::template IsSubset<bool, float>::value));
1072 EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<float>::value));
1073 EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<>::value));
1074
1075 EXPECT_FALSE(
1076 (detail::Set<int, bool, float>::template IsSubset<int, bool, float,
1077 char>::value));
1078 EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<bool, float,
1079 char>::value));
1080 EXPECT_FALSE(
1081 (detail::Set<int, bool, float>::template IsSubset<float, char>::value));
1082 EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<char>::value));
1083
1084 EXPECT_TRUE(detail::Set<>::template IsSubset<>::value);
1085 EXPECT_FALSE(detail::Set<>::template IsSubset<int>::value);
1086 EXPECT_FALSE((detail::Set<>::template IsSubset<int, float>::value));
1087}