25 #include <initializer_list> 29 #include <type_traits> 30 #include <unordered_set> 34 #include "gmock/gmock.h" 35 #include "gtest/gtest.h" 42 #ifdef ABSL_HAVE_EXCEPTIONS 44 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \ 45 EXPECT_THROW(expr, exception_t) 49 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \ 50 EXPECT_DEATH(expr, text) 52 #endif // ABSL_HAVE_EXCEPTIONS 54 #define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...) \ 55 ABSL_VARIANT_TEST_EXPECT_FAIL((void)(__VA_ARGS__), absl::bad_variant_access, \ 62 struct hash<Hashable> {
63 size_t operator()(
const Hashable&);
72 using ::testing::DoubleEq;
73 using ::testing::Pointee;
74 using ::testing::VariantWith;
77 MoveCanThrow() :
v(0) {}
78 MoveCanThrow(
int v) :
v(v) {}
79 MoveCanThrow(
const MoveCanThrow& other) :
v(other.v) {}
80 MoveCanThrow& operator=(
const MoveCanThrow& ) {
return *
this; }
84 bool operator==(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v == rhs.v; }
85 bool operator!=(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v != rhs.v; }
86 bool operator<(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v < rhs.v; }
87 bool operator<=(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v <= rhs.v; }
88 bool operator>=(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v >= rhs.v; }
89 bool operator>(MoveCanThrow lhs, MoveCanThrow rhs) {
return lhs.v > rhs.v; }
94 explicit SpecialSwap(
int i) :
i(i) {}
95 friend void swap(SpecialSwap&
a, SpecialSwap&
b) {
96 a.special_swap = b.special_swap =
true;
99 bool operator==(SpecialSwap other)
const {
return i == other.i; }
104 struct MoveOnlyWithListConstructor {
105 MoveOnlyWithListConstructor() =
default;
106 explicit MoveOnlyWithListConstructor(std::initializer_list<int> ,
109 MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) =
default;
110 MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
116 #ifdef ABSL_HAVE_EXCEPTIONS 118 struct ConversionException {};
121 struct ExceptionOnConversion {
123 throw ConversionException();
128 template <
class H,
class... T>
131 v.template emplace<0>(ExceptionOnConversion<H>());
132 }
catch (ConversionException& ) {
137 #endif // ABSL_HAVE_EXCEPTIONS 141 template<
typename T,
size_t N>
143 explicit ValueHolder(
const T& x) :
value(x) {}
148 template<
typename T,
size_t N>
153 template<
typename T,
size_t N>
154 inline bool operator==(
const ValueHolder<T, N>& left,
155 const ValueHolder<T, N>& right) {
156 return left.value == right.value;
159 template<
typename T,
size_t N>
160 inline bool operator!=(
const ValueHolder<T, N>& left,
161 const ValueHolder<T, N>& right) {
162 return left.value != right.value;
165 template<
typename T,
size_t N>
167 std::ostream& stream,
const ValueHolder<T, N>&
object) {
168 return stream <<
object.value;
173 struct VariantFactory {
174 typedef variant<ValueHolder<T, 1>, ValueHolder<T, 2>, ValueHolder<T, 3>,
180 typedef ::testing::Types<ValueHolder<size_t, 1>, ValueHolder<size_t, 2>,
181 ValueHolder<size_t, 3>,
182 ValueHolder<size_t, 4>> VariantTypes;
185 struct IncrementInDtor {
187 ~IncrementInDtor() { *counter += 1; }
191 struct IncrementInDtorCopyCanThrow {
192 explicit IncrementInDtorCopyCanThrow(
int*
counter) :
counter(counter) {}
193 IncrementInDtorCopyCanThrow(IncrementInDtorCopyCanThrow&& other) noexcept =
195 IncrementInDtorCopyCanThrow(
const IncrementInDtorCopyCanThrow& other)
197 IncrementInDtorCopyCanThrow& operator=(
198 IncrementInDtorCopyCanThrow&&) noexcept =
default;
199 IncrementInDtorCopyCanThrow& operator=(
200 IncrementInDtorCopyCanThrow
const& other) {
201 counter = other.counter;
204 ~IncrementInDtorCopyCanThrow() { *counter += 1; }
211 inline bool operator==(
const IncrementInDtor& left,
212 const IncrementInDtor& right) {
213 return left.counter == right.counter;
218 std::ostream& stream,
const IncrementInDtor&
object) {
219 return stream <<
object.counter;
225 explicit CopyNoAssign(
int value) :
foo(value) {}
226 CopyNoAssign(
const CopyNoAssign& other) :
foo(other.foo) {}
229 const CopyNoAssign& operator=(
const CopyNoAssign&);
239 explicit NonCopyable(
int value1)
243 :
value(value1 + value2) {}
246 :
value(value1 + value2 + value3) {}
248 NonCopyable(
int value1,
int value2,
int value3,
int value4)
249 :
value(value1 + value2 + value3 + value4) {}
250 NonCopyable(
const NonCopyable&) =
delete;
251 NonCopyable& operator=(
const NonCopyable&) =
delete;
258 template <
typename T>
259 class VariantTypesTest :
public ::testing::Test {};
260 TYPED_TEST_SUITE(VariantTypesTest, VariantTypes);
266 struct NonNoexceptDefaultConstructible {
267 NonNoexceptDefaultConstructible() {}
271 struct NonDefaultConstructible {
272 NonDefaultConstructible() =
delete;
275 TEST(VariantTest, TestDefaultConstructor) {
277 using X = variant<int>;
278 constexpr variant<int> x{};
279 ASSERT_FALSE(x.valueless_by_exception());
280 ASSERT_EQ(0, x.index());
281 EXPECT_EQ(0, absl::get<0>(x));
286 using X = variant<NonNoexceptDefaultConstructible>;
288 ASSERT_FALSE(x.valueless_by_exception());
289 ASSERT_EQ(0, x.index());
290 EXPECT_EQ(5, absl::get<0>(x).
value);
295 using X = variant<int, NonNoexceptDefaultConstructible>;
297 ASSERT_FALSE(x.valueless_by_exception());
298 ASSERT_EQ(0, x.index());
299 EXPECT_EQ(0, absl::get<0>(x));
304 using X = variant<NonNoexceptDefaultConstructible, int>;
306 ASSERT_FALSE(x.valueless_by_exception());
307 ASSERT_EQ(0, x.index());
308 EXPECT_EQ(5, absl::get<0>(x).
value);
312 std::is_default_constructible<variant<NonDefaultConstructible>>::
value);
313 EXPECT_FALSE((std::is_default_constructible<
314 variant<NonDefaultConstructible, int>>::
value));
315 EXPECT_TRUE((std::is_default_constructible<
316 variant<int, NonDefaultConstructible>>::
value));
322 TYPED_TEST(VariantTypesTest, TestCopyCtor) {
323 typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
329 Variant original(
value);
330 Variant copied(original);
331 EXPECT_TRUE(absl::holds_alternative<value_type1>(copied) ||
333 EXPECT_TRUE(absl::holds_alternative<value_type2>(copied) ||
335 EXPECT_TRUE(absl::holds_alternative<value_type3>(copied) ||
337 EXPECT_TRUE(absl::holds_alternative<value_type4>(copied) ||
339 EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
340 absl::get_if<value_type1>(&copied)) ||
342 EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
343 absl::get_if<value_type2>(&copied)) ||
345 EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
346 absl::get_if<value_type3>(&copied)) ||
348 EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
349 absl::get_if<value_type4>(&copied)) ||
351 EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
352 absl::get_if<value_type1>(&copied)) ||
354 EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
355 absl::get_if<value_type2>(&copied)) ||
357 EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
358 absl::get_if<value_type3>(&copied)) ||
360 EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
361 absl::get_if<value_type4>(&copied)) ||
363 const TypeParam* ovalptr = absl::get_if<TypeParam>(&original);
364 const TypeParam* cvalptr = absl::get_if<TypeParam>(&copied);
365 ASSERT_TRUE(ovalptr !=
nullptr);
366 ASSERT_TRUE(cvalptr !=
nullptr);
367 EXPECT_EQ(*ovalptr, *cvalptr);
368 TypeParam* mutable_ovalptr = absl::get_if<TypeParam>(&original);
369 TypeParam* mutable_cvalptr = absl::get_if<TypeParam>(&copied);
370 ASSERT_TRUE(mutable_ovalptr !=
nullptr);
371 ASSERT_TRUE(mutable_cvalptr !=
nullptr);
372 EXPECT_EQ(*mutable_ovalptr, *mutable_cvalptr);
377 MoveOnly() =
default;
378 explicit MoveOnly(
int value) : value(value) {}
379 MoveOnly(MoveOnly&&) =
default;
380 MoveOnly& operator=(MoveOnly&&) =
default;
384 TEST(VariantTest, TestMoveConstruct) {
385 using V = variant<MoveOnly<class A>, MoveOnly<class B>, MoveOnly<class C>>;
387 V
v(in_place_index<1>, 10);
389 EXPECT_EQ(10, absl::get<1>(v2).
value);
401 struct is_trivially_move_constructible
402 : std::is_move_constructible<SingleUnion<T>>::type {};
405 struct is_trivially_move_assignable
408 TEST(VariantTest, NothrowMoveConstructible) {
411 using U = std::unique_ptr<int>;
415 static_assert(std::is_nothrow_move_constructible<variant<U>>::
value,
"");
416 static_assert(std::is_nothrow_move_constructible<variant<U, int>>::
value,
"");
417 static_assert(!std::is_nothrow_move_constructible<variant<U, E>>::
value,
"");
423 TYPED_TEST(VariantTypesTest, TestValueCtor) {
424 typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
431 EXPECT_TRUE(absl::holds_alternative<value_type1>(v) ||
433 EXPECT_TRUE(absl::holds_alternative<value_type2>(v) ||
435 EXPECT_TRUE(absl::holds_alternative<value_type3>(v) ||
437 EXPECT_TRUE(absl::holds_alternative<value_type4>(v) ||
439 EXPECT_TRUE(
nullptr != absl::get_if<value_type1>(&v) ||
441 EXPECT_TRUE(
nullptr != absl::get_if<value_type2>(&v) ||
443 EXPECT_TRUE(
nullptr != absl::get_if<value_type3>(&v) ||
445 EXPECT_TRUE(
nullptr != absl::get_if<value_type4>(&v) ||
447 EXPECT_TRUE(
nullptr != absl::get_if<value_type1>(&v) ||
449 EXPECT_TRUE(
nullptr != absl::get_if<value_type2>(&v) ||
451 EXPECT_TRUE(
nullptr != absl::get_if<value_type3>(&v) ||
453 EXPECT_TRUE(
nullptr != absl::get_if<value_type4>(&v) ||
455 const TypeParam* valptr = absl::get_if<TypeParam>(&
v);
456 ASSERT_TRUE(
nullptr != valptr);
457 EXPECT_EQ(value.value, valptr->value);
458 const TypeParam* mutable_valptr = absl::get_if<TypeParam>(&
v);
459 ASSERT_TRUE(
nullptr != mutable_valptr);
460 EXPECT_EQ(value.value, mutable_valptr->value);
463 TEST(VariantTest, InPlaceType) {
464 using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
466 Var v1(in_place_type_t<int>(), 7);
467 ASSERT_TRUE(absl::holds_alternative<int>(v1));
468 EXPECT_EQ(7, absl::get<int>(v1));
470 Var v2(in_place_type_t<std::string>(),
"ABC");
471 ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
472 EXPECT_EQ(
"ABC", absl::get<std::string>(v2));
474 Var v3(in_place_type_t<std::string>(),
"ABC", 2);
475 ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
476 EXPECT_EQ(
"AB", absl::get<std::string>(v3));
478 Var v4(in_place_type_t<NonCopyable>{});
479 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
483 EXPECT_THAT(
absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
486 TEST(VariantTest, InPlaceTypeVariableTemplate) {
487 using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
489 Var v1(in_place_type<int>, 7);
490 ASSERT_TRUE(absl::holds_alternative<int>(v1));
491 EXPECT_EQ(7, absl::get<int>(v1));
493 Var v2(in_place_type<std::string>,
"ABC");
494 ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
495 EXPECT_EQ(
"ABC", absl::get<std::string>(v2));
497 Var v3(in_place_type<std::string>,
"ABC", 2);
498 ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
499 EXPECT_EQ(
"AB", absl::get<std::string>(v3));
501 Var v4(in_place_type<NonCopyable>);
502 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
506 EXPECT_THAT(
absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
509 TEST(VariantTest, InPlaceTypeInitializerList) {
511 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
513 Var v1(in_place_type_t<MoveOnlyWithListConstructor>(), {1, 2, 3, 4, 5}, 6);
514 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
515 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
518 TEST(VariantTest, InPlaceTypeInitializerListVariabletemplate) {
520 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
522 Var v1(in_place_type<MoveOnlyWithListConstructor>, {1, 2, 3, 4, 5}, 6);
523 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
524 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
527 TEST(VariantTest, InPlaceIndex) {
528 using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
530 Var v1(in_place_index_t<0>(), 7);
531 ASSERT_TRUE(absl::holds_alternative<int>(v1));
532 EXPECT_EQ(7, absl::get<int>(v1));
534 Var v2(in_place_index_t<1>(),
"ABC");
535 ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
536 EXPECT_EQ(
"ABC", absl::get<std::string>(v2));
538 Var v3(in_place_index_t<1>(),
"ABC", 2);
539 ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
540 EXPECT_EQ(
"AB", absl::get<std::string>(v3));
542 Var v4(in_place_index_t<2>{});
543 EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
546 EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
547 variant<NonCopyable>(in_place_index_t<0>{})));
549 Var v5(in_place_index_t<3>(), {1, 2, 3});
551 EXPECT_THAT(
absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
554 TEST(VariantTest, InPlaceIndexVariableTemplate) {
555 using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
557 Var v1(in_place_index<0>, 7);
558 ASSERT_TRUE(absl::holds_alternative<int>(v1));
559 EXPECT_EQ(7, absl::get<int>(v1));
561 Var v2(in_place_index<1>,
"ABC");
562 ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
563 EXPECT_EQ(
"ABC", absl::get<std::string>(v2));
565 Var v3(in_place_index<1>,
"ABC", 2);
566 ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
567 EXPECT_EQ(
"AB", absl::get<std::string>(v3));
569 Var v4(in_place_index<2>);
570 EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
573 EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
574 variant<NonCopyable>(in_place_index<0>)));
576 Var v5(in_place_index<3>, {1, 2, 3});
578 EXPECT_THAT(
absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
581 TEST(VariantTest, InPlaceIndexInitializerList) {
583 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
585 Var v1(in_place_index_t<3>(), {1, 2, 3, 4, 5}, 6);
586 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
587 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
590 TEST(VariantTest, InPlaceIndexInitializerListVariableTemplate) {
592 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
594 Var v1(in_place_index<3>, {1, 2, 3, 4, 5}, 6);
595 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
596 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
604 TEST(VariantTest, TestDtor) {
605 typedef VariantFactory<IncrementInDtor>::Type Variant;
611 IncrementInDtor counter_adjuster(&counter);
612 EXPECT_EQ(0, counter);
614 value_type1
value1(counter_adjuster);
615 { Variant object(value1); }
616 EXPECT_EQ(1, counter);
618 value_type2
value2(counter_adjuster);
619 { Variant object(value2); }
620 EXPECT_EQ(2, counter);
622 value_type3 value3(counter_adjuster);
623 { Variant object(value3); }
624 EXPECT_EQ(3, counter);
626 value_type4 value4(counter_adjuster);
627 { Variant object(value4); }
628 EXPECT_EQ(4, counter);
631 #ifdef ABSL_HAVE_EXCEPTIONS 634 #if defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE) 635 TEST(VariantTest, DISABLED_TestDtorValuelessByException)
638 TEST(VariantTest, TestDtorValuelessByException)
642 IncrementInDtor counter_adjuster(&counter);
645 using Variant = VariantFactory<IncrementInDtor>::Type;
647 Variant
v(in_place_index<0>, counter_adjuster);
648 EXPECT_EQ(0, counter);
650 ToValuelessByException(v);
651 ASSERT_TRUE(v.valueless_by_exception());
652 EXPECT_EQ(1, counter);
654 EXPECT_EQ(1, counter);
657 #endif // ABSL_HAVE_EXCEPTIONS 664 TEST(VariantTest, TestSelfAssignment) {
665 typedef VariantFactory<IncrementInDtor>::Type Variant;
667 IncrementInDtor counter_adjuster(&counter);
669 Variant object(value);
670 object.operator=(object);
671 EXPECT_EQ(0, counter);
675 const std::string long_str(128,
'a');
677 std::string
foo = long_str;
679 EXPECT_EQ(long_str, foo);
681 variant<int, std::string> so = long_str;
682 ASSERT_EQ(1, so.index());
683 EXPECT_EQ(long_str, absl::get<1>(so));
686 ASSERT_EQ(1, so.index());
687 EXPECT_EQ(long_str, absl::get<1>(so));
692 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValueSameTypes) {
693 typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
695 const Variant source(value);
696 Variant target(TypeParam(value.value + 1));
697 ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
698 ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
699 ASSERT_NE(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
701 ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
702 ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
703 EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
708 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingSourceType) {
709 typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
712 const Variant source(value);
713 ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
714 Variant target(value_type1(1));
715 ASSERT_TRUE(absl::holds_alternative<value_type1>(target));
717 EXPECT_TRUE(absl::holds_alternative<TypeParam>(source));
718 EXPECT_TRUE(absl::holds_alternative<TypeParam>(target));
719 EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
724 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingTargetType) {
725 typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
727 const Variant source(value_type1(1));
728 ASSERT_TRUE(absl::holds_alternative<value_type1>(source));
730 Variant target(value);
731 ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
733 EXPECT_TRUE(absl::holds_alternative<value_type1>(target));
734 EXPECT_TRUE(absl::holds_alternative<value_type1>(source));
735 EXPECT_EQ(absl::get<value_type1>(source), absl::get<value_type1>(target));
741 TEST(VariantTest, TestAssign) {
742 typedef VariantFactory<IncrementInDtor>::Type Variant;
750 std::unique_ptr<IncrementInDtor> counter_adjustor[kSize];
751 for (
int i = 0;
i != kSize;
i++) {
753 counter_adjustor[
i] = absl::make_unique<IncrementInDtor>(&counter[
i]);
756 value_type1 v1(*counter_adjustor[0]);
757 value_type2 v2(*counter_adjustor[1]);
758 value_type3 v3(*counter_adjustor[2]);
759 value_type4 v4(*counter_adjustor[3]);
770 EXPECT_EQ(2, counter[0]);
771 EXPECT_EQ(1, counter[1]);
772 EXPECT_EQ(1, counter[2]);
773 EXPECT_EQ(1, counter[3]);
780 object.operator=(object);
781 EXPECT_EQ(0, counter[0]);
785 object.operator=(object);
786 EXPECT_EQ(0, counter[1]);
790 object.operator=(object);
791 EXPECT_EQ(0, counter[2]);
795 object.operator=(object);
796 EXPECT_EQ(0, counter[3]);
799 EXPECT_EQ(1, counter[0]);
800 EXPECT_EQ(1, counter[1]);
801 EXPECT_EQ(1, counter[2]);
802 EXPECT_EQ(1, counter[3]);
807 TEST(VariantTest, TestBackupAssign) {
808 typedef VariantFactory<IncrementInDtorCopyCanThrow>::Type Variant;
816 std::unique_ptr<IncrementInDtorCopyCanThrow> counter_adjustor[kSize];
817 for (
int i = 0;
i != kSize;
i++) {
819 counter_adjustor[
i].reset(
new IncrementInDtorCopyCanThrow(&counter[
i]));
822 value_type1 v1(*counter_adjustor[0]);
823 value_type2 v2(*counter_adjustor[1]);
824 value_type3 v3(*counter_adjustor[2]);
825 value_type4 v4(*counter_adjustor[3]);
837 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) 838 EXPECT_EQ(3, counter[0]);
839 EXPECT_EQ(2, counter[1]);
840 EXPECT_EQ(2, counter[2]);
841 EXPECT_EQ(2, counter[3]);
849 object.operator=(object);
850 EXPECT_EQ(0, counter[0]);
854 object.operator=(object);
855 EXPECT_EQ(0, counter[1]);
859 object.operator=(object);
860 EXPECT_EQ(0, counter[2]);
864 object.operator=(object);
865 EXPECT_EQ(0, counter[3]);
868 EXPECT_EQ(1, counter[0]);
869 EXPECT_EQ(1, counter[1]);
870 EXPECT_EQ(1, counter[2]);
871 EXPECT_EQ(1, counter[3]);
878 TEST(VariantTest, TestEmplaceBasic) {
879 using Variant = variant<int, char>;
881 Variant
v(absl::in_place_index<0>, 0);
884 char& emplace_result = v.emplace<
char>();
885 ASSERT_TRUE(absl::holds_alternative<char>(v));
886 EXPECT_EQ(absl::get<char>(v), 0);
887 EXPECT_EQ(&emplace_result, &absl::get<char>(v));
891 absl::get<char>(
v) =
'a';
892 v.emplace<
char>(
'b');
893 ASSERT_TRUE(absl::holds_alternative<char>(v));
894 EXPECT_EQ(absl::get<char>(v),
'b');
897 int& emplace_result = v.emplace<
int>();
898 EXPECT_TRUE(absl::holds_alternative<int>(v));
899 EXPECT_EQ(absl::get<int>(v), 0);
900 EXPECT_EQ(&emplace_result, &absl::get<int>(v));
904 TEST(VariantTest, TestEmplaceInitializerList) {
906 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
908 Var v1(absl::in_place_index<0>, 555);
909 MoveOnlyWithListConstructor& emplace_result =
910 v1.emplace<MoveOnlyWithListConstructor>({1, 2, 3, 4, 5}, 6);
911 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
912 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
913 EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
916 TEST(VariantTest, TestEmplaceIndex) {
917 using Variant = variant<int, char>;
919 Variant
v(absl::in_place_index<0>, 555);
922 char& emplace_result = v.emplace<1>();
923 ASSERT_TRUE(absl::holds_alternative<char>(v));
924 EXPECT_EQ(absl::get<char>(v), 0);
925 EXPECT_EQ(&emplace_result, &absl::get<char>(v));
929 absl::get<char>(
v) =
'a';
931 ASSERT_TRUE(absl::holds_alternative<char>(v));
932 EXPECT_EQ(absl::get<char>(v),
'b');
935 int& emplace_result = v.emplace<0>();
936 EXPECT_TRUE(absl::holds_alternative<int>(v));
937 EXPECT_EQ(absl::get<int>(v), 0);
938 EXPECT_EQ(&emplace_result, &absl::get<int>(v));
942 TEST(VariantTest, TestEmplaceIndexInitializerList) {
944 variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
946 Var v1(absl::in_place_index<0>, 555);
947 MoveOnlyWithListConstructor& emplace_result =
948 v1.emplace<3>({1, 2, 3, 4, 5}, 6);
949 ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
950 EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).
value);
951 EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
958 TEST(VariantTest, Index) {
959 using Var = variant<int, std::string, double>;
962 EXPECT_EQ(0, v.index());
964 EXPECT_EQ(1, v.index());
966 EXPECT_EQ(2, v.index());
969 EXPECT_EQ(2, v2.index());
971 EXPECT_EQ(0, v2.index());
974 TEST(VariantTest, NotValuelessByException) {
975 using Var = variant<int, std::string, double>;
978 EXPECT_FALSE(v.valueless_by_exception());
980 EXPECT_FALSE(v.valueless_by_exception());
982 EXPECT_FALSE(v.valueless_by_exception());
985 EXPECT_FALSE(v.valueless_by_exception());
987 EXPECT_FALSE(v.valueless_by_exception());
990 #ifdef ABSL_HAVE_EXCEPTIONS 992 TEST(VariantTest, IndexValuelessByException) {
993 using Var = variant<MoveCanThrow, std::string, double>;
995 Var
v(absl::in_place_index<0>);
996 EXPECT_EQ(0, v.index());
997 ToValuelessByException(v);
998 EXPECT_EQ(absl::variant_npos, v.index());
1000 EXPECT_EQ(1, v.index());
1003 TEST(VariantTest, ValuelessByException) {
1004 using Var = variant<MoveCanThrow, std::string, double>;
1006 Var
v(absl::in_place_index<0>);
1007 EXPECT_FALSE(v.valueless_by_exception());
1008 ToValuelessByException(v);
1009 EXPECT_TRUE(v.valueless_by_exception());
1011 EXPECT_FALSE(v.valueless_by_exception());
1014 #endif // ABSL_HAVE_EXCEPTIONS 1020 TEST(VariantTest, MemberSwap) {
1024 variant<SpecialSwap>
a = v1,
b = v2;
1026 EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
1027 EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
1030 EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
1031 EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
1034 using V = variant<MoveCanThrow, std::string, int>;
1036 std::string s =
"abc";
1037 V valueless(in_place_index<0>);
1038 ToValuelessByException(valueless);
1043 EXPECT_THAT(lhs, VariantWith<std::string>(s));
1044 EXPECT_THAT(rhs, VariantWith<int>(i));
1048 V lhs(valueless), rhs(i);
1050 EXPECT_THAT(lhs, VariantWith<int>(i));
1051 EXPECT_TRUE(rhs.valueless_by_exception());
1055 V lhs(s), rhs(valueless);
1057 EXPECT_THAT(rhs, VariantWith<std::string>(s));
1058 EXPECT_TRUE(lhs.valueless_by_exception());
1062 V lhs(valueless), rhs(valueless);
1064 EXPECT_TRUE(lhs.valueless_by_exception());
1065 EXPECT_TRUE(rhs.valueless_by_exception());
1073 TEST(VariantTest, VariantSize) {
1091 TEST(VariantTest, VariantAlternative) {
1096 EXPECT_TRUE((std::is_same<
const float,
1099 (std::is_same<
volatile float,
1102 std::is_same<
const volatile float,
1106 EXPECT_TRUE((std::is_same<
const int,
1109 (std::is_same<
volatile int,
1112 std::is_same<
const volatile int,
1117 EXPECT_TRUE((std::is_same<
const char*
const,
1120 (std::is_same<
const char*
volatile,
1123 std::is_same<
const char*
const volatile,
1131 EXPECT_TRUE((std::is_same<
const float,
1134 (std::is_same<
volatile float,
1137 std::is_same<
const volatile float,
1142 EXPECT_TRUE((std::is_same<
const volatile int,
1145 (std::is_same<
volatile int,
1148 std::is_same<
const volatile int,
1153 EXPECT_TRUE((std::is_same<
const char*
const,
1156 (std::is_same<
const char*
volatile,
1159 std::is_same<
const char*
const volatile,
1168 TEST(VariantTest, HoldsAlternative) {
1169 using Var = variant<int, std::string, double>;
1172 EXPECT_TRUE(absl::holds_alternative<int>(v));
1173 EXPECT_FALSE(absl::holds_alternative<std::string>(v));
1174 EXPECT_FALSE(absl::holds_alternative<double>(v));
1176 EXPECT_FALSE(absl::holds_alternative<int>(v));
1177 EXPECT_TRUE(absl::holds_alternative<std::string>(v));
1178 EXPECT_FALSE(absl::holds_alternative<double>(v));
1180 EXPECT_FALSE(absl::holds_alternative<int>(v));
1181 EXPECT_FALSE(absl::holds_alternative<std::string>(v));
1182 EXPECT_TRUE(absl::holds_alternative<double>(v));
1185 EXPECT_FALSE(absl::holds_alternative<int>(v2));
1186 EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
1187 EXPECT_TRUE(absl::holds_alternative<double>(v2));
1189 EXPECT_TRUE(absl::holds_alternative<int>(v2));
1190 EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
1191 EXPECT_FALSE(absl::holds_alternative<double>(v2));
1194 TEST(VariantTest, GetIndex) {
1195 using Var = variant<int, std::string, double, int>;
1198 Var
v(absl::in_place_index<0>, 0);
1200 using LValueGetType = decltype(absl::get<0>(v));
1201 using RValueGetType = decltype(absl::get<0>(
absl::move(v)));
1205 EXPECT_EQ(absl::get<0>(v), 0);
1208 const Var& const_v =
v;
1209 using ConstLValueGetType = decltype(absl::get<0>(const_v));
1210 using ConstRValueGetType = decltype(absl::get<0>(
absl::move(const_v)));
1213 EXPECT_EQ(absl::get<0>(const_v), 0);
1214 EXPECT_EQ(absl::get<0>(
absl::move(const_v)), 0);
1218 Var v = std::string(
"Hello");
1220 using LValueGetType = decltype(absl::get<1>(v));
1221 using RValueGetType = decltype(absl::get<1>(
absl::move(v)));
1225 EXPECT_EQ(absl::get<1>(v),
"Hello");
1226 EXPECT_EQ(absl::get<1>(
absl::move(v)),
"Hello");
1228 const Var& const_v =
v;
1229 using ConstLValueGetType = decltype(absl::get<1>(const_v));
1230 using ConstRValueGetType = decltype(absl::get<1>(
absl::move(const_v)));
1233 EXPECT_EQ(absl::get<1>(const_v),
"Hello");
1234 EXPECT_EQ(absl::get<1>(
absl::move(const_v)),
"Hello");
1240 using LValueGetType = decltype(absl::get<2>(v));
1241 using RValueGetType = decltype(absl::get<2>(
absl::move(v)));
1245 EXPECT_EQ(absl::get<2>(v), 2.);
1248 const Var& const_v =
v;
1249 using ConstLValueGetType = decltype(absl::get<2>(const_v));
1250 using ConstRValueGetType = decltype(absl::get<2>(
absl::move(const_v)));
1253 EXPECT_EQ(absl::get<2>(const_v), 2.);
1254 EXPECT_EQ(absl::get<2>(
absl::move(const_v)), 2.);
1258 Var
v(absl::in_place_index<0>, 0);
1261 using LValueGetType = decltype(absl::get<3>(v));
1262 using RValueGetType = decltype(absl::get<3>(
absl::move(v)));
1266 EXPECT_EQ(absl::get<3>(v), 1);
1269 const Var& const_v =
v;
1270 using ConstLValueGetType = decltype(absl::get<3>(const_v));
1271 using ConstRValueGetType = decltype(absl::get<3>(
absl::move(const_v)));
1274 EXPECT_EQ(absl::get<3>(const_v), 1);
1275 EXPECT_EQ(absl::get<3>(
absl::move(const_v)), 1);
1279 TEST(VariantTest, BadGetIndex) {
1280 using Var = variant<int, std::string, double>;
1288 const Var& const_v =
v;
1295 Var v = std::string(
"Hello");
1300 const Var& const_v =
v;
1307 TEST(VariantTest, GetType) {
1308 using Var = variant<int, std::string, double>;
1313 using LValueGetType = decltype(absl::get<int>(v));
1314 using RValueGetType = decltype(absl::get<int>(
absl::move(v)));
1318 EXPECT_EQ(absl::get<int>(v), 1);
1321 const Var& const_v =
v;
1322 using ConstLValueGetType = decltype(absl::get<int>(const_v));
1323 using ConstRValueGetType = decltype(absl::get<int>(
absl::move(const_v)));
1326 EXPECT_EQ(absl::get<int>(const_v), 1);
1327 EXPECT_EQ(absl::get<int>(
absl::move(const_v)), 1);
1331 Var v = std::string(
"Hello");
1333 using LValueGetType = decltype(absl::get<1>(v));
1334 using RValueGetType = decltype(absl::get<1>(
absl::move(v)));
1338 EXPECT_EQ(absl::get<std::string>(v),
"Hello");
1339 EXPECT_EQ(absl::get<std::string>(
absl::move(v)),
"Hello");
1341 const Var& const_v =
v;
1342 using ConstLValueGetType = decltype(absl::get<1>(const_v));
1343 using ConstRValueGetType = decltype(absl::get<1>(
absl::move(const_v)));
1346 EXPECT_EQ(absl::get<std::string>(const_v),
"Hello");
1347 EXPECT_EQ(absl::get<std::string>(
absl::move(const_v)),
"Hello");
1353 using LValueGetType = decltype(absl::get<2>(v));
1354 using RValueGetType = decltype(absl::get<2>(
absl::move(v)));
1358 EXPECT_EQ(absl::get<double>(v), 2.);
1359 EXPECT_EQ(absl::get<double>(
absl::move(v)), 2.);
1361 const Var& const_v =
v;
1362 using ConstLValueGetType = decltype(absl::get<2>(const_v));
1363 using ConstRValueGetType = decltype(absl::get<2>(
absl::move(const_v)));
1366 EXPECT_EQ(absl::get<double>(const_v), 2.);
1367 EXPECT_EQ(absl::get<double>(
absl::move(const_v)), 2.);
1371 TEST(VariantTest, BadGetType) {
1372 using Var = variant<int, std::string, double>;
1381 const Var& const_v =
v;
1383 absl::get<std::string>(const_v));
1385 absl::get<std::string>(
std::move(const_v)));
1389 Var v = std::string(
"Hello");
1394 const Var& const_v =
v;
1401 TEST(VariantTest, GetIfIndex) {
1402 using Var = variant<int, std::string, double, int>;
1405 Var
v(absl::in_place_index<0>, 0);
1406 EXPECT_TRUE(noexcept(absl::get_if<0>(&v)));
1409 auto* elem = absl::get_if<0>(&
v);
1410 EXPECT_TRUE((std::is_same<decltype(elem),
int*>::
value));
1411 ASSERT_NE(elem,
nullptr);
1412 EXPECT_EQ(*elem, 0);
1414 auto* bad_elem = absl::get_if<1>(&
v);
1415 EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::
value));
1416 EXPECT_EQ(bad_elem,
nullptr);
1419 auto* bad_elem = absl::get_if<2>(&
v);
1420 EXPECT_TRUE((std::is_same<decltype(bad_elem),
double*>::
value));
1421 EXPECT_EQ(bad_elem,
nullptr);
1424 auto* bad_elem = absl::get_if<3>(&
v);
1425 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1426 EXPECT_EQ(bad_elem,
nullptr);
1430 const Var& const_v =
v;
1431 EXPECT_TRUE(noexcept(absl::get_if<0>(&const_v)));
1434 auto* elem = absl::get_if<0>(&const_v);
1435 EXPECT_TRUE((std::is_same<decltype(elem),
const int*>::
value));
1436 ASSERT_NE(elem,
nullptr);
1437 EXPECT_EQ(*elem, 0);
1439 auto* bad_elem = absl::get_if<1>(&const_v);
1441 (std::is_same<decltype(bad_elem),
const std::string*>::
value));
1442 EXPECT_EQ(bad_elem,
nullptr);
1445 auto* bad_elem = absl::get_if<2>(&const_v);
1446 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const double*>::
value));
1447 EXPECT_EQ(bad_elem,
nullptr);
1450 auto* bad_elem = absl::get_if<3>(&const_v);
1451 EXPECT_EQ(bad_elem,
nullptr);
1452 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1458 Var v = std::string(
"Hello");
1459 EXPECT_TRUE(noexcept(absl::get_if<1>(&v)));
1462 auto* elem = absl::get_if<1>(&
v);
1463 EXPECT_TRUE((std::is_same<decltype(elem), std::string*>::
value));
1464 ASSERT_NE(elem,
nullptr);
1465 EXPECT_EQ(*elem,
"Hello");
1467 auto* bad_elem = absl::get_if<0>(&
v);
1468 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1469 EXPECT_EQ(bad_elem,
nullptr);
1472 auto* bad_elem = absl::get_if<2>(&
v);
1473 EXPECT_TRUE((std::is_same<decltype(bad_elem),
double*>::
value));
1474 EXPECT_EQ(bad_elem,
nullptr);
1477 auto* bad_elem = absl::get_if<3>(&
v);
1478 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1479 EXPECT_EQ(bad_elem,
nullptr);
1483 const Var& const_v =
v;
1484 EXPECT_TRUE(noexcept(absl::get_if<1>(&const_v)));
1487 auto* elem = absl::get_if<1>(&const_v);
1488 EXPECT_TRUE((std::is_same<decltype(elem),
const std::string*>::
value));
1489 ASSERT_NE(elem,
nullptr);
1490 EXPECT_EQ(*elem,
"Hello");
1492 auto* bad_elem = absl::get_if<0>(&const_v);
1493 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1494 EXPECT_EQ(bad_elem,
nullptr);
1497 auto* bad_elem = absl::get_if<2>(&const_v);
1498 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const double*>::
value));
1499 EXPECT_EQ(bad_elem,
nullptr);
1502 auto* bad_elem = absl::get_if<3>(&const_v);
1503 EXPECT_EQ(bad_elem,
nullptr);
1504 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1511 EXPECT_TRUE(noexcept(absl::get_if<2>(&v)));
1514 auto* elem = absl::get_if<2>(&
v);
1515 EXPECT_TRUE((std::is_same<decltype(elem),
double*>::
value));
1516 ASSERT_NE(elem,
nullptr);
1517 EXPECT_EQ(*elem, 2.0);
1519 auto* bad_elem = absl::get_if<0>(&
v);
1520 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1521 EXPECT_EQ(bad_elem,
nullptr);
1524 auto* bad_elem = absl::get_if<1>(&
v);
1525 EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::
value));
1526 EXPECT_EQ(bad_elem,
nullptr);
1529 auto* bad_elem = absl::get_if<3>(&
v);
1530 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1531 EXPECT_EQ(bad_elem,
nullptr);
1535 const Var& const_v =
v;
1536 EXPECT_TRUE(noexcept(absl::get_if<2>(&const_v)));
1539 auto* elem = absl::get_if<2>(&const_v);
1540 EXPECT_TRUE((std::is_same<decltype(elem),
const double*>::
value));
1541 ASSERT_NE(elem,
nullptr);
1542 EXPECT_EQ(*elem, 2.0);
1544 auto* bad_elem = absl::get_if<0>(&const_v);
1545 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1546 EXPECT_EQ(bad_elem,
nullptr);
1549 auto* bad_elem = absl::get_if<1>(&const_v);
1551 (std::is_same<decltype(bad_elem),
const std::string*>::
value));
1552 EXPECT_EQ(bad_elem,
nullptr);
1555 auto* bad_elem = absl::get_if<3>(&const_v);
1556 EXPECT_EQ(bad_elem,
nullptr);
1557 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1563 Var
v(absl::in_place_index<0>, 0);
1565 EXPECT_TRUE(noexcept(absl::get_if<3>(&v)));
1568 auto* elem = absl::get_if<3>(&
v);
1569 EXPECT_TRUE((std::is_same<decltype(elem),
int*>::
value));
1570 ASSERT_NE(elem,
nullptr);
1571 EXPECT_EQ(*elem, 1);
1573 auto* bad_elem = absl::get_if<0>(&
v);
1574 EXPECT_TRUE((std::is_same<decltype(bad_elem),
int*>::
value));
1575 EXPECT_EQ(bad_elem,
nullptr);
1578 auto* bad_elem = absl::get_if<1>(&
v);
1579 EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::
value));
1580 EXPECT_EQ(bad_elem,
nullptr);
1583 auto* bad_elem = absl::get_if<2>(&
v);
1584 EXPECT_TRUE((std::is_same<decltype(bad_elem),
double*>::
value));
1585 EXPECT_EQ(bad_elem,
nullptr);
1589 const Var& const_v =
v;
1590 EXPECT_TRUE(noexcept(absl::get_if<3>(&const_v)));
1593 auto* elem = absl::get_if<3>(&const_v);
1594 EXPECT_TRUE((std::is_same<decltype(elem),
const int*>::
value));
1595 ASSERT_NE(elem,
nullptr);
1596 EXPECT_EQ(*elem, 1);
1598 auto* bad_elem = absl::get_if<0>(&const_v);
1599 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const int*>::
value));
1600 EXPECT_EQ(bad_elem,
nullptr);
1603 auto* bad_elem = absl::get_if<1>(&const_v);
1605 (std::is_same<decltype(bad_elem),
const std::string*>::
value));
1606 EXPECT_EQ(bad_elem,
nullptr);
1609 auto* bad_elem = absl::get_if<2>(&const_v);
1610 EXPECT_EQ(bad_elem,
nullptr);
1611 EXPECT_TRUE((std::is_same<decltype(bad_elem),
const double*>::
value));
1621 TEST(VariantTest, OperatorEquals) {
1622 variant<int, std::string>
a(1),
b(1);
1623 EXPECT_TRUE(
a == b);
1624 EXPECT_TRUE(b ==
a);
1625 EXPECT_FALSE(
a != b);
1626 EXPECT_FALSE(b !=
a);
1629 EXPECT_FALSE(
a == b);
1630 EXPECT_FALSE(b ==
a);
1631 EXPECT_TRUE(
a != b);
1632 EXPECT_TRUE(b !=
a);
1635 EXPECT_FALSE(
a == b);
1636 EXPECT_FALSE(b ==
a);
1637 EXPECT_TRUE(
a != b);
1638 EXPECT_TRUE(b !=
a);
1641 EXPECT_TRUE(
a == b);
1642 EXPECT_TRUE(b ==
a);
1643 EXPECT_FALSE(
a != b);
1644 EXPECT_FALSE(b !=
a);
1647 EXPECT_FALSE(
a == b);
1648 EXPECT_FALSE(b ==
a);
1649 EXPECT_TRUE(
a != b);
1650 EXPECT_TRUE(b !=
a);
1653 TEST(VariantTest, OperatorRelational) {
1654 variant<int, std::string>
a(1),
b(1);
1655 EXPECT_FALSE(
a < b);
1656 EXPECT_FALSE(b <
a);
1657 EXPECT_FALSE(
a > b);
1658 EXPECT_FALSE(b >
a);
1659 EXPECT_TRUE(
a <= b);
1660 EXPECT_TRUE(b <=
a);
1661 EXPECT_TRUE(
a >= b);
1662 EXPECT_TRUE(b >=
a);
1666 EXPECT_FALSE(b <
a);
1667 EXPECT_FALSE(
a > b);
1669 EXPECT_TRUE(
a <= b);
1670 EXPECT_FALSE(b <=
a);
1671 EXPECT_FALSE(
a >= b);
1672 EXPECT_TRUE(b >=
a);
1675 EXPECT_FALSE(
a < b);
1678 EXPECT_FALSE(b >
a);
1679 EXPECT_FALSE(
a <= b);
1680 EXPECT_TRUE(b <=
a);
1681 EXPECT_TRUE(
a >= b);
1682 EXPECT_FALSE(b >=
a);
1685 EXPECT_FALSE(
a < b);
1686 EXPECT_FALSE(b <
a);
1687 EXPECT_FALSE(
a > b);
1688 EXPECT_FALSE(b >
a);
1689 EXPECT_TRUE(
a <= b);
1690 EXPECT_TRUE(b <=
a);
1691 EXPECT_TRUE(
a >= b);
1692 EXPECT_TRUE(b >=
a);
1696 EXPECT_FALSE(b <
a);
1697 EXPECT_FALSE(
a > b);
1699 EXPECT_TRUE(
a <= b);
1700 EXPECT_FALSE(b <=
a);
1701 EXPECT_FALSE(
a >= b);
1702 EXPECT_TRUE(b >=
a);
1705 #ifdef ABSL_HAVE_EXCEPTIONS 1707 TEST(VariantTest, ValuelessOperatorEquals) {
1708 variant<MoveCanThrow, std::string> int_v(1), string_v(
"Hello"),
1709 valueless(absl::in_place_index<0>),
1710 other_valueless(absl::in_place_index<0>);
1711 ToValuelessByException(valueless);
1712 ToValuelessByException(other_valueless);
1714 EXPECT_TRUE(valueless == other_valueless);
1715 EXPECT_TRUE(other_valueless == valueless);
1716 EXPECT_FALSE(valueless == int_v);
1717 EXPECT_FALSE(valueless == string_v);
1718 EXPECT_FALSE(int_v == valueless);
1719 EXPECT_FALSE(string_v == valueless);
1721 EXPECT_FALSE(valueless != other_valueless);
1722 EXPECT_FALSE(other_valueless != valueless);
1723 EXPECT_TRUE(valueless != int_v);
1724 EXPECT_TRUE(valueless != string_v);
1725 EXPECT_TRUE(int_v != valueless);
1726 EXPECT_TRUE(string_v != valueless);
1729 TEST(VariantTest, ValuelessOperatorRelational) {
1730 variant<MoveCanThrow, std::string> int_v(1), string_v(
"Hello"),
1731 valueless(absl::in_place_index<0>),
1732 other_valueless(absl::in_place_index<0>);
1733 ToValuelessByException(valueless);
1734 ToValuelessByException(other_valueless);
1736 EXPECT_FALSE(valueless < other_valueless);
1737 EXPECT_FALSE(other_valueless < valueless);
1738 EXPECT_TRUE(valueless < int_v);
1739 EXPECT_TRUE(valueless < string_v);
1740 EXPECT_FALSE(int_v < valueless);
1741 EXPECT_FALSE(string_v < valueless);
1743 EXPECT_TRUE(valueless <= other_valueless);
1744 EXPECT_TRUE(other_valueless <= valueless);
1745 EXPECT_TRUE(valueless <= int_v);
1746 EXPECT_TRUE(valueless <= string_v);
1747 EXPECT_FALSE(int_v <= valueless);
1748 EXPECT_FALSE(string_v <= valueless);
1750 EXPECT_TRUE(valueless >= other_valueless);
1751 EXPECT_TRUE(other_valueless >= valueless);
1752 EXPECT_FALSE(valueless >= int_v);
1753 EXPECT_FALSE(valueless >= string_v);
1754 EXPECT_TRUE(int_v >= valueless);
1755 EXPECT_TRUE(string_v >= valueless);
1757 EXPECT_FALSE(valueless > other_valueless);
1758 EXPECT_FALSE(other_valueless > valueless);
1759 EXPECT_FALSE(valueless > int_v);
1760 EXPECT_FALSE(valueless > string_v);
1761 EXPECT_TRUE(int_v > valueless);
1762 EXPECT_TRUE(string_v > valueless);
1771 template <
typename T>
1773 template <
typename U>
1774 T operator()(
const U& u)
const {
1779 TEST(VariantTest, VisitSimple) {
1780 variant<std::string, const char*> v =
"A";
1782 std::string str =
absl::visit(ConvertTo<std::string>{},
v);
1783 EXPECT_EQ(
"A", str);
1785 v = std::string(
"B");
1788 EXPECT_EQ(
"B", piece);
1791 int operator()(
const std::string& s)
const {
return s.size(); }
1792 int operator()(
const char* s)
const {
return strlen(s); }
1797 v = std::string(
"VeryLargeThisTime");
1801 TEST(VariantTest, VisitRValue) {
1802 variant<std::string> v = std::string(
"X");
1804 bool operator()(
const std::string&)
const {
return false; }
1805 bool operator()(std::string&&)
const {
return true; }
1807 int operator()(
const std::string&,
const std::string&)
const {
return 0; }
1808 int operator()(
const std::string&, std::string&&)
const {
1811 int operator()(std::string&&,
const std::string&)
const {
1814 int operator()(std::string&&, std::string&&)
const {
return 3; }
1826 TEST(VariantTest, VisitRValueVisitor) {
1827 variant<std::string> v = std::string(
"X");
1829 bool operator()(
const std::string&)
const& {
return false; }
1830 bool operator()(
const std::string&) && {
return true; }
1837 TEST(VariantTest, VisitResultTypeDifferent) {
1838 variant<std::string> v = std::string(
"X");
1839 struct LValue_LValue {};
1840 struct RValue_LValue {};
1841 struct LValue_RValue {};
1842 struct RValue_RValue {};
1844 LValue_LValue operator()(
const std::string&)
const& {
return {}; }
1845 RValue_LValue operator()(std::string&&)
const& {
return {}; }
1846 LValue_RValue operator()(
const std::string&) && {
return {}; }
1847 RValue_RValue operator()(std::string&&) && {
return {}; }
1853 (std::is_same<RValue_LValue,
1858 (std::is_same<RValue_RValue,
1862 TEST(VariantTest, VisitVariadic) {
1863 using A = variant<int, std::string>;
1867 std::pair<int, int> operator()(
int a, std::unique_ptr<int>
b)
const {
1870 std::pair<int, int> operator()(absl::string_view a,
1871 std::unique_ptr<int> b)
const {
1872 return {
static_cast<int>(a.
size()), static_cast<int>(*b)};
1874 std::pair<int, int> operator()(
int a, absl::string_view b)
const {
1875 return {
a,
static_cast<int>(b.
size())};
1877 std::pair<int, int> operator()(absl::string_view a,
1878 absl::string_view b)
const {
1879 return {
static_cast<int>(a.
size()), static_cast<int>(b.
size())};
1883 EXPECT_THAT(
absl::visit(Visitor(), A(1), B(std::unique_ptr<int>(
new int(7)))),
1884 ::testing::Pair(1, 7));
1885 EXPECT_THAT(
absl::visit(Visitor(), A(1), B(absl::string_view(
"ABC"))),
1886 ::testing::Pair(1, 3));
1887 EXPECT_THAT(
absl::visit(Visitor(), A(std::string(
"BBBBB")),
1888 B(std::unique_ptr<int>(
new int(7)))),
1889 ::testing::Pair(5, 7));
1890 EXPECT_THAT(
absl::visit(Visitor(), A(std::string(
"BBBBB")),
1891 B(absl::string_view(
"ABC"))),
1892 ::testing::Pair(5, 3));
1895 TEST(VariantTest, VisitNoArgs) {
1899 struct ConstFunctor {
1900 int operator()(
int a,
int b)
const {
return a -
b; }
1903 struct MutableFunctor {
1904 int operator()(
int a,
int b) {
return a -
b; }
1908 int Method(
int a,
int b) {
return a -
b; }
1909 int ConstMethod(
int a,
int b)
const {
return a -
b; }
1914 TEST(VariantTest, VisitReferenceWrapper) {
1920 EXPECT_EQ(1,
absl::visit(std::cref(cf), three, two));
1921 EXPECT_EQ(1,
absl::visit(std::ref(cf), three, two));
1922 EXPECT_EQ(1,
absl::visit(std::ref(mf), three, two));
1926 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) 1927 TEST(VariantTest, VisitMemberFunction) {
1930 absl::make_unique<const Class>());
1934 EXPECT_EQ(1,
absl::visit(&Class::Method, p, three, two));
1935 EXPECT_EQ(1,
absl::visit(&Class::ConstMethod, p, three, two));
1936 EXPECT_EQ(1,
absl::visit(&Class::ConstMethod, cp, three, two));
1939 TEST(VariantTest, VisitDataMember) {
1942 absl::make_unique<const Class>(Class{42}));
1950 #endif // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) 1956 TEST(VariantTest, MonostateBasic) {
1969 TEST(VariantTest, VariantMonostateDefaultConstruction) {
1971 EXPECT_EQ(var.index(), 0);
1978 TEST(VariantTest, MonostateComparisons) {
1981 EXPECT_EQ(lhs, lhs);
1982 EXPECT_EQ(lhs, rhs);
1984 EXPECT_FALSE(lhs != lhs);
1985 EXPECT_FALSE(lhs != rhs);
1986 EXPECT_FALSE(lhs < lhs);
1987 EXPECT_FALSE(lhs < rhs);
1988 EXPECT_FALSE(lhs > lhs);
1989 EXPECT_FALSE(lhs > rhs);
1991 EXPECT_LE(lhs, lhs);
1992 EXPECT_LE(lhs, rhs);
1993 EXPECT_GE(lhs, lhs);
1994 EXPECT_GE(lhs, rhs);
1996 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() ==
1997 std::declval<absl::monostate>()));
1998 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() !=
1999 std::declval<absl::monostate>()));
2000 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <
2001 std::declval<absl::monostate>()));
2002 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >
2003 std::declval<absl::monostate>()));
2004 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <=
2005 std::declval<absl::monostate>()));
2006 EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >=
2007 std::declval<absl::monostate>()));
2014 TEST(VariantTest, NonmemberSwap) {
2020 variant<SpecialSwap>
a = v1,
b = v2;
2022 EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
2023 EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
2026 EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
2027 EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
2028 #ifndef ABSL_HAVE_STD_VARIANT 2033 EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
2034 EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
2035 EXPECT_TRUE(absl::get<SpecialSwap>(b).special_swap);
2042 TEST(VariantTest, BadAccess) {
2045 std::exception* base = &exception_obj;
2053 TEST(VariantTest, MonostateHash) {
2056 static_assert(std::is_same<decltype(
hasher(mono)), std::size_t>::
value,
"");
2061 static_assert(type_traits_internal::IsHashable<variant<int>>::
value,
"");
2062 static_assert(type_traits_internal::IsHashable<variant<Hashable>>::
value,
"");
2063 static_assert(type_traits_internal::IsHashable<variant<int, Hashable>>::
value,
2066 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 2067 static_assert(!type_traits_internal::IsHashable<variant<NonHashable>>::
value,
2070 !type_traits_internal::IsHashable<variant<Hashable, NonHashable>>::
value,
2076 #if !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT)) 2079 variant<int, int> v0(in_place_index<0>, 42);
2080 variant<int, int> v1(in_place_index<1>, 42);
2081 std::hash<variant<int, int>>
hash;
2084 #endif // !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT)) 2087 std::hash<variant<int>>
hash;
2088 std::set<size_t> hashcodes;
2089 for (
int i = 0;
i < 100; ++
i) {
2090 hashcodes.insert(
hash(
i));
2092 EXPECT_GT(hashcodes.size(), 90);
2095 static_assert(type_traits_internal::IsHashable<variant<const int>>::
value,
2098 type_traits_internal::IsHashable<variant<const Hashable>>::
value,
"");
2099 std::hash<absl::variant<const int>> c_hash;
2100 for (
int i = 0;
i < 100; ++
i) {
2101 EXPECT_EQ(
hash(
i), c_hash(
i));
2111 TEST(VariantTest, TestConvertingSet) {
2112 typedef variant<double> Variant;
2116 EXPECT_TRUE(absl::holds_alternative<double>(v));
2117 ASSERT_TRUE(
nullptr != absl::get_if<double>(&v));
2118 EXPECT_DOUBLE_EQ(2, absl::get<double>(v));
2122 TEST(VariantTest, Container) {
2123 typedef variant<int, float> Variant;
2126 std::vector<Variant> vec;
2127 vec.push_back(Variant(10));
2128 vec.push_back(Variant(20.0f));
2131 vec.resize(10, Variant(0));
2136 TEST(VariantTest, TestVariantWithNonCopyableType) {
2137 typedef variant<int, NonCopyable> Variant;
2138 const int kValue = 1;
2140 ASSERT_TRUE(absl::holds_alternative<int>(v));
2141 EXPECT_EQ(kValue, absl::get<int>(v));
2151 TEST(VariantTest, TestEmplace) {
2152 typedef variant<int, NonCopyable> Variant;
2153 const int kValue = 1;
2155 ASSERT_TRUE(absl::holds_alternative<int>(v));
2156 EXPECT_EQ(kValue, absl::get<int>(v));
2159 v.emplace<NonCopyable>();
2160 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2161 EXPECT_EQ(0, absl::get<NonCopyable>(v).
value);
2163 ASSERT_TRUE(absl::holds_alternative<int>(v));
2166 v.emplace<NonCopyable>(1);
2167 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2168 EXPECT_EQ(1, absl::get<NonCopyable>(v).value);
2170 ASSERT_TRUE(absl::holds_alternative<int>(v));
2173 v.emplace<NonCopyable>(1, 2);
2174 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2175 EXPECT_EQ(3, absl::get<NonCopyable>(v).value);
2177 ASSERT_TRUE(absl::holds_alternative<int>(v));
2180 v.emplace<NonCopyable>(1, 2, 3);
2181 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2182 EXPECT_EQ(6, absl::get<NonCopyable>(v).value);
2184 ASSERT_TRUE(absl::holds_alternative<int>(v));
2187 v.emplace<NonCopyable>(1, 2, 3, 4);
2188 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2189 EXPECT_EQ(10, absl::get<NonCopyable>(v).value);
2191 ASSERT_TRUE(absl::holds_alternative<int>(v));
2194 TEST(VariantTest, TestEmplaceDestroysCurrentValue) {
2195 typedef variant<int, IncrementInDtor, NonCopyable> Variant;
2198 ASSERT_TRUE(absl::holds_alternative<int>(v));
2199 v.emplace<IncrementInDtor>(&
counter);
2200 ASSERT_TRUE(absl::holds_alternative<IncrementInDtor>(v));
2201 ASSERT_EQ(0, counter);
2202 v.emplace<NonCopyable>();
2203 ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2204 EXPECT_EQ(1, counter);
2207 TEST(VariantTest, TestMoveSemantics) {
2208 typedef variant<std::unique_ptr<int>, std::unique_ptr<std::string>> Variant;
2217 ASSERT_NE(
nullptr,
absl::get<std::unique_ptr<int>>(v2));
2218 EXPECT_EQ(10, *
absl::get<std::unique_ptr<int>>(v2));
2223 ASSERT_NE(
nullptr,
absl::get_if<std::unique_ptr<int>>(&v));
2224 EXPECT_EQ(
nullptr,
absl::get<std::unique_ptr<int>>(v));
2227 v = absl::make_unique<std::string>(
"foo");
2229 EXPECT_EQ(
"foo", *
absl::get<std::unique_ptr<std::string>>(v));
2234 EXPECT_EQ(
"foo", *
absl::get<std::unique_ptr<std::string>>(v2));
2238 variant<int, std::string> PassThrough(
const variant<int, std::string>&
arg) {
2242 TEST(VariantTest, TestImplicitConversion) {
2243 EXPECT_TRUE(absl::holds_alternative<int>(PassThrough(0)));
2248 absl::holds_alternative<std::string>(PassThrough(std::string(
"foo"))));
2251 struct Convertible2;
2252 struct Convertible1 {
2254 Convertible1(
const Convertible1&) {}
2255 Convertible1& operator=(
const Convertible1&) {
return *
this; }
2258 Convertible1(
const Convertible2&) {}
2261 struct Convertible2 {
2263 Convertible2(
const Convertible2&) {}
2264 Convertible2& operator=(
const Convertible2&) {
return *
this; }
2267 Convertible2(
const Convertible1&) {}
2270 TEST(VariantTest, TestRvalueConversion) {
2271 variant<double, std::string> var(
2273 variant<std::string, int>(0)));
2274 ASSERT_TRUE(absl::holds_alternative<double>(var));
2275 EXPECT_EQ(0.0, absl::get<double>(var));
2277 var = ConvertVariantTo<variant<double, std::string>>(
2278 variant<const char*, float>(
"foo"));
2279 ASSERT_TRUE(absl::holds_alternative<std::string>(var));
2280 EXPECT_EQ(
"foo", absl::get<std::string>(var));
2282 variant<double> singleton(
2284 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2285 EXPECT_EQ(42.0, absl::get<double>(singleton));
2287 singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
2288 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2289 EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2291 singleton = ConvertVariantTo<variant<double>>(variant<int>(0));
2292 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2293 EXPECT_EQ(0.0, absl::get<double>(singleton));
2295 variant<int32_t, uint32_t> variant2(
2297 ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
2298 EXPECT_EQ(42, absl::get<int32_t>(variant2));
2300 variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
2301 ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
2302 EXPECT_EQ(42, absl::get<uint32_t>(variant2));
2304 variant<Convertible1, Convertible2> variant3(
2306 (variant<Convertible2, Convertible1>(Convertible1()))));
2307 ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2309 variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
2310 variant<Convertible2, Convertible1>(Convertible2()));
2311 ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2314 TEST(VariantTest, TestLvalueConversion) {
2315 variant<std::string, int> source1 = 0;
2316 variant<double, std::string> destination(
2318 ASSERT_TRUE(absl::holds_alternative<double>(destination));
2319 EXPECT_EQ(0.0, absl::get<double>(destination));
2321 variant<const char*, float> source2 =
"foo";
2322 destination = ConvertVariantTo<variant<double, std::string>>(source2);
2323 ASSERT_TRUE(absl::holds_alternative<std::string>(destination));
2324 EXPECT_EQ(
"foo", absl::get<std::string>(destination));
2326 variant<int, float> source3(42);
2328 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2329 EXPECT_EQ(42.0, absl::get<double>(singleton));
2332 singleton = ConvertVariantTo<variant<double>>(source3);
2333 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2334 EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2336 variant<int> source4(0);
2337 singleton = ConvertVariantTo<variant<double>>(source4);
2338 ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2339 EXPECT_EQ(0.0, absl::get<double>(singleton));
2341 variant<int32_t> source5(42);
2342 variant<int32_t, uint32_t> variant2(
2344 ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
2345 EXPECT_EQ(42, absl::get<int32_t>(variant2));
2347 variant<uint32_t> source6(42);
2348 variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
2349 ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
2350 EXPECT_EQ(42, absl::get<uint32_t>(variant2));
2352 variant<Convertible2, Convertible1> source7((Convertible1()));
2353 variant<Convertible1, Convertible2> variant3(
2355 ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2357 source7 = Convertible2();
2358 variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
2359 ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2362 TEST(VariantTest, TestMoveConversion) {
2364 variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
2365 using OtherVariant =
2366 variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
2369 ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(0)}));
2371 ASSERT_NE(
absl::get<std::unique_ptr<const int>>(var),
nullptr);
2372 EXPECT_EQ(0, *
absl::get<std::unique_ptr<const int>>(var));
2374 var = ConvertVariantTo<Variant>(
2375 OtherVariant(absl::make_unique<std::string>(
"foo")));
2377 EXPECT_EQ(
"foo", *
absl::get<std::unique_ptr<const std::string>>(var));
2380 TEST(VariantTest, DoesNotMoveFromLvalues) {
2385 variant<std::shared_ptr<const int>, std::shared_ptr<const std::string>>;
2386 using OtherVariant =
2387 variant<std::shared_ptr<int>, std::shared_ptr<std::string>>;
2389 Variant v1(std::make_shared<const int>(0));
2393 EXPECT_EQ(
absl::get<std::shared_ptr<const int>>(v1),
2394 absl::get<std::shared_ptr<const int>>(v2));
2397 v1 = std::make_shared<const std::string>(
"foo");
2399 EXPECT_EQ(
absl::get<std::shared_ptr<const std::string>>(v1),
2400 absl::get<std::shared_ptr<const std::string>>(v2));
2403 OtherVariant other(std::make_shared<int>(0));
2404 Variant v3(ConvertVariantTo<Variant>(other));
2405 EXPECT_EQ(
absl::get<std::shared_ptr<int>>(other),
2406 absl::get<std::shared_ptr<const int>>(v3));
2408 other = std::make_shared<std::string>(
"foo");
2409 v3 = ConvertVariantTo<Variant>(other);
2410 EXPECT_EQ(
absl::get<std::shared_ptr<std::string>>(other),
2411 absl::get<std::shared_ptr<const std::string>>(v3));
2414 TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) {
2415 variant<double, std::string> var(
2417 variant<std::string, int>(3)));
2418 EXPECT_THAT(absl::get_if<double>(&var), Pointee(3.0));
2420 var = ConvertVariantTo<variant<double, std::string>>(
2421 variant<const char*, float>(
"foo"));
2422 EXPECT_THAT(absl::get_if<std::string>(&var), Pointee(std::string(
"foo")));
2424 variant<double> singleton(
2426 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
2428 singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
2429 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
2431 singleton = ConvertVariantTo<variant<double>>(variant<int>(3));
2432 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
2434 variant<int32_t, uint32_t> variant2(
2436 EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
2438 variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
2439 EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
2441 variant<Convertible1, Convertible2> variant3(
2443 (variant<Convertible2, Convertible1>(Convertible1()))));
2444 ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2446 variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
2447 variant<Convertible2, Convertible1>(Convertible2()));
2448 ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2451 TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) {
2452 variant<std::string, int> source1 = 3;
2453 variant<double, std::string> destination(
2455 EXPECT_THAT(absl::get_if<double>(&destination), Pointee(3.0));
2457 variant<const char*, float> source2 =
"foo";
2458 destination = ConvertVariantTo<variant<double, std::string>>(source2);
2459 EXPECT_THAT(absl::get_if<std::string>(&destination),
2460 Pointee(std::string(
"foo")));
2462 variant<int, float> source3(42);
2464 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
2467 singleton = ConvertVariantTo<variant<double>>(source3);
2468 EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2469 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
2471 variant<int> source4(3);
2472 singleton = ConvertVariantTo<variant<double>>(source4);
2473 EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
2475 variant<int32_t> source5(42);
2476 variant<int32_t, uint32_t> variant2(
2478 EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
2480 variant<uint32_t> source6(42);
2481 variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
2482 EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
2484 variant<Convertible2, Convertible1> source7((Convertible1()));
2485 variant<Convertible1, Convertible2> variant3(
2487 ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2489 source7 = Convertible2();
2490 variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
2491 ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2494 TEST(VariantTest, TestMoveConversionViaConvertVariantTo) {
2496 variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
2497 using OtherVariant =
2498 variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
2501 ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(3)}));
2502 EXPECT_THAT(
absl::get_if<std::unique_ptr<const int>>(&var),
2503 Pointee(Pointee(3)));
2505 var = ConvertVariantTo<Variant>(
2506 OtherVariant(absl::make_unique<std::string>(
"foo")));
2507 EXPECT_THAT(
absl::get_if<std::unique_ptr<const std::string>>(&var),
2508 Pointee(Pointee(std::string(
"foo"))));
2516 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) 2517 #define ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 1 2520 TEST(VariantTest, TestCopyAndMoveTypeTraits) {
2521 EXPECT_TRUE(std::is_copy_constructible<variant<std::string>>::
value);
2523 EXPECT_TRUE(std::is_move_constructible<variant<std::string>>::
value);
2525 EXPECT_TRUE(std::is_move_constructible<variant<std::unique_ptr<int>>>::
value);
2528 std::is_copy_constructible<variant<std::unique_ptr<int>>>::
value);
2534 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2537 EXPECT_TRUE(is_trivially_move_constructible<variant<int>>::
value);
2538 EXPECT_TRUE(is_trivially_move_assignable<variant<int>>::
value);
2539 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2542 TEST(VariantTest, TestVectorOfMoveonlyVariant) {
2544 std::vector<variant<std::unique_ptr<int>, std::string>> vec;
2545 vec.push_back(absl::make_unique<int>(42));
2546 vec.emplace_back(
"Hello");
2550 ASSERT_EQ(2, another_vec.size());
2551 EXPECT_EQ(42, *
absl::get<std::unique_ptr<int>>(another_vec[0]));
2552 EXPECT_EQ(
"Hello", absl::get<std::string>(another_vec[1]));
2555 TEST(VariantTest, NestedVariant) {
2556 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2559 static_assert(is_trivially_move_constructible<variant<int>>(),
"");
2560 static_assert(is_trivially_move_assignable<variant<int>>(),
"");
2566 static_assert(is_trivially_move_constructible<variant<variant<int>>>(),
"");
2567 static_assert(is_trivially_move_assignable<variant<variant<int>>>(),
"");
2568 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2571 variant<variant<int>> y(x);
2572 variant<variant<int>> z(y);
2574 EXPECT_EQ(x,
absl::get<variant<int>>(z));
2577 struct TriviallyDestructible {
2578 TriviallyDestructible(TriviallyDestructible&&) {}
2579 TriviallyDestructible(
const TriviallyDestructible&) {}
2580 TriviallyDestructible& operator=(TriviallyDestructible&&) {
return *
this; }
2581 TriviallyDestructible& operator=(
const TriviallyDestructible&) {
2586 struct TriviallyMovable {
2587 TriviallyMovable(TriviallyMovable&&) =
default;
2588 TriviallyMovable(TriviallyMovable
const&) {}
2589 TriviallyMovable& operator=(
const TriviallyMovable&) {
return *
this; }
2592 struct TriviallyCopyable {
2593 TriviallyCopyable(
const TriviallyCopyable&) =
default;
2594 TriviallyCopyable& operator=(
const TriviallyCopyable&) {
return *
this; }
2597 struct TriviallyMoveAssignable {
2598 TriviallyMoveAssignable(TriviallyMoveAssignable&&) =
default;
2599 TriviallyMoveAssignable(
const TriviallyMoveAssignable&) {}
2600 TriviallyMoveAssignable& operator=(TriviallyMoveAssignable&&) =
default;
2601 TriviallyMoveAssignable& operator=(
const TriviallyMoveAssignable&) {
2606 struct TriviallyCopyAssignable {};
2608 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2609 TEST(VariantTest, TestTriviality) {
2662 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 2666 TEST(VariantTest, MoveCtorBug) {
2668 struct TrivialCopyNontrivialMove {
2669 TrivialCopyNontrivialMove() =
default;
2670 TrivialCopyNontrivialMove(
const TrivialCopyNontrivialMove&) =
default;
2671 TrivialCopyNontrivialMove(TrivialCopyNontrivialMove&&) { called =
true; }
2672 bool called =
false;
2676 V v1(absl::in_place_index<0>);
2679 EXPECT_TRUE(absl::get<0>(v2).called);
2684 V v1(absl::in_place_index<1>);
2687 EXPECT_TRUE(absl::get<1>(v2).called);
typename variant_alternative< I, T >::type variant_alternative_t
constexpr absl::add_pointer_t< variant_alternative_t< I, variant< Types... > > > get_if(variant< Types... > *v) noexcept
bool operator<(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
std::unique_ptr< T > WrapUnique(T *ptr)
static size_t StrLen(const char *str)
std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
To ConvertVariantTo(Variant &&variant)
void in_place_type(utility_internal::InPlaceTypeTag< T >)
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
constexpr size_type size() const noexcept
bool operator>(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
constexpr T & get(variant< Types... > &v)
hash_default_hash< T > hasher
static const size_t kIndex
#define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator>=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
hash_default_hash< typename T::first_type > hash
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
variant_internal::VisitResult< Visitor, Variants... > visit(Visitor &&vis, Variants &&... vars)
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
constexpr bool holds_alternative(const variant< Types... > &v) noexcept
bool operator<=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)