variant_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Unit tests for the variant template. The 'is' and 'IsEmpty' methods
16 // of variant are not explicitly tested because they are used repeatedly
17 // in building other tests. All other public variant methods should have
18 // explicit tests.
19 
20 #include "absl/types/variant.h"
21 
22 #include <algorithm>
23 #include <cstddef>
24 #include <functional>
25 #include <initializer_list>
26 #include <memory>
27 #include <ostream>
28 #include <queue>
29 #include <type_traits>
30 #include <unordered_set>
31 #include <utility>
32 #include <vector>
33 
34 #include "gmock/gmock.h"
35 #include "gtest/gtest.h"
36 #include "absl/base/config.h"
37 #include "absl/base/port.h"
38 #include "absl/memory/memory.h"
39 #include "absl/meta/type_traits.h"
41 
42 #ifdef ABSL_HAVE_EXCEPTIONS
43 
44 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
45  EXPECT_THROW(expr, exception_t)
46 
47 #else
48 
49 #define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
50  EXPECT_DEATH(expr, text)
51 
52 #endif // ABSL_HAVE_EXCEPTIONS
53 
54 #define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...) \
55  ABSL_VARIANT_TEST_EXPECT_FAIL((void)(__VA_ARGS__), absl::bad_variant_access, \
56  "Bad variant access")
57 
58 struct Hashable {};
59 
60 namespace std {
61 template <>
62 struct hash<Hashable> {
63  size_t operator()(const Hashable&);
64 };
65 } // namespace std
66 
67 struct NonHashable {};
68 
69 namespace absl {
70 namespace {
71 
72 using ::testing::DoubleEq;
73 using ::testing::Pointee;
74 using ::testing::VariantWith;
75 
76 struct MoveCanThrow {
77  MoveCanThrow() : v(0) {}
78  MoveCanThrow(int v) : v(v) {} // NOLINT(runtime/explicit)
79  MoveCanThrow(const MoveCanThrow& other) : v(other.v) {}
80  MoveCanThrow& operator=(const MoveCanThrow& /*other*/) { return *this; }
81  int v;
82 };
83 
84 bool operator==(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v == rhs.v; }
85 bool operator!=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v != rhs.v; }
86 bool operator<(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v < rhs.v; }
87 bool operator<=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v <= rhs.v; }
88 bool operator>=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v >= rhs.v; }
89 bool operator>(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v > rhs.v; }
90 
91 // This helper class allows us to determine if it was swapped with std::swap()
92 // or with its friend swap() function.
93 struct SpecialSwap {
94  explicit SpecialSwap(int i) : i(i) {}
95  friend void swap(SpecialSwap& a, SpecialSwap& b) {
96  a.special_swap = b.special_swap = true;
97  std::swap(a.i, b.i);
98  }
99  bool operator==(SpecialSwap other) const { return i == other.i; }
100  int i;
101  bool special_swap = false;
102 };
103 
104 struct MoveOnlyWithListConstructor {
105  MoveOnlyWithListConstructor() = default;
106  explicit MoveOnlyWithListConstructor(std::initializer_list<int> /*ilist*/,
107  int value)
108  : value(value) {}
109  MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) = default;
110  MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
111  default;
112 
113  int value = 0;
114 };
115 
116 #ifdef ABSL_HAVE_EXCEPTIONS
117 
118 struct ConversionException {};
119 
120 template <class T>
121 struct ExceptionOnConversion {
122  operator T() const { // NOLINT(runtime/explicit)
123  throw ConversionException();
124  }
125 };
126 
127 // Forces a variant into the valueless by exception state.
128 template <class H, class... T>
129 void ToValuelessByException(absl::variant<H, T...>& v) { // NOLINT
130  try {
131  v.template emplace<0>(ExceptionOnConversion<H>());
132  } catch (ConversionException& /*e*/) {
133  // This space intentionally left blank.
134  }
135 }
136 
137 #endif // ABSL_HAVE_EXCEPTIONS
138 
139 // An indexed sequence of distinct structures holding a single
140 // value of type T
141 template<typename T, size_t N>
142 struct ValueHolder {
143  explicit ValueHolder(const T& x) : value(x) {}
144  typedef T value_type;
145  value_type value;
146  static const size_t kIndex = N;
147 };
148 template<typename T, size_t N>
149 const size_t ValueHolder<T, N>::kIndex;
150 
151 // The following three functions make ValueHolder compatible with
152 // EXPECT_EQ and EXPECT_NE
153 template<typename T, size_t N>
154 inline bool operator==(const ValueHolder<T, N>& left,
155  const ValueHolder<T, N>& right) {
156  return left.value == right.value;
157 }
158 
159 template<typename T, size_t N>
160 inline bool operator!=(const ValueHolder<T, N>& left,
161  const ValueHolder<T, N>& right) {
162  return left.value != right.value;
163 }
164 
165 template<typename T, size_t N>
166 inline std::ostream& operator<<(
167  std::ostream& stream, const ValueHolder<T, N>& object) {
168  return stream << object.value;
169 }
170 
171 // Makes a variant holding twelve uniquely typed T wrappers.
172 template<typename T>
173 struct VariantFactory {
174  typedef variant<ValueHolder<T, 1>, ValueHolder<T, 2>, ValueHolder<T, 3>,
175  ValueHolder<T, 4>>
176  Type;
177 };
178 
179 // A typelist in 1:1 with VariantFactory, to use type driven unit tests.
180 typedef ::testing::Types<ValueHolder<size_t, 1>, ValueHolder<size_t, 2>,
181  ValueHolder<size_t, 3>,
182  ValueHolder<size_t, 4>> VariantTypes;
183 
184 // Increments the provided counter pointer in the destructor
185 struct IncrementInDtor {
186  explicit IncrementInDtor(int* counter) : counter(counter) {}
187  ~IncrementInDtor() { *counter += 1; }
188  int* counter;
189 };
190 
191 struct IncrementInDtorCopyCanThrow {
192  explicit IncrementInDtorCopyCanThrow(int* counter) : counter(counter) {}
193  IncrementInDtorCopyCanThrow(IncrementInDtorCopyCanThrow&& other) noexcept =
194  default;
195  IncrementInDtorCopyCanThrow(const IncrementInDtorCopyCanThrow& other)
196  : counter(other.counter) {}
197  IncrementInDtorCopyCanThrow& operator=(
198  IncrementInDtorCopyCanThrow&&) noexcept = default;
199  IncrementInDtorCopyCanThrow& operator=(
200  IncrementInDtorCopyCanThrow const& other) {
201  counter = other.counter;
202  return *this;
203  }
204  ~IncrementInDtorCopyCanThrow() { *counter += 1; }
205  int* counter;
206 };
207 
208 // This is defined so operator== for ValueHolder<IncrementInDtor> will
209 // return true if two IncrementInDtor objects increment the same
210 // counter
211 inline bool operator==(const IncrementInDtor& left,
212  const IncrementInDtor& right) {
213  return left.counter == right.counter;
214 }
215 
216 // This is defined so EXPECT_EQ can work with IncrementInDtor
217 inline std::ostream& operator<<(
218  std::ostream& stream, const IncrementInDtor& object) {
219  return stream << object.counter;
220 }
221 
222 // A class that can be copied, but not assigned.
223 class CopyNoAssign {
224  public:
225  explicit CopyNoAssign(int value) : foo(value) {}
226  CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {}
227  int foo;
228  private:
229  const CopyNoAssign& operator=(const CopyNoAssign&);
230 };
231 
232 // A class that can neither be copied nor assigned. We provide
233 // overloads for the constructor with up to four parameters so we can
234 // test the overloads of variant::emplace.
235 class NonCopyable {
236  public:
237  NonCopyable()
238  : value(0) {}
239  explicit NonCopyable(int value1)
240  : value(value1) {}
241 
242  NonCopyable(int value1, int value2)
243  : value(value1 + value2) {}
244 
245  NonCopyable(int value1, int value2, int value3)
246  : value(value1 + value2 + value3) {}
247 
248  NonCopyable(int value1, int value2, int value3, int value4)
249  : value(value1 + value2 + value3 + value4) {}
250  NonCopyable(const NonCopyable&) = delete;
251  NonCopyable& operator=(const NonCopyable&) = delete;
252  int value;
253 };
254 
255 // A typed test and typed test case over the VariantTypes typelist,
256 // from which we derive a number of tests that will execute for one of
257 // each type.
258 template <typename T>
259 class VariantTypesTest : public ::testing::Test {};
260 TYPED_TEST_SUITE(VariantTypesTest, VariantTypes);
261 
263 // [variant.ctor] //
265 
266 struct NonNoexceptDefaultConstructible {
267  NonNoexceptDefaultConstructible() {}
268  int value = 5;
269 };
270 
271 struct NonDefaultConstructible {
272  NonDefaultConstructible() = delete;
273 };
274 
275 TEST(VariantTest, TestDefaultConstructor) {
276  {
277  using X = variant<int>;
278  constexpr variant<int> x{};
279  ASSERT_FALSE(x.valueless_by_exception());
280  ASSERT_EQ(0, x.index());
281  EXPECT_EQ(0, absl::get<0>(x));
283  }
284 
285  {
286  using X = variant<NonNoexceptDefaultConstructible>;
287  X x{};
288  ASSERT_FALSE(x.valueless_by_exception());
289  ASSERT_EQ(0, x.index());
290  EXPECT_EQ(5, absl::get<0>(x).value);
292  }
293 
294  {
295  using X = variant<int, NonNoexceptDefaultConstructible>;
296  X x{};
297  ASSERT_FALSE(x.valueless_by_exception());
298  ASSERT_EQ(0, x.index());
299  EXPECT_EQ(0, absl::get<0>(x));
301  }
302 
303  {
304  using X = variant<NonNoexceptDefaultConstructible, int>;
305  X x{};
306  ASSERT_FALSE(x.valueless_by_exception());
307  ASSERT_EQ(0, x.index());
308  EXPECT_EQ(5, absl::get<0>(x).value);
310  }
311  EXPECT_FALSE(
312  std::is_default_constructible<variant<NonDefaultConstructible>>::value);
313  EXPECT_FALSE((std::is_default_constructible<
314  variant<NonDefaultConstructible, int>>::value));
315  EXPECT_TRUE((std::is_default_constructible<
316  variant<int, NonDefaultConstructible>>::value));
317 }
318 
319 // Test that for each slot, copy constructing a variant with that type
320 // produces a sensible object that correctly reports its type, and
321 // that copies the provided value.
322 TYPED_TEST(VariantTypesTest, TestCopyCtor) {
323  typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
324  using value_type1 = absl::variant_alternative_t<0, Variant>;
325  using value_type2 = absl::variant_alternative_t<1, Variant>;
326  using value_type3 = absl::variant_alternative_t<2, Variant>;
327  using value_type4 = absl::variant_alternative_t<3, Variant>;
328  const TypeParam value(TypeParam::kIndex);
329  Variant original(value);
330  Variant copied(original);
331  EXPECT_TRUE(absl::holds_alternative<value_type1>(copied) ||
332  TypeParam::kIndex != 1);
333  EXPECT_TRUE(absl::holds_alternative<value_type2>(copied) ||
334  TypeParam::kIndex != 2);
335  EXPECT_TRUE(absl::holds_alternative<value_type3>(copied) ||
336  TypeParam::kIndex != 3);
337  EXPECT_TRUE(absl::holds_alternative<value_type4>(copied) ||
338  TypeParam::kIndex != 4);
339  EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
340  absl::get_if<value_type1>(&copied)) ||
341  TypeParam::kIndex == 1);
342  EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
343  absl::get_if<value_type2>(&copied)) ||
344  TypeParam::kIndex == 2);
345  EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
346  absl::get_if<value_type3>(&copied)) ||
347  TypeParam::kIndex == 3);
348  EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
349  absl::get_if<value_type4>(&copied)) ||
350  TypeParam::kIndex == 4);
351  EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
352  absl::get_if<value_type1>(&copied)) ||
353  TypeParam::kIndex == 1);
354  EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
355  absl::get_if<value_type2>(&copied)) ||
356  TypeParam::kIndex == 2);
357  EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
358  absl::get_if<value_type3>(&copied)) ||
359  TypeParam::kIndex == 3);
360  EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
361  absl::get_if<value_type4>(&copied)) ||
362  TypeParam::kIndex == 4);
363  const TypeParam* ovalptr = absl::get_if<TypeParam>(&original);
364  const TypeParam* cvalptr = absl::get_if<TypeParam>(&copied);
365  ASSERT_TRUE(ovalptr != nullptr);
366  ASSERT_TRUE(cvalptr != nullptr);
367  EXPECT_EQ(*ovalptr, *cvalptr);
368  TypeParam* mutable_ovalptr = absl::get_if<TypeParam>(&original);
369  TypeParam* mutable_cvalptr = absl::get_if<TypeParam>(&copied);
370  ASSERT_TRUE(mutable_ovalptr != nullptr);
371  ASSERT_TRUE(mutable_cvalptr != nullptr);
372  EXPECT_EQ(*mutable_ovalptr, *mutable_cvalptr);
373 }
374 
375 template <class>
376 struct MoveOnly {
377  MoveOnly() = default;
378  explicit MoveOnly(int value) : value(value) {}
379  MoveOnly(MoveOnly&&) = default;
380  MoveOnly& operator=(MoveOnly&&) = default;
381  int value = 5;
382 };
383 
384 TEST(VariantTest, TestMoveConstruct) {
385  using V = variant<MoveOnly<class A>, MoveOnly<class B>, MoveOnly<class C>>;
386 
387  V v(in_place_index<1>, 10);
388  V v2 = absl::move(v);
389  EXPECT_EQ(10, absl::get<1>(v2).value);
390 }
391 
392 // Used internally to emulate missing triviality traits for tests.
393 template <class T>
394 union SingleUnion {
396 };
397 
398 // NOTE: These don't work with types that can't be union members.
399 // They are just for testing.
400 template <class T>
401 struct is_trivially_move_constructible
402  : std::is_move_constructible<SingleUnion<T>>::type {};
403 
404 template <class T>
405 struct is_trivially_move_assignable
406  : absl::is_move_assignable<SingleUnion<T>>::type {};
407 
408 TEST(VariantTest, NothrowMoveConstructible) {
409  // Verify that variant is nothrow move constructible iff its template
410  // arguments are.
411  using U = std::unique_ptr<int>;
412  struct E {
413  E(E&&) {}
414  };
415  static_assert(std::is_nothrow_move_constructible<variant<U>>::value, "");
416  static_assert(std::is_nothrow_move_constructible<variant<U, int>>::value, "");
417  static_assert(!std::is_nothrow_move_constructible<variant<U, E>>::value, "");
418 }
419 
420 // Test that for each slot, constructing a variant with that type
421 // produces a sensible object that correctly reports its type, and
422 // that copies the provided value.
423 TYPED_TEST(VariantTypesTest, TestValueCtor) {
424  typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
425  using value_type1 = absl::variant_alternative_t<0, Variant>;
426  using value_type2 = absl::variant_alternative_t<1, Variant>;
427  using value_type3 = absl::variant_alternative_t<2, Variant>;
428  using value_type4 = absl::variant_alternative_t<3, Variant>;
429  const TypeParam value(TypeParam::kIndex);
430  Variant v(value);
431  EXPECT_TRUE(absl::holds_alternative<value_type1>(v) ||
432  TypeParam::kIndex != 1);
433  EXPECT_TRUE(absl::holds_alternative<value_type2>(v) ||
434  TypeParam::kIndex != 2);
435  EXPECT_TRUE(absl::holds_alternative<value_type3>(v) ||
436  TypeParam::kIndex != 3);
437  EXPECT_TRUE(absl::holds_alternative<value_type4>(v) ||
438  TypeParam::kIndex != 4);
439  EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
440  TypeParam::kIndex != 1);
441  EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
442  TypeParam::kIndex != 2);
443  EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
444  TypeParam::kIndex != 3);
445  EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
446  TypeParam::kIndex != 4);
447  EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
448  TypeParam::kIndex != 1);
449  EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
450  TypeParam::kIndex != 2);
451  EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
452  TypeParam::kIndex != 3);
453  EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
454  TypeParam::kIndex != 4);
455  const TypeParam* valptr = absl::get_if<TypeParam>(&v);
456  ASSERT_TRUE(nullptr != valptr);
457  EXPECT_EQ(value.value, valptr->value);
458  const TypeParam* mutable_valptr = absl::get_if<TypeParam>(&v);
459  ASSERT_TRUE(nullptr != mutable_valptr);
460  EXPECT_EQ(value.value, mutable_valptr->value);
461 }
462 
463 TEST(VariantTest, InPlaceType) {
464  using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
465 
466  Var v1(in_place_type_t<int>(), 7);
467  ASSERT_TRUE(absl::holds_alternative<int>(v1));
468  EXPECT_EQ(7, absl::get<int>(v1));
469 
470  Var v2(in_place_type_t<std::string>(), "ABC");
471  ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
472  EXPECT_EQ("ABC", absl::get<std::string>(v2));
473 
474  Var v3(in_place_type_t<std::string>(), "ABC", 2);
475  ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
476  EXPECT_EQ("AB", absl::get<std::string>(v3));
477 
478  Var v4(in_place_type_t<NonCopyable>{});
479  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
480 
481  Var v5(in_place_type_t<std::vector<int>>(), {1, 2, 3});
482  ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
483  EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
484 }
485 
486 TEST(VariantTest, InPlaceTypeVariableTemplate) {
487  using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
488 
489  Var v1(in_place_type<int>, 7);
490  ASSERT_TRUE(absl::holds_alternative<int>(v1));
491  EXPECT_EQ(7, absl::get<int>(v1));
492 
493  Var v2(in_place_type<std::string>, "ABC");
494  ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
495  EXPECT_EQ("ABC", absl::get<std::string>(v2));
496 
497  Var v3(in_place_type<std::string>, "ABC", 2);
498  ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
499  EXPECT_EQ("AB", absl::get<std::string>(v3));
500 
501  Var v4(in_place_type<NonCopyable>);
502  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
503 
504  Var v5(in_place_type<std::vector<int>>, {1, 2, 3});
505  ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
506  EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
507 }
508 
509 TEST(VariantTest, InPlaceTypeInitializerList) {
510  using Var =
511  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
512 
513  Var v1(in_place_type_t<MoveOnlyWithListConstructor>(), {1, 2, 3, 4, 5}, 6);
514  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
515  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
516 }
517 
518 TEST(VariantTest, InPlaceTypeInitializerListVariabletemplate) {
519  using Var =
520  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
521 
522  Var v1(in_place_type<MoveOnlyWithListConstructor>, {1, 2, 3, 4, 5}, 6);
523  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
524  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
525 }
526 
527 TEST(VariantTest, InPlaceIndex) {
528  using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
529 
530  Var v1(in_place_index_t<0>(), 7);
531  ASSERT_TRUE(absl::holds_alternative<int>(v1));
532  EXPECT_EQ(7, absl::get<int>(v1));
533 
534  Var v2(in_place_index_t<1>(), "ABC");
535  ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
536  EXPECT_EQ("ABC", absl::get<std::string>(v2));
537 
538  Var v3(in_place_index_t<1>(), "ABC", 2);
539  ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
540  EXPECT_EQ("AB", absl::get<std::string>(v3));
541 
542  Var v4(in_place_index_t<2>{});
543  EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
544 
545  // Verify that a variant with only non-copyables can still be constructed.
546  EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
547  variant<NonCopyable>(in_place_index_t<0>{})));
548 
549  Var v5(in_place_index_t<3>(), {1, 2, 3});
550  ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
551  EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
552 }
553 
554 TEST(VariantTest, InPlaceIndexVariableTemplate) {
555  using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
556 
557  Var v1(in_place_index<0>, 7);
558  ASSERT_TRUE(absl::holds_alternative<int>(v1));
559  EXPECT_EQ(7, absl::get<int>(v1));
560 
561  Var v2(in_place_index<1>, "ABC");
562  ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
563  EXPECT_EQ("ABC", absl::get<std::string>(v2));
564 
565  Var v3(in_place_index<1>, "ABC", 2);
566  ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
567  EXPECT_EQ("AB", absl::get<std::string>(v3));
568 
569  Var v4(in_place_index<2>);
570  EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
571 
572  // Verify that a variant with only non-copyables can still be constructed.
573  EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
574  variant<NonCopyable>(in_place_index<0>)));
575 
576  Var v5(in_place_index<3>, {1, 2, 3});
577  ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
578  EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
579 }
580 
581 TEST(VariantTest, InPlaceIndexInitializerList) {
582  using Var =
583  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
584 
585  Var v1(in_place_index_t<3>(), {1, 2, 3, 4, 5}, 6);
586  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
587  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
588 }
589 
590 TEST(VariantTest, InPlaceIndexInitializerListVariableTemplate) {
591  using Var =
592  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
593 
594  Var v1(in_place_index<3>, {1, 2, 3, 4, 5}, 6);
595  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
596  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
597 }
598 
600 // [variant.dtor] //
602 
603 // Make sure that the destructor destroys the contained value
604 TEST(VariantTest, TestDtor) {
605  typedef VariantFactory<IncrementInDtor>::Type Variant;
606  using value_type1 = absl::variant_alternative_t<0, Variant>;
607  using value_type2 = absl::variant_alternative_t<1, Variant>;
608  using value_type3 = absl::variant_alternative_t<2, Variant>;
609  using value_type4 = absl::variant_alternative_t<3, Variant>;
610  int counter = 0;
611  IncrementInDtor counter_adjuster(&counter);
612  EXPECT_EQ(0, counter);
613 
614  value_type1 value1(counter_adjuster);
615  { Variant object(value1); }
616  EXPECT_EQ(1, counter);
617 
618  value_type2 value2(counter_adjuster);
619  { Variant object(value2); }
620  EXPECT_EQ(2, counter);
621 
622  value_type3 value3(counter_adjuster);
623  { Variant object(value3); }
624  EXPECT_EQ(3, counter);
625 
626  value_type4 value4(counter_adjuster);
627  { Variant object(value4); }
628  EXPECT_EQ(4, counter);
629 }
630 
631 #ifdef ABSL_HAVE_EXCEPTIONS
632 
633 // See comment in absl/base/config.h
634 #if defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE)
635 TEST(VariantTest, DISABLED_TestDtorValuelessByException)
636 #else
637 // Test destruction when in the valueless_by_exception state.
638 TEST(VariantTest, TestDtorValuelessByException)
639 #endif
640 {
641  int counter = 0;
642  IncrementInDtor counter_adjuster(&counter);
643 
644  {
645  using Variant = VariantFactory<IncrementInDtor>::Type;
646 
647  Variant v(in_place_index<0>, counter_adjuster);
648  EXPECT_EQ(0, counter);
649 
650  ToValuelessByException(v);
651  ASSERT_TRUE(v.valueless_by_exception());
652  EXPECT_EQ(1, counter);
653  }
654  EXPECT_EQ(1, counter);
655 }
656 
657 #endif // ABSL_HAVE_EXCEPTIONS
658 
660 // [variant.assign] //
662 
663 // Test that self-assignment doesn't destroy the current value
664 TEST(VariantTest, TestSelfAssignment) {
665  typedef VariantFactory<IncrementInDtor>::Type Variant;
666  int counter = 0;
667  IncrementInDtor counter_adjuster(&counter);
669  Variant object(value);
670  object.operator=(object);
671  EXPECT_EQ(0, counter);
672 
673  // A std::string long enough that it's likely to defeat any inline representation
674  // optimization.
675  const std::string long_str(128, 'a');
676 
677  std::string foo = long_str;
678  foo = *&foo;
679  EXPECT_EQ(long_str, foo);
680 
681  variant<int, std::string> so = long_str;
682  ASSERT_EQ(1, so.index());
683  EXPECT_EQ(long_str, absl::get<1>(so));
684  so = *&so;
685 
686  ASSERT_EQ(1, so.index());
687  EXPECT_EQ(long_str, absl::get<1>(so));
688 }
689 
690 // Test that assigning a variant<..., T, ...> to a variant<..., T, ...> produces
691 // a variant<..., T, ...> with the correct value.
692 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValueSameTypes) {
693  typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
694  const TypeParam value(TypeParam::kIndex);
695  const Variant source(value);
696  Variant target(TypeParam(value.value + 1));
697  ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
698  ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
699  ASSERT_NE(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
700  target = source;
701  ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
702  ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
703  EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
704 }
705 
706 // Test that assisnging a variant<..., T, ...> to a variant<1, ...>
707 // produces a variant<..., T, ...> with the correct value.
708 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingSourceType) {
709  typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
710  using value_type1 = absl::variant_alternative_t<0, Variant>;
711  const TypeParam value(TypeParam::kIndex);
712  const Variant source(value);
713  ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
714  Variant target(value_type1(1));
715  ASSERT_TRUE(absl::holds_alternative<value_type1>(target));
716  target = source;
717  EXPECT_TRUE(absl::holds_alternative<TypeParam>(source));
718  EXPECT_TRUE(absl::holds_alternative<TypeParam>(target));
719  EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
720 }
721 
722 // Test that assigning a variant<1, ...> to a variant<..., T, ...>
723 // produces a variant<1, ...> with the correct value.
724 TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingTargetType) {
725  typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
726  using value_type1 = absl::variant_alternative_t<0, Variant>;
727  const Variant source(value_type1(1));
728  ASSERT_TRUE(absl::holds_alternative<value_type1>(source));
729  const TypeParam value(TypeParam::kIndex);
730  Variant target(value);
731  ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
732  target = source;
733  EXPECT_TRUE(absl::holds_alternative<value_type1>(target));
734  EXPECT_TRUE(absl::holds_alternative<value_type1>(source));
735  EXPECT_EQ(absl::get<value_type1>(source), absl::get<value_type1>(target));
736 }
737 
738 // Test that operator=<T> works, that assigning a new value destroys
739 // the old and that assigning the new value again does not redestroy
740 // the old
741 TEST(VariantTest, TestAssign) {
742  typedef VariantFactory<IncrementInDtor>::Type Variant;
743  using value_type1 = absl::variant_alternative_t<0, Variant>;
744  using value_type2 = absl::variant_alternative_t<1, Variant>;
745  using value_type3 = absl::variant_alternative_t<2, Variant>;
746  using value_type4 = absl::variant_alternative_t<3, Variant>;
747 
748  const int kSize = 4;
749  int counter[kSize];
750  std::unique_ptr<IncrementInDtor> counter_adjustor[kSize];
751  for (int i = 0; i != kSize; i++) {
752  counter[i] = 0;
753  counter_adjustor[i] = absl::make_unique<IncrementInDtor>(&counter[i]);
754  }
755 
756  value_type1 v1(*counter_adjustor[0]);
757  value_type2 v2(*counter_adjustor[1]);
758  value_type3 v3(*counter_adjustor[2]);
759  value_type4 v4(*counter_adjustor[3]);
760 
761  // Test that reassignment causes destruction of old value
762  {
763  Variant object(v1);
764  object = v2;
765  object = v3;
766  object = v4;
767  object = v1;
768  }
769 
770  EXPECT_EQ(2, counter[0]);
771  EXPECT_EQ(1, counter[1]);
772  EXPECT_EQ(1, counter[2]);
773  EXPECT_EQ(1, counter[3]);
774 
775  std::fill(std::begin(counter), std::end(counter), 0);
776 
777  // Test that self-assignment does not cause destruction of old value
778  {
779  Variant object(v1);
780  object.operator=(object);
781  EXPECT_EQ(0, counter[0]);
782  }
783  {
784  Variant object(v2);
785  object.operator=(object);
786  EXPECT_EQ(0, counter[1]);
787  }
788  {
789  Variant object(v3);
790  object.operator=(object);
791  EXPECT_EQ(0, counter[2]);
792  }
793  {
794  Variant object(v4);
795  object.operator=(object);
796  EXPECT_EQ(0, counter[3]);
797  }
798 
799  EXPECT_EQ(1, counter[0]);
800  EXPECT_EQ(1, counter[1]);
801  EXPECT_EQ(1, counter[2]);
802  EXPECT_EQ(1, counter[3]);
803 }
804 
805 // This tests that we perform a backup if the copy-assign can throw but the move
806 // cannot throw.
807 TEST(VariantTest, TestBackupAssign) {
808  typedef VariantFactory<IncrementInDtorCopyCanThrow>::Type Variant;
809  using value_type1 = absl::variant_alternative_t<0, Variant>;
810  using value_type2 = absl::variant_alternative_t<1, Variant>;
811  using value_type3 = absl::variant_alternative_t<2, Variant>;
812  using value_type4 = absl::variant_alternative_t<3, Variant>;
813 
814  const int kSize = 4;
815  int counter[kSize];
816  std::unique_ptr<IncrementInDtorCopyCanThrow> counter_adjustor[kSize];
817  for (int i = 0; i != kSize; i++) {
818  counter[i] = 0;
819  counter_adjustor[i].reset(new IncrementInDtorCopyCanThrow(&counter[i]));
820  }
821 
822  value_type1 v1(*counter_adjustor[0]);
823  value_type2 v2(*counter_adjustor[1]);
824  value_type3 v3(*counter_adjustor[2]);
825  value_type4 v4(*counter_adjustor[3]);
826 
827  // Test that reassignment causes destruction of old value
828  {
829  Variant object(v1);
830  object = v2;
831  object = v3;
832  object = v4;
833  object = v1;
834  }
835 
836  // libstdc++ doesn't pass this test
837 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
838  EXPECT_EQ(3, counter[0]);
839  EXPECT_EQ(2, counter[1]);
840  EXPECT_EQ(2, counter[2]);
841  EXPECT_EQ(2, counter[3]);
842 #endif
843 
844  std::fill(std::begin(counter), std::end(counter), 0);
845 
846  // Test that self-assignment does not cause destruction of old value
847  {
848  Variant object(v1);
849  object.operator=(object);
850  EXPECT_EQ(0, counter[0]);
851  }
852  {
853  Variant object(v2);
854  object.operator=(object);
855  EXPECT_EQ(0, counter[1]);
856  }
857  {
858  Variant object(v3);
859  object.operator=(object);
860  EXPECT_EQ(0, counter[2]);
861  }
862  {
863  Variant object(v4);
864  object.operator=(object);
865  EXPECT_EQ(0, counter[3]);
866  }
867 
868  EXPECT_EQ(1, counter[0]);
869  EXPECT_EQ(1, counter[1]);
870  EXPECT_EQ(1, counter[2]);
871  EXPECT_EQ(1, counter[3]);
872 }
873 
875 // [variant.mod] //
877 
878 TEST(VariantTest, TestEmplaceBasic) {
879  using Variant = variant<int, char>;
880 
881  Variant v(absl::in_place_index<0>, 0);
882 
883  {
884  char& emplace_result = v.emplace<char>();
885  ASSERT_TRUE(absl::holds_alternative<char>(v));
886  EXPECT_EQ(absl::get<char>(v), 0);
887  EXPECT_EQ(&emplace_result, &absl::get<char>(v));
888  }
889 
890  // Make sure that another emplace does zero-initialization
891  absl::get<char>(v) = 'a';
892  v.emplace<char>('b');
893  ASSERT_TRUE(absl::holds_alternative<char>(v));
894  EXPECT_EQ(absl::get<char>(v), 'b');
895 
896  {
897  int& emplace_result = v.emplace<int>();
898  EXPECT_TRUE(absl::holds_alternative<int>(v));
899  EXPECT_EQ(absl::get<int>(v), 0);
900  EXPECT_EQ(&emplace_result, &absl::get<int>(v));
901  }
902 }
903 
904 TEST(VariantTest, TestEmplaceInitializerList) {
905  using Var =
906  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
907 
908  Var v1(absl::in_place_index<0>, 555);
909  MoveOnlyWithListConstructor& emplace_result =
910  v1.emplace<MoveOnlyWithListConstructor>({1, 2, 3, 4, 5}, 6);
911  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
912  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
913  EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
914 }
915 
916 TEST(VariantTest, TestEmplaceIndex) {
917  using Variant = variant<int, char>;
918 
919  Variant v(absl::in_place_index<0>, 555);
920 
921  {
922  char& emplace_result = v.emplace<1>();
923  ASSERT_TRUE(absl::holds_alternative<char>(v));
924  EXPECT_EQ(absl::get<char>(v), 0);
925  EXPECT_EQ(&emplace_result, &absl::get<char>(v));
926  }
927 
928  // Make sure that another emplace does zero-initialization
929  absl::get<char>(v) = 'a';
930  v.emplace<1>('b');
931  ASSERT_TRUE(absl::holds_alternative<char>(v));
932  EXPECT_EQ(absl::get<char>(v), 'b');
933 
934  {
935  int& emplace_result = v.emplace<0>();
936  EXPECT_TRUE(absl::holds_alternative<int>(v));
937  EXPECT_EQ(absl::get<int>(v), 0);
938  EXPECT_EQ(&emplace_result, &absl::get<int>(v));
939  }
940 }
941 
942 TEST(VariantTest, TestEmplaceIndexInitializerList) {
943  using Var =
944  variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
945 
946  Var v1(absl::in_place_index<0>, 555);
947  MoveOnlyWithListConstructor& emplace_result =
948  v1.emplace<3>({1, 2, 3, 4, 5}, 6);
949  ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
950  EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
951  EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
952 }
953 
955 // [variant.status] //
957 
958 TEST(VariantTest, Index) {
959  using Var = variant<int, std::string, double>;
960 
961  Var v = 1;
962  EXPECT_EQ(0, v.index());
963  v = "str";
964  EXPECT_EQ(1, v.index());
965  v = 0.;
966  EXPECT_EQ(2, v.index());
967 
968  Var v2 = v;
969  EXPECT_EQ(2, v2.index());
970  v2.emplace<int>(3);
971  EXPECT_EQ(0, v2.index());
972 }
973 
974 TEST(VariantTest, NotValuelessByException) {
975  using Var = variant<int, std::string, double>;
976 
977  Var v = 1;
978  EXPECT_FALSE(v.valueless_by_exception());
979  v = "str";
980  EXPECT_FALSE(v.valueless_by_exception());
981  v = 0.;
982  EXPECT_FALSE(v.valueless_by_exception());
983 
984  Var v2 = v;
985  EXPECT_FALSE(v.valueless_by_exception());
986  v2.emplace<int>(3);
987  EXPECT_FALSE(v.valueless_by_exception());
988 }
989 
990 #ifdef ABSL_HAVE_EXCEPTIONS
991 
992 TEST(VariantTest, IndexValuelessByException) {
993  using Var = variant<MoveCanThrow, std::string, double>;
994 
995  Var v(absl::in_place_index<0>);
996  EXPECT_EQ(0, v.index());
997  ToValuelessByException(v);
998  EXPECT_EQ(absl::variant_npos, v.index());
999  v = "str";
1000  EXPECT_EQ(1, v.index());
1001 }
1002 
1003 TEST(VariantTest, ValuelessByException) {
1004  using Var = variant<MoveCanThrow, std::string, double>;
1005 
1006  Var v(absl::in_place_index<0>);
1007  EXPECT_FALSE(v.valueless_by_exception());
1008  ToValuelessByException(v);
1009  EXPECT_TRUE(v.valueless_by_exception());
1010  v = "str";
1011  EXPECT_FALSE(v.valueless_by_exception());
1012 }
1013 
1014 #endif // ABSL_HAVE_EXCEPTIONS
1015 
1017 // [variant.swap] //
1019 
1020 TEST(VariantTest, MemberSwap) {
1021  SpecialSwap v1(3);
1022  SpecialSwap v2(7);
1023 
1024  variant<SpecialSwap> a = v1, b = v2;
1025 
1026  EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
1027  EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
1028 
1029  a.swap(b);
1030  EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
1031  EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
1032  EXPECT_TRUE(absl::get<SpecialSwap>(a).special_swap);
1033 
1034  using V = variant<MoveCanThrow, std::string, int>;
1035  int i = 33;
1036  std::string s = "abc";
1037  V valueless(in_place_index<0>);
1038  ToValuelessByException(valueless);
1039  {
1040  // lhs and rhs holds different alternative
1041  V lhs(i), rhs(s);
1042  lhs.swap(rhs);
1043  EXPECT_THAT(lhs, VariantWith<std::string>(s));
1044  EXPECT_THAT(rhs, VariantWith<int>(i));
1045  }
1046  {
1047  // lhs is valueless
1048  V lhs(valueless), rhs(i);
1049  lhs.swap(rhs);
1050  EXPECT_THAT(lhs, VariantWith<int>(i));
1051  EXPECT_TRUE(rhs.valueless_by_exception());
1052  }
1053  {
1054  // rhs is valueless
1055  V lhs(s), rhs(valueless);
1056  lhs.swap(rhs);
1057  EXPECT_THAT(rhs, VariantWith<std::string>(s));
1058  EXPECT_TRUE(lhs.valueless_by_exception());
1059  }
1060  {
1061  // both are valueless
1062  V lhs(valueless), rhs(valueless);
1063  lhs.swap(rhs);
1064  EXPECT_TRUE(lhs.valueless_by_exception());
1065  EXPECT_TRUE(rhs.valueless_by_exception());
1066  }
1067 }
1068 
1070 // [variant.helper] //
1072 
1073 TEST(VariantTest, VariantSize) {
1074  {
1075  using Size1Variant = absl::variant<int>;
1080  }
1081 
1082  {
1083  using Size3Variant = absl::variant<int, float, int>;
1088  }
1089 }
1090 
1091 TEST(VariantTest, VariantAlternative) {
1092  {
1094  EXPECT_TRUE(
1095  (std::is_same<float, absl::variant_alternative_t<0, V>>::value));
1096  EXPECT_TRUE((std::is_same<const float,
1098  EXPECT_TRUE(
1099  (std::is_same<volatile float,
1101  EXPECT_TRUE((
1102  std::is_same<const volatile float,
1104 
1105  EXPECT_TRUE((std::is_same<int, absl::variant_alternative_t<1, V>>::value));
1106  EXPECT_TRUE((std::is_same<const int,
1108  EXPECT_TRUE(
1109  (std::is_same<volatile int,
1111  EXPECT_TRUE((
1112  std::is_same<const volatile int,
1114 
1115  EXPECT_TRUE(
1116  (std::is_same<const char*, absl::variant_alternative_t<2, V>>::value));
1117  EXPECT_TRUE((std::is_same<const char* const,
1119  EXPECT_TRUE(
1120  (std::is_same<const char* volatile,
1122  EXPECT_TRUE((
1123  std::is_same<const char* const volatile,
1125  }
1126 
1127  {
1129  EXPECT_TRUE(
1130  (std::is_same<float, absl::variant_alternative_t<0, V>>::value));
1131  EXPECT_TRUE((std::is_same<const float,
1133  EXPECT_TRUE(
1134  (std::is_same<volatile float,
1136  EXPECT_TRUE((
1137  std::is_same<const volatile float,
1139 
1140  EXPECT_TRUE(
1141  (std::is_same<volatile int, absl::variant_alternative_t<1, V>>::value));
1142  EXPECT_TRUE((std::is_same<const volatile int,
1144  EXPECT_TRUE(
1145  (std::is_same<volatile int,
1147  EXPECT_TRUE((
1148  std::is_same<const volatile int,
1150 
1151  EXPECT_TRUE(
1152  (std::is_same<const char*, absl::variant_alternative_t<2, V>>::value));
1153  EXPECT_TRUE((std::is_same<const char* const,
1155  EXPECT_TRUE(
1156  (std::is_same<const char* volatile,
1158  EXPECT_TRUE((
1159  std::is_same<const char* const volatile,
1161  }
1162 }
1163 
1165 // [variant.get] //
1167 
1168 TEST(VariantTest, HoldsAlternative) {
1169  using Var = variant<int, std::string, double>;
1170 
1171  Var v = 1;
1172  EXPECT_TRUE(absl::holds_alternative<int>(v));
1173  EXPECT_FALSE(absl::holds_alternative<std::string>(v));
1174  EXPECT_FALSE(absl::holds_alternative<double>(v));
1175  v = "str";
1176  EXPECT_FALSE(absl::holds_alternative<int>(v));
1177  EXPECT_TRUE(absl::holds_alternative<std::string>(v));
1178  EXPECT_FALSE(absl::holds_alternative<double>(v));
1179  v = 0.;
1180  EXPECT_FALSE(absl::holds_alternative<int>(v));
1181  EXPECT_FALSE(absl::holds_alternative<std::string>(v));
1182  EXPECT_TRUE(absl::holds_alternative<double>(v));
1183 
1184  Var v2 = v;
1185  EXPECT_FALSE(absl::holds_alternative<int>(v2));
1186  EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
1187  EXPECT_TRUE(absl::holds_alternative<double>(v2));
1188  v2.emplace<int>(3);
1189  EXPECT_TRUE(absl::holds_alternative<int>(v2));
1190  EXPECT_FALSE(absl::holds_alternative<std::string>(v2));
1191  EXPECT_FALSE(absl::holds_alternative<double>(v2));
1192 }
1193 
1194 TEST(VariantTest, GetIndex) {
1195  using Var = variant<int, std::string, double, int>;
1196 
1197  {
1198  Var v(absl::in_place_index<0>, 0);
1199 
1200  using LValueGetType = decltype(absl::get<0>(v));
1201  using RValueGetType = decltype(absl::get<0>(absl::move(v)));
1202 
1205  EXPECT_EQ(absl::get<0>(v), 0);
1206  EXPECT_EQ(absl::get<0>(absl::move(v)), 0);
1207 
1208  const Var& const_v = v;
1209  using ConstLValueGetType = decltype(absl::get<0>(const_v));
1210  using ConstRValueGetType = decltype(absl::get<0>(absl::move(const_v)));
1213  EXPECT_EQ(absl::get<0>(const_v), 0);
1214  EXPECT_EQ(absl::get<0>(absl::move(const_v)), 0);
1215  }
1216 
1217  {
1218  Var v = std::string("Hello");
1219 
1220  using LValueGetType = decltype(absl::get<1>(v));
1221  using RValueGetType = decltype(absl::get<1>(absl::move(v)));
1222 
1225  EXPECT_EQ(absl::get<1>(v), "Hello");
1226  EXPECT_EQ(absl::get<1>(absl::move(v)), "Hello");
1227 
1228  const Var& const_v = v;
1229  using ConstLValueGetType = decltype(absl::get<1>(const_v));
1230  using ConstRValueGetType = decltype(absl::get<1>(absl::move(const_v)));
1233  EXPECT_EQ(absl::get<1>(const_v), "Hello");
1234  EXPECT_EQ(absl::get<1>(absl::move(const_v)), "Hello");
1235  }
1236 
1237  {
1238  Var v = 2.0;
1239 
1240  using LValueGetType = decltype(absl::get<2>(v));
1241  using RValueGetType = decltype(absl::get<2>(absl::move(v)));
1242 
1245  EXPECT_EQ(absl::get<2>(v), 2.);
1246  EXPECT_EQ(absl::get<2>(absl::move(v)), 2.);
1247 
1248  const Var& const_v = v;
1249  using ConstLValueGetType = decltype(absl::get<2>(const_v));
1250  using ConstRValueGetType = decltype(absl::get<2>(absl::move(const_v)));
1253  EXPECT_EQ(absl::get<2>(const_v), 2.);
1254  EXPECT_EQ(absl::get<2>(absl::move(const_v)), 2.);
1255  }
1256 
1257  {
1258  Var v(absl::in_place_index<0>, 0);
1259  v.emplace<3>(1);
1260 
1261  using LValueGetType = decltype(absl::get<3>(v));
1262  using RValueGetType = decltype(absl::get<3>(absl::move(v)));
1263 
1266  EXPECT_EQ(absl::get<3>(v), 1);
1267  EXPECT_EQ(absl::get<3>(absl::move(v)), 1);
1268 
1269  const Var& const_v = v;
1270  using ConstLValueGetType = decltype(absl::get<3>(const_v));
1271  using ConstRValueGetType = decltype(absl::get<3>(absl::move(const_v)));
1274  EXPECT_EQ(absl::get<3>(const_v), 1);
1275  EXPECT_EQ(absl::get<3>(absl::move(const_v)), 1); // NOLINT
1276  }
1277 }
1278 
1279 TEST(VariantTest, BadGetIndex) {
1280  using Var = variant<int, std::string, double>;
1281 
1282  {
1283  Var v = 1;
1284 
1287 
1288  const Var& const_v = v;
1289  ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<1>(const_v));
1291  absl::get<1>(std::move(const_v))); // NOLINT
1292  }
1293 
1294  {
1295  Var v = std::string("Hello");
1296 
1299 
1300  const Var& const_v = v;
1301  ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<0>(const_v));
1303  absl::get<0>(std::move(const_v))); // NOLINT
1304  }
1305 }
1306 
1307 TEST(VariantTest, GetType) {
1308  using Var = variant<int, std::string, double>;
1309 
1310  {
1311  Var v = 1;
1312 
1313  using LValueGetType = decltype(absl::get<int>(v));
1314  using RValueGetType = decltype(absl::get<int>(absl::move(v)));
1315 
1318  EXPECT_EQ(absl::get<int>(v), 1);
1319  EXPECT_EQ(absl::get<int>(absl::move(v)), 1);
1320 
1321  const Var& const_v = v;
1322  using ConstLValueGetType = decltype(absl::get<int>(const_v));
1323  using ConstRValueGetType = decltype(absl::get<int>(absl::move(const_v)));
1326  EXPECT_EQ(absl::get<int>(const_v), 1);
1327  EXPECT_EQ(absl::get<int>(absl::move(const_v)), 1);
1328  }
1329 
1330  {
1331  Var v = std::string("Hello");
1332 
1333  using LValueGetType = decltype(absl::get<1>(v));
1334  using RValueGetType = decltype(absl::get<1>(absl::move(v)));
1335 
1338  EXPECT_EQ(absl::get<std::string>(v), "Hello");
1339  EXPECT_EQ(absl::get<std::string>(absl::move(v)), "Hello");
1340 
1341  const Var& const_v = v;
1342  using ConstLValueGetType = decltype(absl::get<1>(const_v));
1343  using ConstRValueGetType = decltype(absl::get<1>(absl::move(const_v)));
1346  EXPECT_EQ(absl::get<std::string>(const_v), "Hello");
1347  EXPECT_EQ(absl::get<std::string>(absl::move(const_v)), "Hello");
1348  }
1349 
1350  {
1351  Var v = 2.0;
1352 
1353  using LValueGetType = decltype(absl::get<2>(v));
1354  using RValueGetType = decltype(absl::get<2>(absl::move(v)));
1355 
1358  EXPECT_EQ(absl::get<double>(v), 2.);
1359  EXPECT_EQ(absl::get<double>(absl::move(v)), 2.);
1360 
1361  const Var& const_v = v;
1362  using ConstLValueGetType = decltype(absl::get<2>(const_v));
1363  using ConstRValueGetType = decltype(absl::get<2>(absl::move(const_v)));
1366  EXPECT_EQ(absl::get<double>(const_v), 2.);
1367  EXPECT_EQ(absl::get<double>(absl::move(const_v)), 2.);
1368  }
1369 }
1370 
1371 TEST(VariantTest, BadGetType) {
1372  using Var = variant<int, std::string, double>;
1373 
1374  {
1375  Var v = 1;
1376 
1377  ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<std::string>(v));
1379  absl::get<std::string>(std::move(v)));
1380 
1381  const Var& const_v = v;
1383  absl::get<std::string>(const_v));
1385  absl::get<std::string>(std::move(const_v))); // NOLINT
1386  }
1387 
1388  {
1389  Var v = std::string("Hello");
1390 
1393 
1394  const Var& const_v = v;
1395  ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(absl::get<int>(const_v));
1397  absl::get<int>(std::move(const_v))); // NOLINT
1398  }
1399 }
1400 
1401 TEST(VariantTest, GetIfIndex) {
1402  using Var = variant<int, std::string, double, int>;
1403 
1404  {
1405  Var v(absl::in_place_index<0>, 0);
1406  EXPECT_TRUE(noexcept(absl::get_if<0>(&v)));
1407 
1408  {
1409  auto* elem = absl::get_if<0>(&v);
1410  EXPECT_TRUE((std::is_same<decltype(elem), int*>::value));
1411  ASSERT_NE(elem, nullptr);
1412  EXPECT_EQ(*elem, 0);
1413  {
1414  auto* bad_elem = absl::get_if<1>(&v);
1415  EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
1416  EXPECT_EQ(bad_elem, nullptr);
1417  }
1418  {
1419  auto* bad_elem = absl::get_if<2>(&v);
1420  EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
1421  EXPECT_EQ(bad_elem, nullptr);
1422  }
1423  {
1424  auto* bad_elem = absl::get_if<3>(&v);
1425  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1426  EXPECT_EQ(bad_elem, nullptr);
1427  }
1428  }
1429 
1430  const Var& const_v = v;
1431  EXPECT_TRUE(noexcept(absl::get_if<0>(&const_v)));
1432 
1433  {
1434  auto* elem = absl::get_if<0>(&const_v);
1435  EXPECT_TRUE((std::is_same<decltype(elem), const int*>::value));
1436  ASSERT_NE(elem, nullptr);
1437  EXPECT_EQ(*elem, 0);
1438  {
1439  auto* bad_elem = absl::get_if<1>(&const_v);
1440  EXPECT_TRUE(
1441  (std::is_same<decltype(bad_elem), const std::string*>::value));
1442  EXPECT_EQ(bad_elem, nullptr);
1443  }
1444  {
1445  auto* bad_elem = absl::get_if<2>(&const_v);
1446  EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
1447  EXPECT_EQ(bad_elem, nullptr);
1448  }
1449  {
1450  auto* bad_elem = absl::get_if<3>(&const_v);
1451  EXPECT_EQ(bad_elem, nullptr);
1452  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1453  }
1454  }
1455  }
1456 
1457  {
1458  Var v = std::string("Hello");
1459  EXPECT_TRUE(noexcept(absl::get_if<1>(&v)));
1460 
1461  {
1462  auto* elem = absl::get_if<1>(&v);
1463  EXPECT_TRUE((std::is_same<decltype(elem), std::string*>::value));
1464  ASSERT_NE(elem, nullptr);
1465  EXPECT_EQ(*elem, "Hello");
1466  {
1467  auto* bad_elem = absl::get_if<0>(&v);
1468  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1469  EXPECT_EQ(bad_elem, nullptr);
1470  }
1471  {
1472  auto* bad_elem = absl::get_if<2>(&v);
1473  EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
1474  EXPECT_EQ(bad_elem, nullptr);
1475  }
1476  {
1477  auto* bad_elem = absl::get_if<3>(&v);
1478  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1479  EXPECT_EQ(bad_elem, nullptr);
1480  }
1481  }
1482 
1483  const Var& const_v = v;
1484  EXPECT_TRUE(noexcept(absl::get_if<1>(&const_v)));
1485 
1486  {
1487  auto* elem = absl::get_if<1>(&const_v);
1488  EXPECT_TRUE((std::is_same<decltype(elem), const std::string*>::value));
1489  ASSERT_NE(elem, nullptr);
1490  EXPECT_EQ(*elem, "Hello");
1491  {
1492  auto* bad_elem = absl::get_if<0>(&const_v);
1493  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1494  EXPECT_EQ(bad_elem, nullptr);
1495  }
1496  {
1497  auto* bad_elem = absl::get_if<2>(&const_v);
1498  EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
1499  EXPECT_EQ(bad_elem, nullptr);
1500  }
1501  {
1502  auto* bad_elem = absl::get_if<3>(&const_v);
1503  EXPECT_EQ(bad_elem, nullptr);
1504  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1505  }
1506  }
1507  }
1508 
1509  {
1510  Var v = 2.0;
1511  EXPECT_TRUE(noexcept(absl::get_if<2>(&v)));
1512 
1513  {
1514  auto* elem = absl::get_if<2>(&v);
1515  EXPECT_TRUE((std::is_same<decltype(elem), double*>::value));
1516  ASSERT_NE(elem, nullptr);
1517  EXPECT_EQ(*elem, 2.0);
1518  {
1519  auto* bad_elem = absl::get_if<0>(&v);
1520  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1521  EXPECT_EQ(bad_elem, nullptr);
1522  }
1523  {
1524  auto* bad_elem = absl::get_if<1>(&v);
1525  EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
1526  EXPECT_EQ(bad_elem, nullptr);
1527  }
1528  {
1529  auto* bad_elem = absl::get_if<3>(&v);
1530  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1531  EXPECT_EQ(bad_elem, nullptr);
1532  }
1533  }
1534 
1535  const Var& const_v = v;
1536  EXPECT_TRUE(noexcept(absl::get_if<2>(&const_v)));
1537 
1538  {
1539  auto* elem = absl::get_if<2>(&const_v);
1540  EXPECT_TRUE((std::is_same<decltype(elem), const double*>::value));
1541  ASSERT_NE(elem, nullptr);
1542  EXPECT_EQ(*elem, 2.0);
1543  {
1544  auto* bad_elem = absl::get_if<0>(&const_v);
1545  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1546  EXPECT_EQ(bad_elem, nullptr);
1547  }
1548  {
1549  auto* bad_elem = absl::get_if<1>(&const_v);
1550  EXPECT_TRUE(
1551  (std::is_same<decltype(bad_elem), const std::string*>::value));
1552  EXPECT_EQ(bad_elem, nullptr);
1553  }
1554  {
1555  auto* bad_elem = absl::get_if<3>(&const_v);
1556  EXPECT_EQ(bad_elem, nullptr);
1557  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1558  }
1559  }
1560  }
1561 
1562  {
1563  Var v(absl::in_place_index<0>, 0);
1564  v.emplace<3>(1);
1565  EXPECT_TRUE(noexcept(absl::get_if<3>(&v)));
1566 
1567  {
1568  auto* elem = absl::get_if<3>(&v);
1569  EXPECT_TRUE((std::is_same<decltype(elem), int*>::value));
1570  ASSERT_NE(elem, nullptr);
1571  EXPECT_EQ(*elem, 1);
1572  {
1573  auto* bad_elem = absl::get_if<0>(&v);
1574  EXPECT_TRUE((std::is_same<decltype(bad_elem), int*>::value));
1575  EXPECT_EQ(bad_elem, nullptr);
1576  }
1577  {
1578  auto* bad_elem = absl::get_if<1>(&v);
1579  EXPECT_TRUE((std::is_same<decltype(bad_elem), std::string*>::value));
1580  EXPECT_EQ(bad_elem, nullptr);
1581  }
1582  {
1583  auto* bad_elem = absl::get_if<2>(&v);
1584  EXPECT_TRUE((std::is_same<decltype(bad_elem), double*>::value));
1585  EXPECT_EQ(bad_elem, nullptr);
1586  }
1587  }
1588 
1589  const Var& const_v = v;
1590  EXPECT_TRUE(noexcept(absl::get_if<3>(&const_v)));
1591 
1592  {
1593  auto* elem = absl::get_if<3>(&const_v);
1594  EXPECT_TRUE((std::is_same<decltype(elem), const int*>::value));
1595  ASSERT_NE(elem, nullptr);
1596  EXPECT_EQ(*elem, 1);
1597  {
1598  auto* bad_elem = absl::get_if<0>(&const_v);
1599  EXPECT_TRUE((std::is_same<decltype(bad_elem), const int*>::value));
1600  EXPECT_EQ(bad_elem, nullptr);
1601  }
1602  {
1603  auto* bad_elem = absl::get_if<1>(&const_v);
1604  EXPECT_TRUE(
1605  (std::is_same<decltype(bad_elem), const std::string*>::value));
1606  EXPECT_EQ(bad_elem, nullptr);
1607  }
1608  {
1609  auto* bad_elem = absl::get_if<2>(&const_v);
1610  EXPECT_EQ(bad_elem, nullptr);
1611  EXPECT_TRUE((std::is_same<decltype(bad_elem), const double*>::value));
1612  }
1613  }
1614  }
1615 }
1616 
1618 // [variant.relops] //
1620 
1621 TEST(VariantTest, OperatorEquals) {
1622  variant<int, std::string> a(1), b(1);
1623  EXPECT_TRUE(a == b);
1624  EXPECT_TRUE(b == a);
1625  EXPECT_FALSE(a != b);
1626  EXPECT_FALSE(b != a);
1627 
1628  b = "str";
1629  EXPECT_FALSE(a == b);
1630  EXPECT_FALSE(b == a);
1631  EXPECT_TRUE(a != b);
1632  EXPECT_TRUE(b != a);
1633 
1634  b = 0;
1635  EXPECT_FALSE(a == b);
1636  EXPECT_FALSE(b == a);
1637  EXPECT_TRUE(a != b);
1638  EXPECT_TRUE(b != a);
1639 
1640  a = b = "foo";
1641  EXPECT_TRUE(a == b);
1642  EXPECT_TRUE(b == a);
1643  EXPECT_FALSE(a != b);
1644  EXPECT_FALSE(b != a);
1645 
1646  a = "bar";
1647  EXPECT_FALSE(a == b);
1648  EXPECT_FALSE(b == a);
1649  EXPECT_TRUE(a != b);
1650  EXPECT_TRUE(b != a);
1651 }
1652 
1653 TEST(VariantTest, OperatorRelational) {
1654  variant<int, std::string> a(1), b(1);
1655  EXPECT_FALSE(a < b);
1656  EXPECT_FALSE(b < a);
1657  EXPECT_FALSE(a > b);
1658  EXPECT_FALSE(b > a);
1659  EXPECT_TRUE(a <= b);
1660  EXPECT_TRUE(b <= a);
1661  EXPECT_TRUE(a >= b);
1662  EXPECT_TRUE(b >= a);
1663 
1664  b = "str";
1665  EXPECT_TRUE(a < b);
1666  EXPECT_FALSE(b < a);
1667  EXPECT_FALSE(a > b);
1668  EXPECT_TRUE(b > a);
1669  EXPECT_TRUE(a <= b);
1670  EXPECT_FALSE(b <= a);
1671  EXPECT_FALSE(a >= b);
1672  EXPECT_TRUE(b >= a);
1673 
1674  b = 0;
1675  EXPECT_FALSE(a < b);
1676  EXPECT_TRUE(b < a);
1677  EXPECT_TRUE(a > b);
1678  EXPECT_FALSE(b > a);
1679  EXPECT_FALSE(a <= b);
1680  EXPECT_TRUE(b <= a);
1681  EXPECT_TRUE(a >= b);
1682  EXPECT_FALSE(b >= a);
1683 
1684  a = b = "foo";
1685  EXPECT_FALSE(a < b);
1686  EXPECT_FALSE(b < a);
1687  EXPECT_FALSE(a > b);
1688  EXPECT_FALSE(b > a);
1689  EXPECT_TRUE(a <= b);
1690  EXPECT_TRUE(b <= a);
1691  EXPECT_TRUE(a >= b);
1692  EXPECT_TRUE(b >= a);
1693 
1694  a = "bar";
1695  EXPECT_TRUE(a < b);
1696  EXPECT_FALSE(b < a);
1697  EXPECT_FALSE(a > b);
1698  EXPECT_TRUE(b > a);
1699  EXPECT_TRUE(a <= b);
1700  EXPECT_FALSE(b <= a);
1701  EXPECT_FALSE(a >= b);
1702  EXPECT_TRUE(b >= a);
1703 }
1704 
1705 #ifdef ABSL_HAVE_EXCEPTIONS
1706 
1707 TEST(VariantTest, ValuelessOperatorEquals) {
1708  variant<MoveCanThrow, std::string> int_v(1), string_v("Hello"),
1709  valueless(absl::in_place_index<0>),
1710  other_valueless(absl::in_place_index<0>);
1711  ToValuelessByException(valueless);
1712  ToValuelessByException(other_valueless);
1713 
1714  EXPECT_TRUE(valueless == other_valueless);
1715  EXPECT_TRUE(other_valueless == valueless);
1716  EXPECT_FALSE(valueless == int_v);
1717  EXPECT_FALSE(valueless == string_v);
1718  EXPECT_FALSE(int_v == valueless);
1719  EXPECT_FALSE(string_v == valueless);
1720 
1721  EXPECT_FALSE(valueless != other_valueless);
1722  EXPECT_FALSE(other_valueless != valueless);
1723  EXPECT_TRUE(valueless != int_v);
1724  EXPECT_TRUE(valueless != string_v);
1725  EXPECT_TRUE(int_v != valueless);
1726  EXPECT_TRUE(string_v != valueless);
1727 }
1728 
1729 TEST(VariantTest, ValuelessOperatorRelational) {
1730  variant<MoveCanThrow, std::string> int_v(1), string_v("Hello"),
1731  valueless(absl::in_place_index<0>),
1732  other_valueless(absl::in_place_index<0>);
1733  ToValuelessByException(valueless);
1734  ToValuelessByException(other_valueless);
1735 
1736  EXPECT_FALSE(valueless < other_valueless);
1737  EXPECT_FALSE(other_valueless < valueless);
1738  EXPECT_TRUE(valueless < int_v);
1739  EXPECT_TRUE(valueless < string_v);
1740  EXPECT_FALSE(int_v < valueless);
1741  EXPECT_FALSE(string_v < valueless);
1742 
1743  EXPECT_TRUE(valueless <= other_valueless);
1744  EXPECT_TRUE(other_valueless <= valueless);
1745  EXPECT_TRUE(valueless <= int_v);
1746  EXPECT_TRUE(valueless <= string_v);
1747  EXPECT_FALSE(int_v <= valueless);
1748  EXPECT_FALSE(string_v <= valueless);
1749 
1750  EXPECT_TRUE(valueless >= other_valueless);
1751  EXPECT_TRUE(other_valueless >= valueless);
1752  EXPECT_FALSE(valueless >= int_v);
1753  EXPECT_FALSE(valueless >= string_v);
1754  EXPECT_TRUE(int_v >= valueless);
1755  EXPECT_TRUE(string_v >= valueless);
1756 
1757  EXPECT_FALSE(valueless > other_valueless);
1758  EXPECT_FALSE(other_valueless > valueless);
1759  EXPECT_FALSE(valueless > int_v);
1760  EXPECT_FALSE(valueless > string_v);
1761  EXPECT_TRUE(int_v > valueless);
1762  EXPECT_TRUE(string_v > valueless);
1763 }
1764 
1765 #endif
1766 
1768 // [variant.visit] //
1770 
1771 template <typename T>
1772 struct ConvertTo {
1773  template <typename U>
1774  T operator()(const U& u) const {
1775  return u;
1776  }
1777 };
1778 
1779 TEST(VariantTest, VisitSimple) {
1780  variant<std::string, const char*> v = "A";
1781 
1782  std::string str = absl::visit(ConvertTo<std::string>{}, v);
1783  EXPECT_EQ("A", str);
1784 
1785  v = std::string("B");
1786 
1787  absl::string_view piece = absl::visit(ConvertTo<absl::string_view>{}, v);
1788  EXPECT_EQ("B", piece);
1789 
1790  struct StrLen {
1791  int operator()(const std::string& s) const { return s.size(); }
1792  int operator()(const char* s) const { return strlen(s); }
1793  };
1794 
1795  v = "SomeStr";
1796  EXPECT_EQ(7, absl::visit(StrLen{}, v));
1797  v = std::string("VeryLargeThisTime");
1798  EXPECT_EQ(17, absl::visit(StrLen{}, v));
1799 }
1800 
1801 TEST(VariantTest, VisitRValue) {
1802  variant<std::string> v = std::string("X");
1803  struct Visitor {
1804  bool operator()(const std::string&) const { return false; }
1805  bool operator()(std::string&&) const { return true; } // NOLINT
1806 
1807  int operator()(const std::string&, const std::string&) const { return 0; }
1808  int operator()(const std::string&, std::string&&) const {
1809  return 1;
1810  } // NOLINT
1811  int operator()(std::string&&, const std::string&) const {
1812  return 2;
1813  } // NOLINT
1814  int operator()(std::string&&, std::string&&) const { return 3; } // NOLINT
1815  };
1816  EXPECT_FALSE(absl::visit(Visitor{}, v));
1817  EXPECT_TRUE(absl::visit(Visitor{}, absl::move(v)));
1818 
1819  // Also test the variadic overload.
1820  EXPECT_EQ(0, absl::visit(Visitor{}, v, v));
1821  EXPECT_EQ(1, absl::visit(Visitor{}, v, absl::move(v)));
1822  EXPECT_EQ(2, absl::visit(Visitor{}, absl::move(v), v));
1823  EXPECT_EQ(3, absl::visit(Visitor{}, absl::move(v), absl::move(v)));
1824 }
1825 
1826 TEST(VariantTest, VisitRValueVisitor) {
1827  variant<std::string> v = std::string("X");
1828  struct Visitor {
1829  bool operator()(const std::string&) const& { return false; }
1830  bool operator()(const std::string&) && { return true; }
1831  };
1832  Visitor visitor;
1833  EXPECT_FALSE(absl::visit(visitor, v));
1834  EXPECT_TRUE(absl::visit(Visitor{}, v));
1835 }
1836 
1837 TEST(VariantTest, VisitResultTypeDifferent) {
1838  variant<std::string> v = std::string("X");
1839  struct LValue_LValue {};
1840  struct RValue_LValue {};
1841  struct LValue_RValue {};
1842  struct RValue_RValue {};
1843  struct Visitor {
1844  LValue_LValue operator()(const std::string&) const& { return {}; }
1845  RValue_LValue operator()(std::string&&) const& { return {}; } // NOLINT
1846  LValue_RValue operator()(const std::string&) && { return {}; }
1847  RValue_RValue operator()(std::string&&) && { return {}; } // NOLINT
1848  } visitor;
1849 
1850  EXPECT_TRUE(
1851  (std::is_same<LValue_LValue, decltype(absl::visit(visitor, v))>::value));
1852  EXPECT_TRUE(
1853  (std::is_same<RValue_LValue,
1854  decltype(absl::visit(visitor, absl::move(v)))>::value));
1855  EXPECT_TRUE((
1856  std::is_same<LValue_RValue, decltype(absl::visit(Visitor{}, v))>::value));
1857  EXPECT_TRUE(
1858  (std::is_same<RValue_RValue,
1859  decltype(absl::visit(Visitor{}, absl::move(v)))>::value));
1860 }
1861 
1862 TEST(VariantTest, VisitVariadic) {
1863  using A = variant<int, std::string>;
1864  using B = variant<std::unique_ptr<int>, absl::string_view>;
1865 
1866  struct Visitor {
1867  std::pair<int, int> operator()(int a, std::unique_ptr<int> b) const {
1868  return {a, *b};
1869  }
1870  std::pair<int, int> operator()(absl::string_view a,
1871  std::unique_ptr<int> b) const {
1872  return {static_cast<int>(a.size()), static_cast<int>(*b)};
1873  }
1874  std::pair<int, int> operator()(int a, absl::string_view b) const {
1875  return {a, static_cast<int>(b.size())};
1876  }
1877  std::pair<int, int> operator()(absl::string_view a,
1878  absl::string_view b) const {
1879  return {static_cast<int>(a.size()), static_cast<int>(b.size())};
1880  }
1881  };
1882 
1883  EXPECT_THAT(absl::visit(Visitor(), A(1), B(std::unique_ptr<int>(new int(7)))),
1884  ::testing::Pair(1, 7));
1885  EXPECT_THAT(absl::visit(Visitor(), A(1), B(absl::string_view("ABC"))),
1886  ::testing::Pair(1, 3));
1887  EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")),
1888  B(std::unique_ptr<int>(new int(7)))),
1889  ::testing::Pair(5, 7));
1890  EXPECT_THAT(absl::visit(Visitor(), A(std::string("BBBBB")),
1891  B(absl::string_view("ABC"))),
1892  ::testing::Pair(5, 3));
1893 }
1894 
1895 TEST(VariantTest, VisitNoArgs) {
1896  EXPECT_EQ(5, absl::visit([] { return 5; }));
1897 }
1898 
1899 struct ConstFunctor {
1900  int operator()(int a, int b) const { return a - b; }
1901 };
1902 
1903 struct MutableFunctor {
1904  int operator()(int a, int b) { return a - b; }
1905 };
1906 
1907 struct Class {
1908  int Method(int a, int b) { return a - b; }
1909  int ConstMethod(int a, int b) const { return a - b; }
1910 
1911  int member;
1912 };
1913 
1914 TEST(VariantTest, VisitReferenceWrapper) {
1915  ConstFunctor cf;
1916  MutableFunctor mf;
1917  absl::variant<int> three = 3;
1918  absl::variant<int> two = 2;
1919 
1920  EXPECT_EQ(1, absl::visit(std::cref(cf), three, two));
1921  EXPECT_EQ(1, absl::visit(std::ref(cf), three, two));
1922  EXPECT_EQ(1, absl::visit(std::ref(mf), three, two));
1923 }
1924 
1925 // libstdc++ std::variant doesn't support the INVOKE semantics.
1926 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
1927 TEST(VariantTest, VisitMemberFunction) {
1928  absl::variant<std::unique_ptr<Class>> p(absl::make_unique<Class>());
1930  absl::make_unique<const Class>());
1931  absl::variant<int> three = 3;
1932  absl::variant<int> two = 2;
1933 
1934  EXPECT_EQ(1, absl::visit(&Class::Method, p, three, two));
1935  EXPECT_EQ(1, absl::visit(&Class::ConstMethod, p, three, two));
1936  EXPECT_EQ(1, absl::visit(&Class::ConstMethod, cp, three, two));
1937 }
1938 
1939 TEST(VariantTest, VisitDataMember) {
1940  absl::variant<std::unique_ptr<Class>> p(absl::make_unique<Class>(Class{42}));
1942  absl::make_unique<const Class>(Class{42}));
1943  EXPECT_EQ(42, absl::visit(&Class::member, p));
1944 
1945  absl::visit(&Class::member, p) = 5;
1946  EXPECT_EQ(5, absl::visit(&Class::member, p));
1947 
1948  EXPECT_EQ(42, absl::visit(&Class::member, cp));
1949 }
1950 #endif // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
1951 
1953 // [variant.monostate] //
1955 
1956 TEST(VariantTest, MonostateBasic) {
1957  absl::monostate mono;
1958  (void)mono;
1959 
1960  // TODO(mattcalabrese) Expose move triviality metafunctions in absl.
1967 }
1968 
1969 TEST(VariantTest, VariantMonostateDefaultConstruction) {
1971  EXPECT_EQ(var.index(), 0);
1972 }
1973 
1975 // [variant.monostate.relops] //
1977 
1978 TEST(VariantTest, MonostateComparisons) {
1979  absl::monostate lhs, rhs;
1980 
1981  EXPECT_EQ(lhs, lhs);
1982  EXPECT_EQ(lhs, rhs);
1983 
1984  EXPECT_FALSE(lhs != lhs);
1985  EXPECT_FALSE(lhs != rhs);
1986  EXPECT_FALSE(lhs < lhs);
1987  EXPECT_FALSE(lhs < rhs);
1988  EXPECT_FALSE(lhs > lhs);
1989  EXPECT_FALSE(lhs > rhs);
1990 
1991  EXPECT_LE(lhs, lhs);
1992  EXPECT_LE(lhs, rhs);
1993  EXPECT_GE(lhs, lhs);
1994  EXPECT_GE(lhs, rhs);
1995 
1996  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() ==
1997  std::declval<absl::monostate>()));
1998  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() !=
1999  std::declval<absl::monostate>()));
2000  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <
2001  std::declval<absl::monostate>()));
2002  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >
2003  std::declval<absl::monostate>()));
2004  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() <=
2005  std::declval<absl::monostate>()));
2006  EXPECT_TRUE(noexcept(std::declval<absl::monostate>() >=
2007  std::declval<absl::monostate>()));
2008 }
2009 
2011 // [variant.specalg] //
2013 
2014 TEST(VariantTest, NonmemberSwap) {
2015  using std::swap;
2016 
2017  SpecialSwap v1(3);
2018  SpecialSwap v2(7);
2019 
2020  variant<SpecialSwap> a = v1, b = v2;
2021 
2022  EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
2023  EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
2024 
2025  std::swap(a, b);
2026  EXPECT_THAT(a, VariantWith<SpecialSwap>(v2));
2027  EXPECT_THAT(b, VariantWith<SpecialSwap>(v1));
2028 #ifndef ABSL_HAVE_STD_VARIANT
2029  EXPECT_FALSE(absl::get<SpecialSwap>(a).special_swap);
2030 #endif
2031 
2032  swap(a, b);
2033  EXPECT_THAT(a, VariantWith<SpecialSwap>(v1));
2034  EXPECT_THAT(b, VariantWith<SpecialSwap>(v2));
2035  EXPECT_TRUE(absl::get<SpecialSwap>(b).special_swap);
2036 }
2037 
2039 // [variant.bad.access] //
2041 
2042 TEST(VariantTest, BadAccess) {
2043  EXPECT_TRUE(noexcept(absl::bad_variant_access()));
2044  absl::bad_variant_access exception_obj;
2045  std::exception* base = &exception_obj;
2046  (void)base;
2047 }
2048 
2050 // [variant.hash] //
2052 
2053 TEST(VariantTest, MonostateHash) {
2054  absl::monostate mono, other_mono;
2056  static_assert(std::is_same<decltype(hasher(mono)), std::size_t>::value, "");
2057  EXPECT_EQ(hasher(mono), hasher(other_mono));
2058 }
2059 
2060 TEST(VariantTest, Hash) {
2061  static_assert(type_traits_internal::IsHashable<variant<int>>::value, "");
2062  static_assert(type_traits_internal::IsHashable<variant<Hashable>>::value, "");
2063  static_assert(type_traits_internal::IsHashable<variant<int, Hashable>>::value,
2064  "");
2065 
2066 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
2067  static_assert(!type_traits_internal::IsHashable<variant<NonHashable>>::value,
2068  "");
2069  static_assert(
2070  !type_traits_internal::IsHashable<variant<Hashable, NonHashable>>::value,
2071  "");
2072 #endif
2073 
2074 // MSVC std::hash<std::variant> does not use the index, thus produce the same
2075 // result on the same value as different alternative.
2076 #if !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT))
2077  {
2078  // same value as different alternative
2079  variant<int, int> v0(in_place_index<0>, 42);
2080  variant<int, int> v1(in_place_index<1>, 42);
2081  std::hash<variant<int, int>> hash;
2082  EXPECT_NE(hash(v0), hash(v1));
2083  }
2084 #endif // !(defined(_MSC_VER) && defined(ABSL_HAVE_STD_VARIANT))
2085 
2086  {
2087  std::hash<variant<int>> hash;
2088  std::set<size_t> hashcodes;
2089  for (int i = 0; i < 100; ++i) {
2090  hashcodes.insert(hash(i));
2091  }
2092  EXPECT_GT(hashcodes.size(), 90);
2093 
2094  // test const-qualified
2095  static_assert(type_traits_internal::IsHashable<variant<const int>>::value,
2096  "");
2097  static_assert(
2098  type_traits_internal::IsHashable<variant<const Hashable>>::value, "");
2099  std::hash<absl::variant<const int>> c_hash;
2100  for (int i = 0; i < 100; ++i) {
2101  EXPECT_EQ(hash(i), c_hash(i));
2102  }
2103  }
2104 }
2105 
2107 // Miscellaneous and deprecated tests //
2109 
2110 // Test that a set requiring a basic type conversion works correctly.
2111 TEST(VariantTest, TestConvertingSet) {
2112  typedef variant<double> Variant;
2113  Variant v(1.0);
2114  const int two = 2;
2115  v = two;
2116  EXPECT_TRUE(absl::holds_alternative<double>(v));
2117  ASSERT_TRUE(nullptr != absl::get_if<double>(&v));
2118  EXPECT_DOUBLE_EQ(2, absl::get<double>(v));
2119 }
2120 
2121 // Test that a vector of variants behaves reasonably.
2122 TEST(VariantTest, Container) {
2123  typedef variant<int, float> Variant;
2124 
2125  // Creation of vector should work
2126  std::vector<Variant> vec;
2127  vec.push_back(Variant(10));
2128  vec.push_back(Variant(20.0f));
2129 
2130  // Vector resizing should work if we supply a value for new slots
2131  vec.resize(10, Variant(0));
2132 }
2133 
2134 // Test that a variant with a non-copyable type can be constructed and
2135 // manipulated to some degree.
2136 TEST(VariantTest, TestVariantWithNonCopyableType) {
2137  typedef variant<int, NonCopyable> Variant;
2138  const int kValue = 1;
2139  Variant v(kValue);
2140  ASSERT_TRUE(absl::holds_alternative<int>(v));
2141  EXPECT_EQ(kValue, absl::get<int>(v));
2142 }
2143 
2144 // Test that a variant with a non-copyable type can be transformed to
2145 // the non-copyable type with a call to `emplace` for different numbers
2146 // of arguments. We do not need to test this for each of T1 ... T8
2147 // because `emplace` does not overload on T1 ... to T8, so if this
2148 // works for any one of T1 ... T8, then it works for all of them. We
2149 // do need to test that it works with varying numbers of parameters
2150 // though.
2151 TEST(VariantTest, TestEmplace) {
2152  typedef variant<int, NonCopyable> Variant;
2153  const int kValue = 1;
2154  Variant v(kValue);
2155  ASSERT_TRUE(absl::holds_alternative<int>(v));
2156  EXPECT_EQ(kValue, absl::get<int>(v));
2157 
2158  // emplace with zero arguments, then back to 'int'
2159  v.emplace<NonCopyable>();
2160  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2161  EXPECT_EQ(0, absl::get<NonCopyable>(v).value);
2162  v = kValue;
2163  ASSERT_TRUE(absl::holds_alternative<int>(v));
2164 
2165  // emplace with one argument:
2166  v.emplace<NonCopyable>(1);
2167  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2168  EXPECT_EQ(1, absl::get<NonCopyable>(v).value);
2169  v = kValue;
2170  ASSERT_TRUE(absl::holds_alternative<int>(v));
2171 
2172  // emplace with two arguments:
2173  v.emplace<NonCopyable>(1, 2);
2174  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2175  EXPECT_EQ(3, absl::get<NonCopyable>(v).value);
2176  v = kValue;
2177  ASSERT_TRUE(absl::holds_alternative<int>(v));
2178 
2179  // emplace with three arguments
2180  v.emplace<NonCopyable>(1, 2, 3);
2181  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2182  EXPECT_EQ(6, absl::get<NonCopyable>(v).value);
2183  v = kValue;
2184  ASSERT_TRUE(absl::holds_alternative<int>(v));
2185 
2186  // emplace with four arguments
2187  v.emplace<NonCopyable>(1, 2, 3, 4);
2188  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2189  EXPECT_EQ(10, absl::get<NonCopyable>(v).value);
2190  v = kValue;
2191  ASSERT_TRUE(absl::holds_alternative<int>(v));
2192 }
2193 
2194 TEST(VariantTest, TestEmplaceDestroysCurrentValue) {
2195  typedef variant<int, IncrementInDtor, NonCopyable> Variant;
2196  int counter = 0;
2197  Variant v(0);
2198  ASSERT_TRUE(absl::holds_alternative<int>(v));
2199  v.emplace<IncrementInDtor>(&counter);
2200  ASSERT_TRUE(absl::holds_alternative<IncrementInDtor>(v));
2201  ASSERT_EQ(0, counter);
2202  v.emplace<NonCopyable>();
2203  ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v));
2204  EXPECT_EQ(1, counter);
2205 }
2206 
2207 TEST(VariantTest, TestMoveSemantics) {
2208  typedef variant<std::unique_ptr<int>, std::unique_ptr<std::string>> Variant;
2209 
2210  // Construct a variant by moving from an element value.
2211  Variant v(absl::WrapUnique(new int(10)));
2212  EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v));
2213 
2214  // Construct a variant by moving from another variant.
2215  Variant v2(absl::move(v));
2216  ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v2));
2217  ASSERT_NE(nullptr, absl::get<std::unique_ptr<int>>(v2));
2218  EXPECT_EQ(10, *absl::get<std::unique_ptr<int>>(v2));
2219 
2220  // Moving from a variant object leaves it holding moved-from value of the
2221  // same element type.
2222  EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<int>>(v));
2223  ASSERT_NE(nullptr, absl::get_if<std::unique_ptr<int>>(&v));
2224  EXPECT_EQ(nullptr, absl::get<std::unique_ptr<int>>(v));
2225 
2226  // Assign a variant from an element value by move.
2227  v = absl::make_unique<std::string>("foo");
2228  ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v));
2229  EXPECT_EQ("foo", *absl::get<std::unique_ptr<std::string>>(v));
2230 
2231  // Move-assign a variant.
2232  v2 = absl::move(v);
2233  ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v2));
2234  EXPECT_EQ("foo", *absl::get<std::unique_ptr<std::string>>(v2));
2235  EXPECT_TRUE(absl::holds_alternative<std::unique_ptr<std::string>>(v));
2236 }
2237 
2238 variant<int, std::string> PassThrough(const variant<int, std::string>& arg) {
2239  return arg;
2240 }
2241 
2242 TEST(VariantTest, TestImplicitConversion) {
2243  EXPECT_TRUE(absl::holds_alternative<int>(PassThrough(0)));
2244 
2245  // We still need the explicit cast for std::string, because C++ won't apply
2246  // two user-defined implicit conversions in a row.
2247  EXPECT_TRUE(
2248  absl::holds_alternative<std::string>(PassThrough(std::string("foo"))));
2249 }
2250 
2251 struct Convertible2;
2252 struct Convertible1 {
2253  Convertible1() {}
2254  Convertible1(const Convertible1&) {}
2255  Convertible1& operator=(const Convertible1&) { return *this; }
2256 
2257  // implicit conversion from Convertible2
2258  Convertible1(const Convertible2&) {} // NOLINT(runtime/explicit)
2259 };
2260 
2261 struct Convertible2 {
2262  Convertible2() {}
2263  Convertible2(const Convertible2&) {}
2264  Convertible2& operator=(const Convertible2&) { return *this; }
2265 
2266  // implicit conversion from Convertible1
2267  Convertible2(const Convertible1&) {} // NOLINT(runtime/explicit)
2268 };
2269 
2270 TEST(VariantTest, TestRvalueConversion) {
2271  variant<double, std::string> var(
2272  ConvertVariantTo<variant<double, std::string>>(
2273  variant<std::string, int>(0)));
2274  ASSERT_TRUE(absl::holds_alternative<double>(var));
2275  EXPECT_EQ(0.0, absl::get<double>(var));
2276 
2277  var = ConvertVariantTo<variant<double, std::string>>(
2278  variant<const char*, float>("foo"));
2279  ASSERT_TRUE(absl::holds_alternative<std::string>(var));
2280  EXPECT_EQ("foo", absl::get<std::string>(var));
2281 
2282  variant<double> singleton(
2283  ConvertVariantTo<variant<double>>(variant<int, float>(42)));
2284  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2285  EXPECT_EQ(42.0, absl::get<double>(singleton));
2286 
2287  singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
2288  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2289  EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2290 
2291  singleton = ConvertVariantTo<variant<double>>(variant<int>(0));
2292  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2293  EXPECT_EQ(0.0, absl::get<double>(singleton));
2294 
2295  variant<int32_t, uint32_t> variant2(
2296  ConvertVariantTo<variant<int32_t, uint32_t>>(variant<int32_t>(42)));
2297  ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
2298  EXPECT_EQ(42, absl::get<int32_t>(variant2));
2299 
2300  variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
2301  ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
2302  EXPECT_EQ(42, absl::get<uint32_t>(variant2));
2303 
2304  variant<Convertible1, Convertible2> variant3(
2305  ConvertVariantTo<variant<Convertible1, Convertible2>>(
2306  (variant<Convertible2, Convertible1>(Convertible1()))));
2307  ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2308 
2309  variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
2310  variant<Convertible2, Convertible1>(Convertible2()));
2311  ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2312 }
2313 
2314 TEST(VariantTest, TestLvalueConversion) {
2315  variant<std::string, int> source1 = 0;
2316  variant<double, std::string> destination(
2317  ConvertVariantTo<variant<double, std::string>>(source1));
2318  ASSERT_TRUE(absl::holds_alternative<double>(destination));
2319  EXPECT_EQ(0.0, absl::get<double>(destination));
2320 
2321  variant<const char*, float> source2 = "foo";
2322  destination = ConvertVariantTo<variant<double, std::string>>(source2);
2323  ASSERT_TRUE(absl::holds_alternative<std::string>(destination));
2324  EXPECT_EQ("foo", absl::get<std::string>(destination));
2325 
2326  variant<int, float> source3(42);
2327  variant<double> singleton(ConvertVariantTo<variant<double>>(source3));
2328  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2329  EXPECT_EQ(42.0, absl::get<double>(singleton));
2330 
2331  source3 = 3.14f;
2332  singleton = ConvertVariantTo<variant<double>>(source3);
2333  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2334  EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2335 
2336  variant<int> source4(0);
2337  singleton = ConvertVariantTo<variant<double>>(source4);
2338  ASSERT_TRUE(absl::holds_alternative<double>(singleton));
2339  EXPECT_EQ(0.0, absl::get<double>(singleton));
2340 
2341  variant<int32_t> source5(42);
2342  variant<int32_t, uint32_t> variant2(
2343  ConvertVariantTo<variant<int32_t, uint32_t>>(source5));
2344  ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
2345  EXPECT_EQ(42, absl::get<int32_t>(variant2));
2346 
2347  variant<uint32_t> source6(42);
2348  variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
2349  ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
2350  EXPECT_EQ(42, absl::get<uint32_t>(variant2));
2351 
2352  variant<Convertible2, Convertible1> source7((Convertible1()));
2353  variant<Convertible1, Convertible2> variant3(
2354  ConvertVariantTo<variant<Convertible1, Convertible2>>(source7));
2355  ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2356 
2357  source7 = Convertible2();
2358  variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
2359  ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2360 }
2361 
2362 TEST(VariantTest, TestMoveConversion) {
2363  using Variant =
2364  variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
2365  using OtherVariant =
2366  variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
2367 
2368  Variant var(
2369  ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(0)}));
2370  ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<const int>>(var));
2371  ASSERT_NE(absl::get<std::unique_ptr<const int>>(var), nullptr);
2372  EXPECT_EQ(0, *absl::get<std::unique_ptr<const int>>(var));
2373 
2374  var = ConvertVariantTo<Variant>(
2375  OtherVariant(absl::make_unique<std::string>("foo")));
2376  ASSERT_TRUE(absl::holds_alternative<std::unique_ptr<const std::string>>(var));
2377  EXPECT_EQ("foo", *absl::get<std::unique_ptr<const std::string>>(var));
2378 }
2379 
2380 TEST(VariantTest, DoesNotMoveFromLvalues) {
2381  // We use shared_ptr here because it's both copyable and movable, and
2382  // a moved-from shared_ptr is guaranteed to be null, so we can detect
2383  // whether moving or copying has occurred.
2384  using Variant =
2385  variant<std::shared_ptr<const int>, std::shared_ptr<const std::string>>;
2386  using OtherVariant =
2387  variant<std::shared_ptr<int>, std::shared_ptr<std::string>>;
2388 
2389  Variant v1(std::make_shared<const int>(0));
2390 
2391  // Test copy constructor
2392  Variant v2(v1);
2393  EXPECT_EQ(absl::get<std::shared_ptr<const int>>(v1),
2394  absl::get<std::shared_ptr<const int>>(v2));
2395 
2396  // Test copy-assignment operator
2397  v1 = std::make_shared<const std::string>("foo");
2398  v2 = v1;
2399  EXPECT_EQ(absl::get<std::shared_ptr<const std::string>>(v1),
2400  absl::get<std::shared_ptr<const std::string>>(v2));
2401 
2402  // Test converting copy constructor
2403  OtherVariant other(std::make_shared<int>(0));
2404  Variant v3(ConvertVariantTo<Variant>(other));
2405  EXPECT_EQ(absl::get<std::shared_ptr<int>>(other),
2406  absl::get<std::shared_ptr<const int>>(v3));
2407 
2408  other = std::make_shared<std::string>("foo");
2409  v3 = ConvertVariantTo<Variant>(other);
2410  EXPECT_EQ(absl::get<std::shared_ptr<std::string>>(other),
2411  absl::get<std::shared_ptr<const std::string>>(v3));
2412 }
2413 
2414 TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) {
2415  variant<double, std::string> var(
2416  ConvertVariantTo<variant<double, std::string>>(
2417  variant<std::string, int>(3)));
2418  EXPECT_THAT(absl::get_if<double>(&var), Pointee(3.0));
2419 
2420  var = ConvertVariantTo<variant<double, std::string>>(
2421  variant<const char*, float>("foo"));
2422  EXPECT_THAT(absl::get_if<std::string>(&var), Pointee(std::string("foo")));
2423 
2424  variant<double> singleton(
2425  ConvertVariantTo<variant<double>>(variant<int, float>(42)));
2426  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
2427 
2428  singleton = ConvertVariantTo<variant<double>>(variant<int, float>(3.14f));
2429  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
2430 
2431  singleton = ConvertVariantTo<variant<double>>(variant<int>(3));
2432  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
2433 
2434  variant<int32_t, uint32_t> variant2(
2435  ConvertVariantTo<variant<int32_t, uint32_t>>(variant<int32_t>(42)));
2436  EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
2437 
2438  variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
2439  EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
2440 
2441  variant<Convertible1, Convertible2> variant3(
2442  ConvertVariantTo<variant<Convertible1, Convertible2>>(
2443  (variant<Convertible2, Convertible1>(Convertible1()))));
2444  ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2445 
2446  variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(
2447  variant<Convertible2, Convertible1>(Convertible2()));
2448  ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2449 }
2450 
2451 TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) {
2452  variant<std::string, int> source1 = 3;
2453  variant<double, std::string> destination(
2454  ConvertVariantTo<variant<double, std::string>>(source1));
2455  EXPECT_THAT(absl::get_if<double>(&destination), Pointee(3.0));
2456 
2457  variant<const char*, float> source2 = "foo";
2458  destination = ConvertVariantTo<variant<double, std::string>>(source2);
2459  EXPECT_THAT(absl::get_if<std::string>(&destination),
2460  Pointee(std::string("foo")));
2461 
2462  variant<int, float> source3(42);
2463  variant<double> singleton(ConvertVariantTo<variant<double>>(source3));
2464  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(42.0));
2465 
2466  source3 = 3.14f;
2467  singleton = ConvertVariantTo<variant<double>>(source3);
2468  EXPECT_FLOAT_EQ(3.14f, static_cast<float>(absl::get<double>(singleton)));
2469  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(DoubleEq(3.14f)));
2470 
2471  variant<int> source4(3);
2472  singleton = ConvertVariantTo<variant<double>>(source4);
2473  EXPECT_THAT(absl::get_if<double>(&singleton), Pointee(3.0));
2474 
2475  variant<int32_t> source5(42);
2476  variant<int32_t, uint32_t> variant2(
2477  ConvertVariantTo<variant<int32_t, uint32_t>>(source5));
2478  EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
2479 
2480  variant<uint32_t> source6(42);
2481  variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
2482  EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
2483 
2484  variant<Convertible2, Convertible1> source7((Convertible1()));
2485  variant<Convertible1, Convertible2> variant3(
2486  ConvertVariantTo<variant<Convertible1, Convertible2>>(source7));
2487  ASSERT_TRUE(absl::holds_alternative<Convertible1>(variant3));
2488 
2489  source7 = Convertible2();
2490  variant3 = ConvertVariantTo<variant<Convertible1, Convertible2>>(source7);
2491  ASSERT_TRUE(absl::holds_alternative<Convertible2>(variant3));
2492 }
2493 
2494 TEST(VariantTest, TestMoveConversionViaConvertVariantTo) {
2495  using Variant =
2496  variant<std::unique_ptr<const int>, std::unique_ptr<const std::string>>;
2497  using OtherVariant =
2498  variant<std::unique_ptr<int>, std::unique_ptr<std::string>>;
2499 
2500  Variant var(
2501  ConvertVariantTo<Variant>(OtherVariant{absl::make_unique<int>(3)}));
2502  EXPECT_THAT(absl::get_if<std::unique_ptr<const int>>(&var),
2503  Pointee(Pointee(3)));
2504 
2505  var = ConvertVariantTo<Variant>(
2506  OtherVariant(absl::make_unique<std::string>("foo")));
2507  EXPECT_THAT(absl::get_if<std::unique_ptr<const std::string>>(&var),
2508  Pointee(Pointee(std::string("foo"))));
2509 }
2510 
2511 // If all alternatives are trivially copy/move constructible, variant should
2512 // also be trivially copy/move constructible. This is not required by the
2513 // standard and we know that libstdc++ variant doesn't have this feature.
2514 // For more details see the paper:
2515 // http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0602r0.html
2516 #if !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
2517 #define ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY 1
2518 #endif
2519 
2520 TEST(VariantTest, TestCopyAndMoveTypeTraits) {
2521  EXPECT_TRUE(std::is_copy_constructible<variant<std::string>>::value);
2522  EXPECT_TRUE(absl::is_copy_assignable<variant<std::string>>::value);
2523  EXPECT_TRUE(std::is_move_constructible<variant<std::string>>::value);
2524  EXPECT_TRUE(absl::is_move_assignable<variant<std::string>>::value);
2525  EXPECT_TRUE(std::is_move_constructible<variant<std::unique_ptr<int>>>::value);
2526  EXPECT_TRUE(absl::is_move_assignable<variant<std::unique_ptr<int>>>::value);
2527  EXPECT_FALSE(
2528  std::is_copy_constructible<variant<std::unique_ptr<int>>>::value);
2529  EXPECT_FALSE(absl::is_copy_assignable<variant<std::unique_ptr<int>>>::value);
2530 
2531  EXPECT_FALSE(
2532  absl::is_trivially_copy_constructible<variant<std::string>>::value);
2533  EXPECT_FALSE(absl::is_trivially_copy_assignable<variant<std::string>>::value);
2534 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2535  EXPECT_TRUE(absl::is_trivially_copy_constructible<variant<int>>::value);
2536  EXPECT_TRUE(absl::is_trivially_copy_assignable<variant<int>>::value);
2537  EXPECT_TRUE(is_trivially_move_constructible<variant<int>>::value);
2538  EXPECT_TRUE(is_trivially_move_assignable<variant<int>>::value);
2539 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2540 }
2541 
2542 TEST(VariantTest, TestVectorOfMoveonlyVariant) {
2543  // Verify that variant<MoveonlyType> works correctly as a std::vector element.
2544  std::vector<variant<std::unique_ptr<int>, std::string>> vec;
2545  vec.push_back(absl::make_unique<int>(42));
2546  vec.emplace_back("Hello");
2547  vec.reserve(3);
2548  auto another_vec = absl::move(vec);
2549  // As a sanity check, verify vector contents.
2550  ASSERT_EQ(2, another_vec.size());
2551  EXPECT_EQ(42, *absl::get<std::unique_ptr<int>>(another_vec[0]));
2552  EXPECT_EQ("Hello", absl::get<std::string>(another_vec[1]));
2553 }
2554 
2555 TEST(VariantTest, NestedVariant) {
2556 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2557  static_assert(absl::is_trivially_copy_constructible<variant<int>>(), "");
2558  static_assert(absl::is_trivially_copy_assignable<variant<int>>(), "");
2559  static_assert(is_trivially_move_constructible<variant<int>>(), "");
2560  static_assert(is_trivially_move_assignable<variant<int>>(), "");
2561 
2562  static_assert(absl::is_trivially_copy_constructible<variant<variant<int>>>(),
2563  "");
2564  static_assert(absl::is_trivially_copy_assignable<variant<variant<int>>>(),
2565  "");
2566  static_assert(is_trivially_move_constructible<variant<variant<int>>>(), "");
2567  static_assert(is_trivially_move_assignable<variant<variant<int>>>(), "");
2568 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2569 
2570  variant<int> x(42);
2571  variant<variant<int>> y(x);
2572  variant<variant<int>> z(y);
2573  EXPECT_TRUE(absl::holds_alternative<variant<int>>(z));
2574  EXPECT_EQ(x, absl::get<variant<int>>(z));
2575 }
2576 
2577 struct TriviallyDestructible {
2578  TriviallyDestructible(TriviallyDestructible&&) {}
2579  TriviallyDestructible(const TriviallyDestructible&) {}
2580  TriviallyDestructible& operator=(TriviallyDestructible&&) { return *this; }
2581  TriviallyDestructible& operator=(const TriviallyDestructible&) {
2582  return *this;
2583  }
2584 };
2585 
2586 struct TriviallyMovable {
2587  TriviallyMovable(TriviallyMovable&&) = default;
2588  TriviallyMovable(TriviallyMovable const&) {}
2589  TriviallyMovable& operator=(const TriviallyMovable&) { return *this; }
2590 };
2591 
2592 struct TriviallyCopyable {
2593  TriviallyCopyable(const TriviallyCopyable&) = default;
2594  TriviallyCopyable& operator=(const TriviallyCopyable&) { return *this; }
2595 };
2596 
2597 struct TriviallyMoveAssignable {
2598  TriviallyMoveAssignable(TriviallyMoveAssignable&&) = default;
2599  TriviallyMoveAssignable(const TriviallyMoveAssignable&) {}
2600  TriviallyMoveAssignable& operator=(TriviallyMoveAssignable&&) = default;
2601  TriviallyMoveAssignable& operator=(const TriviallyMoveAssignable&) {
2602  return *this;
2603  }
2604 };
2605 
2606 struct TriviallyCopyAssignable {};
2607 
2608 #if ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2609 TEST(VariantTest, TestTriviality) {
2610  {
2611  using TrivDestVar = absl::variant<TriviallyDestructible>;
2612 
2618  }
2619 
2620  {
2621  using TrivMoveVar = absl::variant<TriviallyMovable>;
2622 
2628  }
2629 
2630  {
2631  using TrivCopyVar = absl::variant<TriviallyCopyable>;
2632 
2638  }
2639 
2640  {
2641  using TrivMoveAssignVar = absl::variant<TriviallyMoveAssignable>;
2642 
2644  EXPECT_FALSE(
2649  }
2650 
2651  {
2652  using TrivCopyAssignVar = absl::variant<TriviallyCopyAssignable>;
2653 
2655  EXPECT_TRUE(
2660  }
2661 }
2662 #endif // ABSL_VARIANT_PROPAGATE_COPY_MOVE_TRIVIALITY
2663 
2664 // To verify that absl::variant correctly use the nontrivial move ctor of its
2665 // member rather than use the trivial copy constructor.
2666 TEST(VariantTest, MoveCtorBug) {
2667  // To simulate std::tuple in libstdc++.
2668  struct TrivialCopyNontrivialMove {
2669  TrivialCopyNontrivialMove() = default;
2670  TrivialCopyNontrivialMove(const TrivialCopyNontrivialMove&) = default;
2671  TrivialCopyNontrivialMove(TrivialCopyNontrivialMove&&) { called = true; }
2672  bool called = false;
2673  };
2674  {
2676  V v1(absl::in_place_index<0>);
2677  // this should invoke the move ctor, rather than the trivial copy ctor.
2678  V v2(std::move(v1));
2679  EXPECT_TRUE(absl::get<0>(v2).called);
2680  }
2681  {
2682  // this case failed to compile before our fix due to a GCC bug.
2684  V v1(absl::in_place_index<1>);
2685  // this should invoke the move ctor, rather than the trivial copy ctor.
2686  V v2(std::move(v1));
2687  EXPECT_TRUE(absl::get<1>(v2).called);
2688  }
2689 }
2690 
2691 } // namespace
2692 } // namespace absl
int v
Definition: variant_test.cc:81
typename variant_alternative< I, T >::type variant_alternative_t
Definition: variant.h:247
int i
constexpr absl::add_pointer_t< variant_alternative_t< I, variant< Types... > > > get_if(variant< Types... > *v) noexcept
Definition: variant.h:369
char * begin
bool operator<(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: memory.h:71
bool special_swap
static size_t StrLen(const char *str)
Definition: demangle.cc:214
std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
Definition: log_severity.cc:21
To ConvertVariantTo(Variant &&variant)
Definition: variant.h:848
int fill
void in_place_type(utility_internal::InPlaceTypeTag< T >)
Definition: utility.h:192
#define X(c)
int * counter
char * end
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
Definition: utility.h:189
Definition: algorithm.h:29
T member
constexpr size_type size() const noexcept
Definition: string_view.h:260
bool operator>(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
constexpr T & get(variant< Types... > &v)
Definition: variant.h:297
hash_default_hash< T > hasher
static const size_t kIndex
#define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...)
Definition: variant_test.cc:54
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator>=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
hash_default_hash< typename T::first_type > hash
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
void * arg
Definition: mutex.cc:292
variant_internal::VisitResult< Visitor, Variants... > visit(Visitor &&vis, Variants &&...vars)
Definition: variant.h:427
int value
TEST(Symbolize, Unimplemented)
uint64_t b
Definition: layout_test.cc:50
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
int foo
constexpr bool holds_alternative(const variant< Types... > &v) noexcept
Definition: variant.h:261
bool operator<=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)


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