variant_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // Unit tests for the variant template. The 'is' and 'IsEmpty' methods
00016 // of variant are not explicitly tested because they are used repeatedly
00017 // in building other tests. All other public variant methods should have
00018 // explicit tests.
00019 
00020 #include "absl/types/variant.h"
00021 
00022 #include <algorithm>
00023 #include <cstddef>
00024 #include <functional>
00025 #include <initializer_list>
00026 #include <memory>
00027 #include <ostream>
00028 #include <queue>
00029 #include <type_traits>
00030 #include <unordered_set>
00031 #include <utility>
00032 #include <vector>
00033 
00034 #include "gmock/gmock.h"
00035 #include "gtest/gtest.h"
00036 #include "absl/base/config.h"
00037 #include "absl/base/port.h"
00038 #include "absl/memory/memory.h"
00039 #include "absl/meta/type_traits.h"
00040 #include "absl/strings/string_view.h"
00041 
00042 #ifdef ABSL_HAVE_EXCEPTIONS
00043 
00044 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
00045   EXPECT_THROW(expr, exception_t)
00046 
00047 #else
00048 
00049 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
00050   EXPECT_DEATH(expr, text)
00051 
00052 #endif  // ABSL_HAVE_EXCEPTIONS
00053 
00054 #define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...)                 \
00055   ABSL_VARIANT_TEST_EXPECT_FAIL((void)(__VA_ARGS__), absl::bad_variant_access, \
00056                                 "Bad variant access")
00057 
00058 struct Hashable {};
00059 
00060 namespace std {
00061 template <>
00062 struct hash<Hashable> {
00063   size_t operator()(const Hashable&);
00064 };
00065 }  // namespace std
00066 
00067 struct NonHashable {};
00068 
00069 namespace absl {
00070 namespace {
00071 
00072 using ::testing::DoubleEq;
00073 using ::testing::Pointee;
00074 using ::testing::VariantWith;
00075 
00076 struct MoveCanThrow {
00077   MoveCanThrow() : v(0) {}
00078   MoveCanThrow(int v) : v(v) {}  // NOLINT(runtime/explicit)
00079   MoveCanThrow(const MoveCanThrow& other) : v(other.v) {}
00080   MoveCanThrow& operator=(const MoveCanThrow& /*other*/) { return *this; }
00081   int v;
00082 };
00083 
00084 bool operator==(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v == rhs.v; }
00085 bool operator!=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v != rhs.v; }
00086 bool operator<(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v < rhs.v; }
00087 bool operator<=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v <= rhs.v; }
00088 bool operator>=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v >= rhs.v; }
00089 bool operator>(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v > rhs.v; }
00090 
00091 // This helper class allows us to determine if it was swapped with std::swap()
00092 // or with its friend swap() function.
00093 struct SpecialSwap {
00094   explicit SpecialSwap(int i) : i(i) {}
00095   friend void swap(SpecialSwap& a, SpecialSwap& b) {
00096     a.special_swap = b.special_swap = true;
00097     std::swap(a.i, b.i);
00098   }
00099   bool operator==(SpecialSwap other) const { return i == other.i; }
00100   int i;
00101   bool special_swap = false;
00102 };
00103 
00104 struct MoveOnlyWithListConstructor {
00105   MoveOnlyWithListConstructor() = default;
00106   explicit MoveOnlyWithListConstructor(std::initializer_list<int> /*ilist*/,
00107                                        int value)
00108       : value(value) {}
00109   MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) = default;
00110   MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
00111       default;
00112 
00113   int value = 0;
00114 };
00115 
00116 #ifdef ABSL_HAVE_EXCEPTIONS
00117 
00118 struct ConversionException {};
00119 
00120 template <class T>
00121 struct ExceptionOnConversion {
00122   operator T() const {  // NOLINT(runtime/explicit)
00123     throw ConversionException();
00124   }
00125 };
00126 
00127 // Forces a variant into the valueless by exception state.
00128 template <class H, class... T>
00129 void ToValuelessByException(absl::variant<H, T...>& v) {  // NOLINT
00130   try {
00131     v.template emplace<0>(ExceptionOnConversion<H>());
00132   } catch (ConversionException& /*e*/) {
00133     // This space intentionally left blank.
00134   }
00135 }
00136 
00137 #endif  // ABSL_HAVE_EXCEPTIONS
00138 
00139 // An indexed sequence of distinct structures holding a single
00140 // value of type T
00141 template<typename T, size_t N>
00142 struct ValueHolder {
00143   explicit ValueHolder(const T& x) : value(x) {}
00144   typedef T value_type;
00145   value_type value;
00146   static const size_t kIndex = N;
00147 };
00148 template<typename T, size_t N>
00149 const size_t ValueHolder<T, N>::kIndex;
00150 
00151 // The following three functions make ValueHolder compatible with
00152 // EXPECT_EQ and EXPECT_NE
00153 template<typename T, size_t N>
00154 inline bool operator==(const ValueHolder<T, N>& left,
00155                        const ValueHolder<T, N>& right) {
00156   return left.value == right.value;
00157 }
00158 
00159 template<typename T, size_t N>
00160 inline bool operator!=(const ValueHolder<T, N>& left,
00161                        const ValueHolder<T, N>& right) {
00162   return left.value != right.value;
00163 }
00164 
00165 template<typename T, size_t N>
00166 inline std::ostream& operator<<(
00167     std::ostream& stream, const ValueHolder<T, N>& object) {
00168   return stream << object.value;
00169 }
00170 
00171 // Makes a variant holding twelve uniquely typed T wrappers.
00172 template<typename T>
00173 struct VariantFactory {
00174   typedef variant<ValueHolder<T, 1>, ValueHolder<T, 2>, ValueHolder<T, 3>,
00175                   ValueHolder<T, 4>>
00176       Type;
00177 };
00178 
00179 // A typelist in 1:1 with VariantFactory, to use type driven unit tests.
00180 typedef ::testing::Types<ValueHolder<size_t, 1>, ValueHolder<size_t, 2>,
00181                          ValueHolder<size_t, 3>,
00182                          ValueHolder<size_t, 4>> VariantTypes;
00183 
00184 // Increments the provided counter pointer in the destructor
00185 struct IncrementInDtor {
00186   explicit IncrementInDtor(int* counter) : counter(counter) {}
00187   ~IncrementInDtor() { *counter += 1; }
00188   int* counter;
00189 };
00190 
00191 struct IncrementInDtorCopyCanThrow {
00192   explicit IncrementInDtorCopyCanThrow(int* counter) : counter(counter) {}
00193   IncrementInDtorCopyCanThrow(IncrementInDtorCopyCanThrow&& other) noexcept =
00194       default;
00195   IncrementInDtorCopyCanThrow(const IncrementInDtorCopyCanThrow& other)
00196       : counter(other.counter) {}
00197   IncrementInDtorCopyCanThrow& operator=(
00198       IncrementInDtorCopyCanThrow&&) noexcept = default;
00199   IncrementInDtorCopyCanThrow& operator=(
00200       IncrementInDtorCopyCanThrow const& other) {
00201     counter = other.counter;
00202     return *this;
00203   }
00204   ~IncrementInDtorCopyCanThrow() { *counter += 1; }
00205   int* counter;
00206 };
00207 
00208 // This is defined so operator== for ValueHolder<IncrementInDtor> will
00209 // return true if two IncrementInDtor objects increment the same
00210 // counter
00211 inline bool operator==(const IncrementInDtor& left,
00212                        const IncrementInDtor& right) {
00213   return left.counter == right.counter;
00214 }
00215 
00216 // This is defined so EXPECT_EQ can work with IncrementInDtor
00217 inline std::ostream& operator<<(
00218     std::ostream& stream, const IncrementInDtor& object) {
00219   return stream << object.counter;
00220 }
00221 
00222 // A class that can be copied, but not assigned.
00223 class CopyNoAssign {
00224  public:
00225   explicit CopyNoAssign(int value) : foo(value) {}
00226   CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {}
00227   int foo;
00228  private:
00229   const CopyNoAssign& operator=(const CopyNoAssign&);
00230 };
00231 
00232 // A class that can neither be copied nor assigned. We provide
00233 // overloads for the constructor with up to four parameters so we can
00234 // test the overloads of variant::emplace.
00235 class NonCopyable {
00236  public:
00237   NonCopyable()
00238       : value(0) {}
00239   explicit NonCopyable(int value1)
00240       : value(value1) {}
00241 
00242   NonCopyable(int value1, int value2)
00243       : value(value1 + value2) {}
00244 
00245   NonCopyable(int value1, int value2, int value3)
00246       : value(value1 + value2 + value3) {}
00247 
00248   NonCopyable(int value1, int value2, int value3, int value4)
00249       : value(value1 + value2 + value3 + value4) {}
00250   NonCopyable(const NonCopyable&) = delete;
00251   NonCopyable& operator=(const NonCopyable&) = delete;
00252   int value;
00253 };
00254 
00255 // A typed test and typed test case over the VariantTypes typelist,
00256 // from which we derive a number of tests that will execute for one of
00257 // each type.
00258 template <typename T>
00259 class VariantTypesTest : public ::testing::Test {};
00260 TYPED_TEST_SUITE(VariantTypesTest, VariantTypes);
00261 
00263 // [variant.ctor] //
00265 
00266 struct NonNoexceptDefaultConstructible {
00267   NonNoexceptDefaultConstructible() {}
00268   int value = 5;
00269 };
00270 
00271 struct NonDefaultConstructible {
00272   NonDefaultConstructible() = delete;
00273 };
00274 
00275 TEST(VariantTest, TestDefaultConstructor) {
00276   {
00277     using X = variant<int>;
00278     constexpr variant<int> x{};
00279     ASSERT_FALSE(x.valueless_by_exception());
00280     ASSERT_EQ(0, x.index());
00281     EXPECT_EQ(0, absl::get<0>(x));
00282     EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
00283   }
00284 
00285   {
00286     using X = variant<NonNoexceptDefaultConstructible>;
00287     X x{};
00288     ASSERT_FALSE(x.valueless_by_exception());
00289     ASSERT_EQ(0, x.index());
00290     EXPECT_EQ(5, absl::get<0>(x).value);
00291     EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
00292   }
00293 
00294   {
00295     using X = variant<int, NonNoexceptDefaultConstructible>;
00296     X x{};
00297     ASSERT_FALSE(x.valueless_by_exception());
00298     ASSERT_EQ(0, x.index());
00299     EXPECT_EQ(0, absl::get<0>(x));
00300     EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
00301   }
00302 
00303   {
00304     using X = variant<NonNoexceptDefaultConstructible, int>;
00305     X x{};
00306     ASSERT_FALSE(x.valueless_by_exception());
00307     ASSERT_EQ(0, x.index());
00308     EXPECT_EQ(5, absl::get<0>(x).value);
00309     EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
00310   }
00311   EXPECT_FALSE(
00312       std::is_default_constructible<variant<NonDefaultConstructible>>::value);
00313   EXPECT_FALSE((std::is_default_constructible<
00314                 variant<NonDefaultConstructible, int>>::value));
00315   EXPECT_TRUE((std::is_default_constructible<
00316                variant<int, NonDefaultConstructible>>::value));
00317 }
00318 
00319 // Test that for each slot, copy constructing a variant with that type
00320 // produces a sensible object that correctly reports its type, and
00321 // that copies the provided value.
00322 TYPED_TEST(VariantTypesTest, TestCopyCtor) {
00323   typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
00324   using value_type1 = absl::variant_alternative_t<0, Variant>;
00325   using value_type2 = absl::variant_alternative_t<1, Variant>;
00326   using value_type3 = absl::variant_alternative_t<2, Variant>;
00327   using value_type4 = absl::variant_alternative_t<3, Variant>;
00328   const TypeParam value(TypeParam::kIndex);
00329   Variant original(value);
00330   Variant copied(original);
00331   EXPECT_TRUE(absl::holds_alternative<value_type1>(copied) ||
00332               TypeParam::kIndex != 1);
00333   EXPECT_TRUE(absl::holds_alternative<value_type2>(copied) ||
00334               TypeParam::kIndex != 2);
00335   EXPECT_TRUE(absl::holds_alternative<value_type3>(copied) ||
00336               TypeParam::kIndex != 3);
00337   EXPECT_TRUE(absl::holds_alternative<value_type4>(copied) ||
00338               TypeParam::kIndex != 4);
00339   EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
00340                absl::get_if<value_type1>(&copied)) ||
00341               TypeParam::kIndex == 1);
00342   EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
00343                absl::get_if<value_type2>(&copied)) ||
00344               TypeParam::kIndex == 2);
00345   EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
00346                absl::get_if<value_type3>(&copied)) ||
00347               TypeParam::kIndex == 3);
00348   EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
00349                absl::get_if<value_type4>(&copied)) ||
00350               TypeParam::kIndex == 4);
00351   EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
00352                absl::get_if<value_type1>(&copied)) ||
00353               TypeParam::kIndex == 1);
00354   EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
00355                absl::get_if<value_type2>(&copied)) ||
00356               TypeParam::kIndex == 2);
00357   EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
00358                absl::get_if<value_type3>(&copied)) ||
00359               TypeParam::kIndex == 3);
00360   EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
00361                absl::get_if<value_type4>(&copied)) ||
00362               TypeParam::kIndex == 4);
00363   const TypeParam* ovalptr = absl::get_if<TypeParam>(&original);
00364   const TypeParam* cvalptr = absl::get_if<TypeParam>(&copied);
00365   ASSERT_TRUE(ovalptr != nullptr);
00366   ASSERT_TRUE(cvalptr != nullptr);
00367   EXPECT_EQ(*ovalptr, *cvalptr);
00368   TypeParam* mutable_ovalptr = absl::get_if<TypeParam>(&original);
00369   TypeParam* mutable_cvalptr = absl::get_if<TypeParam>(&copied);
00370   ASSERT_TRUE(mutable_ovalptr != nullptr);
00371   ASSERT_TRUE(mutable_cvalptr != nullptr);
00372   EXPECT_EQ(*mutable_ovalptr, *mutable_cvalptr);
00373 }
00374 
00375 template <class>
00376 struct MoveOnly {
00377   MoveOnly() = default;
00378   explicit MoveOnly(int value) : value(value) {}
00379   MoveOnly(MoveOnly&&) = default;
00380   MoveOnly& operator=(MoveOnly&&) = default;
00381   int value = 5;
00382 };
00383 
00384 TEST(VariantTest, TestMoveConstruct) {
00385   using V = variant<MoveOnly<class A>, MoveOnly<class B>, MoveOnly<class C>>;
00386 
00387   V v(in_place_index<1>, 10);
00388   V v2 = absl::move(v);
00389   EXPECT_EQ(10, absl::get<1>(v2).value);
00390 }
00391 
00392 // Used internally to emulate missing triviality traits for tests.
00393 template <class T>
00394 union SingleUnion {
00395   T member;
00396 };
00397 
00398 // NOTE: These don't work with types that can't be union members.
00399 //       They are just for testing.
00400 template <class T>
00401 struct is_trivially_move_constructible
00402     : std::is_move_constructible<SingleUnion<T>>::type {};
00403 
00404 template <class T>
00405 struct is_trivially_move_assignable
00406     : absl::is_move_assignable<SingleUnion<T>>::type {};
00407 
00408 TEST(VariantTest, NothrowMoveConstructible) {
00409   // Verify that variant is nothrow move constructible iff its template
00410   // arguments are.
00411   using U = std::unique_ptr<int>;
00412   struct E {
00413     E(E&&) {}
00414   };
00415   static_assert(std::is_nothrow_move_constructible<variant<U>>::value, "");
00416   static_assert(std::is_nothrow_move_constructible<variant<U, int>>::value, "");
00417   static_assert(!std::is_nothrow_move_constructible<variant<U, E>>::value, "");
00418 }
00419 
00420 // Test that for each slot, constructing a variant with that type
00421 // produces a sensible object that correctly reports its type, and
00422 // that copies the provided value.
00423 TYPED_TEST(VariantTypesTest, TestValueCtor) {
00424   typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
00425   using value_type1 = absl::variant_alternative_t<0, Variant>;
00426   using value_type2 = absl::variant_alternative_t<1, Variant>;
00427   using value_type3 = absl::variant_alternative_t<2, Variant>;
00428   using value_type4 = absl::variant_alternative_t<3, Variant>;
00429   const TypeParam value(TypeParam::kIndex);
00430   Variant v(value);
00431   EXPECT_TRUE(absl::holds_alternative<value_type1>(v) ||
00432               TypeParam::kIndex != 1);
00433   EXPECT_TRUE(absl::holds_alternative<value_type2>(v) ||
00434               TypeParam::kIndex != 2);
00435   EXPECT_TRUE(absl::holds_alternative<value_type3>(v) ||
00436               TypeParam::kIndex != 3);
00437   EXPECT_TRUE(absl::holds_alternative<value_type4>(v) ||
00438               TypeParam::kIndex != 4);
00439   EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
00440               TypeParam::kIndex != 1);
00441   EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
00442               TypeParam::kIndex != 2);
00443   EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
00444               TypeParam::kIndex != 3);
00445   EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
00446               TypeParam::kIndex != 4);
00447   EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
00448               TypeParam::kIndex != 1);
00449   EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
00450               TypeParam::kIndex != 2);
00451   EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
00452               TypeParam::kIndex != 3);
00453   EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
00454               TypeParam::kIndex != 4);
00455   const TypeParam* valptr = absl::get_if<TypeParam>(&v);
00456   ASSERT_TRUE(nullptr != valptr);
00457   EXPECT_EQ(value.value, valptr->value);
00458   const TypeParam* mutable_valptr = absl::get_if<TypeParam>(&v);
00459   ASSERT_TRUE(nullptr != mutable_valptr);
00460   EXPECT_EQ(value.value, mutable_valptr->value);
00461 }
00462 
00463 TEST(VariantTest, InPlaceType) {
00464   using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
00465 
00466   Var v1(in_place_type_t<int>(), 7);
00467   ASSERT_TRUE(absl::holds_alternative<int>(v1));
00468   EXPECT_EQ(7, absl::get<int>(v1));
00469 
00470   Var v2(in_place_type_t<std::string>(), "ABC");
00471   ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
00472   EXPECT_EQ("ABC", absl::get<std::string>(v2));
00473 
00474   Var v3(in_place_type_t<std::string>(), "ABC", 2);
00475   ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
00476   EXPECT_EQ("AB", absl::get<std::string>(v3));
00477 
00478   Var v4(in_place_type_t<NonCopyable>{});
00479   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
00480 
00481   Var v5(in_place_type_t<std::vector<int>>(), {1, 2, 3});
00482   ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
00483   EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
00484 }
00485 
00486 TEST(VariantTest, InPlaceTypeVariableTemplate) {
00487   using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
00488 
00489   Var v1(in_place_type<int>, 7);
00490   ASSERT_TRUE(absl::holds_alternative<int>(v1));
00491   EXPECT_EQ(7, absl::get<int>(v1));
00492 
00493   Var v2(in_place_type<std::string>, "ABC");
00494   ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
00495   EXPECT_EQ("ABC", absl::get<std::string>(v2));
00496 
00497   Var v3(in_place_type<std::string>, "ABC", 2);
00498   ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
00499   EXPECT_EQ("AB", absl::get<std::string>(v3));
00500 
00501   Var v4(in_place_type<NonCopyable>);
00502   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
00503 
00504   Var v5(in_place_type<std::vector<int>>, {1, 2, 3});
00505   ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
00506   EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
00507 }
00508 
00509 TEST(VariantTest, InPlaceTypeInitializerList) {
00510   using Var =
00511       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00512 
00513   Var v1(in_place_type_t<MoveOnlyWithListConstructor>(), {1, 2, 3, 4, 5}, 6);
00514   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00515   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00516 }
00517 
00518 TEST(VariantTest, InPlaceTypeInitializerListVariabletemplate) {
00519   using Var =
00520       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00521 
00522   Var v1(in_place_type<MoveOnlyWithListConstructor>, {1, 2, 3, 4, 5}, 6);
00523   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00524   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00525 }
00526 
00527 TEST(VariantTest, InPlaceIndex) {
00528   using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
00529 
00530   Var v1(in_place_index_t<0>(), 7);
00531   ASSERT_TRUE(absl::holds_alternative<int>(v1));
00532   EXPECT_EQ(7, absl::get<int>(v1));
00533 
00534   Var v2(in_place_index_t<1>(), "ABC");
00535   ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
00536   EXPECT_EQ("ABC", absl::get<std::string>(v2));
00537 
00538   Var v3(in_place_index_t<1>(), "ABC", 2);
00539   ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
00540   EXPECT_EQ("AB", absl::get<std::string>(v3));
00541 
00542   Var v4(in_place_index_t<2>{});
00543   EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
00544 
00545   // Verify that a variant with only non-copyables can still be constructed.
00546   EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
00547       variant<NonCopyable>(in_place_index_t<0>{})));
00548 
00549   Var v5(in_place_index_t<3>(), {1, 2, 3});
00550   ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
00551   EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
00552 }
00553 
00554 TEST(VariantTest, InPlaceIndexVariableTemplate) {
00555   using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
00556 
00557   Var v1(in_place_index<0>, 7);
00558   ASSERT_TRUE(absl::holds_alternative<int>(v1));
00559   EXPECT_EQ(7, absl::get<int>(v1));
00560 
00561   Var v2(in_place_index<1>, "ABC");
00562   ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
00563   EXPECT_EQ("ABC", absl::get<std::string>(v2));
00564 
00565   Var v3(in_place_index<1>, "ABC", 2);
00566   ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
00567   EXPECT_EQ("AB", absl::get<std::string>(v3));
00568 
00569   Var v4(in_place_index<2>);
00570   EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
00571 
00572   // Verify that a variant with only non-copyables can still be constructed.
00573   EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
00574       variant<NonCopyable>(in_place_index<0>)));
00575 
00576   Var v5(in_place_index<3>, {1, 2, 3});
00577   ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
00578   EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
00579 }
00580 
00581 TEST(VariantTest, InPlaceIndexInitializerList) {
00582   using Var =
00583       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00584 
00585   Var v1(in_place_index_t<3>(), {1, 2, 3, 4, 5}, 6);
00586   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00587   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00588 }
00589 
00590 TEST(VariantTest, InPlaceIndexInitializerListVariableTemplate) {
00591   using Var =
00592       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00593 
00594   Var v1(in_place_index<3>, {1, 2, 3, 4, 5}, 6);
00595   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00596   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00597 }
00598 
00600 // [variant.dtor] //
00602 
00603 // Make sure that the destructor destroys the contained value
00604 TEST(VariantTest, TestDtor) {
00605   typedef VariantFactory<IncrementInDtor>::Type Variant;
00606   using value_type1 = absl::variant_alternative_t<0, Variant>;
00607   using value_type2 = absl::variant_alternative_t<1, Variant>;
00608   using value_type3 = absl::variant_alternative_t<2, Variant>;
00609   using value_type4 = absl::variant_alternative_t<3, Variant>;
00610   int counter = 0;
00611   IncrementInDtor counter_adjuster(&counter);
00612   EXPECT_EQ(0, counter);
00613 
00614   value_type1 value1(counter_adjuster);
00615   { Variant object(value1); }
00616   EXPECT_EQ(1, counter);
00617 
00618   value_type2 value2(counter_adjuster);
00619   { Variant object(value2); }
00620   EXPECT_EQ(2, counter);
00621 
00622   value_type3 value3(counter_adjuster);
00623   { Variant object(value3); }
00624   EXPECT_EQ(3, counter);
00625 
00626   value_type4 value4(counter_adjuster);
00627   { Variant object(value4); }
00628   EXPECT_EQ(4, counter);
00629 }
00630 
00631 #ifdef ABSL_HAVE_EXCEPTIONS
00632 
00633 // See comment in absl/base/config.h
00634 #if defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE)
00635 TEST(VariantTest, DISABLED_TestDtorValuelessByException)
00636 #else
00637 // Test destruction when in the valueless_by_exception state.
00638 TEST(VariantTest, TestDtorValuelessByException)
00639 #endif
00640 {
00641   int counter = 0;
00642   IncrementInDtor counter_adjuster(&counter);
00643 
00644   {
00645     using Variant = VariantFactory<IncrementInDtor>::Type;
00646 
00647     Variant v(in_place_index<0>, counter_adjuster);
00648     EXPECT_EQ(0, counter);
00649 
00650     ToValuelessByException(v);
00651     ASSERT_TRUE(v.valueless_by_exception());
00652     EXPECT_EQ(1, counter);
00653   }
00654   EXPECT_EQ(1, counter);
00655 }
00656 
00657 #endif  // ABSL_HAVE_EXCEPTIONS
00658 
00660 // [variant.assign] //
00662 
00663 // Test that self-assignment doesn't destroy the current value
00664 TEST(VariantTest, TestSelfAssignment) {
00665   typedef VariantFactory<IncrementInDtor>::Type Variant;
00666   int counter = 0;
00667   IncrementInDtor counter_adjuster(&counter);
00668   absl::variant_alternative_t<0, Variant> value(counter_adjuster);
00669   Variant object(value);
00670   object.operator=(object);
00671   EXPECT_EQ(0, counter);
00672 
00673   // A std::string long enough that it's likely to defeat any inline representation
00674   // optimization.
00675   const std::string long_str(128, 'a');
00676 
00677   std::string foo = long_str;
00678   foo = *&foo;
00679   EXPECT_EQ(long_str, foo);
00680 
00681   variant<int, std::string> so = long_str;
00682   ASSERT_EQ(1, so.index());
00683   EXPECT_EQ(long_str, absl::get<1>(so));
00684   so = *&so;
00685 
00686   ASSERT_EQ(1, so.index());
00687   EXPECT_EQ(long_str, absl::get<1>(so));
00688 }
00689 
00690 // Test that assigning a variant<..., T, ...> to a variant<..., T, ...> produces
00691 // a variant<..., T, ...> with the correct value.
00692 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValueSameTypes) {
00693   typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
00694   const TypeParam value(TypeParam::kIndex);
00695   const Variant source(value);
00696   Variant target(TypeParam(value.value + 1));
00697   ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
00698   ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
00699   ASSERT_NE(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
00700   target = source;
00701   ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
00702   ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
00703   EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
00704 }
00705 
00706 // Test that assisnging a variant<..., T, ...> to a variant<1, ...>
00707 // produces a variant<..., T, ...> with the correct value.
00708 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingSourceType) {
00709   typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
00710   using value_type1 = absl::variant_alternative_t<0, Variant>;
00711   const TypeParam value(TypeParam::kIndex);
00712   const Variant source(value);
00713   ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
00714   Variant target(value_type1(1));
00715   ASSERT_TRUE(absl::holds_alternative<value_type1>(target));
00716   target = source;
00717   EXPECT_TRUE(absl::holds_alternative<TypeParam>(source));
00718   EXPECT_TRUE(absl::holds_alternative<TypeParam>(target));
00719   EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
00720 }
00721 
00722 // Test that assigning a variant<1, ...> to a variant<..., T, ...>
00723 // produces a variant<1, ...> with the correct value.
00724 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingTargetType) {
00725   typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
00726   using value_type1 = absl::variant_alternative_t<0, Variant>;
00727   const Variant source(value_type1(1));
00728   ASSERT_TRUE(absl::holds_alternative<value_type1>(source));
00729   const TypeParam value(TypeParam::kIndex);
00730   Variant target(value);
00731   ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
00732   target = source;
00733   EXPECT_TRUE(absl::holds_alternative<value_type1>(target));
00734   EXPECT_TRUE(absl::holds_alternative<value_type1>(source));
00735   EXPECT_EQ(absl::get<value_type1>(source), absl::get<value_type1>(target));
00736 }
00737 
00738 // Test that operator=<T> works, that assigning a new value destroys
00739 // the old and that assigning the new value again does not redestroy
00740 // the old
00741 TEST(VariantTest, TestAssign) {
00742   typedef VariantFactory<IncrementInDtor>::Type Variant;
00743   using value_type1 = absl::variant_alternative_t<0, Variant>;
00744   using value_type2 = absl::variant_alternative_t<1, Variant>;
00745   using value_type3 = absl::variant_alternative_t<2, Variant>;
00746   using value_type4 = absl::variant_alternative_t<3, Variant>;
00747 
00748   const int kSize = 4;
00749   int counter[kSize];
00750   std::unique_ptr<IncrementInDtor> counter_adjustor[kSize];
00751   for (int i = 0; i != kSize; i++) {
00752     counter[i] = 0;
00753     counter_adjustor[i] = absl::make_unique<IncrementInDtor>(&counter[i]);
00754   }
00755 
00756   value_type1 v1(*counter_adjustor[0]);
00757   value_type2 v2(*counter_adjustor[1]);
00758   value_type3 v3(*counter_adjustor[2]);
00759   value_type4 v4(*counter_adjustor[3]);
00760 
00761   // Test that reassignment causes destruction of old value
00762   {
00763     Variant object(v1);
00764     object = v2;
00765     object = v3;
00766     object = v4;
00767     object = v1;
00768   }
00769 
00770   EXPECT_EQ(2, counter[0]);
00771   EXPECT_EQ(1, counter[1]);
00772   EXPECT_EQ(1, counter[2]);
00773   EXPECT_EQ(1, counter[3]);
00774 
00775   std::fill(std::begin(counter), std::end(counter), 0);
00776 
00777   // Test that self-assignment does not cause destruction of old value
00778   {
00779     Variant object(v1);
00780     object.operator=(object);
00781     EXPECT_EQ(0, counter[0]);
00782   }
00783   {
00784     Variant object(v2);
00785     object.operator=(object);
00786     EXPECT_EQ(0, counter[1]);
00787   }
00788   {
00789     Variant object(v3);
00790     object.operator=(object);
00791     EXPECT_EQ(0, counter[2]);
00792   }
00793   {
00794     Variant object(v4);
00795     object.operator=(object);
00796     EXPECT_EQ(0, counter[3]);
00797   }
00798 
00799   EXPECT_EQ(1, counter[0]);
00800   EXPECT_EQ(1, counter[1]);
00801   EXPECT_EQ(1, counter[2]);
00802   EXPECT_EQ(1, counter[3]);
00803 }
00804 
00805 // This tests that we perform a backup if the copy-assign can throw but the move
00806 // cannot throw.
00807 TEST(VariantTest, TestBackupAssign) {
00808   typedef VariantFactory<IncrementInDtorCopyCanThrow>::Type Variant;
00809   using value_type1 = absl::variant_alternative_t<0, Variant>;
00810   using value_type2 = absl::variant_alternative_t<1, Variant>;
00811   using value_type3 = absl::variant_alternative_t<2, Variant>;
00812   using value_type4 = absl::variant_alternative_t<3, Variant>;
00813 
00814   const int kSize = 4;
00815   int counter[kSize];
00816   std::unique_ptr<IncrementInDtorCopyCanThrow> counter_adjustor[kSize];
00817   for (int i = 0; i != kSize; i++) {
00818     counter[i] = 0;
00819     counter_adjustor[i].reset(new IncrementInDtorCopyCanThrow(&counter[i]));
00820   }
00821 
00822   value_type1 v1(*counter_adjustor[0]);
00823   value_type2 v2(*counter_adjustor[1]);
00824   value_type3 v3(*counter_adjustor[2]);
00825   value_type4 v4(*counter_adjustor[3]);
00826 
00827   // Test that reassignment causes destruction of old value
00828   {
00829     Variant object(v1);
00830     object = v2;
00831     object = v3;
00832     object = v4;
00833     object = v1;
00834   }
00835 
00836   // libstdc++ doesn't pass this test
00837 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
00838   EXPECT_EQ(3, counter[0]);
00839   EXPECT_EQ(2, counter[1]);
00840   EXPECT_EQ(2, counter[2]);
00841   EXPECT_EQ(2, counter[3]);
00842 #endif
00843 
00844   std::fill(std::begin(counter), std::end(counter), 0);
00845 
00846   // Test that self-assignment does not cause destruction of old value
00847   {
00848     Variant object(v1);
00849     object.operator=(object);
00850     EXPECT_EQ(0, counter[0]);
00851   }
00852   {
00853     Variant object(v2);
00854     object.operator=(object);
00855     EXPECT_EQ(0, counter[1]);
00856   }
00857   {
00858     Variant object(v3);
00859     object.operator=(object);
00860     EXPECT_EQ(0, counter[2]);
00861   }
00862   {
00863     Variant object(v4);
00864     object.operator=(object);
00865     EXPECT_EQ(0, counter[3]);
00866   }
00867 
00868   EXPECT_EQ(1, counter[0]);
00869   EXPECT_EQ(1, counter[1]);
00870   EXPECT_EQ(1, counter[2]);
00871   EXPECT_EQ(1, counter[3]);
00872 }
00873 
00875 // [variant.mod] //
00877 
00878 TEST(VariantTest, TestEmplaceBasic) {
00879   using Variant = variant<int, char>;
00880 
00881   Variant v(absl::in_place_index<0>, 0);
00882 
00883   {
00884     char& emplace_result = v.emplace<char>();
00885     ASSERT_TRUE(absl::holds_alternative<char>(v));
00886     EXPECT_EQ(absl::get<char>(v), 0);
00887     EXPECT_EQ(&emplace_result, &absl::get<char>(v));
00888   }
00889 
00890   // Make sure that another emplace does zero-initialization
00891   absl::get<char>(v) = 'a';
00892   v.emplace<char>('b');
00893   ASSERT_TRUE(absl::holds_alternative<char>(v));
00894   EXPECT_EQ(absl::get<char>(v), 'b');
00895 
00896   {
00897     int& emplace_result = v.emplace<int>();
00898     EXPECT_TRUE(absl::holds_alternative<int>(v));
00899     EXPECT_EQ(absl::get<int>(v), 0);
00900     EXPECT_EQ(&emplace_result, &absl::get<int>(v));
00901   }
00902 }
00903 
00904 TEST(VariantTest, TestEmplaceInitializerList) {
00905   using Var =
00906       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00907 
00908   Var v1(absl::in_place_index<0>, 555);
00909   MoveOnlyWithListConstructor& emplace_result =
00910       v1.emplace<MoveOnlyWithListConstructor>({1, 2, 3, 4, 5}, 6);
00911   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00912   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00913   EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
00914 }
00915 
00916 TEST(VariantTest, TestEmplaceIndex) {
00917   using Variant = variant<int, char>;
00918 
00919   Variant v(absl::in_place_index<0>, 555);
00920 
00921   {
00922     char& emplace_result = v.emplace<1>();
00923     ASSERT_TRUE(absl::holds_alternative<char>(v));
00924     EXPECT_EQ(absl::get<char>(v), 0);
00925     EXPECT_EQ(&emplace_result, &absl::get<char>(v));
00926   }
00927 
00928   // Make sure that another emplace does zero-initialization
00929   absl::get<char>(v) = 'a';
00930   v.emplace<1>('b');
00931   ASSERT_TRUE(absl::holds_alternative<char>(v));
00932   EXPECT_EQ(absl::get<char>(v), 'b');
00933 
00934   {
00935     int& emplace_result = v.emplace<0>();
00936     EXPECT_TRUE(absl::holds_alternative<int>(v));
00937     EXPECT_EQ(absl::get<int>(v), 0);
00938     EXPECT_EQ(&emplace_result, &absl::get<int>(v));
00939   }
00940 }
00941 
00942 TEST(VariantTest, TestEmplaceIndexInitializerList) {
00943   using Var =
00944       variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
00945 
00946   Var v1(absl::in_place_index<0>, 555);
00947   MoveOnlyWithListConstructor& emplace_result =
00948       v1.emplace<3>({1, 2, 3, 4, 5}, 6);
00949   ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
00950   EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
00951   EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
00952 }
00953 
00955 // [variant.status] //
00957 
00958 TEST(VariantTest, Index) {
00959   using Var = variant<int, std::string, double>;
00960 
00961   Var v = 1;
00962   EXPECT_EQ(0, v.index());
00963   v = "str";
00964   EXPECT_EQ(1, v.index());
00965   v = 0.;
00966   EXPECT_EQ(2, v.index());
00967 
00968   Var v2 = v;
00969   EXPECT_EQ(2, v2.index());
00970   v2.emplace<int>(3);
00971   EXPECT_EQ(0, v2.index());
00972 }
00973 
00974 TEST(VariantTest, NotValuelessByException) {
00975   using Var = variant<int, std::string, double>;
00976 
00977   Var v = 1;
00978   EXPECT_FALSE(v.valueless_by_exception());
00979   v = "str";
00980   EXPECT_FALSE(v.valueless_by_exception());
00981   v = 0.;
00982   EXPECT_FALSE(v.valueless_by_exception());
00983 
00984   Var v2 = v;
00985   EXPECT_FALSE(v.valueless_by_exception());
00986   v2.emplace<int>(3);
00987   EXPECT_FALSE(v.valueless_by_exception());
00988 }
00989 
00990 #ifdef ABSL_HAVE_EXCEPTIONS
00991 
00992 TEST(VariantTest, IndexValuelessByException) {
00993   using Var = variant<MoveCanThrow, std::string, double>;
00994 
00995   Var v(absl::in_place_index<0>);
00996   EXPECT_EQ(0, v.index());
00997   ToValuelessByException(v);
00998   EXPECT_EQ(absl::variant_npos, v.index());
00999   v = "str";
01000   EXPECT_EQ(1, v.index());
01001 }
01002 
01003 TEST(VariantTest, ValuelessByException) {
01004   using Var = variant<MoveCanThrow, std::string, double>;
01005 
01006   Var v(absl::in_place_index<0>);
01007   EXPECT_FALSE(v.valueless_by_exception());
01008   ToValuelessByException(v);
01009   EXPECT_TRUE(v.valueless_by_exception());
01010   v = "str";
01011   EXPECT_FALSE(v.valueless_by_exception());
01012 }
01013 
01014 #endif  // ABSL_HAVE_EXCEPTIONS
01015 
01017 // [variant.swap] //
01019 
01020 TEST(VariantTest, MemberSwap) {
01021   SpecialSwap v1(3);
01022   SpecialSwap v2(7);
01023 
01024   variant<SpecialSwap> a = v1, b = v2;
01025 
01026   EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
01027   EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
01028 
01029   a.swap(b);
01030   EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
01031   EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
01032   EXPECT_TRUE(absl::get<SpecialSwap>(a).special_swap);
01033 
01034   using V = variant<MoveCanThrow, std::string, int>;
01035   int i = 33;
01036   std::string s = "abc";
01037   V valueless(in_place_index<0>);
01038   ToValuelessByException(valueless);
01039   {
01040     // lhs and rhs holds different alternative
01041     V lhs(i), rhs(s);
01042     lhs.swap(rhs);
01043     EXPECT_THAT(lhs, VariantWith<std::string>(s));
01044     EXPECT_THAT(rhs, VariantWith<int>(i));
01045   }
01046   {
01047     // lhs is valueless
01048     V lhs(valueless), rhs(i);
01049     lhs.swap(rhs);
01050     EXPECT_THAT(lhs, VariantWith<int>(i));
01051     EXPECT_TRUE(rhs.valueless_by_exception());
01052   }
01053   {
01054     // rhs is valueless
01055     V lhs(s), rhs(valueless);
01056     lhs.swap(rhs);
01057     EXPECT_THAT(rhs, VariantWith<std::string>(s));
01058     EXPECT_TRUE(lhs.valueless_by_exception());
01059   }
01060   {
01061     // both are valueless
01062     V lhs(valueless), rhs(valueless);
01063     lhs.swap(rhs);
01064     EXPECT_TRUE(lhs.valueless_by_exception());
01065     EXPECT_TRUE(rhs.valueless_by_exception());
01066   }
01067 }
01068 
01070 // [variant.helper] //
01072 
01073 TEST(VariantTest, VariantSize) {
01074   {
01075     using Size1Variant = absl::variant<int>;
01076     EXPECT_EQ(1, absl::variant_size<Size1Variant>::value);
01077     EXPECT_EQ(1, absl::variant_size<const Size1Variant>::value);
01078     EXPECT_EQ(1, absl::variant_size<volatile Size1Variant>::value);
01079     EXPECT_EQ(1, absl::variant_size<const volatile Size1Variant>::value);
01080   }
01081 
01082   {
01083     using Size3Variant = absl::variant<int, float, int>;
01084     EXPECT_EQ(3, absl::variant_size<Size3Variant>::value);
01085     EXPECT_EQ(3, absl::variant_size<const Size3Variant>::value);
01086     EXPECT_EQ(3, absl::variant_size<volatile Size3Variant>::value);
01087     EXPECT_EQ(3, absl::variant_size<const volatile Size3Variant>::value);
01088   }
01089 }
01090 
01091 TEST(VariantTest, VariantAlternative) {
01092   {
01093     using V = absl::variant<float, int, const char*>;
01094     EXPECT_TRUE(
01095         (std::is_same<float, absl::variant_alternative_t<0, V>>::value));
01096     EXPECT_TRUE((std::is_same<const float,
01097                               absl::variant_alternative_t<0, const V>>::value));
01098     EXPECT_TRUE(
01099         (std::is_same<volatile float,
01100                       absl::variant_alternative_t<0, volatile V>>::value));
01101     EXPECT_TRUE((
01102         std::is_same<const volatile float,
01103                      absl::variant_alternative_t<0, const volatile V>>::value));
01104 
01105     EXPECT_TRUE((std::is_same<int, absl::variant_alternative_t<1, V>>::value));
01106     EXPECT_TRUE((std::is_same<const int,
01107                               absl::variant_alternative_t<1, const V>>::value));
01108     EXPECT_TRUE(
01109         (std::is_same<volatile int,
01110                       absl::variant_alternative_t<1, volatile V>>::value));
01111     EXPECT_TRUE((
01112         std::is_same<const volatile int,
01113                      absl::variant_alternative_t<1, const volatile V>>::value));
01114 
01115     EXPECT_TRUE(
01116         (std::is_same<const char*, absl::variant_alternative_t<2, V>>::value));
01117     EXPECT_TRUE((std::is_same<const char* const,
01118                               absl::variant_alternative_t<2, const V>>::value));
01119     EXPECT_TRUE(
01120         (std::is_same<const char* volatile,
01121                       absl::variant_alternative_t<2, volatile V>>::value));
01122     EXPECT_TRUE((
01123         std::is_same<const char* const volatile,
01124                      absl::variant_alternative_t<2, const volatile V>>::value));
01125   }
01126 
01127   {
01128     using V = absl::variant<float, volatile int, const char*>;
01129     EXPECT_TRUE(
01130         (std::is_same<float, absl::variant_alternative_t<0, V>>::value));
01131     EXPECT_TRUE((std::is_same<const float,
01132                               absl::variant_alternative_t<0, const V>>::value));
01133     EXPECT_TRUE(
01134         (std::is_same<volatile float,
01135                       absl::variant_alternative_t<0, volatile V>>::value));
01136     EXPECT_TRUE((
01137         std::is_same<const volatile float,
01138                      absl::variant_alternative_t<0, const volatile V>>::value));
01139 
01140     EXPECT_TRUE(
01141         (std::is_same<volatile int, absl::variant_alternative_t<1, V>>::value));
01142     EXPECT_TRUE((std::is_same<const volatile int,
01143                               absl::variant_alternative_t<1, const V>>::value));
01144     EXPECT_TRUE(
01145         (std::is_same<volatile int,
01146                       absl::variant_alternative_t<1, volatile V>>::value));
01147     EXPECT_TRUE((
01148         std::is_same<const volatile int,
01149                      absl::variant_alternative_t<1, const volatile V>>::value));
01150 
01151     EXPECT_TRUE(
01152         (std::is_same<const char*, absl::variant_alternative_t<2, V>>::value));
01153     EXPECT_TRUE((std::is_same<const char* const,
01154                               absl::variant_alternative_t<2, const V>>::value));
01155     EXPECT_TRUE(
01156         (std::is_same<const char* volatile,
01157                       absl::variant_alternative_t<2, volatile V>>::value));
01158     EXPECT_TRUE((
01159         std::is_same<const char* const volatile,
01160                      absl::variant_alternative_t<2, const volatile V>>::value));
01161   }
01162 }
01163 
01165 // [variant.get] //
01167 
01168 TEST(VariantTest, HoldsAlternative) {
01169   using Var = variant<int, std::string, double>;
01170 
01171   Var v = 1;
01172   EXPECT_TRUE(absl::holds_alternative<int>(v));
01173   EXPECT_FALSE(absl::holds_alternative<std::string>(v));
01174   EXPECT_FALSE(absl::holds_alternative<double>(v));
01175   v = "str";
01176   EXPECT_FALSE(absl::holds_alternative<int>(v));
01177   EXPECT_TRUE(absl::holds_alternative<std::string>(v));
01178   EXPECT_FALSE(absl::holds_alternative<double>(v));
01179   v = 0.;
01180   EXPECT_FALSE(absl::holds_alternative<int>(v));
01181   EXPECT_FALSE(absl::holds_alternative<std::string>(v));
01182   EXPECT_TRUE(absl::holds_alternative<double>(v));
01183 
01184   Var v2 = v;
01185   EXPECT_FALSE(absl::holds_alternative<int>(v2));
01186   EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
01187   EXPECT_TRUE(absl::holds_alternative<double>(v2));
01188   v2.emplace<int>(3);
01189   EXPECT_TRUE(absl::holds_alternative<int>(v2));
01190   EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
01191   EXPECT_FALSE(absl::holds_alternative<double>(v2));
01192 }
01193 
01194 TEST(VariantTest, GetIndex) {
01195   using Var = variant<int, std::string, double, int>;
01196 
01197   {
01198     Var v(absl::in_place_index<0>, 0);
01199 
01200     using LValueGetType = decltype(absl::get<0>(v));
01201     using RValueGetType = decltype(absl::get<0>(absl::move(v)));
01202 
01203     EXPECT_TRUE((std::is_same<LValueGetType, int&>::value));
01204     EXPECT_TRUE((std::is_same<RValueGetType, int&&>::value));
01205     EXPECT_EQ(absl::get<0>(v), 0);
01206     EXPECT_EQ(absl::get<0>(absl::move(v)), 0);
01207 
01208     const Var& const_v = v;
01209     using ConstLValueGetType = decltype(absl::get<0>(const_v));
01210     using ConstRValueGetType = decltype(absl::get<0>(absl::move(const_v)));
01211     EXPECT_TRUE((std::is_same<ConstLValueGetType, const int&>::value));
01212     EXPECT_TRUE((std::is_same<ConstRValueGetType, const int&&>::value));
01213     EXPECT_EQ(absl::get<0>(const_v), 0);
01214     EXPECT_EQ(absl::get<0>(absl::move(const_v)), 0);
01215   }
01216 
01217   {
01218     Var v = std::string("Hello");
01219 
01220     using LValueGetType = decltype(absl::get<1>(v));
01221     using RValueGetType = decltype(absl::get<1>(absl::move(v)));
01222 
01223     EXPECT_TRUE((std::is_same<LValueGetType, std::string&>::value));
01224     EXPECT_TRUE((std::is_same<RValueGetType, std::string&&>::value));
01225     EXPECT_EQ(absl::get<1>(v), "Hello");
01226     EXPECT_EQ(absl::get<1>(absl::move(v)), "Hello");
01227 
01228     const Var& const_v = v;
01229     using ConstLValueGetType = decltype(absl::get<1>(const_v));
01230     using ConstRValueGetType = decltype(absl::get<1>(absl::move(const_v)));
01231     EXPECT_TRUE((std::is_same<ConstLValueGetType, const std::string&>::value));
01232     EXPECT_TRUE((std::is_same<ConstRValueGetType, const std::string&&>::value));
01233     EXPECT_EQ(absl::get<1>(const_v), "Hello");
01234     EXPECT_EQ(absl::get<1>(absl::move(const_v)), "Hello");
01235   }
01236 
01237   {
01238     Var v = 2.0;
01239 
01240     using LValueGetType = decltype(absl::get<2>(v));
01241     using RValueGetType = decltype(absl::get<2>(absl::move(v)));
01242 
01243     EXPECT_TRUE((std::is_same<LValueGetType, double&>::value));
01244     EXPECT_TRUE((std::is_same<RValueGetType, double&&>::value));
01245     EXPECT_EQ(absl::get<2>(v), 2.);
01246     EXPECT_EQ(absl::get<2>(absl::move(v)), 2.);
01247 
01248     const Var& const_v = v;
01249     using ConstLValueGetType = decltype(absl::get<2>(const_v));
01250     using ConstRValueGetType = decltype(absl::get<2>(absl::move(const_v)));
01251     EXPECT_TRUE((std::is_same<ConstLValueGetType, const double&>::value));
01252     EXPECT_TRUE((std::is_same<ConstRValueGetType, const double&&>::value));
01253     EXPECT_EQ(absl::get<2>(const_v), 2.);
01254     EXPECT_EQ(absl::get<2>(absl::move(const_v)), 2.);
01255   }
01256 
01257   {
01258     Var v(absl::in_place_index<0>, 0);
01259     v.emplace<3>(1);
01260 
01261     using LValueGetType = decltype(absl::get<3>(v));
01262     using RValueGetType = decltype(absl::get<3>(absl::move(v)));
01263 
01264     EXPECT_TRUE((std::is_same<LValueGetType, int&>::value));
01265     EXPECT_TRUE((std::is_same<RValueGetType, int&&>::value));
01266     EXPECT_EQ(absl::get<3>(v), 1);
01267     EXPECT_EQ(absl::get<3>(absl::move(v)), 1);
01268 
01269     const Var& const_v = v;
01270     using ConstLValueGetType = decltype(absl::get<3>(const_v));
01271     using ConstRValueGetType = decltype(absl::get<3>(absl::move(const_v)));
01272     EXPECT_TRUE((std::is_same<ConstLValueGetType, const int&>::value));
01273     EXPECT_TRUE((std::is_same<ConstRValueGetType, const int&&>::value));
01274     EXPECT_EQ(absl::get<3>(const_v), 1);
01275     EXPECT_EQ(absl::get<3>(absl::move(const_v)), 1);  // NOLINT
01276   }
01277 }
01278 
01279 TEST(VariantTest, BadGetIndex) {
01280   using Var = variant<int, std::string, double>;
01281 
01282   {
01283     Var v = 1;
01284 
01285     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<1>(v));
01286     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<1>(std::move(v)));
01287 
01288     const Var& const_v = v;
01289     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<1>(const_v));
01290     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01291         absl::get<1>(std::move(const_v)));  // NOLINT
01292   }
01293 
01294   {
01295     Var v = std::string("Hello");
01296 
01297     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<0>(v));
01298     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<0>(std::move(v)));
01299 
01300     const Var& const_v = v;
01301     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<0>(const_v));
01302     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01303         absl::get<0>(std::move(const_v)));  // NOLINT
01304   }
01305 }
01306 
01307 TEST(VariantTest, GetType) {
01308   using Var = variant<int, std::string, double>;
01309 
01310   {
01311     Var v = 1;
01312 
01313     using LValueGetType = decltype(absl::get<int>(v));
01314     using RValueGetType = decltype(absl::get<int>(absl::move(v)));
01315 
01316     EXPECT_TRUE((std::is_same<LValueGetType, int&>::value));
01317     EXPECT_TRUE((std::is_same<RValueGetType, int&&>::value));
01318     EXPECT_EQ(absl::get<int>(v), 1);
01319     EXPECT_EQ(absl::get<int>(absl::move(v)), 1);
01320 
01321     const Var& const_v = v;
01322     using ConstLValueGetType = decltype(absl::get<int>(const_v));
01323     using ConstRValueGetType = decltype(absl::get<int>(absl::move(const_v)));
01324     EXPECT_TRUE((std::is_same<ConstLValueGetType, const int&>::value));
01325     EXPECT_TRUE((std::is_same<ConstRValueGetType, const int&&>::value));
01326     EXPECT_EQ(absl::get<int>(const_v), 1);
01327     EXPECT_EQ(absl::get<int>(absl::move(const_v)), 1);
01328   }
01329 
01330   {
01331     Var v = std::string("Hello");
01332 
01333     using LValueGetType = decltype(absl::get<1>(v));
01334     using RValueGetType = decltype(absl::get<1>(absl::move(v)));
01335 
01336     EXPECT_TRUE((std::is_same<LValueGetType, std::string&>::value));
01337     EXPECT_TRUE((std::is_same<RValueGetType, std::string&&>::value));
01338     EXPECT_EQ(absl::get<std::string>(v), "Hello");
01339     EXPECT_EQ(absl::get<std::string>(absl::move(v)), "Hello");
01340 
01341     const Var& const_v = v;
01342     using ConstLValueGetType = decltype(absl::get<1>(const_v));
01343     using ConstRValueGetType = decltype(absl::get<1>(absl::move(const_v)));
01344     EXPECT_TRUE((std::is_same<ConstLValueGetType, const std::string&>::value));
01345     EXPECT_TRUE((std::is_same<ConstRValueGetType, const std::string&&>::value));
01346     EXPECT_EQ(absl::get<std::string>(const_v), "Hello");
01347     EXPECT_EQ(absl::get<std::string>(absl::move(const_v)), "Hello");
01348   }
01349 
01350   {
01351     Var v = 2.0;
01352 
01353     using LValueGetType = decltype(absl::get<2>(v));
01354     using RValueGetType = decltype(absl::get<2>(absl::move(v)));
01355 
01356     EXPECT_TRUE((std::is_same<LValueGetType, double&>::value));
01357     EXPECT_TRUE((std::is_same<RValueGetType, double&&>::value));
01358     EXPECT_EQ(absl::get<double>(v), 2.);
01359     EXPECT_EQ(absl::get<double>(absl::move(v)), 2.);
01360 
01361     const Var& const_v = v;
01362     using ConstLValueGetType = decltype(absl::get<2>(const_v));
01363     using ConstRValueGetType = decltype(absl::get<2>(absl::move(const_v)));
01364     EXPECT_TRUE((std::is_same<ConstLValueGetType, const double&>::value));
01365     EXPECT_TRUE((std::is_same<ConstRValueGetType, const double&&>::value));
01366     EXPECT_EQ(absl::get<double>(const_v), 2.);
01367     EXPECT_EQ(absl::get<double>(absl::move(const_v)), 2.);
01368   }
01369 }
01370 
01371 TEST(VariantTest, BadGetType) {
01372   using Var = variant<int, std::string, double>;
01373 
01374   {
01375     Var v = 1;
01376 
01377     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<std::string>(v));
01378     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01379         absl::get<std::string>(std::move(v)));
01380 
01381     const Var& const_v = v;
01382     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01383         absl::get<std::string>(const_v));
01384     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01385         absl::get<std::string>(std::move(const_v)));  // NOLINT
01386   }
01387 
01388   {
01389     Var v = std::string("Hello");
01390 
01391     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<int>(v));
01392     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<int>(std::move(v)));
01393 
01394     const Var& const_v = v;
01395     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<int>(const_v));
01396     ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(
01397         absl::get<int>(std::move(const_v)));  // NOLINT
01398   }
01399 }
01400 
01401 TEST(VariantTest, GetIfIndex) {
01402   using Var = variant<int, std::string, double, int>;
01403 
01404   {
01405     Var v(absl::in_place_index<0>, 0);
01406     EXPECT_TRUE(noexcept(absl::get_if<0>(&v)));
01407 
01408     {
01409       auto* elem = absl::get_if<0>(&v);
01410       EXPECT_TRUE((std::is_same<decltype(elem), int*>::value));
01411       ASSERT_NE(elem, nullptr);
01412       EXPECT_EQ(*elem, 0);
01413       {
01414         auto* bad_elem = absl::get_if<1>(&v);
01415         EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
01416         EXPECT_EQ(bad_elem, nullptr);
01417       }
01418       {
01419         auto* bad_elem = absl::get_if<2>(&v);
01420         EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
01421         EXPECT_EQ(bad_elem, nullptr);
01422       }
01423       {
01424         auto* bad_elem = absl::get_if<3>(&v);
01425         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01426         EXPECT_EQ(bad_elem, nullptr);
01427       }
01428     }
01429 
01430     const Var& const_v = v;
01431     EXPECT_TRUE(noexcept(absl::get_if<0>(&const_v)));
01432 
01433     {
01434       auto* elem = absl::get_if<0>(&const_v);
01435       EXPECT_TRUE((std::is_same<decltype(elem), const int*>::value));
01436       ASSERT_NE(elem, nullptr);
01437       EXPECT_EQ(*elem, 0);
01438       {
01439         auto* bad_elem = absl::get_if<1>(&const_v);
01440         EXPECT_TRUE(
01441             (std::is_same<decltype(bad_elem), const std::string*>::value));
01442         EXPECT_EQ(bad_elem, nullptr);
01443       }
01444       {
01445         auto* bad_elem = absl::get_if<2>(&const_v);
01446         EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
01447         EXPECT_EQ(bad_elem, nullptr);
01448       }
01449       {
01450         auto* bad_elem = absl::get_if<3>(&const_v);
01451         EXPECT_EQ(bad_elem, nullptr);
01452         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01453       }
01454     }
01455   }
01456 
01457   {
01458     Var v = std::string("Hello");
01459     EXPECT_TRUE(noexcept(absl::get_if<1>(&v)));
01460 
01461     {
01462       auto* elem = absl::get_if<1>(&v);
01463       EXPECT_TRUE((std::is_same<decltype(elem), std::string*>::value));
01464       ASSERT_NE(elem, nullptr);
01465       EXPECT_EQ(*elem, "Hello");
01466       {
01467         auto* bad_elem = absl::get_if<0>(&v);
01468         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01469         EXPECT_EQ(bad_elem, nullptr);
01470       }
01471       {
01472         auto* bad_elem = absl::get_if<2>(&v);
01473         EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
01474         EXPECT_EQ(bad_elem, nullptr);
01475       }
01476       {
01477         auto* bad_elem = absl::get_if<3>(&v);
01478         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01479         EXPECT_EQ(bad_elem, nullptr);
01480       }
01481     }
01482 
01483     const Var& const_v = v;
01484     EXPECT_TRUE(noexcept(absl::get_if<1>(&const_v)));
01485 
01486     {
01487       auto* elem = absl::get_if<1>(&const_v);
01488       EXPECT_TRUE((std::is_same<decltype(elem), const std::string*>::value));
01489       ASSERT_NE(elem, nullptr);
01490       EXPECT_EQ(*elem, "Hello");
01491       {
01492         auto* bad_elem = absl::get_if<0>(&const_v);
01493         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01494         EXPECT_EQ(bad_elem, nullptr);
01495       }
01496       {
01497         auto* bad_elem = absl::get_if<2>(&const_v);
01498         EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
01499         EXPECT_EQ(bad_elem, nullptr);
01500       }
01501       {
01502         auto* bad_elem = absl::get_if<3>(&const_v);
01503         EXPECT_EQ(bad_elem, nullptr);
01504         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01505       }
01506     }
01507   }
01508 
01509   {
01510     Var v = 2.0;
01511     EXPECT_TRUE(noexcept(absl::get_if<2>(&v)));
01512 
01513     {
01514       auto* elem = absl::get_if<2>(&v);
01515       EXPECT_TRUE((std::is_same<decltype(elem), double*>::value));
01516       ASSERT_NE(elem, nullptr);
01517       EXPECT_EQ(*elem, 2.0);
01518       {
01519         auto* bad_elem = absl::get_if<0>(&v);
01520         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01521         EXPECT_EQ(bad_elem, nullptr);
01522       }
01523       {
01524         auto* bad_elem = absl::get_if<1>(&v);
01525         EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
01526         EXPECT_EQ(bad_elem, nullptr);
01527       }
01528       {
01529         auto* bad_elem = absl::get_if<3>(&v);
01530         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01531         EXPECT_EQ(bad_elem, nullptr);
01532       }
01533     }
01534 
01535     const Var& const_v = v;
01536     EXPECT_TRUE(noexcept(absl::get_if<2>(&const_v)));
01537 
01538     {
01539       auto* elem = absl::get_if<2>(&const_v);
01540       EXPECT_TRUE((std::is_same<decltype(elem), const double*>::value));
01541       ASSERT_NE(elem, nullptr);
01542       EXPECT_EQ(*elem, 2.0);
01543       {
01544         auto* bad_elem = absl::get_if<0>(&const_v);
01545         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01546         EXPECT_EQ(bad_elem, nullptr);
01547       }
01548       {
01549         auto* bad_elem = absl::get_if<1>(&const_v);
01550         EXPECT_TRUE(
01551             (std::is_same<decltype(bad_elem), const std::string*>::value));
01552         EXPECT_EQ(bad_elem, nullptr);
01553       }
01554       {
01555         auto* bad_elem = absl::get_if<3>(&const_v);
01556         EXPECT_EQ(bad_elem, nullptr);
01557         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01558       }
01559     }
01560   }
01561 
01562   {
01563     Var v(absl::in_place_index<0>, 0);
01564     v.emplace<3>(1);
01565     EXPECT_TRUE(noexcept(absl::get_if<3>(&v)));
01566 
01567     {
01568       auto* elem = absl::get_if<3>(&v);
01569       EXPECT_TRUE((std::is_same<decltype(elem), int*>::value));
01570       ASSERT_NE(elem, nullptr);
01571       EXPECT_EQ(*elem, 1);
01572       {
01573         auto* bad_elem = absl::get_if<0>(&v);
01574         EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
01575         EXPECT_EQ(bad_elem, nullptr);
01576       }
01577       {
01578         auto* bad_elem = absl::get_if<1>(&v);
01579         EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
01580         EXPECT_EQ(bad_elem, nullptr);
01581       }
01582       {
01583         auto* bad_elem = absl::get_if<2>(&v);
01584         EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
01585         EXPECT_EQ(bad_elem, nullptr);
01586       }
01587     }
01588 
01589     const Var& const_v = v;
01590     EXPECT_TRUE(noexcept(absl::get_if<3>(&const_v)));
01591 
01592     {
01593       auto* elem = absl::get_if<3>(&const_v);
01594       EXPECT_TRUE((std::is_same<decltype(elem), const int*>::value));
01595       ASSERT_NE(elem, nullptr);
01596       EXPECT_EQ(*elem, 1);
01597       {
01598         auto* bad_elem = absl::get_if<0>(&const_v);
01599         EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
01600         EXPECT_EQ(bad_elem, nullptr);
01601       }
01602       {
01603         auto* bad_elem = absl::get_if<1>(&const_v);
01604         EXPECT_TRUE(
01605             (std::is_same<decltype(bad_elem), const std::string*>::value));
01606         EXPECT_EQ(bad_elem, nullptr);
01607       }
01608       {
01609         auto* bad_elem = absl::get_if<2>(&const_v);
01610         EXPECT_EQ(bad_elem, nullptr);
01611         EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
01612       }
01613     }
01614   }
01615 }
01616 
01618 // [variant.relops] //
01620 
01621 TEST(VariantTest, OperatorEquals) {
01622   variant<int, std::string> a(1), b(1);
01623   EXPECT_TRUE(a == b);
01624   EXPECT_TRUE(b == a);
01625   EXPECT_FALSE(a != b);
01626   EXPECT_FALSE(b != a);
01627 
01628   b = "str";
01629   EXPECT_FALSE(a == b);
01630   EXPECT_FALSE(b == a);
01631   EXPECT_TRUE(a != b);
01632   EXPECT_TRUE(b != a);
01633 
01634   b = 0;
01635   EXPECT_FALSE(a == b);
01636   EXPECT_FALSE(b == a);
01637   EXPECT_TRUE(a != b);
01638   EXPECT_TRUE(b != a);
01639 
01640   a = b = "foo";
01641   EXPECT_TRUE(a == b);
01642   EXPECT_TRUE(b == a);
01643   EXPECT_FALSE(a != b);
01644   EXPECT_FALSE(b != a);
01645 
01646   a = "bar";
01647   EXPECT_FALSE(a == b);
01648   EXPECT_FALSE(b == a);
01649   EXPECT_TRUE(a != b);
01650   EXPECT_TRUE(b != a);
01651 }
01652 
01653 TEST(VariantTest, OperatorRelational) {
01654   variant<int, std::string> a(1), b(1);
01655   EXPECT_FALSE(a < b);
01656   EXPECT_FALSE(b < a);
01657   EXPECT_FALSE(a > b);
01658   EXPECT_FALSE(b > a);
01659   EXPECT_TRUE(a <= b);
01660   EXPECT_TRUE(b <= a);
01661   EXPECT_TRUE(a >= b);
01662   EXPECT_TRUE(b >= a);
01663 
01664   b = "str";
01665   EXPECT_TRUE(a < b);
01666   EXPECT_FALSE(b < a);
01667   EXPECT_FALSE(a > b);
01668   EXPECT_TRUE(b > a);
01669   EXPECT_TRUE(a <= b);
01670   EXPECT_FALSE(b <= a);
01671   EXPECT_FALSE(a >= b);
01672   EXPECT_TRUE(b >= a);
01673 
01674   b = 0;
01675   EXPECT_FALSE(a < b);
01676   EXPECT_TRUE(b < a);
01677   EXPECT_TRUE(a > b);
01678   EXPECT_FALSE(b > a);
01679   EXPECT_FALSE(a <= b);
01680   EXPECT_TRUE(b <= a);
01681   EXPECT_TRUE(a >= b);
01682   EXPECT_FALSE(b >= a);
01683 
01684   a = b = "foo";
01685   EXPECT_FALSE(a < b);
01686   EXPECT_FALSE(b < a);
01687   EXPECT_FALSE(a > b);
01688   EXPECT_FALSE(b > a);
01689   EXPECT_TRUE(a <= b);
01690   EXPECT_TRUE(b <= a);
01691   EXPECT_TRUE(a >= b);
01692   EXPECT_TRUE(b >= a);
01693 
01694   a = "bar";
01695   EXPECT_TRUE(a < b);
01696   EXPECT_FALSE(b < a);
01697   EXPECT_FALSE(a > b);
01698   EXPECT_TRUE(b > a);
01699   EXPECT_TRUE(a <= b);
01700   EXPECT_FALSE(b <= a);
01701   EXPECT_FALSE(a >= b);
01702   EXPECT_TRUE(b >= a);
01703 }
01704 
01705 #ifdef ABSL_HAVE_EXCEPTIONS
01706 
01707 TEST(VariantTest, ValuelessOperatorEquals) {
01708   variant<MoveCanThrow, std::string> int_v(1), string_v("Hello"),
01709       valueless(absl::in_place_index<0>),
01710       other_valueless(absl::in_place_index<0>);
01711   ToValuelessByException(valueless);
01712   ToValuelessByException(other_valueless);
01713 
01714   EXPECT_TRUE(valueless == other_valueless);
01715   EXPECT_TRUE(other_valueless == valueless);
01716   EXPECT_FALSE(valueless == int_v);
01717   EXPECT_FALSE(valueless == string_v);
01718   EXPECT_FALSE(int_v == valueless);
01719   EXPECT_FALSE(string_v == valueless);
01720 
01721   EXPECT_FALSE(valueless != other_valueless);
01722   EXPECT_FALSE(other_valueless != valueless);
01723   EXPECT_TRUE(valueless != int_v);
01724   EXPECT_TRUE(valueless != string_v);
01725   EXPECT_TRUE(int_v != valueless);
01726   EXPECT_TRUE(string_v != valueless);
01727 }
01728 
01729 TEST(VariantTest, ValuelessOperatorRelational) {
01730   variant<MoveCanThrow, std::string> int_v(1), string_v("Hello"),
01731       valueless(absl::in_place_index<0>),
01732       other_valueless(absl::in_place_index<0>);
01733   ToValuelessByException(valueless);
01734   ToValuelessByException(other_valueless);
01735 
01736   EXPECT_FALSE(valueless < other_valueless);
01737   EXPECT_FALSE(other_valueless < valueless);
01738   EXPECT_TRUE(valueless < int_v);
01739   EXPECT_TRUE(valueless < string_v);
01740   EXPECT_FALSE(int_v < valueless);
01741   EXPECT_FALSE(string_v < valueless);
01742 
01743   EXPECT_TRUE(valueless <= other_valueless);
01744   EXPECT_TRUE(other_valueless <= valueless);
01745   EXPECT_TRUE(valueless <= int_v);
01746   EXPECT_TRUE(valueless <= string_v);
01747   EXPECT_FALSE(int_v <= valueless);
01748   EXPECT_FALSE(string_v <= valueless);
01749 
01750   EXPECT_TRUE(valueless >= other_valueless);
01751   EXPECT_TRUE(other_valueless >= valueless);
01752   EXPECT_FALSE(valueless >= int_v);
01753   EXPECT_FALSE(valueless >= string_v);
01754   EXPECT_TRUE(int_v >= valueless);
01755   EXPECT_TRUE(string_v >= valueless);
01756 
01757   EXPECT_FALSE(valueless > other_valueless);
01758   EXPECT_FALSE(other_valueless > valueless);
01759   EXPECT_FALSE(valueless > int_v);
01760   EXPECT_FALSE(valueless > string_v);
01761   EXPECT_TRUE(int_v > valueless);
01762   EXPECT_TRUE(string_v > valueless);
01763 }
01764 
01765 #endif
01766 
01768 // [variant.visit] //
01770 
01771 template <typename T>
01772 struct ConvertTo {
01773   template <typename U>
01774   T operator()(const U& u) const {
01775     return u;
01776   }
01777 };
01778 
01779 TEST(VariantTest, VisitSimple) {
01780   variant<std::string, const char*> v = "A";
01781 
01782   std::string str = absl::visit(ConvertTo<std::string>{}, v);
01783   EXPECT_EQ("A", str);
01784 
01785   v = std::string("B");
01786 
01787   absl::string_view piece = absl::visit(ConvertTo<absl::string_view>{}, v);
01788   EXPECT_EQ("B", piece);
01789 
01790   struct StrLen {
01791     int operator()(const std::string& s) const { return s.size(); }
01792     int operator()(const char* s) const { return strlen(s); }
01793   };
01794 
01795   v = "SomeStr";
01796   EXPECT_EQ(7, absl::visit(StrLen{}, v));
01797   v = std::string("VeryLargeThisTime");
01798   EXPECT_EQ(17, absl::visit(StrLen{}, v));
01799 }
01800 
01801 TEST(VariantTest, VisitRValue) {
01802   variant<std::string> v = std::string("X");
01803   struct Visitor {
01804     bool operator()(const std::string&) const { return false; }
01805     bool operator()(std::string&&) const { return true; }  // NOLINT
01806 
01807     int operator()(const std::string&, const std::string&) const { return 0; }
01808     int operator()(const std::string&, std::string&&) const {
01809       return 1;
01810     }  // NOLINT
01811     int operator()(std::string&&, const std::string&) const {
01812       return 2;
01813     }                                                                 // NOLINT
01814     int operator()(std::string&&, std::string&&) const { return 3; }  // NOLINT
01815   };
01816   EXPECT_FALSE(absl::visit(Visitor{}, v));
01817   EXPECT_TRUE(absl::visit(Visitor{}, absl::move(v)));
01818 
01819   // Also test the variadic overload.
01820   EXPECT_EQ(0, absl::visit(Visitor{}, v, v));
01821   EXPECT_EQ(1, absl::visit(Visitor{}, v, absl::move(v)));
01822   EXPECT_EQ(2, absl::visit(Visitor{}, absl::move(v), v));
01823   EXPECT_EQ(3, absl::visit(Visitor{}, absl::move(v), absl::move(v)));
01824 }
01825 
01826 TEST(VariantTest, VisitRValueVisitor) {
01827   variant<std::string> v = std::string("X");
01828   struct Visitor {
01829     bool operator()(const std::string&) const& { return false; }
01830     bool operator()(const std::string&) && { return true; }
01831   };
01832   Visitor visitor;
01833   EXPECT_FALSE(absl::visit(visitor, v));
01834   EXPECT_TRUE(absl::visit(Visitor{}, v));
01835 }
01836 
01837 TEST(VariantTest, VisitResultTypeDifferent) {
01838   variant<std::string> v = std::string("X");
01839   struct LValue_LValue {};
01840   struct RValue_LValue {};
01841   struct LValue_RValue {};
01842   struct RValue_RValue {};
01843   struct Visitor {
01844     LValue_LValue operator()(const std::string&) const& { return {}; }
01845     RValue_LValue operator()(std::string&&) const& { return {}; }  // NOLINT
01846     LValue_RValue operator()(const std::string&) && { return {}; }
01847     RValue_RValue operator()(std::string&&) && { return {}; }  // NOLINT
01848   } visitor;
01849 
01850   EXPECT_TRUE(
01851       (std::is_same<LValue_LValue, decltype(absl::visit(visitor, v))>::value));
01852   EXPECT_TRUE(
01853       (std::is_same<RValue_LValue,
01854                     decltype(absl::visit(visitor, absl::move(v)))>::value));
01855   EXPECT_TRUE((
01856       std::is_same<LValue_RValue, decltype(absl::visit(Visitor{}, v))>::value));
01857   EXPECT_TRUE(
01858       (std::is_same<RValue_RValue,
01859                     decltype(absl::visit(Visitor{}, absl::move(v)))>::value));
01860 }
01861 
01862 TEST(VariantTest, VisitVariadic) {
01863   using A = variant<int, std::string>;
01864   using B = variant<std::unique_ptr<int>, absl::string_view>;
01865 
01866   struct Visitor {
01867     std::pair<int, int> operator()(int a, std::unique_ptr<int> b) const {
01868       return {a, *b};
01869     }
01870     std::pair<int, int> operator()(absl::string_view a,
01871                                    std::unique_ptr<int> b) const {
01872       return {static_cast<int>(a.size()), static_cast<int>(*b)};
01873     }
01874     std::pair<int, int> operator()(int a, absl::string_view b) const {
01875       return {a, static_cast<int>(b.size())};
01876     }
01877     std::pair<int, int> operator()(absl::string_view a,
01878                                    absl::string_view b) const {
01879       return {static_cast<int>(a.size()), static_cast<int>(b.size())};
01880     }
01881   };
01882 
01883   EXPECT_THAT(absl::visit(Visitor(), A(1), B(std::unique_ptr<int>(new int(7)))),
01884               ::testing::Pair(1, 7));
01885   EXPECT_THAT(absl::visit(Visitor(), A(1), B(absl::string_view("ABC"))),
01886               ::testing::Pair(1, 3));
01887   EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")),
01888                           B(std::unique_ptr<int>(new int(7)))),
01889               ::testing::Pair(5, 7));
01890   EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")),
01891                           B(absl::string_view("ABC"))),
01892               ::testing::Pair(5, 3));
01893 }
01894 
01895 TEST(VariantTest, VisitNoArgs) {
01896   EXPECT_EQ(5, absl::visit([] { return 5; }));
01897 }
01898 
01899 struct ConstFunctor {
01900   int operator()(int a, int b) const { return a - b; }
01901 };
01902 
01903 struct MutableFunctor {
01904   int operator()(int a, int b) { return a - b; }
01905 };
01906 
01907 struct Class {
01908   int Method(int a, int b) { return a - b; }
01909   int ConstMethod(int a, int b) const { return a - b; }
01910 
01911   int member;
01912 };
01913 
01914 TEST(VariantTest, VisitReferenceWrapper) {
01915   ConstFunctor cf;
01916   MutableFunctor mf;
01917   absl::variant<int> three = 3;
01918   absl::variant<int> two = 2;
01919 
01920   EXPECT_EQ(1, absl::visit(std::cref(cf), three, two));
01921   EXPECT_EQ(1, absl::visit(std::ref(cf), three, two));
01922   EXPECT_EQ(1, absl::visit(std::ref(mf), three, two));
01923 }
01924 
01925 // libstdc++ std::variant doesn't support the INVOKE semantics.
01926 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
01927 TEST(VariantTest, VisitMemberFunction) {
01928   absl::variant<std::unique_ptr<Class>> p(absl::make_unique<Class>());
01929   absl::variant<std::unique_ptr<const Class>> cp(
01930       absl::make_unique<const Class>());
01931   absl::variant<int> three = 3;
01932   absl::variant<int> two = 2;
01933 
01934   EXPECT_EQ(1, absl::visit(&Class::Method, p, three, two));
01935   EXPECT_EQ(1, absl::visit(&Class::ConstMethod, p, three, two));
01936   EXPECT_EQ(1, absl::visit(&Class::ConstMethod, cp, three, two));
01937 }
01938 
01939 TEST(VariantTest, VisitDataMember) {
01940   absl::variant<std::unique_ptr<Class>> p(absl::make_unique<Class>(Class{42}));
01941   absl::variant<std::unique_ptr<const Class>> cp(
01942       absl::make_unique<const Class>(Class{42}));
01943   EXPECT_EQ(42, absl::visit(&Class::member, p));
01944 
01945   absl::visit(&Class::member, p) = 5;
01946   EXPECT_EQ(5, absl::visit(&Class::member, p));
01947 
01948   EXPECT_EQ(42, absl::visit(&Class::member, cp));
01949 }
01950 #endif  // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
01951 
01953 // [variant.monostate] //
01955 
01956 TEST(VariantTest, MonostateBasic) {
01957   absl::monostate mono;
01958   (void)mono;
01959 
01960   // TODO(mattcalabrese) Expose move triviality metafunctions in absl.
01961   EXPECT_TRUE(absl::is_trivially_default_constructible<absl::monostate>::value);
01962   EXPECT_TRUE(is_trivially_move_constructible<absl::monostate>::value);
01963   EXPECT_TRUE(absl::is_trivially_copy_constructible<absl::monostate>::value);
01964   EXPECT_TRUE(is_trivially_move_assignable<absl::monostate>::value);
01965   EXPECT_TRUE(absl::is_trivially_copy_assignable<absl::monostate>::value);
01966   EXPECT_TRUE(absl::is_trivially_destructible<absl::monostate>::value);
01967 }
01968 
01969 TEST(VariantTest, VariantMonostateDefaultConstruction) {
01970   absl::variant<absl::monostate, NonDefaultConstructible> var;
01971   EXPECT_EQ(var.index(), 0);
01972 }
01973 
01975 // [variant.monostate.relops] //
01977 
01978 TEST(VariantTest, MonostateComparisons) {
01979   absl::monostate lhs, rhs;
01980 
01981   EXPECT_EQ(lhs, lhs);
01982   EXPECT_EQ(lhs, rhs);
01983 
01984   EXPECT_FALSE(lhs != lhs);
01985   EXPECT_FALSE(lhs != rhs);
01986   EXPECT_FALSE(lhs < lhs);
01987   EXPECT_FALSE(lhs < rhs);
01988   EXPECT_FALSE(lhs > lhs);
01989   EXPECT_FALSE(lhs > rhs);
01990 
01991   EXPECT_LE(lhs, lhs);
01992   EXPECT_LE(lhs, rhs);
01993   EXPECT_GE(lhs, lhs);
01994   EXPECT_GE(lhs, rhs);
01995 
01996   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() ==
01997                        std::declval<absl::monostate>()));
01998   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() !=
01999                        std::declval<absl::monostate>()));
02000   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <
02001                        std::declval<absl::monostate>()));
02002   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >
02003                        std::declval<absl::monostate>()));
02004   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <=
02005                        std::declval<absl::monostate>()));
02006   EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >=
02007                        std::declval<absl::monostate>()));
02008 }
02009 
02011 // [variant.specalg] //
02013 
02014 TEST(VariantTest, NonmemberSwap) {
02015   using std::swap;
02016 
02017   SpecialSwap v1(3);
02018   SpecialSwap v2(7);
02019 
02020   variant<SpecialSwap> a = v1, b = v2;
02021 
02022   EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
02023   EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
02024 
02025   std::swap(a, b);
02026   EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
02027   EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
02028 #ifndef ABSL_HAVE_STD_VARIANT
02029   EXPECT_FALSE(absl::get<SpecialSwap>(a).special_swap);
02030 #endif
02031 
02032   swap(a, b);
02033   EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
02034   EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
02035   EXPECT_TRUE(absl::get<SpecialSwap>(b).special_swap);
02036 }
02037 
02039 // [variant.bad.access] //
02041 
02042 TEST(VariantTest, BadAccess) {
02043   EXPECT_TRUE(noexcept(absl::bad_variant_access()));
02044   absl::bad_variant_access exception_obj;
02045   std::exception* base = &exception_obj;
02046   (void)base;
02047 }
02048 
02050 // [variant.hash] //
02052 
02053 TEST(VariantTest, MonostateHash) {
02054   absl::monostate mono, other_mono;
02055   std::hash<absl::monostate> const hasher{};
02056   static_assert(std::is_same<decltype(hasher(mono)), std::size_t>::value, "");
02057   EXPECT_EQ(hasher(mono), hasher(other_mono));
02058 }
02059 
02060 TEST(VariantTest, Hash) {
02061   static_assert(type_traits_internal::IsHashable<variant<int>>::value, "");
02062   static_assert(type_traits_internal::IsHashable<variant<Hashable>>::value, "");
02063   static_assert(type_traits_internal::IsHashable<variant<int, Hashable>>::value,
02064                 "");
02065 
02066 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
02067   static_assert(!type_traits_internal::IsHashable<variant<NonHashable>>::value,
02068                 "");
02069   static_assert(
02070       !type_traits_internal::IsHashable<variant<Hashable, NonHashable>>::value,
02071       "");
02072 #endif
02073 
02074 // MSVC std::hash<std::variant> does not use the index, thus produce the same
02075 // result on the same value as different alternative.
02076 #if !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT))
02077   {
02078     // same value as different alternative
02079     variant<int, int> v0(in_place_index<0>, 42);
02080     variant<int, int> v1(in_place_index<1>, 42);
02081     std::hash<variant<int, int>> hash;
02082     EXPECT_NE(hash(v0), hash(v1));
02083   }
02084 #endif  // !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT))
02085 
02086   {
02087     std::hash<variant<int>> hash;
02088     std::set<size_t> hashcodes;
02089     for (int i = 0; i < 100; ++i) {
02090       hashcodes.insert(hash(i));
02091     }
02092     EXPECT_GT(hashcodes.size(), 90);
02093 
02094     // test const-qualified
02095     static_assert(type_traits_internal::IsHashable<variant<const int>>::value,
02096                   "");
02097     static_assert(
02098         type_traits_internal::IsHashable<variant<const Hashable>>::value, "");
02099     std::hash<absl::variant<const int>> c_hash;
02100     for (int i = 0; i < 100; ++i) {
02101       EXPECT_EQ(hash(i), c_hash(i));
02102     }
02103   }
02104 }
02105 
02107 // Miscellaneous and deprecated tests //
02109 
02110 // Test that a set requiring a basic type conversion works correctly.
02111 TEST(VariantTest, TestConvertingSet) {
02112   typedef variant<double> Variant;
02113   Variant v(1.0);
02114   const int two = 2;
02115   v = two;
02116   EXPECT_TRUE(absl::holds_alternative<double>(v));
02117   ASSERT_TRUE(nullptr != absl::get_if<double>(&v));
02118   EXPECT_DOUBLE_EQ(2, absl::get<double>(v));
02119 }
02120 
02121 // Test that a vector of variants behaves reasonably.
02122 TEST(VariantTest, Container) {
02123   typedef variant<int, float> Variant;
02124 
02125   // Creation of vector should work
02126   std::vector<Variant> vec;
02127   vec.push_back(Variant(10));
02128   vec.push_back(Variant(20.0f));
02129 
02130   // Vector resizing should work if we supply a value for new slots
02131   vec.resize(10, Variant(0));
02132 }
02133 
02134 // Test that a variant with a non-copyable type can be constructed and
02135 // manipulated to some degree.
02136 TEST(VariantTest, TestVariantWithNonCopyableType) {
02137   typedef variant<int, NonCopyable> Variant;
02138   const int kValue = 1;
02139   Variant v(kValue);
02140   ASSERT_TRUE(absl::holds_alternative<int>(v));
02141   EXPECT_EQ(kValue, absl::get<int>(v));
02142 }
02143 
02144 // Test that a variant with a non-copyable type can be transformed to
02145 // the non-copyable type with a call to `emplace` for different numbers
02146 // of arguments. We do not need to test this for each of T1 ... T8
02147 // because `emplace` does not overload on T1 ... to T8, so if this
02148 // works for any one of T1 ... T8, then it works for all of them. We
02149 // do need to test that it works with varying numbers of parameters
02150 // though.
02151 TEST(VariantTest, TestEmplace) {
02152   typedef variant<int, NonCopyable> Variant;
02153   const int kValue = 1;
02154   Variant v(kValue);
02155   ASSERT_TRUE(absl::holds_alternative<int>(v));
02156   EXPECT_EQ(kValue, absl::get<int>(v));
02157 
02158   // emplace with zero arguments, then back to 'int'
02159   v.emplace<NonCopyable>();
02160   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02161   EXPECT_EQ(0, absl::get<NonCopyable>(v).value);
02162   v = kValue;
02163   ASSERT_TRUE(absl::holds_alternative<int>(v));
02164 
02165   // emplace with one argument:
02166   v.emplace<NonCopyable>(1);
02167   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02168   EXPECT_EQ(1, absl::get<NonCopyable>(v).value);
02169   v = kValue;
02170   ASSERT_TRUE(absl::holds_alternative<int>(v));
02171 
02172   // emplace with two arguments:
02173   v.emplace<NonCopyable>(1, 2);
02174   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02175   EXPECT_EQ(3, absl::get<NonCopyable>(v).value);
02176   v = kValue;
02177   ASSERT_TRUE(absl::holds_alternative<int>(v));
02178 
02179   // emplace with three arguments
02180   v.emplace<NonCopyable>(1, 2, 3);
02181   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02182   EXPECT_EQ(6, absl::get<NonCopyable>(v).value);
02183   v = kValue;
02184   ASSERT_TRUE(absl::holds_alternative<int>(v));
02185 
02186   // emplace with four arguments
02187   v.emplace<NonCopyable>(1, 2, 3, 4);
02188   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02189   EXPECT_EQ(10, absl::get<NonCopyable>(v).value);
02190   v = kValue;
02191   ASSERT_TRUE(absl::holds_alternative<int>(v));
02192 }
02193 
02194 TEST(VariantTest, TestEmplaceDestroysCurrentValue) {
02195   typedef variant<int, IncrementInDtor, NonCopyable> Variant;
02196   int counter = 0;
02197   Variant v(0);
02198   ASSERT_TRUE(absl::holds_alternative<int>(v));
02199   v.emplace<IncrementInDtor>(&counter);
02200   ASSERT_TRUE(absl::holds_alternative<IncrementInDtor>(v));
02201   ASSERT_EQ(0, counter);
02202   v.emplace<NonCopyable>();
02203   ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
02204   EXPECT_EQ(1, counter);
02205 }
02206 
02207 TEST(VariantTest, TestMoveSemantics) {
02208   typedef variant<std::unique_ptr<int>, std::unique_ptr<std::string>> Variant;
02209 
02210   // Construct a variant by moving from an element value.
02211   Variant v(absl::WrapUnique(new int(10)));
02212   EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v));
02213 
02214   // Construct a variant by moving from another variant.
02215   Variant v2(absl::move(v));
02216   ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v2));
02217   ASSERT_NE(nullptr, absl::get<std::unique_ptr<int>>(v2));
02218   EXPECT_EQ(10, *absl::get<std::unique_ptr<int>>(v2));
02219 
02220   // Moving from a variant object leaves it holding moved-from value of the
02221   // same element type.
02222   EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v));
02223   ASSERT_NE(nullptr, absl::get_if<std::unique_ptr<int>>(&v));
02224   EXPECT_EQ(nullptr, absl::get<std::unique_ptr<int>>(v));
02225 
02226   // Assign a variant from an element value by move.
02227   v = absl::make_unique<std::string>("foo");
02228   ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v));
02229   EXPECT_EQ("foo", *absl::get<std::unique_ptr<std::string>>(v));
02230 
02231   // Move-assign a variant.
02232   v2 = absl::move(v);
02233   ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v2));
02234   EXPECT_EQ("foo", *absl::get<std::unique_ptr<std::string>>(v2));
02235   EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v));
02236 }
02237 
02238 variant<int, std::string> PassThrough(const variant<int, std::string>& arg) {
02239   return arg;
02240 }
02241 
02242 TEST(VariantTest, TestImplicitConversion) {
02243   EXPECT_TRUE(absl::holds_alternative<int>(PassThrough(0)));
02244 
02245   // We still need the explicit cast for std::string, because C++ won't apply
02246   // two user-defined implicit conversions in a row.
02247   EXPECT_TRUE(
02248       absl::holds_alternative<std::string>(PassThrough(std::string("foo"))));
02249 }
02250 
02251 struct Convertible2;
02252 struct Convertible1 {
02253   Convertible1() {}
02254   Convertible1(const Convertible1&) {}
02255   Convertible1& operator=(const Convertible1&) { return *this; }
02256 
02257   // implicit conversion from Convertible2
02258   Convertible1(const Convertible2&) {}  // NOLINT(runtime/explicit)
02259 };
02260 
02261 struct Convertible2 {
02262   Convertible2() {}
02263   Convertible2(const Convertible2&) {}
02264   Convertible2& operator=(const Convertible2&) { return *this; }
02265 
02266   // implicit conversion from Convertible1
02267   Convertible2(const Convertible1&) {}  // NOLINT(runtime/explicit)
02268 };
02269 
02270 TEST(VariantTest, TestRvalueConversion) {
02271   variant<double, std::string> var(
02272       ConvertVariantTo<variant<double, std::string>>(
02273           variant<std::string, int>(0)));
02274   ASSERT_TRUE(absl::holds_alternative<double>(var));
02275   EXPECT_EQ(0.0, absl::get<double>(var));
02276 
02277   var = ConvertVariantTo<variant<double, std::string>>(
02278       variant<const char*, float>("foo"));
02279   ASSERT_TRUE(absl::holds_alternative<std::string>(var));
02280   EXPECT_EQ("foo", absl::get<std::string>(var));
02281 
02282   variant<double> singleton(
02283       ConvertVariantTo<variant<double>>(variant<int, float>(42)));
02284   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02285   EXPECT_EQ(42.0, absl::get<double>(singleton));
02286 
02287   singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
02288   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02289   EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
02290 
02291   singleton = ConvertVariantTo<variant<double>>(variant<int>(0));
02292   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02293   EXPECT_EQ(0.0, absl::get<double>(singleton));
02294 
02295   variant<int32_t, uint32_t> variant2(
02296       ConvertVariantTo<variant<int32_t, uint32_t>>(variant<int32_t>(42)));
02297   ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
02298   EXPECT_EQ(42, absl::get<int32_t>(variant2));
02299 
02300   variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
02301   ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
02302   EXPECT_EQ(42, absl::get<uint32_t>(variant2));
02303 
02304   variant<Convertible1, Convertible2> variant3(
02305       ConvertVariantTo<variant<Convertible1, Convertible2>>(
02306           (variant<Convertible2, Convertible1>(Convertible1()))));
02307   ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
02308 
02309   variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
02310       variant<Convertible2, Convertible1>(Convertible2()));
02311   ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
02312 }
02313 
02314 TEST(VariantTest, TestLvalueConversion) {
02315   variant<std::string, int> source1 = 0;
02316   variant<double, std::string> destination(
02317       ConvertVariantTo<variant<double, std::string>>(source1));
02318   ASSERT_TRUE(absl::holds_alternative<double>(destination));
02319   EXPECT_EQ(0.0, absl::get<double>(destination));
02320 
02321   variant<const char*, float> source2 = "foo";
02322   destination = ConvertVariantTo<variant<double, std::string>>(source2);
02323   ASSERT_TRUE(absl::holds_alternative<std::string>(destination));
02324   EXPECT_EQ("foo", absl::get<std::string>(destination));
02325 
02326   variant<int, float> source3(42);
02327   variant<double> singleton(ConvertVariantTo<variant<double>>(source3));
02328   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02329   EXPECT_EQ(42.0, absl::get<double>(singleton));
02330 
02331   source3 = 3.14f;
02332   singleton = ConvertVariantTo<variant<double>>(source3);
02333   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02334   EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
02335 
02336   variant<int> source4(0);
02337   singleton = ConvertVariantTo<variant<double>>(source4);
02338   ASSERT_TRUE(absl::holds_alternative<double>(singleton));
02339   EXPECT_EQ(0.0, absl::get<double>(singleton));
02340 
02341   variant<int32_t> source5(42);
02342   variant<int32_t, uint32_t> variant2(
02343       ConvertVariantTo<variant<int32_t, uint32_t>>(source5));
02344   ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
02345   EXPECT_EQ(42, absl::get<int32_t>(variant2));
02346 
02347   variant<uint32_t> source6(42);
02348   variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
02349   ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
02350   EXPECT_EQ(42, absl::get<uint32_t>(variant2));
02351 
02352   variant<Convertible2, Convertible1> source7((Convertible1()));
02353   variant<Convertible1, Convertible2> variant3(
02354       ConvertVariantTo<variant<Convertible1, Convertible2>>(source7));
02355   ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
02356 
02357   source7 = Convertible2();
02358   variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
02359   ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
02360 }
02361 
02362 TEST(VariantTest, TestMoveConversion) {
02363   using Variant =
02364       variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
02365   using OtherVariant =
02366       variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
02367 
02368   Variant var(
02369       ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(0)}));
02370   ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<const int>>(var));
02371   ASSERT_NE(absl::get<std::unique_ptr<const int>>(var), nullptr);
02372   EXPECT_EQ(0, *absl::get<std::unique_ptr<const int>>(var));
02373 
02374   var = ConvertVariantTo<Variant>(
02375       OtherVariant(absl::make_unique<std::string>("foo")));
02376   ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<const std::string>>(var));
02377   EXPECT_EQ("foo", *absl::get<std::unique_ptr<const std::string>>(var));
02378 }
02379 
02380 TEST(VariantTest, DoesNotMoveFromLvalues) {
02381   // We use shared_ptr here because it's both copyable and movable, and
02382   // a moved-from shared_ptr is guaranteed to be null, so we can detect
02383   // whether moving or copying has occurred.
02384   using Variant =
02385       variant<std::shared_ptr<const int>, std::shared_ptr<const std::string>>;
02386   using OtherVariant =
02387       variant<std::shared_ptr<int>, std::shared_ptr<std::string>>;
02388 
02389   Variant v1(std::make_shared<const int>(0));
02390 
02391   // Test copy constructor
02392   Variant v2(v1);
02393   EXPECT_EQ(absl::get<std::shared_ptr<const int>>(v1),
02394             absl::get<std::shared_ptr<const int>>(v2));
02395 
02396   // Test copy-assignment operator
02397   v1 = std::make_shared<const std::string>("foo");
02398   v2 = v1;
02399   EXPECT_EQ(absl::get<std::shared_ptr<const std::string>>(v1),
02400             absl::get<std::shared_ptr<const std::string>>(v2));
02401 
02402   // Test converting copy constructor
02403   OtherVariant other(std::make_shared<int>(0));
02404   Variant v3(ConvertVariantTo<Variant>(other));
02405   EXPECT_EQ(absl::get<std::shared_ptr<int>>(other),
02406             absl::get<std::shared_ptr<const int>>(v3));
02407 
02408   other = std::make_shared<std::string>("foo");
02409   v3 = ConvertVariantTo<Variant>(other);
02410   EXPECT_EQ(absl::get<std::shared_ptr<std::string>>(other),
02411             absl::get<std::shared_ptr<const std::string>>(v3));
02412 }
02413 
02414 TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) {
02415   variant<double, std::string> var(
02416       ConvertVariantTo<variant<double, std::string>>(
02417           variant<std::string, int>(3)));
02418   EXPECT_THAT(absl::get_if<double>(&var), Pointee(3.0));
02419 
02420   var = ConvertVariantTo<variant<double, std::string>>(
02421       variant<const char*, float>("foo"));
02422   EXPECT_THAT(absl::get_if<std::string>(&var), Pointee(std::string("foo")));
02423 
02424   variant<double> singleton(
02425       ConvertVariantTo<variant<double>>(variant<int, float>(42)));
02426   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
02427 
02428   singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
02429   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
02430 
02431   singleton = ConvertVariantTo<variant<double>>(variant<int>(3));
02432   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
02433 
02434   variant<int32_t, uint32_t> variant2(
02435       ConvertVariantTo<variant<int32_t, uint32_t>>(variant<int32_t>(42)));
02436   EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
02437 
02438   variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
02439   EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
02440 
02441   variant<Convertible1, Convertible2> variant3(
02442       ConvertVariantTo<variant<Convertible1, Convertible2>>(
02443           (variant<Convertible2, Convertible1>(Convertible1()))));
02444   ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
02445 
02446   variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
02447       variant<Convertible2, Convertible1>(Convertible2()));
02448   ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
02449 }
02450 
02451 TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) {
02452   variant<std::string, int> source1 = 3;
02453   variant<double, std::string> destination(
02454       ConvertVariantTo<variant<double, std::string>>(source1));
02455   EXPECT_THAT(absl::get_if<double>(&destination), Pointee(3.0));
02456 
02457   variant<const char*, float> source2 = "foo";
02458   destination = ConvertVariantTo<variant<double, std::string>>(source2);
02459   EXPECT_THAT(absl::get_if<std::string>(&destination),
02460               Pointee(std::string("foo")));
02461 
02462   variant<int, float> source3(42);
02463   variant<double> singleton(ConvertVariantTo<variant<double>>(source3));
02464   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
02465 
02466   source3 = 3.14f;
02467   singleton = ConvertVariantTo<variant<double>>(source3);
02468   EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
02469   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
02470 
02471   variant<int> source4(3);
02472   singleton = ConvertVariantTo<variant<double>>(source4);
02473   EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
02474 
02475   variant<int32_t> source5(42);
02476   variant<int32_t, uint32_t> variant2(
02477       ConvertVariantTo<variant<int32_t, uint32_t>>(source5));
02478   EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
02479 
02480   variant<uint32_t> source6(42);
02481   variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
02482   EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
02483 
02484   variant<Convertible2, Convertible1> source7((Convertible1()));
02485   variant<Convertible1, Convertible2> variant3(
02486       ConvertVariantTo<variant<Convertible1, Convertible2>>(source7));
02487   ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
02488 
02489   source7 = Convertible2();
02490   variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
02491   ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
02492 }
02493 
02494 TEST(VariantTest, TestMoveConversionViaConvertVariantTo) {
02495   using Variant =
02496       variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
02497   using OtherVariant =
02498       variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
02499 
02500   Variant var(
02501       ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(3)}));
02502   EXPECT_THAT(absl::get_if<std::unique_ptr<const int>>(&var),
02503               Pointee(Pointee(3)));
02504 
02505   var = ConvertVariantTo<Variant>(
02506       OtherVariant(absl::make_unique<std::string>("foo")));
02507   EXPECT_THAT(absl::get_if<std::unique_ptr<const std::string>>(&var),
02508               Pointee(Pointee(std::string("foo"))));
02509 }
02510 
02511 // If all alternatives are trivially copy/move constructible, variant should
02512 // also be trivially copy/move constructible. This is not required by the
02513 // standard and we know that libstdc++ variant doesn't have this feature.
02514 // For more details see the paper:
02515 // http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0602r0.html
02516 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
02517 #define ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 1
02518 #endif
02519 
02520 TEST(VariantTest, TestCopyAndMoveTypeTraits) {
02521   EXPECT_TRUE(std::is_copy_constructible<variant<std::string>>::value);
02522   EXPECT_TRUE(absl::is_copy_assignable<variant<std::string>>::value);
02523   EXPECT_TRUE(std::is_move_constructible<variant<std::string>>::value);
02524   EXPECT_TRUE(absl::is_move_assignable<variant<std::string>>::value);
02525   EXPECT_TRUE(std::is_move_constructible<variant<std::unique_ptr<int>>>::value);
02526   EXPECT_TRUE(absl::is_move_assignable<variant<std::unique_ptr<int>>>::value);
02527   EXPECT_FALSE(
02528       std::is_copy_constructible<variant<std::unique_ptr<int>>>::value);
02529   EXPECT_FALSE(absl::is_copy_assignable<variant<std::unique_ptr<int>>>::value);
02530 
02531   EXPECT_FALSE(
02532       absl::is_trivially_copy_constructible<variant<std::string>>::value);
02533   EXPECT_FALSE(absl::is_trivially_copy_assignable<variant<std::string>>::value);
02534 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02535   EXPECT_TRUE(absl::is_trivially_copy_constructible<variant<int>>::value);
02536   EXPECT_TRUE(absl::is_trivially_copy_assignable<variant<int>>::value);
02537   EXPECT_TRUE(is_trivially_move_constructible<variant<int>>::value);
02538   EXPECT_TRUE(is_trivially_move_assignable<variant<int>>::value);
02539 #endif  // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02540 }
02541 
02542 TEST(VariantTest, TestVectorOfMoveonlyVariant) {
02543   // Verify that variant<MoveonlyType> works correctly as a std::vector element.
02544   std::vector<variant<std::unique_ptr<int>, std::string>> vec;
02545   vec.push_back(absl::make_unique<int>(42));
02546   vec.emplace_back("Hello");
02547   vec.reserve(3);
02548   auto another_vec = absl::move(vec);
02549   // As a sanity check, verify vector contents.
02550   ASSERT_EQ(2, another_vec.size());
02551   EXPECT_EQ(42, *absl::get<std::unique_ptr<int>>(another_vec[0]));
02552   EXPECT_EQ("Hello", absl::get<std::string>(another_vec[1]));
02553 }
02554 
02555 TEST(VariantTest, NestedVariant) {
02556 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02557   static_assert(absl::is_trivially_copy_constructible<variant<int>>(), "");
02558   static_assert(absl::is_trivially_copy_assignable<variant<int>>(), "");
02559   static_assert(is_trivially_move_constructible<variant<int>>(), "");
02560   static_assert(is_trivially_move_assignable<variant<int>>(), "");
02561 
02562   static_assert(absl::is_trivially_copy_constructible<variant<variant<int>>>(),
02563                 "");
02564   static_assert(absl::is_trivially_copy_assignable<variant<variant<int>>>(),
02565                 "");
02566   static_assert(is_trivially_move_constructible<variant<variant<int>>>(), "");
02567   static_assert(is_trivially_move_assignable<variant<variant<int>>>(), "");
02568 #endif  // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02569 
02570   variant<int> x(42);
02571   variant<variant<int>> y(x);
02572   variant<variant<int>> z(y);
02573   EXPECT_TRUE(absl::holds_alternative<variant<int>>(z));
02574   EXPECT_EQ(x, absl::get<variant<int>>(z));
02575 }
02576 
02577 struct TriviallyDestructible {
02578   TriviallyDestructible(TriviallyDestructible&&) {}
02579   TriviallyDestructible(const TriviallyDestructible&) {}
02580   TriviallyDestructible& operator=(TriviallyDestructible&&) { return *this; }
02581   TriviallyDestructible& operator=(const TriviallyDestructible&) {
02582     return *this;
02583   }
02584 };
02585 
02586 struct TriviallyMovable {
02587   TriviallyMovable(TriviallyMovable&&) = default;
02588   TriviallyMovable(TriviallyMovable const&) {}
02589   TriviallyMovable& operator=(const TriviallyMovable&) { return *this; }
02590 };
02591 
02592 struct TriviallyCopyable {
02593   TriviallyCopyable(const TriviallyCopyable&) = default;
02594   TriviallyCopyable& operator=(const TriviallyCopyable&) { return *this; }
02595 };
02596 
02597 struct TriviallyMoveAssignable {
02598   TriviallyMoveAssignable(TriviallyMoveAssignable&&) = default;
02599   TriviallyMoveAssignable(const TriviallyMoveAssignable&) {}
02600   TriviallyMoveAssignable& operator=(TriviallyMoveAssignable&&) = default;
02601   TriviallyMoveAssignable& operator=(const TriviallyMoveAssignable&) {
02602     return *this;
02603   }
02604 };
02605 
02606 struct TriviallyCopyAssignable {};
02607 
02608 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02609 TEST(VariantTest, TestTriviality) {
02610   {
02611     using TrivDestVar = absl::variant<TriviallyDestructible>;
02612 
02613     EXPECT_FALSE(is_trivially_move_constructible<TrivDestVar>::value);
02614     EXPECT_FALSE(absl::is_trivially_copy_constructible<TrivDestVar>::value);
02615     EXPECT_FALSE(is_trivially_move_assignable<TrivDestVar>::value);
02616     EXPECT_FALSE(absl::is_trivially_copy_assignable<TrivDestVar>::value);
02617     EXPECT_TRUE(absl::is_trivially_destructible<TrivDestVar>::value);
02618   }
02619 
02620   {
02621     using TrivMoveVar = absl::variant<TriviallyMovable>;
02622 
02623     EXPECT_TRUE(is_trivially_move_constructible<TrivMoveVar>::value);
02624     EXPECT_FALSE(absl::is_trivially_copy_constructible<TrivMoveVar>::value);
02625     EXPECT_FALSE(is_trivially_move_assignable<TrivMoveVar>::value);
02626     EXPECT_FALSE(absl::is_trivially_copy_assignable<TrivMoveVar>::value);
02627     EXPECT_TRUE(absl::is_trivially_destructible<TrivMoveVar>::value);
02628   }
02629 
02630   {
02631     using TrivCopyVar = absl::variant<TriviallyCopyable>;
02632 
02633     EXPECT_TRUE(is_trivially_move_constructible<TrivCopyVar>::value);
02634     EXPECT_TRUE(absl::is_trivially_copy_constructible<TrivCopyVar>::value);
02635     EXPECT_FALSE(is_trivially_move_assignable<TrivCopyVar>::value);
02636     EXPECT_FALSE(absl::is_trivially_copy_assignable<TrivCopyVar>::value);
02637     EXPECT_TRUE(absl::is_trivially_destructible<TrivCopyVar>::value);
02638   }
02639 
02640   {
02641     using TrivMoveAssignVar = absl::variant<TriviallyMoveAssignable>;
02642 
02643     EXPECT_TRUE(is_trivially_move_constructible<TrivMoveAssignVar>::value);
02644     EXPECT_FALSE(
02645         absl::is_trivially_copy_constructible<TrivMoveAssignVar>::value);
02646     EXPECT_TRUE(is_trivially_move_assignable<TrivMoveAssignVar>::value);
02647     EXPECT_FALSE(absl::is_trivially_copy_assignable<TrivMoveAssignVar>::value);
02648     EXPECT_TRUE(absl::is_trivially_destructible<TrivMoveAssignVar>::value);
02649   }
02650 
02651   {
02652     using TrivCopyAssignVar = absl::variant<TriviallyCopyAssignable>;
02653 
02654     EXPECT_TRUE(is_trivially_move_constructible<TrivCopyAssignVar>::value);
02655     EXPECT_TRUE(
02656         absl::is_trivially_copy_constructible<TrivCopyAssignVar>::value);
02657     EXPECT_TRUE(is_trivially_move_assignable<TrivCopyAssignVar>::value);
02658     EXPECT_TRUE(absl::is_trivially_copy_assignable<TrivCopyAssignVar>::value);
02659     EXPECT_TRUE(absl::is_trivially_destructible<TrivCopyAssignVar>::value);
02660   }
02661 }
02662 #endif  // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
02663 
02664 // To verify that absl::variant correctly use the nontrivial move ctor of its
02665 // member rather than use the trivial copy constructor.
02666 TEST(VariantTest, MoveCtorBug) {
02667   // To simulate std::tuple in libstdc++.
02668   struct TrivialCopyNontrivialMove {
02669     TrivialCopyNontrivialMove() = default;
02670     TrivialCopyNontrivialMove(const TrivialCopyNontrivialMove&) = default;
02671     TrivialCopyNontrivialMove(TrivialCopyNontrivialMove&&) { called = true; }
02672     bool called = false;
02673   };
02674   {
02675     using V = absl::variant<TrivialCopyNontrivialMove, int>;
02676     V v1(absl::in_place_index<0>);
02677     // this should invoke the move ctor, rather than the trivial copy ctor.
02678     V v2(std::move(v1));
02679     EXPECT_TRUE(absl::get<0>(v2).called);
02680   }
02681   {
02682     // this case failed to compile before our fix due to a GCC bug.
02683     using V = absl::variant<int, TrivialCopyNontrivialMove>;
02684     V v1(absl::in_place_index<1>);
02685     // this should invoke the move ctor, rather than the trivial copy ctor.
02686     V v2(std::move(v1));
02687     EXPECT_TRUE(absl::get<1>(v2).called);
02688   }
02689 }
02690 
02691 }  // namespace
02692 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:16