forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
variant_test.cc
2718 lines (2305 loc) · 90.8 KB
/
variant_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Unit tests for the variant template. The 'is' and 'IsEmpty' methods
// of variant are not explicitly tested because they are used repeatedly
// in building other tests. All other public variant methods should have
// explicit tests.
#include "absl/types/variant.h"
// This test is a no-op when absl::variant is an alias for std::variant.
#if !defined(ABSL_USES_STD_VARIANT)
#include <algorithm>
#include <cstddef>
#include <functional>
#include <initializer_list>
#include <memory>
#include <ostream>
#include <queue>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/base/port.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#ifdef ABSL_HAVE_EXCEPTIONS
#define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
EXPECT_THROW(expr, exception_t)
#else
#define ABSL_VARIANT_TEST_EXPECT_FAIL(expr, exception_t, text) \
EXPECT_DEATH_IF_SUPPORTED(expr, text)
#endif // ABSL_HAVE_EXCEPTIONS
#define ABSL_VARIANT_TEST_EXPECT_BAD_VARIANT_ACCESS(...) \
ABSL_VARIANT_TEST_EXPECT_FAIL((void)(__VA_ARGS__), absl::bad_variant_access, \
"Bad variant access")
struct Hashable {};
namespace std {
template <>
struct hash<Hashable> {
size_t operator()(const Hashable&);
};
} // namespace std
struct NonHashable {};
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace {
using ::testing::DoubleEq;
using ::testing::Pointee;
using ::testing::VariantWith;
struct MoveCanThrow {
MoveCanThrow() : v(0) {}
MoveCanThrow(int v) : v(v) {} // NOLINT(runtime/explicit)
MoveCanThrow(const MoveCanThrow& other) : v(other.v) {}
MoveCanThrow& operator=(const MoveCanThrow& /*other*/) { return *this; }
int v;
};
bool operator==(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v == rhs.v; }
bool operator!=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v != rhs.v; }
bool operator<(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v < rhs.v; }
bool operator<=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v <= rhs.v; }
bool operator>=(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v >= rhs.v; }
bool operator>(MoveCanThrow lhs, MoveCanThrow rhs) { return lhs.v > rhs.v; }
// This helper class allows us to determine if it was swapped with std::swap()
// or with its friend swap() function.
struct SpecialSwap {
explicit SpecialSwap(int i) : i(i) {}
friend void swap(SpecialSwap& a, SpecialSwap& b) {
a.special_swap = b.special_swap = true;
std::swap(a.i, b.i);
}
bool operator==(SpecialSwap other) const { return i == other.i; }
int i;
bool special_swap = false;
};
struct MoveOnlyWithListConstructor {
MoveOnlyWithListConstructor() = default;
explicit MoveOnlyWithListConstructor(std::initializer_list<int> /*ilist*/,
int value)
: value(value) {}
MoveOnlyWithListConstructor(MoveOnlyWithListConstructor&&) = default;
MoveOnlyWithListConstructor& operator=(MoveOnlyWithListConstructor&&) =
default;
int value = 0;
};
#ifdef ABSL_HAVE_EXCEPTIONS
struct ConversionException {};
template <class T>
struct ExceptionOnConversion {
operator T() const { // NOLINT(runtime/explicit)
throw ConversionException();
}
};
// Forces a variant into the valueless by exception state.
template <class H, class... T>
void ToValuelessByException(absl::variant<H, T...>& v) { // NOLINT
try {
v.template emplace<0>(ExceptionOnConversion<H>());
} catch (ConversionException& /*e*/) {
// This space intentionally left blank.
}
}
#endif // ABSL_HAVE_EXCEPTIONS
// An indexed sequence of distinct structures holding a single
// value of type T
template<typename T, size_t N>
struct ValueHolder {
explicit ValueHolder(const T& x) : value(x) {}
typedef T value_type;
value_type value;
static const size_t kIndex = N;
};
template<typename T, size_t N>
const size_t ValueHolder<T, N>::kIndex;
// The following three functions make ValueHolder compatible with
// EXPECT_EQ and EXPECT_NE
template<typename T, size_t N>
inline bool operator==(const ValueHolder<T, N>& left,
const ValueHolder<T, N>& right) {
return left.value == right.value;
}
template<typename T, size_t N>
inline bool operator!=(const ValueHolder<T, N>& left,
const ValueHolder<T, N>& right) {
return left.value != right.value;
}
template<typename T, size_t N>
inline std::ostream& operator<<(
std::ostream& stream, const ValueHolder<T, N>& object) {
return stream << object.value;
}
// Makes a variant holding twelve uniquely typed T wrappers.
template<typename T>
struct VariantFactory {
typedef variant<ValueHolder<T, 1>, ValueHolder<T, 2>, ValueHolder<T, 3>,
ValueHolder<T, 4>>
Type;
};
// A typelist in 1:1 with VariantFactory, to use type driven unit tests.
typedef ::testing::Types<ValueHolder<size_t, 1>, ValueHolder<size_t, 2>,
ValueHolder<size_t, 3>,
ValueHolder<size_t, 4>> VariantTypes;
// Increments the provided counter pointer in the destructor
struct IncrementInDtor {
explicit IncrementInDtor(int* counter) : counter(counter) {}
~IncrementInDtor() { *counter += 1; }
int* counter;
};
struct IncrementInDtorCopyCanThrow {
explicit IncrementInDtorCopyCanThrow(int* counter) : counter(counter) {}
IncrementInDtorCopyCanThrow(IncrementInDtorCopyCanThrow&& other) noexcept =
default;
IncrementInDtorCopyCanThrow(const IncrementInDtorCopyCanThrow& other)
: counter(other.counter) {}
IncrementInDtorCopyCanThrow& operator=(
IncrementInDtorCopyCanThrow&&) noexcept = default;
IncrementInDtorCopyCanThrow& operator=(
IncrementInDtorCopyCanThrow const& other) {
counter = other.counter;
return *this;
}
~IncrementInDtorCopyCanThrow() { *counter += 1; }
int* counter;
};
// This is defined so operator== for ValueHolder<IncrementInDtor> will
// return true if two IncrementInDtor objects increment the same
// counter
inline bool operator==(const IncrementInDtor& left,
const IncrementInDtor& right) {
return left.counter == right.counter;
}
// This is defined so EXPECT_EQ can work with IncrementInDtor
inline std::ostream& operator<<(
std::ostream& stream, const IncrementInDtor& object) {
return stream << object.counter;
}
// A class that can be copied, but not assigned.
class CopyNoAssign {
public:
explicit CopyNoAssign(int value) : foo(value) {}
CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {}
int foo;
private:
const CopyNoAssign& operator=(const CopyNoAssign&);
};
// A class that can neither be copied nor assigned. We provide
// overloads for the constructor with up to four parameters so we can
// test the overloads of variant::emplace.
class NonCopyable {
public:
NonCopyable()
: value(0) {}
explicit NonCopyable(int value1)
: value(value1) {}
NonCopyable(int value1, int value2)
: value(value1 + value2) {}
NonCopyable(int value1, int value2, int value3)
: value(value1 + value2 + value3) {}
NonCopyable(int value1, int value2, int value3, int value4)
: value(value1 + value2 + value3 + value4) {}
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
int value;
};
// A typed test and typed test case over the VariantTypes typelist,
// from which we derive a number of tests that will execute for one of
// each type.
template <typename T>
class VariantTypesTest : public ::testing::Test {};
TYPED_TEST_SUITE(VariantTypesTest, VariantTypes);
////////////////////
// [variant.ctor] //
////////////////////
struct NonNoexceptDefaultConstructible {
NonNoexceptDefaultConstructible() {}
int value = 5;
};
struct NonDefaultConstructible {
NonDefaultConstructible() = delete;
};
TEST(VariantTest, TestDefaultConstructor) {
{
using X = variant<int>;
constexpr variant<int> x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
EXPECT_EQ(0, absl::get<0>(x));
EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
}
{
using X = variant<NonNoexceptDefaultConstructible>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
EXPECT_EQ(5, absl::get<0>(x).value);
EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
}
{
using X = variant<int, NonNoexceptDefaultConstructible>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
EXPECT_EQ(0, absl::get<0>(x));
EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
}
{
using X = variant<NonNoexceptDefaultConstructible, int>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
EXPECT_EQ(5, absl::get<0>(x).value);
EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
}
EXPECT_FALSE(
std::is_default_constructible<variant<NonDefaultConstructible>>::value);
EXPECT_FALSE((std::is_default_constructible<
variant<NonDefaultConstructible, int>>::value));
EXPECT_TRUE((std::is_default_constructible<
variant<int, NonDefaultConstructible>>::value));
}
// Test that for each slot, copy constructing a variant with that type
// produces a sensible object that correctly reports its type, and
// that copies the provided value.
TYPED_TEST(VariantTypesTest, TestCopyCtor) {
typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
using value_type2 = absl::variant_alternative_t<1, Variant>;
using value_type3 = absl::variant_alternative_t<2, Variant>;
using value_type4 = absl::variant_alternative_t<3, Variant>;
const TypeParam value(TypeParam::kIndex);
Variant original(value);
Variant copied(original);
EXPECT_TRUE(absl::holds_alternative<value_type1>(copied) ||
TypeParam::kIndex != 1);
EXPECT_TRUE(absl::holds_alternative<value_type2>(copied) ||
TypeParam::kIndex != 2);
EXPECT_TRUE(absl::holds_alternative<value_type3>(copied) ||
TypeParam::kIndex != 3);
EXPECT_TRUE(absl::holds_alternative<value_type4>(copied) ||
TypeParam::kIndex != 4);
EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
absl::get_if<value_type1>(&copied)) ||
TypeParam::kIndex == 1);
EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
absl::get_if<value_type2>(&copied)) ||
TypeParam::kIndex == 2);
EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
absl::get_if<value_type3>(&copied)) ||
TypeParam::kIndex == 3);
EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
absl::get_if<value_type4>(&copied)) ||
TypeParam::kIndex == 4);
EXPECT_TRUE((absl::get_if<value_type1>(&original) ==
absl::get_if<value_type1>(&copied)) ||
TypeParam::kIndex == 1);
EXPECT_TRUE((absl::get_if<value_type2>(&original) ==
absl::get_if<value_type2>(&copied)) ||
TypeParam::kIndex == 2);
EXPECT_TRUE((absl::get_if<value_type3>(&original) ==
absl::get_if<value_type3>(&copied)) ||
TypeParam::kIndex == 3);
EXPECT_TRUE((absl::get_if<value_type4>(&original) ==
absl::get_if<value_type4>(&copied)) ||
TypeParam::kIndex == 4);
const TypeParam* ovalptr = absl::get_if<TypeParam>(&original);
const TypeParam* cvalptr = absl::get_if<TypeParam>(&copied);
ASSERT_TRUE(ovalptr != nullptr);
ASSERT_TRUE(cvalptr != nullptr);
EXPECT_EQ(*ovalptr, *cvalptr);
TypeParam* mutable_ovalptr = absl::get_if<TypeParam>(&original);
TypeParam* mutable_cvalptr = absl::get_if<TypeParam>(&copied);
ASSERT_TRUE(mutable_ovalptr != nullptr);
ASSERT_TRUE(mutable_cvalptr != nullptr);
EXPECT_EQ(*mutable_ovalptr, *mutable_cvalptr);
}
template <class>
struct MoveOnly {
MoveOnly() = default;
explicit MoveOnly(int value) : value(value) {}
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
int value = 5;
};
TEST(VariantTest, TestMoveConstruct) {
using V = variant<MoveOnly<class A>, MoveOnly<class B>, MoveOnly<class C>>;
V v(in_place_index<1>, 10);
V v2 = absl::move(v);
EXPECT_EQ(10, absl::get<1>(v2).value);
}
// Used internally to emulate missing triviality traits for tests.
template <class T>
union SingleUnion {
T member;
};
// NOTE: These don't work with types that can't be union members.
// They are just for testing.
template <class T>
struct is_trivially_move_constructible
: std::is_move_constructible<SingleUnion<T>>::type {};
template <class T>
struct is_trivially_move_assignable
: absl::is_move_assignable<SingleUnion<T>>::type {};
TEST(VariantTest, NothrowMoveConstructible) {
// Verify that variant is nothrow move constructible iff its template
// arguments are.
using U = std::unique_ptr<int>;
struct E {
E(E&&) {}
};
static_assert(std::is_nothrow_move_constructible<variant<U>>::value, "");
static_assert(std::is_nothrow_move_constructible<variant<U, int>>::value, "");
static_assert(!std::is_nothrow_move_constructible<variant<U, E>>::value, "");
}
// Test that for each slot, constructing a variant with that type
// produces a sensible object that correctly reports its type, and
// that copies the provided value.
TYPED_TEST(VariantTypesTest, TestValueCtor) {
typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
using value_type2 = absl::variant_alternative_t<1, Variant>;
using value_type3 = absl::variant_alternative_t<2, Variant>;
using value_type4 = absl::variant_alternative_t<3, Variant>;
const TypeParam value(TypeParam::kIndex);
Variant v(value);
EXPECT_TRUE(absl::holds_alternative<value_type1>(v) ||
TypeParam::kIndex != 1);
EXPECT_TRUE(absl::holds_alternative<value_type2>(v) ||
TypeParam::kIndex != 2);
EXPECT_TRUE(absl::holds_alternative<value_type3>(v) ||
TypeParam::kIndex != 3);
EXPECT_TRUE(absl::holds_alternative<value_type4>(v) ||
TypeParam::kIndex != 4);
EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
TypeParam::kIndex != 1);
EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
TypeParam::kIndex != 2);
EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
TypeParam::kIndex != 3);
EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
TypeParam::kIndex != 4);
EXPECT_TRUE(nullptr != absl::get_if<value_type1>(&v) ||
TypeParam::kIndex != 1);
EXPECT_TRUE(nullptr != absl::get_if<value_type2>(&v) ||
TypeParam::kIndex != 2);
EXPECT_TRUE(nullptr != absl::get_if<value_type3>(&v) ||
TypeParam::kIndex != 3);
EXPECT_TRUE(nullptr != absl::get_if<value_type4>(&v) ||
TypeParam::kIndex != 4);
const TypeParam* valptr = absl::get_if<TypeParam>(&v);
ASSERT_TRUE(nullptr != valptr);
EXPECT_EQ(value.value, valptr->value);
const TypeParam* mutable_valptr = absl::get_if<TypeParam>(&v);
ASSERT_TRUE(nullptr != mutable_valptr);
EXPECT_EQ(value.value, mutable_valptr->value);
}
TEST(VariantTest, AmbiguousValueConstructor) {
EXPECT_FALSE((std::is_convertible<int, absl::variant<int, int>>::value));
EXPECT_FALSE((std::is_constructible<absl::variant<int, int>, int>::value));
}
TEST(VariantTest, InPlaceType) {
using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
Var v1(in_place_type_t<int>(), 7);
ASSERT_TRUE(absl::holds_alternative<int>(v1));
EXPECT_EQ(7, absl::get<int>(v1));
Var v2(in_place_type_t<std::string>(), "ABC");
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_type_t<std::string>(), "ABC", 2);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
Var v4(in_place_type_t<NonCopyable>{});
ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
Var v5(in_place_type_t<std::vector<int>>(), {1, 2, 3});
ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
}
TEST(VariantTest, InPlaceTypeVariableTemplate) {
using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
Var v1(in_place_type<int>, 7);
ASSERT_TRUE(absl::holds_alternative<int>(v1));
EXPECT_EQ(7, absl::get<int>(v1));
Var v2(in_place_type<std::string>, "ABC");
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_type<std::string>, "ABC", 2);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
Var v4(in_place_type<NonCopyable>);
ASSERT_TRUE(absl::holds_alternative<NonCopyable>(v4));
Var v5(in_place_type<std::vector<int>>, {1, 2, 3});
ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
}
TEST(VariantTest, InPlaceTypeInitializerList) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(in_place_type_t<MoveOnlyWithListConstructor>(), {1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
}
TEST(VariantTest, InPlaceTypeInitializerListVariabletemplate) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(in_place_type<MoveOnlyWithListConstructor>, {1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
}
TEST(VariantTest, InPlaceIndex) {
using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
Var v1(in_place_index_t<0>(), 7);
ASSERT_TRUE(absl::holds_alternative<int>(v1));
EXPECT_EQ(7, absl::get<int>(v1));
Var v2(in_place_index_t<1>(), "ABC");
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_index_t<1>(), "ABC", 2);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
Var v4(in_place_index_t<2>{});
EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
// Verify that a variant with only non-copyables can still be constructed.
EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
variant<NonCopyable>(in_place_index_t<0>{})));
Var v5(in_place_index_t<3>(), {1, 2, 3});
ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
}
TEST(VariantTest, InPlaceIndexVariableTemplate) {
using Var = variant<int, std::string, NonCopyable, std::vector<int>>;
Var v1(in_place_index<0>, 7);
ASSERT_TRUE(absl::holds_alternative<int>(v1));
EXPECT_EQ(7, absl::get<int>(v1));
Var v2(in_place_index<1>, "ABC");
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_index<1>, "ABC", 2);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
Var v4(in_place_index<2>);
EXPECT_TRUE(absl::holds_alternative<NonCopyable>(v4));
// Verify that a variant with only non-copyables can still be constructed.
EXPECT_TRUE(absl::holds_alternative<NonCopyable>(
variant<NonCopyable>(in_place_index<0>)));
Var v5(in_place_index<3>, {1, 2, 3});
ASSERT_TRUE(absl::holds_alternative<std::vector<int>>(v5));
EXPECT_THAT(absl::get<std::vector<int>>(v5), ::testing::ElementsAre(1, 2, 3));
}
TEST(VariantTest, InPlaceIndexInitializerList) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(in_place_index_t<3>(), {1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
}
TEST(VariantTest, InPlaceIndexInitializerListVariableTemplate) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(in_place_index<3>, {1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
}
////////////////////
// [variant.dtor] //
////////////////////
// Make sure that the destructor destroys the contained value
TEST(VariantTest, TestDtor) {
typedef VariantFactory<IncrementInDtor>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
using value_type2 = absl::variant_alternative_t<1, Variant>;
using value_type3 = absl::variant_alternative_t<2, Variant>;
using value_type4 = absl::variant_alternative_t<3, Variant>;
int counter = 0;
IncrementInDtor counter_adjuster(&counter);
EXPECT_EQ(0, counter);
value_type1 value1(counter_adjuster);
{ Variant object(value1); }
EXPECT_EQ(1, counter);
value_type2 value2(counter_adjuster);
{ Variant object(value2); }
EXPECT_EQ(2, counter);
value_type3 value3(counter_adjuster);
{ Variant object(value3); }
EXPECT_EQ(3, counter);
value_type4 value4(counter_adjuster);
{ Variant object(value4); }
EXPECT_EQ(4, counter);
}
#ifdef ABSL_HAVE_EXCEPTIONS
// See comment in absl/base/config.h
#if defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE)
TEST(VariantTest, DISABLED_TestDtorValuelessByException)
#else
// Test destruction when in the valueless_by_exception state.
TEST(VariantTest, TestDtorValuelessByException)
#endif
{
int counter = 0;
IncrementInDtor counter_adjuster(&counter);
{
using Variant = VariantFactory<IncrementInDtor>::Type;
Variant v(in_place_index<0>, counter_adjuster);
EXPECT_EQ(0, counter);
ToValuelessByException(v);
ASSERT_TRUE(v.valueless_by_exception());
EXPECT_EQ(1, counter);
}
EXPECT_EQ(1, counter);
}
#endif // ABSL_HAVE_EXCEPTIONS
//////////////////////
// [variant.assign] //
//////////////////////
// Test that self-assignment doesn't destroy the current value
TEST(VariantTest, TestSelfAssignment) {
typedef VariantFactory<IncrementInDtor>::Type Variant;
int counter = 0;
IncrementInDtor counter_adjuster(&counter);
absl::variant_alternative_t<0, Variant> value(counter_adjuster);
Variant object(value);
object.operator=(object);
EXPECT_EQ(0, counter);
// A string long enough that it's likely to defeat any inline representation
// optimization.
const std::string long_str(128, 'a');
std::string foo = long_str;
foo = *&foo;
EXPECT_EQ(long_str, foo);
variant<int, std::string> so = long_str;
ASSERT_EQ(1, so.index());
EXPECT_EQ(long_str, absl::get<1>(so));
so = *&so;
ASSERT_EQ(1, so.index());
EXPECT_EQ(long_str, absl::get<1>(so));
}
// Test that assigning a variant<..., T, ...> to a variant<..., T, ...> produces
// a variant<..., T, ...> with the correct value.
TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValueSameTypes) {
typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
const TypeParam value(TypeParam::kIndex);
const Variant source(value);
Variant target(TypeParam(value.value + 1));
ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
ASSERT_NE(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
target = source;
ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
}
// Test that assisnging a variant<..., T, ...> to a variant<1, ...>
// produces a variant<..., T, ...> with the correct value.
TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingSourceType) {
typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
const TypeParam value(TypeParam::kIndex);
const Variant source(value);
ASSERT_TRUE(absl::holds_alternative<TypeParam>(source));
Variant target(value_type1(1));
ASSERT_TRUE(absl::holds_alternative<value_type1>(target));
target = source;
EXPECT_TRUE(absl::holds_alternative<TypeParam>(source));
EXPECT_TRUE(absl::holds_alternative<TypeParam>(target));
EXPECT_EQ(absl::get<TypeParam>(source), absl::get<TypeParam>(target));
}
// Test that assigning a variant<1, ...> to a variant<..., T, ...>
// produces a variant<1, ...> with the correct value.
TYPED_TEST(VariantTypesTest, TestAssignmentCopiesValuesVaryingTargetType) {
typedef typename VariantFactory<typename TypeParam::value_type>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
const Variant source(value_type1(1));
ASSERT_TRUE(absl::holds_alternative<value_type1>(source));
const TypeParam value(TypeParam::kIndex);
Variant target(value);
ASSERT_TRUE(absl::holds_alternative<TypeParam>(target));
target = source;
EXPECT_TRUE(absl::holds_alternative<value_type1>(target));
EXPECT_TRUE(absl::holds_alternative<value_type1>(source));
EXPECT_EQ(absl::get<value_type1>(source), absl::get<value_type1>(target));
}
// Test that operator=<T> works, that assigning a new value destroys
// the old and that assigning the new value again does not redestroy
// the old
TEST(VariantTest, TestAssign) {
typedef VariantFactory<IncrementInDtor>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
using value_type2 = absl::variant_alternative_t<1, Variant>;
using value_type3 = absl::variant_alternative_t<2, Variant>;
using value_type4 = absl::variant_alternative_t<3, Variant>;
const int kSize = 4;
int counter[kSize];
std::unique_ptr<IncrementInDtor> counter_adjustor[kSize];
for (int i = 0; i != kSize; i++) {
counter[i] = 0;
counter_adjustor[i] = absl::make_unique<IncrementInDtor>(&counter[i]);
}
value_type1 v1(*counter_adjustor[0]);
value_type2 v2(*counter_adjustor[1]);
value_type3 v3(*counter_adjustor[2]);
value_type4 v4(*counter_adjustor[3]);
// Test that reassignment causes destruction of old value
{
Variant object(v1);
object = v2;
object = v3;
object = v4;
object = v1;
}
EXPECT_EQ(2, counter[0]);
EXPECT_EQ(1, counter[1]);
EXPECT_EQ(1, counter[2]);
EXPECT_EQ(1, counter[3]);
std::fill(std::begin(counter), std::end(counter), 0);
// Test that self-assignment does not cause destruction of old value
{
Variant object(v1);
object.operator=(object);
EXPECT_EQ(0, counter[0]);
}
{
Variant object(v2);
object.operator=(object);
EXPECT_EQ(0, counter[1]);
}
{
Variant object(v3);
object.operator=(object);
EXPECT_EQ(0, counter[2]);
}
{
Variant object(v4);
object.operator=(object);
EXPECT_EQ(0, counter[3]);
}
EXPECT_EQ(1, counter[0]);
EXPECT_EQ(1, counter[1]);
EXPECT_EQ(1, counter[2]);
EXPECT_EQ(1, counter[3]);
}
// This tests that we perform a backup if the copy-assign can throw but the move
// cannot throw.
TEST(VariantTest, TestBackupAssign) {
typedef VariantFactory<IncrementInDtorCopyCanThrow>::Type Variant;
using value_type1 = absl::variant_alternative_t<0, Variant>;
using value_type2 = absl::variant_alternative_t<1, Variant>;
using value_type3 = absl::variant_alternative_t<2, Variant>;
using value_type4 = absl::variant_alternative_t<3, Variant>;
const int kSize = 4;
int counter[kSize];
std::unique_ptr<IncrementInDtorCopyCanThrow> counter_adjustor[kSize];
for (int i = 0; i != kSize; i++) {
counter[i] = 0;
counter_adjustor[i].reset(new IncrementInDtorCopyCanThrow(&counter[i]));
}
value_type1 v1(*counter_adjustor[0]);
value_type2 v2(*counter_adjustor[1]);
value_type3 v3(*counter_adjustor[2]);
value_type4 v4(*counter_adjustor[3]);
// Test that reassignment causes destruction of old value
{
Variant object(v1);
object = v2;
object = v3;
object = v4;
object = v1;
}
// libstdc++ doesn't pass this test
#if !(defined(ABSL_USES_STD_VARIANT) && defined(__GLIBCXX__))
EXPECT_EQ(3, counter[0]);
EXPECT_EQ(2, counter[1]);
EXPECT_EQ(2, counter[2]);
EXPECT_EQ(2, counter[3]);
#endif
std::fill(std::begin(counter), std::end(counter), 0);
// Test that self-assignment does not cause destruction of old value
{
Variant object(v1);
object.operator=(object);
EXPECT_EQ(0, counter[0]);
}
{
Variant object(v2);
object.operator=(object);
EXPECT_EQ(0, counter[1]);
}
{
Variant object(v3);
object.operator=(object);
EXPECT_EQ(0, counter[2]);
}
{
Variant object(v4);
object.operator=(object);
EXPECT_EQ(0, counter[3]);
}
EXPECT_EQ(1, counter[0]);
EXPECT_EQ(1, counter[1]);
EXPECT_EQ(1, counter[2]);
EXPECT_EQ(1, counter[3]);
}
///////////////////
// [variant.mod] //
///////////////////
TEST(VariantTest, TestEmplaceBasic) {
using Variant = variant<int, char>;
Variant v(absl::in_place_index<0>, 0);
{
char& emplace_result = v.emplace<char>();
ASSERT_TRUE(absl::holds_alternative<char>(v));
EXPECT_EQ(absl::get<char>(v), 0);
EXPECT_EQ(&emplace_result, &absl::get<char>(v));
}
// Make sure that another emplace does zero-initialization
absl::get<char>(v) = 'a';
v.emplace<char>('b');
ASSERT_TRUE(absl::holds_alternative<char>(v));
EXPECT_EQ(absl::get<char>(v), 'b');
{
int& emplace_result = v.emplace<int>();
EXPECT_TRUE(absl::holds_alternative<int>(v));
EXPECT_EQ(absl::get<int>(v), 0);
EXPECT_EQ(&emplace_result, &absl::get<int>(v));
}
}
TEST(VariantTest, TestEmplaceInitializerList) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(absl::in_place_index<0>, 555);
MoveOnlyWithListConstructor& emplace_result =
v1.emplace<MoveOnlyWithListConstructor>({1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
}
TEST(VariantTest, TestEmplaceIndex) {
using Variant = variant<int, char>;
Variant v(absl::in_place_index<0>, 555);
{
char& emplace_result = v.emplace<1>();
ASSERT_TRUE(absl::holds_alternative<char>(v));
EXPECT_EQ(absl::get<char>(v), 0);
EXPECT_EQ(&emplace_result, &absl::get<char>(v));
}
// Make sure that another emplace does zero-initialization
absl::get<char>(v) = 'a';
v.emplace<1>('b');
ASSERT_TRUE(absl::holds_alternative<char>(v));
EXPECT_EQ(absl::get<char>(v), 'b');
{
int& emplace_result = v.emplace<0>();
EXPECT_TRUE(absl::holds_alternative<int>(v));
EXPECT_EQ(absl::get<int>(v), 0);
EXPECT_EQ(&emplace_result, &absl::get<int>(v));
}
}
TEST(VariantTest, TestEmplaceIndexInitializerList) {
using Var =
variant<int, std::string, NonCopyable, MoveOnlyWithListConstructor>;
Var v1(absl::in_place_index<0>, 555);
MoveOnlyWithListConstructor& emplace_result =
v1.emplace<3>({1, 2, 3, 4, 5}, 6);
ASSERT_TRUE(absl::holds_alternative<MoveOnlyWithListConstructor>(v1));
EXPECT_EQ(6, absl::get<MoveOnlyWithListConstructor>(v1).value);
EXPECT_EQ(&emplace_result, &absl::get<MoveOnlyWithListConstructor>(v1));
}
//////////////////////
// [variant.status] //
//////////////////////
TEST(VariantTest, Index) {
using Var = variant<int, std::string, double>;
Var v = 1;
EXPECT_EQ(0, v.index());
v = "str";
EXPECT_EQ(1, v.index());
v = 0.;
EXPECT_EQ(2, v.index());
Var v2 = v;
EXPECT_EQ(2, v2.index());
v2.emplace<int>(3);
EXPECT_EQ(0, v2.index());
}
TEST(VariantTest, NotValuelessByException) {
using Var = variant<int, std::string, double>;
Var v = 1;
EXPECT_FALSE(v.valueless_by_exception());
v = "str";
EXPECT_FALSE(v.valueless_by_exception());
v = 0.;
EXPECT_FALSE(v.valueless_by_exception());
Var v2 = v;
EXPECT_FALSE(v.valueless_by_exception());
v2.emplace<int>(3);
EXPECT_FALSE(v.valueless_by_exception());
}
#ifdef ABSL_HAVE_EXCEPTIONS