forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional_test.cc
1615 lines (1441 loc) · 53.4 KB
/
optional_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.
#include "absl/types/optional.h"
// This test is a no-op when absl::optional is an alias for std::optional.
#if !defined(ABSL_USES_STD_OPTIONAL)
#include <string>
#include <type_traits>
#include <utility>
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/log/log.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#if defined(__cplusplus) && __cplusplus >= 202002L
// In C++20, volatile-qualified return types are deprecated.
#define ABSL_VOLATILE_RETURN_TYPES_DEPRECATED 1
#endif
// The following types help test an internal compiler error in GCC5 though
// GCC10. The case OptionalTest.InternalCompilerErrorInGcc5ToGcc10 crashes the
// compiler without a workaround. This test case should remain at the beginning
// of the file as the internal compiler error is sensitive to other constructs
// in this file.
template <class T, class...>
using GccIceHelper1 = T;
template <typename T>
struct GccIceHelper2 {};
template <typename T>
class GccIce {
template <typename U,
typename SecondTemplateArgHasToExistForSomeReason = void,
typename DependentType = void,
typename = std::is_assignable<GccIceHelper1<T, DependentType>&, U>>
GccIce& operator=(GccIceHelper2<U> const&) {}
};
TEST(OptionalTest, InternalCompilerErrorInGcc5ToGcc10) {
GccIce<int> instantiate_ice_with_same_type_as_optional;
static_cast<void>(instantiate_ice_with_same_type_as_optional);
absl::optional<int> val1;
absl::optional<int> val2;
val1 = val2;
}
struct Hashable {};
namespace std {
template <>
struct hash<Hashable> {
size_t operator()(const Hashable&) { return 0; }
};
} // namespace std
struct NonHashable {};
namespace {
std::string TypeQuals(std::string&) { return "&"; }
std::string TypeQuals(std::string&&) { return "&&"; }
std::string TypeQuals(const std::string&) { return "c&"; }
std::string TypeQuals(const std::string&&) { return "c&&"; }
struct StructorListener {
int construct0 = 0;
int construct1 = 0;
int construct2 = 0;
int listinit = 0;
int copy = 0;
int move = 0;
int copy_assign = 0;
int move_assign = 0;
int destruct = 0;
int volatile_copy = 0;
int volatile_move = 0;
int volatile_copy_assign = 0;
int volatile_move_assign = 0;
};
// Suppress MSVC warnings.
// 4521: multiple copy constructors specified
// 4522: multiple assignment operators specified
// We wrote multiple of them to test that the correct overloads are selected.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4521)
#pragma warning(disable : 4522)
#endif
struct Listenable {
static StructorListener* listener;
Listenable() { ++listener->construct0; }
explicit Listenable(int /*unused*/) { ++listener->construct1; }
Listenable(int /*unused*/, int /*unused*/) { ++listener->construct2; }
Listenable(std::initializer_list<int> /*unused*/) { ++listener->listinit; }
Listenable(const Listenable& /*unused*/) { ++listener->copy; }
Listenable(const volatile Listenable& /*unused*/) {
++listener->volatile_copy;
}
Listenable(volatile Listenable&& /*unused*/) { ++listener->volatile_move; }
Listenable(Listenable&& /*unused*/) { ++listener->move; }
Listenable& operator=(const Listenable& /*unused*/) {
++listener->copy_assign;
return *this;
}
Listenable& operator=(Listenable&& /*unused*/) {
++listener->move_assign;
return *this;
}
// use void return type instead of volatile T& to work around GCC warning
// when the assignment's returned reference is ignored.
void operator=(const volatile Listenable& /*unused*/) volatile {
++listener->volatile_copy_assign;
}
void operator=(volatile Listenable&& /*unused*/) volatile {
++listener->volatile_move_assign;
}
~Listenable() { ++listener->destruct; }
};
#ifdef _MSC_VER
#pragma warning(pop)
#endif
StructorListener* Listenable::listener = nullptr;
struct ConstexprType {
enum CtorTypes {
kCtorDefault,
kCtorInt,
kCtorInitializerList,
kCtorConstChar
};
constexpr ConstexprType() : x(kCtorDefault) {}
constexpr explicit ConstexprType(int i) : x(kCtorInt) {}
constexpr ConstexprType(std::initializer_list<int> il)
: x(kCtorInitializerList) {}
constexpr ConstexprType(const char*) // NOLINT(runtime/explicit)
: x(kCtorConstChar) {}
int x;
};
struct Copyable {
Copyable() {}
Copyable(const Copyable&) {}
Copyable& operator=(const Copyable&) { return *this; }
};
struct MoveableThrow {
MoveableThrow() {}
MoveableThrow(MoveableThrow&&) {}
MoveableThrow& operator=(MoveableThrow&&) { return *this; }
};
struct MoveableNoThrow {
MoveableNoThrow() {}
MoveableNoThrow(MoveableNoThrow&&) noexcept {}
MoveableNoThrow& operator=(MoveableNoThrow&&) noexcept { return *this; }
};
struct NonMovable {
NonMovable() {}
NonMovable(const NonMovable&) = delete;
NonMovable& operator=(const NonMovable&) = delete;
NonMovable(NonMovable&&) = delete;
NonMovable& operator=(NonMovable&&) = delete;
};
struct NoDefault {
NoDefault() = delete;
NoDefault(const NoDefault&) {}
NoDefault& operator=(const NoDefault&) { return *this; }
};
struct ConvertsFromInPlaceT {
ConvertsFromInPlaceT(absl::in_place_t) {} // NOLINT
};
TEST(optionalTest, DefaultConstructor) {
absl::optional<int> empty;
EXPECT_FALSE(empty);
constexpr absl::optional<int> cempty;
static_assert(!cempty.has_value(), "");
EXPECT_TRUE(
std::is_nothrow_default_constructible<absl::optional<int>>::value);
}
TEST(optionalTest, nulloptConstructor) {
absl::optional<int> empty(absl::nullopt);
EXPECT_FALSE(empty);
constexpr absl::optional<int> cempty{absl::nullopt};
static_assert(!cempty.has_value(), "");
EXPECT_TRUE((std::is_nothrow_constructible<absl::optional<int>,
absl::nullopt_t>::value));
}
TEST(optionalTest, CopyConstructor) {
{
absl::optional<int> empty, opt42 = 42;
absl::optional<int> empty_copy(empty);
EXPECT_FALSE(empty_copy);
absl::optional<int> opt42_copy(opt42);
EXPECT_TRUE(opt42_copy);
EXPECT_EQ(42, *opt42_copy);
}
{
absl::optional<const int> empty, opt42 = 42;
absl::optional<const int> empty_copy(empty);
EXPECT_FALSE(empty_copy);
absl::optional<const int> opt42_copy(opt42);
EXPECT_TRUE(opt42_copy);
EXPECT_EQ(42, *opt42_copy);
}
#if !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
{
absl::optional<volatile int> empty, opt42 = 42;
absl::optional<volatile int> empty_copy(empty);
EXPECT_FALSE(empty_copy);
absl::optional<volatile int> opt42_copy(opt42);
EXPECT_TRUE(opt42_copy);
EXPECT_EQ(42, *opt42_copy);
}
#endif
// test copyablility
EXPECT_TRUE(std::is_copy_constructible<absl::optional<int>>::value);
EXPECT_TRUE(std::is_copy_constructible<absl::optional<Copyable>>::value);
EXPECT_FALSE(
std::is_copy_constructible<absl::optional<MoveableThrow>>::value);
EXPECT_FALSE(
std::is_copy_constructible<absl::optional<MoveableNoThrow>>::value);
EXPECT_FALSE(std::is_copy_constructible<absl::optional<NonMovable>>::value);
EXPECT_FALSE(
absl::is_trivially_copy_constructible<absl::optional<Copyable>>::value);
EXPECT_TRUE(
absl::is_trivially_copy_constructible<absl::optional<int>>::value);
EXPECT_TRUE(
absl::is_trivially_copy_constructible<absl::optional<const int>>::value);
#if !defined(_MSC_VER) && !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
// See defect report "Trivial copy/move constructor for class with volatile
// member" at
// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2094
// A class with non-static data member of volatile-qualified type should still
// have a trivial copy constructor if the data member is trivial.
// Also a cv-qualified scalar type should be trivially copyable.
EXPECT_TRUE(absl::is_trivially_copy_constructible<
absl::optional<volatile int>>::value);
#endif // !defined(_MSC_VER) && !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
// constexpr copy constructor for trivially copyable types
{
constexpr absl::optional<int> o1;
constexpr absl::optional<int> o2 = o1;
static_assert(!o2, "");
}
{
constexpr absl::optional<int> o1 = 42;
constexpr absl::optional<int> o2 = o1;
static_assert(o2, "");
static_assert(*o2 == 42, "");
}
{
struct TrivialCopyable {
constexpr TrivialCopyable() : x(0) {}
constexpr explicit TrivialCopyable(int i) : x(i) {}
int x;
};
constexpr absl::optional<TrivialCopyable> o1(42);
constexpr absl::optional<TrivialCopyable> o2 = o1;
static_assert(o2, "");
static_assert((*o2).x == 42, "");
#ifndef ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG
EXPECT_TRUE(absl::is_trivially_copy_constructible<
absl::optional<TrivialCopyable>>::value);
EXPECT_TRUE(absl::is_trivially_copy_constructible<
absl::optional<const TrivialCopyable>>::value);
#endif
#if !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
EXPECT_FALSE(std::is_copy_constructible<
absl::optional<volatile TrivialCopyable>>::value);
#endif // !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
}
}
TEST(optionalTest, MoveConstructor) {
absl::optional<int> empty, opt42 = 42;
absl::optional<int> empty_move(std::move(empty));
EXPECT_FALSE(empty_move);
absl::optional<int> opt42_move(std::move(opt42));
EXPECT_TRUE(opt42_move);
EXPECT_EQ(42, opt42_move);
// test movability
EXPECT_TRUE(std::is_move_constructible<absl::optional<int>>::value);
EXPECT_TRUE(std::is_move_constructible<absl::optional<Copyable>>::value);
EXPECT_TRUE(std::is_move_constructible<absl::optional<MoveableThrow>>::value);
EXPECT_TRUE(
std::is_move_constructible<absl::optional<MoveableNoThrow>>::value);
EXPECT_FALSE(std::is_move_constructible<absl::optional<NonMovable>>::value);
// test noexcept
EXPECT_TRUE(std::is_nothrow_move_constructible<absl::optional<int>>::value);
EXPECT_EQ(
absl::default_allocator_is_nothrow::value,
std::is_nothrow_move_constructible<absl::optional<MoveableThrow>>::value);
EXPECT_TRUE(std::is_nothrow_move_constructible<
absl::optional<MoveableNoThrow>>::value);
}
TEST(optionalTest, Destructor) {
struct Trivial {};
struct NonTrivial {
NonTrivial(const NonTrivial&) {}
NonTrivial& operator=(const NonTrivial&) { return *this; }
~NonTrivial() {}
};
EXPECT_TRUE(std::is_trivially_destructible<absl::optional<int>>::value);
EXPECT_TRUE(std::is_trivially_destructible<absl::optional<Trivial>>::value);
EXPECT_FALSE(
std::is_trivially_destructible<absl::optional<NonTrivial>>::value);
}
TEST(optionalTest, InPlaceConstructor) {
constexpr absl::optional<ConstexprType> opt0{absl::in_place_t()};
static_assert(opt0, "");
static_assert((*opt0).x == ConstexprType::kCtorDefault, "");
constexpr absl::optional<ConstexprType> opt1{absl::in_place_t(), 1};
static_assert(opt1, "");
static_assert((*opt1).x == ConstexprType::kCtorInt, "");
constexpr absl::optional<ConstexprType> opt2{absl::in_place_t(), {1, 2}};
static_assert(opt2, "");
static_assert((*opt2).x == ConstexprType::kCtorInitializerList, "");
EXPECT_FALSE((std::is_constructible<absl::optional<ConvertsFromInPlaceT>,
absl::in_place_t>::value));
EXPECT_FALSE((std::is_constructible<absl::optional<ConvertsFromInPlaceT>,
const absl::in_place_t&>::value));
EXPECT_TRUE(
(std::is_constructible<absl::optional<ConvertsFromInPlaceT>,
absl::in_place_t, absl::in_place_t>::value));
EXPECT_FALSE((std::is_constructible<absl::optional<NoDefault>,
absl::in_place_t>::value));
EXPECT_FALSE((std::is_constructible<absl::optional<NoDefault>,
absl::in_place_t&&>::value));
}
// template<U=T> optional(U&&);
TEST(optionalTest, ValueConstructor) {
constexpr absl::optional<int> opt0(0);
static_assert(opt0, "");
static_assert(*opt0 == 0, "");
EXPECT_TRUE((std::is_convertible<int, absl::optional<int>>::value));
// Copy initialization ( = "abc") won't work due to optional(optional&&)
// is not constexpr. Use list initialization instead. This invokes
// absl::optional<ConstexprType>::absl::optional<U>(U&&), with U = const char
// (&) [4], which direct-initializes the ConstexprType value held by the
// optional via ConstexprType::ConstexprType(const char*).
constexpr absl::optional<ConstexprType> opt1 = {"abc"};
static_assert(opt1, "");
static_assert(ConstexprType::kCtorConstChar == (*opt1).x, "");
EXPECT_TRUE(
(std::is_convertible<const char*, absl::optional<ConstexprType>>::value));
// direct initialization
constexpr absl::optional<ConstexprType> opt2{2};
static_assert(opt2, "");
static_assert(ConstexprType::kCtorInt == (*opt2).x, "");
EXPECT_FALSE(
(std::is_convertible<int, absl::optional<ConstexprType>>::value));
// this invokes absl::optional<int>::optional(int&&)
// NOTE: this has different behavior than assignment, e.g.
// "opt3 = {};" clears the optional rather than setting the value to 0
// According to C++17 standard N4659 [over.ics.list] 16.3.3.1.5, (9.2)- "if
// the initializer list has no elements, the implicit conversion is the
// identity conversion", so `optional(int&&)` should be a better match than
// `optional(optional&&)` which is a user-defined conversion.
// Note: GCC 7 has a bug with this overload selection when compiled with
// `-std=c++17`.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 7 && \
__cplusplus == 201703L
#define ABSL_GCC7_OVER_ICS_LIST_BUG 1
#endif
#ifndef ABSL_GCC7_OVER_ICS_LIST_BUG
constexpr absl::optional<int> opt3({});
static_assert(opt3, "");
static_assert(*opt3 == 0, "");
#endif
// this invokes the move constructor with a default constructed optional
// because non-template function is a better match than template function.
absl::optional<ConstexprType> opt4({});
EXPECT_FALSE(opt4);
}
struct Implicit {};
struct Explicit {};
struct Convert {
Convert(const Implicit&) // NOLINT(runtime/explicit)
: implicit(true), move(false) {}
Convert(Implicit&&) // NOLINT(runtime/explicit)
: implicit(true), move(true) {}
explicit Convert(const Explicit&) : implicit(false), move(false) {}
explicit Convert(Explicit&&) : implicit(false), move(true) {}
bool implicit;
bool move;
};
struct ConvertFromOptional {
ConvertFromOptional(const Implicit&) // NOLINT(runtime/explicit)
: implicit(true), move(false), from_optional(false) {}
ConvertFromOptional(Implicit&&) // NOLINT(runtime/explicit)
: implicit(true), move(true), from_optional(false) {}
ConvertFromOptional(
const absl::optional<Implicit>&) // NOLINT(runtime/explicit)
: implicit(true), move(false), from_optional(true) {}
ConvertFromOptional(absl::optional<Implicit>&&) // NOLINT(runtime/explicit)
: implicit(true), move(true), from_optional(true) {}
explicit ConvertFromOptional(const Explicit&)
: implicit(false), move(false), from_optional(false) {}
explicit ConvertFromOptional(Explicit&&)
: implicit(false), move(true), from_optional(false) {}
explicit ConvertFromOptional(const absl::optional<Explicit>&)
: implicit(false), move(false), from_optional(true) {}
explicit ConvertFromOptional(absl::optional<Explicit>&&)
: implicit(false), move(true), from_optional(true) {}
bool implicit;
bool move;
bool from_optional;
};
TEST(optionalTest, ConvertingConstructor) {
absl::optional<Implicit> i_empty;
absl::optional<Implicit> i(absl::in_place);
absl::optional<Explicit> e_empty;
absl::optional<Explicit> e(absl::in_place);
{
// implicitly constructing absl::optional<Convert> from
// absl::optional<Implicit>
absl::optional<Convert> empty = i_empty;
EXPECT_FALSE(empty);
absl::optional<Convert> opt_copy = i;
EXPECT_TRUE(opt_copy);
EXPECT_TRUE(opt_copy->implicit);
EXPECT_FALSE(opt_copy->move);
absl::optional<Convert> opt_move = absl::optional<Implicit>(absl::in_place);
EXPECT_TRUE(opt_move);
EXPECT_TRUE(opt_move->implicit);
EXPECT_TRUE(opt_move->move);
}
{
// explicitly constructing absl::optional<Convert> from
// absl::optional<Explicit>
absl::optional<Convert> empty(e_empty);
EXPECT_FALSE(empty);
absl::optional<Convert> opt_copy(e);
EXPECT_TRUE(opt_copy);
EXPECT_FALSE(opt_copy->implicit);
EXPECT_FALSE(opt_copy->move);
EXPECT_FALSE((std::is_convertible<const absl::optional<Explicit>&,
absl::optional<Convert>>::value));
absl::optional<Convert> opt_move{absl::optional<Explicit>(absl::in_place)};
EXPECT_TRUE(opt_move);
EXPECT_FALSE(opt_move->implicit);
EXPECT_TRUE(opt_move->move);
EXPECT_FALSE((std::is_convertible<absl::optional<Explicit>&&,
absl::optional<Convert>>::value));
}
{
// implicitly constructing absl::optional<ConvertFromOptional> from
// absl::optional<Implicit> via
// ConvertFromOptional(absl::optional<Implicit>&&) check that
// ConvertFromOptional(Implicit&&) is NOT called
static_assert(
std::is_convertible<absl::optional<Implicit>,
absl::optional<ConvertFromOptional>>::value,
"");
absl::optional<ConvertFromOptional> opt0 = i_empty;
EXPECT_TRUE(opt0);
EXPECT_TRUE(opt0->implicit);
EXPECT_FALSE(opt0->move);
EXPECT_TRUE(opt0->from_optional);
absl::optional<ConvertFromOptional> opt1 = absl::optional<Implicit>();
EXPECT_TRUE(opt1);
EXPECT_TRUE(opt1->implicit);
EXPECT_TRUE(opt1->move);
EXPECT_TRUE(opt1->from_optional);
}
{
// implicitly constructing absl::optional<ConvertFromOptional> from
// absl::optional<Explicit> via
// ConvertFromOptional(absl::optional<Explicit>&&) check that
// ConvertFromOptional(Explicit&&) is NOT called
absl::optional<ConvertFromOptional> opt0(e_empty);
EXPECT_TRUE(opt0);
EXPECT_FALSE(opt0->implicit);
EXPECT_FALSE(opt0->move);
EXPECT_TRUE(opt0->from_optional);
EXPECT_FALSE(
(std::is_convertible<const absl::optional<Explicit>&,
absl::optional<ConvertFromOptional>>::value));
absl::optional<ConvertFromOptional> opt1{absl::optional<Explicit>()};
EXPECT_TRUE(opt1);
EXPECT_FALSE(opt1->implicit);
EXPECT_TRUE(opt1->move);
EXPECT_TRUE(opt1->from_optional);
EXPECT_FALSE(
(std::is_convertible<absl::optional<Explicit>&&,
absl::optional<ConvertFromOptional>>::value));
}
}
TEST(optionalTest, StructorBasic) {
StructorListener listener;
Listenable::listener = &listener;
{
absl::optional<Listenable> empty;
EXPECT_FALSE(empty);
absl::optional<Listenable> opt0(absl::in_place);
EXPECT_TRUE(opt0);
absl::optional<Listenable> opt1(absl::in_place, 1);
EXPECT_TRUE(opt1);
absl::optional<Listenable> opt2(absl::in_place, 1, 2);
EXPECT_TRUE(opt2);
}
EXPECT_EQ(1, listener.construct0);
EXPECT_EQ(1, listener.construct1);
EXPECT_EQ(1, listener.construct2);
EXPECT_EQ(3, listener.destruct);
}
TEST(optionalTest, CopyMoveStructor) {
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> original(absl::in_place);
EXPECT_EQ(1, listener.construct0);
EXPECT_EQ(0, listener.copy);
EXPECT_EQ(0, listener.move);
absl::optional<Listenable> copy(original);
EXPECT_EQ(1, listener.construct0);
EXPECT_EQ(1, listener.copy);
EXPECT_EQ(0, listener.move);
absl::optional<Listenable> move(std::move(original));
EXPECT_EQ(1, listener.construct0);
EXPECT_EQ(1, listener.copy);
EXPECT_EQ(1, listener.move);
}
TEST(optionalTest, ListInit) {
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> listinit1(absl::in_place, {1});
absl::optional<Listenable> listinit2(absl::in_place, {1, 2});
EXPECT_EQ(2, listener.listinit);
}
TEST(optionalTest, AssignFromNullopt) {
absl::optional<int> opt(1);
opt = absl::nullopt;
EXPECT_FALSE(opt);
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> opt1(absl::in_place);
opt1 = absl::nullopt;
EXPECT_FALSE(opt1);
EXPECT_EQ(1, listener.construct0);
EXPECT_EQ(1, listener.destruct);
EXPECT_TRUE((
std::is_nothrow_assignable<absl::optional<int>, absl::nullopt_t>::value));
EXPECT_TRUE((std::is_nothrow_assignable<absl::optional<Listenable>,
absl::nullopt_t>::value));
}
TEST(optionalTest, CopyAssignment) {
const absl::optional<int> empty, opt1 = 1, opt2 = 2;
absl::optional<int> empty_to_opt1, opt1_to_opt2, opt2_to_empty;
EXPECT_FALSE(empty_to_opt1);
empty_to_opt1 = empty;
EXPECT_FALSE(empty_to_opt1);
empty_to_opt1 = opt1;
EXPECT_TRUE(empty_to_opt1);
EXPECT_EQ(1, empty_to_opt1.value());
EXPECT_FALSE(opt1_to_opt2);
opt1_to_opt2 = opt1;
EXPECT_TRUE(opt1_to_opt2);
EXPECT_EQ(1, opt1_to_opt2.value());
opt1_to_opt2 = opt2;
EXPECT_TRUE(opt1_to_opt2);
EXPECT_EQ(2, opt1_to_opt2.value());
EXPECT_FALSE(opt2_to_empty);
opt2_to_empty = opt2;
EXPECT_TRUE(opt2_to_empty);
EXPECT_EQ(2, opt2_to_empty.value());
opt2_to_empty = empty;
EXPECT_FALSE(opt2_to_empty);
EXPECT_FALSE(absl::is_copy_assignable<absl::optional<const int>>::value);
EXPECT_TRUE(absl::is_copy_assignable<absl::optional<Copyable>>::value);
EXPECT_FALSE(absl::is_copy_assignable<absl::optional<MoveableThrow>>::value);
EXPECT_FALSE(
absl::is_copy_assignable<absl::optional<MoveableNoThrow>>::value);
EXPECT_FALSE(absl::is_copy_assignable<absl::optional<NonMovable>>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<int>::value);
EXPECT_TRUE(absl::is_trivially_copy_assignable<volatile int>::value);
struct Trivial {
int i;
};
struct NonTrivial {
NonTrivial& operator=(const NonTrivial&) { return *this; }
int i;
};
EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial>::value);
EXPECT_FALSE(absl::is_copy_assignable<const Trivial>::value);
EXPECT_FALSE(absl::is_copy_assignable<volatile Trivial>::value);
EXPECT_TRUE(absl::is_copy_assignable<NonTrivial>::value);
EXPECT_FALSE(absl::is_trivially_copy_assignable<NonTrivial>::value);
#if !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
{
StructorListener listener;
Listenable::listener = &listener;
absl::optional<volatile Listenable> empty, set(absl::in_place);
EXPECT_EQ(1, listener.construct0);
absl::optional<volatile Listenable> empty_to_empty, empty_to_set,
set_to_empty(absl::in_place), set_to_set(absl::in_place);
EXPECT_EQ(3, listener.construct0);
empty_to_empty = empty; // no effect
empty_to_set = set; // copy construct
set_to_empty = empty; // destruct
set_to_set = set; // copy assign
EXPECT_EQ(1, listener.volatile_copy);
EXPECT_EQ(0, listener.volatile_move);
EXPECT_EQ(1, listener.destruct);
EXPECT_EQ(1, listener.volatile_copy_assign);
}
#endif // !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
}
TEST(optionalTest, MoveAssignment) {
{
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> empty1, empty2, set1(absl::in_place),
set2(absl::in_place);
EXPECT_EQ(2, listener.construct0);
absl::optional<Listenable> empty_to_empty, empty_to_set,
set_to_empty(absl::in_place), set_to_set(absl::in_place);
EXPECT_EQ(4, listener.construct0);
empty_to_empty = std::move(empty1);
empty_to_set = std::move(set1);
set_to_empty = std::move(empty2);
set_to_set = std::move(set2);
EXPECT_EQ(0, listener.copy);
EXPECT_EQ(1, listener.move);
EXPECT_EQ(1, listener.destruct);
EXPECT_EQ(1, listener.move_assign);
}
#if !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
{
StructorListener listener;
Listenable::listener = &listener;
absl::optional<volatile Listenable> empty1, empty2, set1(absl::in_place),
set2(absl::in_place);
EXPECT_EQ(2, listener.construct0);
absl::optional<volatile Listenable> empty_to_empty, empty_to_set,
set_to_empty(absl::in_place), set_to_set(absl::in_place);
EXPECT_EQ(4, listener.construct0);
empty_to_empty = std::move(empty1); // no effect
empty_to_set = std::move(set1); // move construct
set_to_empty = std::move(empty2); // destruct
set_to_set = std::move(set2); // move assign
EXPECT_EQ(0, listener.volatile_copy);
EXPECT_EQ(1, listener.volatile_move);
EXPECT_EQ(1, listener.destruct);
EXPECT_EQ(1, listener.volatile_move_assign);
}
#endif // !defined(ABSL_VOLATILE_RETURN_TYPES_DEPRECATED)
EXPECT_FALSE(absl::is_move_assignable<absl::optional<const int>>::value);
EXPECT_TRUE(absl::is_move_assignable<absl::optional<Copyable>>::value);
EXPECT_TRUE(absl::is_move_assignable<absl::optional<MoveableThrow>>::value);
EXPECT_TRUE(absl::is_move_assignable<absl::optional<MoveableNoThrow>>::value);
EXPECT_FALSE(absl::is_move_assignable<absl::optional<NonMovable>>::value);
EXPECT_FALSE(
std::is_nothrow_move_assignable<absl::optional<MoveableThrow>>::value);
EXPECT_TRUE(
std::is_nothrow_move_assignable<absl::optional<MoveableNoThrow>>::value);
}
struct NoConvertToOptional {
// disable implicit conversion from const NoConvertToOptional&
// to absl::optional<NoConvertToOptional>.
NoConvertToOptional(const NoConvertToOptional&) = delete;
};
struct CopyConvert {
CopyConvert(const NoConvertToOptional&);
CopyConvert& operator=(const CopyConvert&) = delete;
CopyConvert& operator=(const NoConvertToOptional&);
};
struct CopyConvertFromOptional {
CopyConvertFromOptional(const NoConvertToOptional&);
CopyConvertFromOptional(const absl::optional<NoConvertToOptional>&);
CopyConvertFromOptional& operator=(const CopyConvertFromOptional&) = delete;
CopyConvertFromOptional& operator=(const NoConvertToOptional&);
CopyConvertFromOptional& operator=(
const absl::optional<NoConvertToOptional>&);
};
struct MoveConvert {
MoveConvert(NoConvertToOptional&&);
MoveConvert& operator=(const MoveConvert&) = delete;
MoveConvert& operator=(NoConvertToOptional&&);
};
struct MoveConvertFromOptional {
MoveConvertFromOptional(NoConvertToOptional&&);
MoveConvertFromOptional(absl::optional<NoConvertToOptional>&&);
MoveConvertFromOptional& operator=(const MoveConvertFromOptional&) = delete;
MoveConvertFromOptional& operator=(NoConvertToOptional&&);
MoveConvertFromOptional& operator=(absl::optional<NoConvertToOptional>&&);
};
// template <typename U = T> absl::optional<T>& operator=(U&& v);
TEST(optionalTest, ValueAssignment) {
absl::optional<int> opt;
EXPECT_FALSE(opt);
opt = 42;
EXPECT_TRUE(opt);
EXPECT_EQ(42, opt.value());
opt = absl::nullopt;
EXPECT_FALSE(opt);
opt = 42;
EXPECT_TRUE(opt);
EXPECT_EQ(42, opt.value());
opt = 43;
EXPECT_TRUE(opt);
EXPECT_EQ(43, opt.value());
opt = {}; // this should clear optional
EXPECT_FALSE(opt);
opt = {44};
EXPECT_TRUE(opt);
EXPECT_EQ(44, opt.value());
// U = const NoConvertToOptional&
EXPECT_TRUE((std::is_assignable<absl::optional<CopyConvert>&,
const NoConvertToOptional&>::value));
// U = const absl::optional<NoConvertToOptional>&
EXPECT_TRUE((std::is_assignable<absl::optional<CopyConvertFromOptional>&,
const NoConvertToOptional&>::value));
// U = const NoConvertToOptional& triggers SFINAE because
// std::is_constructible_v<MoveConvert, const NoConvertToOptional&> is false
EXPECT_FALSE((std::is_assignable<absl::optional<MoveConvert>&,
const NoConvertToOptional&>::value));
// U = NoConvertToOptional
EXPECT_TRUE((std::is_assignable<absl::optional<MoveConvert>&,
NoConvertToOptional&&>::value));
// U = const NoConvertToOptional& triggers SFINAE because
// std::is_constructible_v<MoveConvertFromOptional, const
// NoConvertToOptional&> is false
EXPECT_FALSE((std::is_assignable<absl::optional<MoveConvertFromOptional>&,
const NoConvertToOptional&>::value));
// U = NoConvertToOptional
EXPECT_TRUE((std::is_assignable<absl::optional<MoveConvertFromOptional>&,
NoConvertToOptional&&>::value));
// U = const absl::optional<NoConvertToOptional>&
EXPECT_TRUE(
(std::is_assignable<absl::optional<CopyConvertFromOptional>&,
const absl::optional<NoConvertToOptional>&>::value));
// U = absl::optional<NoConvertToOptional>
EXPECT_TRUE(
(std::is_assignable<absl::optional<MoveConvertFromOptional>&,
absl::optional<NoConvertToOptional>&&>::value));
}
// template <typename U> absl::optional<T>& operator=(const absl::optional<U>&
// rhs); template <typename U> absl::optional<T>& operator=(absl::optional<U>&&
// rhs);
TEST(optionalTest, ConvertingAssignment) {
absl::optional<int> opt_i;
absl::optional<char> opt_c('c');
opt_i = opt_c;
EXPECT_TRUE(opt_i);
EXPECT_EQ(*opt_c, *opt_i);
opt_i = absl::optional<char>();
EXPECT_FALSE(opt_i);
opt_i = absl::optional<char>('d');
EXPECT_TRUE(opt_i);
EXPECT_EQ('d', *opt_i);
absl::optional<std::string> opt_str;
absl::optional<const char*> opt_cstr("abc");
opt_str = opt_cstr;
EXPECT_TRUE(opt_str);
EXPECT_EQ(std::string("abc"), *opt_str);
opt_str = absl::optional<const char*>();
EXPECT_FALSE(opt_str);
opt_str = absl::optional<const char*>("def");
EXPECT_TRUE(opt_str);
EXPECT_EQ(std::string("def"), *opt_str);
// operator=(const absl::optional<U>&) with U = NoConvertToOptional
EXPECT_TRUE(
(std::is_assignable<absl::optional<CopyConvert>,
const absl::optional<NoConvertToOptional>&>::value));
// operator=(const absl::optional<U>&) with U = NoConvertToOptional
// triggers SFINAE because
// std::is_constructible_v<MoveConvert, const NoConvertToOptional&> is false
EXPECT_FALSE(
(std::is_assignable<absl::optional<MoveConvert>&,
const absl::optional<NoConvertToOptional>&>::value));
// operator=(absl::optional<U>&&) with U = NoConvertToOptional
EXPECT_TRUE(
(std::is_assignable<absl::optional<MoveConvert>&,
absl::optional<NoConvertToOptional>&&>::value));
// operator=(const absl::optional<U>&) with U = NoConvertToOptional triggers
// SFINAE because std::is_constructible_v<MoveConvertFromOptional, const
// NoConvertToOptional&> is false. operator=(U&&) with U = const
// absl::optional<NoConverToOptional>& triggers SFINAE because
// std::is_constructible<MoveConvertFromOptional,
// absl::optional<NoConvertToOptional>&&> is true.
EXPECT_FALSE(
(std::is_assignable<absl::optional<MoveConvertFromOptional>&,
const absl::optional<NoConvertToOptional>&>::value));
}
TEST(optionalTest, ResetAndHasValue) {
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> opt;
EXPECT_FALSE(opt);
EXPECT_FALSE(opt.has_value());
opt.emplace();
EXPECT_TRUE(opt);
EXPECT_TRUE(opt.has_value());
opt.reset();
EXPECT_FALSE(opt);
EXPECT_FALSE(opt.has_value());
EXPECT_EQ(1, listener.destruct);
opt.reset();
EXPECT_FALSE(opt);
EXPECT_FALSE(opt.has_value());
constexpr absl::optional<int> empty;
static_assert(!empty.has_value(), "");
constexpr absl::optional<int> nonempty(1);
static_assert(nonempty.has_value(), "");
}
TEST(optionalTest, Emplace) {
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> opt;
EXPECT_FALSE(opt);
opt.emplace(1);
EXPECT_TRUE(opt);
opt.emplace(1, 2);
EXPECT_EQ(1, listener.construct1);
EXPECT_EQ(1, listener.construct2);
EXPECT_EQ(1, listener.destruct);
absl::optional<std::string> o;
EXPECT_TRUE((std::is_same<std::string&, decltype(o.emplace("abc"))>::value));
std::string& ref = o.emplace("abc");
EXPECT_EQ(&ref, &o.value());
}
TEST(optionalTest, ListEmplace) {
StructorListener listener;
Listenable::listener = &listener;
absl::optional<Listenable> opt;
EXPECT_FALSE(opt);
opt.emplace({1});
EXPECT_TRUE(opt);
opt.emplace({1, 2});
EXPECT_EQ(2, listener.listinit);
EXPECT_EQ(1, listener.destruct);
absl::optional<Listenable> o;
EXPECT_TRUE((std::is_same<Listenable&, decltype(o.emplace({1}))>::value));
Listenable& ref = o.emplace({1});
EXPECT_EQ(&ref, &o.value());
}
TEST(optionalTest, Swap) {
absl::optional<int> opt_empty, opt1 = 1, opt2 = 2;
EXPECT_FALSE(opt_empty);
EXPECT_TRUE(opt1);
EXPECT_EQ(1, opt1.value());
EXPECT_TRUE(opt2);
EXPECT_EQ(2, opt2.value());
swap(opt_empty, opt1);
EXPECT_FALSE(opt1);
EXPECT_TRUE(opt_empty);
EXPECT_EQ(1, opt_empty.value());
EXPECT_TRUE(opt2);
EXPECT_EQ(2, opt2.value());
swap(opt_empty, opt1);
EXPECT_FALSE(opt_empty);
EXPECT_TRUE(opt1);
EXPECT_EQ(1, opt1.value());
EXPECT_TRUE(opt2);
EXPECT_EQ(2, opt2.value());
swap(opt1, opt2);
EXPECT_FALSE(opt_empty);
EXPECT_TRUE(opt1);
EXPECT_EQ(2, opt1.value());
EXPECT_TRUE(opt2);
EXPECT_EQ(1, opt2.value());
EXPECT_TRUE(noexcept(opt1.swap(opt2)));
EXPECT_TRUE(noexcept(swap(opt1, opt2)));
}
template <int v>
struct DeletedOpAddr {
int value = v;
constexpr DeletedOpAddr() = default;
constexpr const DeletedOpAddr<v>* operator&() const = delete; // NOLINT
DeletedOpAddr<v>* operator&() = delete; // NOLINT
};
// The static_assert featuring a constexpr call to operator->() is commented out
// to document the fact that the current implementation of absl::optional<T>
// expects such usecases to be malformed and not compile.
TEST(optionalTest, OperatorAddr) {
constexpr int v = -1;
{ // constexpr
constexpr absl::optional<DeletedOpAddr<v>> opt(absl::in_place_t{});
static_assert(opt.has_value(), "");
// static_assert(opt->value == v, "");
static_assert((*opt).value == v, "");
}
{ // non-constexpr
const absl::optional<DeletedOpAddr<v>> opt(absl::in_place_t{});
EXPECT_TRUE(opt.has_value());
EXPECT_TRUE(opt->value == v);
EXPECT_TRUE((*opt).value == v);
}
}
TEST(optionalTest, PointerStuff) {
absl::optional<std::string> opt(absl::in_place, "foo");
EXPECT_EQ("foo", *opt);
const auto& opt_const = opt;
EXPECT_EQ("foo", *opt_const);
EXPECT_EQ(opt->size(), 3u);
EXPECT_EQ(opt_const->size(), 3u);
constexpr absl::optional<ConstexprType> opt1(1);
static_assert((*opt1).x == ConstexprType::kCtorInt, "");
}
TEST(optionalTest, Value) {
using O = absl::optional<std::string>;
using CO = const absl::optional<std::string>;
using OC = absl::optional<const std::string>;
O lvalue(absl::in_place, "lvalue");
CO clvalue(absl::in_place, "clvalue");
OC lvalue_c(absl::in_place, "lvalue_c");
EXPECT_EQ("lvalue", lvalue.value());
EXPECT_EQ("clvalue", clvalue.value());
EXPECT_EQ("lvalue_c", lvalue_c.value());
EXPECT_EQ("xvalue", O(absl::in_place, "xvalue").value());
EXPECT_EQ("xvalue_c", OC(absl::in_place, "xvalue_c").value());
EXPECT_EQ("cxvalue", CO(absl::in_place, "cxvalue").value());
EXPECT_EQ("&", TypeQuals(lvalue.value()));
EXPECT_EQ("c&", TypeQuals(clvalue.value()));
EXPECT_EQ("c&", TypeQuals(lvalue_c.value()));