18 #include <type_traits> 21 #include "gtest/gtest.h" 40 std::string TypeQuals(std::string&) {
return "&"; }
41 std::string TypeQuals(std::string&&) {
return "&&"; }
42 std::string TypeQuals(
const std::string&) {
return "c&"; }
43 std::string TypeQuals(
const std::string&&) {
return "c&&"; }
45 struct StructorListener {
55 int volatile_copy = 0;
56 int volatile_move = 0;
57 int volatile_copy_assign = 0;
58 int volatile_move_assign = 0;
66 #pragma warning( push ) 67 #pragma warning( disable : 4521) 68 #pragma warning( disable : 4522) 71 static StructorListener* listener;
73 Listenable() { ++listener->construct0; }
74 explicit Listenable(
int ) { ++listener->construct1; }
75 Listenable(
int ,
int ) { ++listener->construct2; }
76 Listenable(std::initializer_list<int> ) { ++listener->listinit; }
77 Listenable(
const Listenable& ) { ++listener->copy; }
78 Listenable(
const volatile Listenable& ) {
79 ++listener->volatile_copy;
81 Listenable(
volatile Listenable&& ) { ++listener->volatile_move; }
82 Listenable(Listenable&& ) { ++listener->move; }
83 Listenable& operator=(
const Listenable& ) {
84 ++listener->copy_assign;
87 Listenable& operator=(Listenable&& ) {
88 ++listener->move_assign;
93 void operator=(
const volatile Listenable& )
volatile {
94 ++listener->volatile_copy_assign;
96 void operator=(
volatile Listenable&& )
volatile {
97 ++listener->volatile_move_assign;
99 ~Listenable() { ++listener->destruct; }
102 #pragma warning( pop ) 105 StructorListener* Listenable::listener =
nullptr;
111 #if defined(_LIBCPP_VERSION) && \ 112 (_LIBCPP_STD_VER <= 11 || defined(_LIBCPP_HAS_NO_CXX14_CONSTEXPR)) 113 #define ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST 1 116 struct ConstexprType {
120 kCtorInitializerList,
123 constexpr ConstexprType() : x(kCtorDefault) {}
124 constexpr
explicit ConstexprType(
int i) : x(kCtorInt) {}
125 #ifndef ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST 126 constexpr ConstexprType(std::initializer_list<int> il)
127 : x(kCtorInitializerList) {}
129 constexpr ConstexprType(
const char*)
130 : x(kCtorConstChar) {}
136 Copyable(
const Copyable&) {}
137 Copyable& operator=(
const Copyable&) {
return *
this; }
140 struct MoveableThrow {
142 MoveableThrow(MoveableThrow&&) {}
143 MoveableThrow& operator=(MoveableThrow&&) {
return *
this; }
146 struct MoveableNoThrow {
148 MoveableNoThrow(MoveableNoThrow&&) noexcept {}
149 MoveableNoThrow& operator=(MoveableNoThrow&&) noexcept {
return *
this; }
154 NonMovable(
const NonMovable&) =
delete;
155 NonMovable& operator=(
const NonMovable&) =
delete;
156 NonMovable(NonMovable&&) =
delete;
157 NonMovable& operator=(NonMovable&&) =
delete;
161 NoDefault() =
delete;
162 NoDefault(
const NoDefault&) {}
163 NoDefault& operator=(
const NoDefault&) {
return *
this; }
166 struct ConvertsFromInPlaceT {
170 TEST(optionalTest, DefaultConstructor) {
174 static_assert(!cempty.has_value(),
"");
179 TEST(optionalTest, nulloptConstructor) {
183 static_assert(!cempty.has_value(),
"");
188 TEST(optionalTest, CopyConstructor) {
192 EXPECT_FALSE(empty_copy);
194 EXPECT_TRUE(opt42_copy);
195 EXPECT_EQ(42, *opt42_copy);
200 EXPECT_FALSE(empty_copy);
202 EXPECT_TRUE(opt42_copy);
203 EXPECT_EQ(42, *opt42_copy);
208 EXPECT_FALSE(empty_copy);
210 EXPECT_TRUE(opt42_copy);
211 EXPECT_EQ(42, *opt42_copy);
224 #if defined(ABSL_HAVE_STD_OPTIONAL) && defined(__GLIBCXX__) 228 #define ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG 1 230 #ifndef ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG 245 #endif // ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG 251 static_assert(!o2,
"");
256 static_assert(o2,
"");
257 static_assert(*o2 == 42,
"");
260 struct TrivialCopyable {
261 constexpr TrivialCopyable() : x(0) {}
262 constexpr
explicit TrivialCopyable(
int i) : x(i) {}
267 static_assert(o2,
"");
268 static_assert((*o2).x == 42,
"");
269 #ifndef ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG 279 #if defined(ABSL_HAVE_STD_OPTIONAL) && defined(_MSC_VER) && _MSC_VER >= 1911 280 #define ABSL_MSVC_OPTIONAL_VOLATILE_COPY_BUG 1 282 #ifndef ABSL_MSVC_OPTIONAL_VOLATILE_COPY_BUG 283 EXPECT_FALSE(std::is_copy_constructible<
289 TEST(optionalTest, MoveConstructor) {
292 EXPECT_FALSE(empty_move);
294 EXPECT_TRUE(opt42_move);
295 EXPECT_EQ(42, opt42_move);
305 #ifndef ABSL_HAVE_STD_OPTIONAL 310 EXPECT_TRUE(std::is_nothrow_move_constructible<
314 TEST(optionalTest, Destructor) {
318 NonTrivial(
const NonTrivial&) {}
319 NonTrivial& operator=(
const NonTrivial&) {
return *
this; }
329 TEST(optionalTest, InPlaceConstructor) {
331 static_assert(opt0,
"");
332 static_assert((*opt0).x == ConstexprType::kCtorDefault,
"");
334 static_assert(opt1,
"");
335 static_assert((*opt1).x == ConstexprType::kCtorInt,
"");
336 #ifndef ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST 338 static_assert(opt2,
"");
339 static_assert((*opt2).x == ConstexprType::kCtorInitializerList,
"");
357 TEST(optionalTest, ValueConstructor) {
359 static_assert(opt0,
"");
360 static_assert(*opt0 == 0,
"");
368 static_assert(opt1,
"");
369 static_assert(ConstexprType::kCtorConstChar == (*opt1).x,
"");
374 static_assert(opt2,
"");
375 static_assert(ConstexprType::kCtorInt == (*opt2).x,
"");
388 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 7 && \ 389 __cplusplus == 201703L 390 #define ABSL_GCC7_OVER_ICS_LIST_BUG 1 392 #ifndef ABSL_GCC7_OVER_ICS_LIST_BUG 394 static_assert(opt3,
"");
395 static_assert(*opt3 == 0,
"");
409 Convert(
const Implicit&)
410 : implicit(
true),
move(
false) {}
412 : implicit(
true),
move(
true) {}
413 explicit Convert(
const Explicit&) : implicit(
false),
move(
false) {}
414 explicit Convert(Explicit&&) : implicit(
false),
move(
true) {}
420 struct ConvertFromOptional {
421 ConvertFromOptional(
const Implicit&)
422 : implicit(
true),
move(
false), from_optional(
false) {}
423 ConvertFromOptional(Implicit&&)
424 : implicit(
true),
move(
true), from_optional(
false) {}
427 : implicit(
true),
move(
false), from_optional(
true) {}
429 : implicit(
true),
move(
true), from_optional(
true) {}
430 explicit ConvertFromOptional(
const Explicit&)
431 : implicit(
false),
move(
false), from_optional(
false) {}
432 explicit ConvertFromOptional(Explicit&&)
433 : implicit(
false),
move(
true), from_optional(
false) {}
435 : implicit(
false),
move(
false), from_optional(
true) {}
437 : implicit(
false),
move(
true), from_optional(
true) {}
444 TEST(optionalTest, ConvertingConstructor) {
455 EXPECT_TRUE(opt_copy);
456 EXPECT_TRUE(opt_copy->implicit);
457 EXPECT_FALSE(opt_copy->move);
459 EXPECT_TRUE(opt_move);
460 EXPECT_TRUE(opt_move->implicit);
461 EXPECT_TRUE(opt_move->move);
469 EXPECT_TRUE(opt_copy);
470 EXPECT_FALSE(opt_copy->implicit);
471 EXPECT_FALSE(opt_copy->move);
475 EXPECT_TRUE(opt_move);
476 EXPECT_FALSE(opt_move->implicit);
477 EXPECT_TRUE(opt_move->move);
492 EXPECT_TRUE(opt0->implicit);
493 EXPECT_FALSE(opt0->move);
494 EXPECT_TRUE(opt0->from_optional);
497 EXPECT_TRUE(opt1->implicit);
498 EXPECT_TRUE(opt1->move);
499 EXPECT_TRUE(opt1->from_optional);
508 EXPECT_FALSE(opt0->implicit);
509 EXPECT_FALSE(opt0->move);
510 EXPECT_TRUE(opt0->from_optional);
516 EXPECT_FALSE(opt1->implicit);
517 EXPECT_TRUE(opt1->move);
518 EXPECT_TRUE(opt1->from_optional);
525 TEST(optionalTest, StructorBasic) {
526 StructorListener listener;
527 Listenable::listener = &listener;
538 EXPECT_EQ(1, listener.construct0);
539 EXPECT_EQ(1, listener.construct1);
540 EXPECT_EQ(1, listener.construct2);
541 EXPECT_EQ(3, listener.destruct);
544 TEST(optionalTest, CopyMoveStructor) {
545 StructorListener listener;
546 Listenable::listener = &listener;
548 EXPECT_EQ(1, listener.construct0);
549 EXPECT_EQ(0, listener.copy);
550 EXPECT_EQ(0, listener.move);
552 EXPECT_EQ(1, listener.construct0);
553 EXPECT_EQ(1, listener.copy);
554 EXPECT_EQ(0, listener.move);
556 EXPECT_EQ(1, listener.construct0);
557 EXPECT_EQ(1, listener.copy);
558 EXPECT_EQ(1, listener.move);
561 TEST(optionalTest, ListInit) {
562 StructorListener listener;
563 Listenable::listener = &listener;
566 EXPECT_EQ(2, listener.listinit);
569 TEST(optionalTest, AssignFromNullopt) {
574 StructorListener listener;
575 Listenable::listener = &listener;
577 opt1 = absl::nullopt;
579 EXPECT_EQ(1, listener.construct0);
580 EXPECT_EQ(1, listener.destruct);
588 TEST(optionalTest, CopyAssignment) {
592 EXPECT_FALSE(empty_to_opt1);
593 empty_to_opt1 = empty;
594 EXPECT_FALSE(empty_to_opt1);
595 empty_to_opt1 = opt1;
596 EXPECT_TRUE(empty_to_opt1);
597 EXPECT_EQ(1, empty_to_opt1.
value());
599 EXPECT_FALSE(opt1_to_opt2);
601 EXPECT_TRUE(opt1_to_opt2);
602 EXPECT_EQ(1, opt1_to_opt2.
value());
604 EXPECT_TRUE(opt1_to_opt2);
605 EXPECT_EQ(2, opt1_to_opt2.value());
607 EXPECT_FALSE(opt2_to_empty);
608 opt2_to_empty = opt2;
609 EXPECT_TRUE(opt2_to_empty);
610 EXPECT_EQ(2, opt2_to_empty.
value());
611 opt2_to_empty = empty;
612 EXPECT_FALSE(opt2_to_empty);
628 NonTrivial& operator=(
const NonTrivial&) {
return *
this; }
639 #ifndef ABSL_HAVE_STD_OPTIONAL 641 StructorListener listener;
642 Listenable::listener = &listener;
645 EXPECT_EQ(1, listener.construct0);
647 set_to_empty(absl::in_place), set_to_set(absl::in_place);
648 EXPECT_EQ(3, listener.construct0);
649 empty_to_empty = empty;
651 set_to_empty = empty;
653 EXPECT_EQ(1, listener.volatile_copy);
654 EXPECT_EQ(0, listener.volatile_move);
655 EXPECT_EQ(1, listener.destruct);
656 EXPECT_EQ(1, listener.volatile_copy_assign);
658 #endif // ABSL_HAVE_STD_OPTIONAL 661 TEST(optionalTest, MoveAssignment) {
663 StructorListener listener;
664 Listenable::listener = &listener;
667 set2(absl::in_place);
668 EXPECT_EQ(2, listener.construct0);
670 set_to_empty(absl::in_place), set_to_set(absl::in_place);
671 EXPECT_EQ(4, listener.construct0);
676 EXPECT_EQ(0, listener.copy);
677 EXPECT_EQ(1, listener.move);
678 EXPECT_EQ(1, listener.destruct);
679 EXPECT_EQ(1, listener.move_assign);
682 #ifndef ABSL_HAVE_STD_OPTIONAL 684 StructorListener listener;
685 Listenable::listener = &listener;
688 set2(absl::in_place);
689 EXPECT_EQ(2, listener.construct0);
691 set_to_empty(absl::in_place), set_to_set(absl::in_place);
692 EXPECT_EQ(4, listener.construct0);
697 EXPECT_EQ(0, listener.volatile_copy);
698 EXPECT_EQ(1, listener.volatile_move);
699 EXPECT_EQ(1, listener.destruct);
700 EXPECT_EQ(1, listener.volatile_move_assign);
702 #endif // ABSL_HAVE_STD_OPTIONAL 715 struct NoConvertToOptional {
718 NoConvertToOptional(
const NoConvertToOptional&) =
delete;
722 CopyConvert(
const NoConvertToOptional&);
723 CopyConvert& operator=(
const CopyConvert&) =
delete;
724 CopyConvert& operator=(
const NoConvertToOptional&);
727 struct CopyConvertFromOptional {
728 CopyConvertFromOptional(
const NoConvertToOptional&);
730 CopyConvertFromOptional& operator=(
const CopyConvertFromOptional&) =
delete;
731 CopyConvertFromOptional& operator=(
const NoConvertToOptional&);
732 CopyConvertFromOptional& operator=(
737 MoveConvert(NoConvertToOptional&&);
738 MoveConvert& operator=(
const MoveConvert&) =
delete;
739 MoveConvert& operator=(NoConvertToOptional&&);
742 struct MoveConvertFromOptional {
743 MoveConvertFromOptional(NoConvertToOptional&&);
745 MoveConvertFromOptional& operator=(
const MoveConvertFromOptional&) =
delete;
746 MoveConvertFromOptional& operator=(NoConvertToOptional&&);
751 TEST(optionalTest, ValueAssignment) {
756 EXPECT_EQ(42, opt.
value());
761 EXPECT_EQ(42, opt.value());
764 EXPECT_EQ(43, opt.value());
770 EXPECT_EQ(44, opt.value());
774 const NoConvertToOptional&>::
value));
777 const NoConvertToOptional&>::
value));
781 const NoConvertToOptional&>::
value));
784 NoConvertToOptional&&>::
value));
789 const NoConvertToOptional&>::
value));
792 NoConvertToOptional&&>::
value));
806 TEST(optionalTest, ConvertingAssignment) {
811 EXPECT_EQ(*opt_c, *opt_i);
816 EXPECT_EQ(
'd', *opt_i);
821 EXPECT_TRUE(opt_str);
822 EXPECT_EQ(std::string(
"abc"), *opt_str);
824 EXPECT_FALSE(opt_str);
826 EXPECT_TRUE(opt_str);
827 EXPECT_EQ(std::string(
"def"), *opt_str);
854 TEST(optionalTest, ResetAndHasValue) {
855 StructorListener listener;
856 Listenable::listener = &listener;
866 EXPECT_EQ(1, listener.destruct);
872 static_assert(!empty.has_value(),
"");
874 static_assert(nonempty.has_value(),
"");
877 TEST(optionalTest, Emplace) {
878 StructorListener listener;
879 Listenable::listener = &listener;
885 EXPECT_EQ(1, listener.construct1);
886 EXPECT_EQ(1, listener.construct2);
887 EXPECT_EQ(1, listener.destruct);
890 EXPECT_TRUE((std::is_same<std::string&, decltype(o.
emplace(
"abc"))>::
value));
891 std::string& ref = o.
emplace(
"abc");
892 EXPECT_EQ(&ref, &o.
value());
895 TEST(optionalTest, ListEmplace) {
896 StructorListener listener;
897 Listenable::listener = &listener;
903 EXPECT_EQ(2, listener.listinit);
904 EXPECT_EQ(1, listener.destruct);
907 EXPECT_TRUE((std::is_same<Listenable&, decltype(o.
emplace({1}))>::
value));
908 Listenable& ref = o.
emplace({1});
909 EXPECT_EQ(&ref, &o.
value());
914 EXPECT_FALSE(opt_empty);
916 EXPECT_EQ(1, opt1.
value());
918 EXPECT_EQ(2, opt2.value());
919 swap(opt_empty, opt1);
921 EXPECT_TRUE(opt_empty);
922 EXPECT_EQ(1, opt_empty.
value());
924 EXPECT_EQ(2, opt2.value());
925 swap(opt_empty, opt1);
926 EXPECT_FALSE(opt_empty);
928 EXPECT_EQ(1, opt1.
value());
930 EXPECT_EQ(2, opt2.value());
932 EXPECT_FALSE(opt_empty);
934 EXPECT_EQ(2, opt1.
value());
936 EXPECT_EQ(1, opt2.value());
938 EXPECT_TRUE(noexcept(opt1.
swap(opt2)));
939 EXPECT_TRUE(noexcept(
swap(opt1, opt2)));
943 struct DeletedOpAddr {
944 constexpr
static const int value =
v;
945 constexpr DeletedOpAddr() =
default;
946 constexpr
const DeletedOpAddr<v>*
operator&()
const =
delete;
953 TEST(optionalTest, OperatorAddr) {
954 constexpr
const int v = -1;
957 static_assert(opt.has_value(),
"");
959 static_assert((*opt).value == v,
"");
963 EXPECT_TRUE(opt.has_value());
964 EXPECT_TRUE(opt->value == v);
965 EXPECT_TRUE((*opt).value == v);
969 TEST(optionalTest, PointerStuff) {
971 EXPECT_EQ(
"foo", *opt);
972 const auto& opt_const = opt;
973 EXPECT_EQ(
"foo", *opt_const);
974 EXPECT_EQ(opt->size(), 3);
975 EXPECT_EQ(opt_const->size(), 3);
978 static_assert((*opt1).x == ConstexprType::kCtorInt,
"");
985 #if defined(__GNUC__) && !defined(__clang__) 986 #define GCC_VERSION (__GNUC__ * 10000 \ 987 + __GNUC_MINOR__ * 100 \ 988 + __GNUC_PATCHLEVEL__) 989 #if GCC_VERSION < 40901 990 #define ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG 1009 #if defined(_MSC_VER) && _MSC_VER < 1910 1010 #define ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG 1013 TEST(optionalTest, Value) {
1017 O lvalue(absl::in_place,
"lvalue");
1018 CO clvalue(absl::in_place,
"clvalue");
1019 OC lvalue_c(absl::in_place,
"lvalue_c");
1020 EXPECT_EQ(
"lvalue", lvalue.value());
1021 EXPECT_EQ(
"clvalue", clvalue.value());
1022 EXPECT_EQ(
"lvalue_c", lvalue_c.value());
1023 EXPECT_EQ(
"xvalue", O(absl::in_place,
"xvalue").
value());
1024 EXPECT_EQ(
"xvalue_c", OC(absl::in_place,
"xvalue_c").
value());
1025 #ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG 1026 EXPECT_EQ(
"cxvalue", CO(absl::in_place,
"cxvalue").
value());
1028 EXPECT_EQ(
"&", TypeQuals(lvalue.value()));
1029 EXPECT_EQ(
"c&", TypeQuals(clvalue.value()));
1030 EXPECT_EQ(
"c&", TypeQuals(lvalue_c.value()));
1031 EXPECT_EQ(
"&&", TypeQuals(O(absl::in_place,
"xvalue").
value()));
1032 #if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ 1033 !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) 1034 EXPECT_EQ(
"c&&", TypeQuals(CO(absl::in_place,
"cxvalue").
value()));
1036 EXPECT_EQ(
"c&&", TypeQuals(OC(absl::in_place,
"xvalue_c").
value()));
1040 OV lvalue_v(absl::in_place, 42);
1041 EXPECT_EQ(42, lvalue_v.value());
1042 EXPECT_EQ(42, OV(42).
value());
1043 EXPECT_TRUE((std::is_same<
volatile int&, decltype(lvalue_v.value())>::
value));
1044 EXPECT_TRUE((std::is_same<
volatile int&&, decltype(OV(42).
value())>::
value));
1048 #ifdef ABSL_HAVE_EXCEPTIONS 1051 EXPECT_DEATH((
void)empty.
value(),
"Bad optional access");
1056 static_assert(1 == o1.value(),
"");
1057 #if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ 1058 !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) 1060 static_assert(2 == COI(2).
value(),
"");
1064 TEST(optionalTest, DerefOperator) {
1068 O lvalue(absl::in_place,
"lvalue");
1069 CO clvalue(absl::in_place,
"clvalue");
1070 OC lvalue_c(absl::in_place,
"lvalue_c");
1071 EXPECT_EQ(
"lvalue", *lvalue);
1072 EXPECT_EQ(
"clvalue", *clvalue);
1073 EXPECT_EQ(
"lvalue_c", *lvalue_c);
1074 EXPECT_EQ(
"xvalue", *O(absl::in_place,
"xvalue"));
1075 EXPECT_EQ(
"xvalue_c", *OC(absl::in_place,
"xvalue_c"));
1076 #ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG 1077 EXPECT_EQ(
"cxvalue", *CO(absl::in_place,
"cxvalue"));
1079 EXPECT_EQ(
"&", TypeQuals(*lvalue));
1080 EXPECT_EQ(
"c&", TypeQuals(*clvalue));
1081 EXPECT_EQ(
"&&", TypeQuals(*O(absl::in_place,
"xvalue")));
1082 #if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ 1083 !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) 1084 EXPECT_EQ(
"c&&", TypeQuals(*CO(absl::in_place,
"cxvalue")));
1086 EXPECT_EQ(
"c&&", TypeQuals(*OC(absl::in_place,
"xvalue_c")));
1090 OV lvalue_v(absl::in_place, 42);
1091 EXPECT_EQ(42, *lvalue_v);
1092 EXPECT_EQ(42, *OV(42));
1093 EXPECT_TRUE((std::is_same<
volatile int&, decltype(*lvalue_v)>::
value));
1094 EXPECT_TRUE((std::is_same<
volatile int&&, decltype(*OV(42))>::
value));
1097 static_assert(*opt1 == 1,
"");
1098 #if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ 1099 !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) 1101 static_assert(*COI(2) == 2,
"");
1105 TEST(optionalTest, ValueOr) {
1107 EXPECT_EQ(42.0, opt_empty.
value_or(42));
1108 EXPECT_EQ(1.2, opt_set.
value_or(42));
1113 static_assert(42.0 == copt_empty.value_or(42),
"");
1114 static_assert(1.2 == copt_set.value_or(42),
"");
1115 #ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG 1117 static_assert(42.0 == COD().value_or(42),
"");
1118 static_assert(1.2 == COD(1.2).value_or(42),
"");
1126 EXPECT_EQ(42, opt_int);
1128 StructorListener listener;
1129 Listenable::listener = &listener;
1132 EXPECT_EQ(1, listener.construct0);
1134 EXPECT_EQ(1, listener.construct1);
1136 EXPECT_EQ(1, listener.construct2);
1139 EXPECT_EQ(2, listener.listinit);
1147 static_assert(c_opt.value() == 42,
"");
1150 struct TrivialCopyable {
1151 constexpr TrivialCopyable() : x(0) {}
1152 constexpr
explicit TrivialCopyable(
int i) : x(i) {}
1156 constexpr TrivialCopyable
v;
1158 static_assert((*c_opt0).x == 0,
"");
1160 absl::make_optional<TrivialCopyable>();
1161 static_assert((*c_opt1).x == 0,
"");
1163 absl::make_optional<TrivialCopyable>(42);
1164 static_assert((*c_opt2).x == 42,
"");
1168 template <
typename T,
typename U>
1169 void optionalTest_Comparisons_EXPECT_LESS(T x, U y) {
1170 EXPECT_FALSE(x == y);
1171 EXPECT_TRUE(x != y);
1173 EXPECT_FALSE(x > y);
1174 EXPECT_TRUE(x <= y);
1175 EXPECT_FALSE(x >= y);
1178 template <
typename T,
typename U>
1179 void optionalTest_Comparisons_EXPECT_SAME(T x, U y) {
1180 EXPECT_TRUE(x == y);
1181 EXPECT_FALSE(x != y);
1182 EXPECT_FALSE(x < y);
1183 EXPECT_FALSE(x > y);
1184 EXPECT_TRUE(x <= y);
1185 EXPECT_TRUE(x >= y);
1188 template <
typename T,
typename U>
1189 void optionalTest_Comparisons_EXPECT_GREATER(T x, U y) {
1190 EXPECT_FALSE(x == y);
1191 EXPECT_TRUE(x != y);
1192 EXPECT_FALSE(x < y);
1194 EXPECT_FALSE(x <= y);
1195 EXPECT_TRUE(x >= y);
1199 template <
typename T,
typename U,
typename V>
1200 void TestComparisons() {
1209 optionalTest_Comparisons_EXPECT_SAME(absl::nullopt, be);
1210 optionalTest_Comparisons_EXPECT_LESS(absl::nullopt,
b2);
1212 optionalTest_Comparisons_EXPECT_LESS(absl::nullopt, b4);
1214 optionalTest_Comparisons_EXPECT_SAME(ae, absl::nullopt);
1215 optionalTest_Comparisons_EXPECT_SAME(ae, be);
1216 optionalTest_Comparisons_EXPECT_LESS(ae,
b2);
1217 optionalTest_Comparisons_EXPECT_LESS(ae, v3);
1218 optionalTest_Comparisons_EXPECT_LESS(ae, b4);
1220 optionalTest_Comparisons_EXPECT_GREATER(
a2, absl::nullopt);
1221 optionalTest_Comparisons_EXPECT_GREATER(
a2, be);
1222 optionalTest_Comparisons_EXPECT_SAME(
a2,
b2);
1223 optionalTest_Comparisons_EXPECT_LESS(
a2, v3);
1224 optionalTest_Comparisons_EXPECT_LESS(
a2, b4);
1227 optionalTest_Comparisons_EXPECT_GREATER(v3, be);
1228 optionalTest_Comparisons_EXPECT_GREATER(v3,
b2);
1229 optionalTest_Comparisons_EXPECT_SAME(v3, v3);
1230 optionalTest_Comparisons_EXPECT_LESS(v3, b4);
1232 optionalTest_Comparisons_EXPECT_GREATER(a4, absl::nullopt);
1233 optionalTest_Comparisons_EXPECT_GREATER(a4, be);
1234 optionalTest_Comparisons_EXPECT_GREATER(a4,
b2);
1235 optionalTest_Comparisons_EXPECT_GREATER(a4, v3);
1236 optionalTest_Comparisons_EXPECT_SAME(a4, b4);
1241 Int1(
int i) :
i(i) {}
1247 Int2(
int i) :
i(i) {}
1252 constexpr
bool operator==(
const Int1& lhs,
const Int2& rhs) {
1253 return lhs.i == rhs.i;
1255 constexpr
bool operator!=(
const Int1& lhs,
const Int2& rhs) {
1256 return !(lhs == rhs);
1258 constexpr
bool operator<(
const Int1& lhs,
const Int2& rhs) {
1259 return lhs.i < rhs.i;
1261 constexpr
bool operator<=(
const Int1& lhs,
const Int2& rhs) {
1262 return lhs < rhs || lhs == rhs;
1264 constexpr
bool operator>(
const Int1& lhs,
const Int2& rhs) {
1265 return !(lhs <= rhs);
1267 constexpr
bool operator>=(
const Int1& lhs,
const Int2& rhs) {
1268 return !(lhs < rhs);
1271 TEST(optionalTest, Comparisons) {
1272 TestComparisons<int, int, int>();
1273 TestComparisons<const int, int, int>();
1274 TestComparisons<Int1, int, int>();
1275 TestComparisons<int, Int2, int>();
1276 TestComparisons<Int1, Int2, int>();
1280 const char* cstr =
"abc";
1281 EXPECT_TRUE(opt_str == cstr);
1284 EXPECT_TRUE(opt_str == opt_cstr);
1288 EXPECT_TRUE(e1 == e2);
1292 TEST(optionalTest, SwapRegression) {
1293 StructorListener listener;
1294 Listenable::listener = &listener;
1302 EXPECT_EQ(1, listener.construct0);
1303 EXPECT_EQ(1, listener.move);
1304 EXPECT_EQ(2, listener.destruct);
1312 EXPECT_EQ(2, listener.construct0);
1313 EXPECT_EQ(2, listener.move);
1314 EXPECT_EQ(4, listener.destruct);
1317 TEST(optionalTest, BigStringLeakCheck) {
1318 constexpr
size_t n = 1 << 16;
1323 OS
b = absl::nullopt;
1324 OS c = std::string(n,
'c');
1325 std::string sd(n,
'd');
1327 OS e(absl::in_place, n,
'e');
1338 OS ob = absl::nullopt;
1339 OS oc = std::string(n,
'c');
1340 std::string sod(n,
'd');
1342 OS oe(absl::in_place, n,
'e');
1354 OS ab1 = absl::nullopt;
1355 OS ac1 = std::string(n,
'c');
1356 std::string sad1(n,
'd');
1358 OS ae1(absl::in_place, n,
'e');
1360 af1.emplace(n,
'f');
1363 OS ab2 = absl::nullopt;
1364 OS ac2 = std::string(n,
'c');
1365 std::string sad2(n,
'd');
1367 OS ae2(absl::in_place, n,
'e');
1369 af2.emplace(n,
'f');
1379 OS ab3 = absl::nullopt;
1380 OS ac3 = std::string(n,
'c');
1381 std::string sad3(n,
'd');
1383 OS ae3(absl::in_place, n,
'e');
1385 af3.emplace(n,
'f');
1387 aa3 = absl::nullopt;
1388 ab3 = absl::nullopt;
1389 ac3 = absl::nullopt;
1390 ad3 = absl::nullopt;
1391 ae3 = absl::nullopt;
1392 af3 = absl::nullopt;
1395 OS ab4 = absl::nullopt;
1396 OS ac4 = std::string(n,
'c');
1397 std::string sad4(n,
'd');
1399 OS ae4(absl::in_place, n,
'e');
1401 af4.emplace(n,
'f');
1403 aa4 = OS(absl::in_place, n,
'a');
1404 ab4 = OS(absl::in_place, n,
'b');
1405 ac4 = OS(absl::in_place, n,
'c');
1406 ad4 = OS(absl::in_place, n,
'd');
1407 ae4 = OS(absl::in_place, n,
'e');
1408 af4 = OS(absl::in_place, n,
'f');
1411 OS ab5 = absl::nullopt;
1412 OS ac5 = std::string(n,
'c');
1413 std::string sad5(n,
'd');
1415 OS ae5(absl::in_place, n,
'e');
1417 af5.emplace(n,
'f');
1419 std::string saa5(n,
'a');
1420 std::string sab5(n,
'a');
1421 std::string sac5(n,
'a');
1422 std::string sad52(n,
'a');
1423 std::string sae5(n,
'a');
1424 std::string saf5(n,
'a');
1434 OS ab6 = absl::nullopt;
1435 OS ac6 = std::string(n,
'c');
1436 std::string sad6(n,
'd');
1438 OS ae6(absl::in_place, n,
'e');
1440 af6.emplace(n,
'f');
1442 aa6 = std::string(n,
'a');
1443 ab6 = std::string(n,
'b');
1444 ac6 = std::string(n,
'c');
1445 ad6 = std::string(n,
'd');
1446 ae6 = std::string(n,
'e');
1447 af6 = std::string(n,
'f');
1450 OS ab7 = absl::nullopt;
1451 OS ac7 = std::string(n,
'c');
1452 std::string sad7(n,
'd');
1454 OS ae7(absl::in_place, n,
'e');
1456 af7.emplace(n,
'f');
1458 aa7.emplace(n,
'A');
1459 ab7.emplace(n,
'B');
1460 ac7.emplace(n,
'C');
1461 ad7.emplace(n,
'D');
1462 ae7.emplace(n,
'E');
1463 af7.emplace(n,
'F');
1466 TEST(optionalTest, MoveAssignRegression) {
1467 StructorListener listener;
1468 Listenable::listener = &listener;
1476 EXPECT_EQ(1, listener.construct0);
1477 EXPECT_EQ(1, listener.move);
1478 EXPECT_EQ(2, listener.destruct);
1481 TEST(optionalTest, ValueType) {
1484 std::string>::
value));
1489 template <
typename T>
1490 struct is_hash_enabled_for {
1491 template <
typename U,
typename = decltype(std::hash<U>()(std::declval<U>()))>
1492 static std::true_type test(
int);
1494 template <
typename U>
1495 static std::false_type test(...);
1497 static constexpr
bool value = decltype(test<T>(0))::
value;
1501 std::hash<absl::optional<int>>
hash;
1502 std::set<size_t> hashcodes;
1503 hashcodes.insert(
hash(absl::nullopt));
1504 for (
int i = 0;
i < 100; ++
i) {
1505 hashcodes.insert(
hash(
i));
1507 EXPECT_GT(hashcodes.size(), 90);
1516 absl::type_traits_internal::AssertHashEnabled<absl::optional<int>>();
1517 absl::type_traits_internal::AssertHashEnabled<absl::optional<Hashable>>();
1519 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 1532 std::hash<absl::optional<const int>> c_hash;
1533 for (
int i = 0;
i < 100; ++
i) {
1534 EXPECT_EQ(
hash(
i), c_hash(
i));
1539 struct MoveMeNoThrow {
1540 MoveMeNoThrow() : x(0) {}
1541 [[noreturn]] MoveMeNoThrow(
const MoveMeNoThrow& other) : x(other.x) {
1545 MoveMeNoThrow(MoveMeNoThrow&& other) noexcept : x(other.x) {}
1549 struct MoveMeThrow {
1550 MoveMeThrow() : x(0) {}
1551 MoveMeThrow(
const MoveMeThrow& other) : x(other.x) {}
1552 MoveMeThrow(MoveMeThrow&& other) : x(other.x) {}
1556 TEST(optionalTest, NoExcept) {
1560 #ifndef ABSL_HAVE_STD_OPTIONAL 1562 std::is_nothrow_move_constructible<
1566 std::vector<absl::optional<MoveMeNoThrow>>
v;
1567 for (
int i = 0;
i < 10; ++
i) v.emplace_back();
1571 AnyLike(AnyLike&&) =
default;
1572 AnyLike(
const AnyLike&) =
default;
1574 template <
typename ValueType,
1575 typename T =
typename std::decay<ValueType>::type,
1576 typename std::enable_if<
1578 std::is_same<AnyLike, T>,
1581 AnyLike(ValueType&&) {}
1583 AnyLike& operator=(AnyLike&&) =
default;
1584 AnyLike& operator=(
const AnyLike&) =
default;
1586 template <
typename ValueType,
1587 typename T =
typename std::decay<ValueType>::type>
1588 typename std::enable_if<
1590 std::is_copy_constructible<T>>
::value,
1592 operator=(ValueType&& ) {
1597 TEST(optionalTest, ConstructionConstraints) {
1620 TEST(optionalTest, AssignmentConstraints) {
1631 struct NestedClassBug {
1638 TEST(optionalTest, InPlaceTSFINAEBug) {
1641 using Inner = NestedClassBug::Inner;
T & emplace(Args &&... args)
#define ABSL_RAW_LOG(severity,...)
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)
void swap(optional &rhs) noexcept(std::is_nothrow_move_constructible< T >::value &&type_traits_internal::IsNothrowSwappable< T >::value)
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)
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
hash_default_hash< typename T::first_type > hash
constexpr bool has_value() const noexcept
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
constexpr const T & value() const &
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept
TEST(Symbolize, Unimplemented)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
constexpr T value_or(U &&v) const &
bool operator<=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
constexpr TypeSpec operator &(TypeSpec a, TypeSpec b)
size_t operator()(const Hashable &)