1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/optional.h"
6
7 #include <memory>
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using ::testing::ElementsAre;
16
17 namespace base {
18
19 namespace {
20
21 // Object used to test complex object with Optional<T> in addition of the move
22 // semantics.
23 class TestObject {
24 public:
25 enum class State {
26 DEFAULT_CONSTRUCTED,
27 VALUE_CONSTRUCTED,
28 COPY_CONSTRUCTED,
29 MOVE_CONSTRUCTED,
30 MOVED_FROM,
31 COPY_ASSIGNED,
32 MOVE_ASSIGNED,
33 SWAPPED,
34 };
35
TestObject()36 TestObject() : foo_(0), bar_(0.0), state_(State::DEFAULT_CONSTRUCTED) {}
37
TestObject(int foo,double bar)38 TestObject(int foo, double bar)
39 : foo_(foo), bar_(bar), state_(State::VALUE_CONSTRUCTED) {}
40
TestObject(const TestObject & other)41 TestObject(const TestObject& other)
42 : foo_(other.foo_),
43 bar_(other.bar_),
44 state_(State::COPY_CONSTRUCTED),
45 move_ctors_count_(other.move_ctors_count_) {}
46
TestObject(TestObject && other)47 TestObject(TestObject&& other)
48 : foo_(std::move(other.foo_)),
49 bar_(std::move(other.bar_)),
50 state_(State::MOVE_CONSTRUCTED),
51 move_ctors_count_(other.move_ctors_count_ + 1) {
52 other.state_ = State::MOVED_FROM;
53 }
54
operator =(const TestObject & other)55 TestObject& operator=(const TestObject& other) {
56 foo_ = other.foo_;
57 bar_ = other.bar_;
58 state_ = State::COPY_ASSIGNED;
59 move_ctors_count_ = other.move_ctors_count_;
60 return *this;
61 }
62
operator =(TestObject && other)63 TestObject& operator=(TestObject&& other) {
64 foo_ = other.foo_;
65 bar_ = other.bar_;
66 state_ = State::MOVE_ASSIGNED;
67 move_ctors_count_ = other.move_ctors_count_;
68 other.state_ = State::MOVED_FROM;
69 return *this;
70 }
71
Swap(TestObject * other)72 void Swap(TestObject* other) {
73 using std::swap;
74 swap(foo_, other->foo_);
75 swap(bar_, other->bar_);
76 swap(move_ctors_count_, other->move_ctors_count_);
77 state_ = State::SWAPPED;
78 other->state_ = State::SWAPPED;
79 }
80
operator ==(const TestObject & other) const81 bool operator==(const TestObject& other) const {
82 return std::tie(foo_, bar_) == std::tie(other.foo_, other.bar_);
83 }
84
operator !=(const TestObject & other) const85 bool operator!=(const TestObject& other) const { return !(*this == other); }
86
foo() const87 int foo() const { return foo_; }
state() const88 State state() const { return state_; }
move_ctors_count() const89 int move_ctors_count() const { return move_ctors_count_; }
90
91 private:
92 int foo_;
93 double bar_;
94 State state_;
95 int move_ctors_count_ = 0;
96 };
97
98 // Implementing Swappable concept.
swap(TestObject & lhs,TestObject & rhs)99 void swap(TestObject& lhs, TestObject& rhs) {
100 lhs.Swap(&rhs);
101 }
102
103 class NonTriviallyDestructible {
~NonTriviallyDestructible()104 ~NonTriviallyDestructible() {}
105 };
106
107 class DeletedDefaultConstructor {
108 public:
109 DeletedDefaultConstructor() = delete;
DeletedDefaultConstructor(int foo)110 DeletedDefaultConstructor(int foo) : foo_(foo) {}
111
foo() const112 int foo() const { return foo_; }
113
114 private:
115 int foo_;
116 };
117
118 class DeletedCopy {
119 public:
DeletedCopy(int foo)120 explicit DeletedCopy(int foo) : foo_(foo) {}
121 DeletedCopy(const DeletedCopy&) = delete;
122 DeletedCopy(DeletedCopy&&) = default;
123
124 DeletedCopy& operator=(const DeletedCopy&) = delete;
125 DeletedCopy& operator=(DeletedCopy&&) = default;
126
foo() const127 int foo() const { return foo_; }
128
129 private:
130 int foo_;
131 };
132
133 class DeletedMove {
134 public:
DeletedMove(int foo)135 explicit DeletedMove(int foo) : foo_(foo) {}
136 DeletedMove(const DeletedMove&) = default;
137 DeletedMove(DeletedMove&&) = delete;
138
139 DeletedMove& operator=(const DeletedMove&) = default;
140 DeletedMove& operator=(DeletedMove&&) = delete;
141
foo() const142 int foo() const { return foo_; }
143
144 private:
145 int foo_;
146 };
147
148 class NonTriviallyDestructibleDeletedCopyConstructor {
149 public:
NonTriviallyDestructibleDeletedCopyConstructor(int foo)150 explicit NonTriviallyDestructibleDeletedCopyConstructor(int foo)
151 : foo_(foo) {}
152 NonTriviallyDestructibleDeletedCopyConstructor(
153 const NonTriviallyDestructibleDeletedCopyConstructor&) = delete;
154 NonTriviallyDestructibleDeletedCopyConstructor(
155 NonTriviallyDestructibleDeletedCopyConstructor&&) = default;
156
~NonTriviallyDestructibleDeletedCopyConstructor()157 ~NonTriviallyDestructibleDeletedCopyConstructor() {}
158
foo() const159 int foo() const { return foo_; }
160
161 private:
162 int foo_;
163 };
164
165 class DeleteNewOperators {
166 public:
167 void* operator new(size_t) = delete;
168 void* operator new(size_t, void*) = delete;
169 void* operator new[](size_t) = delete;
170 void* operator new[](size_t, void*) = delete;
171 };
172
173 } // anonymous namespace
174
175 static_assert(std::is_trivially_destructible<Optional<int>>::value,
176 "OptionalIsTriviallyDestructible");
177
178 static_assert(
179 !std::is_trivially_destructible<Optional<NonTriviallyDestructible>>::value,
180 "OptionalIsTriviallyDestructible");
181
182 static_assert(sizeof(Optional<int>) == sizeof(internal::OptionalBase<int>),
183 "internal::{Copy,Move}{Constructible,Assignable} structs "
184 "should be 0-sized");
185
TEST(OptionalTest,DefaultConstructor)186 TEST(OptionalTest, DefaultConstructor) {
187 {
188 constexpr Optional<float> o;
189 EXPECT_FALSE(o);
190 }
191
192 {
193 Optional<std::string> o;
194 EXPECT_FALSE(o);
195 }
196
197 {
198 Optional<TestObject> o;
199 EXPECT_FALSE(o);
200 }
201 }
202
TEST(OptionalTest,CopyConstructor)203 TEST(OptionalTest, CopyConstructor) {
204 {
205 constexpr Optional<float> first(0.1f);
206 constexpr Optional<float> other(first);
207
208 EXPECT_TRUE(other);
209 EXPECT_EQ(other.value(), 0.1f);
210 EXPECT_EQ(first, other);
211 }
212
213 {
214 Optional<std::string> first("foo");
215 Optional<std::string> other(first);
216
217 EXPECT_TRUE(other);
218 EXPECT_EQ(other.value(), "foo");
219 EXPECT_EQ(first, other);
220 }
221
222 {
223 const Optional<std::string> first("foo");
224 Optional<std::string> other(first);
225
226 EXPECT_TRUE(other);
227 EXPECT_EQ(other.value(), "foo");
228 EXPECT_EQ(first, other);
229 }
230
231 {
232 Optional<TestObject> first(TestObject(3, 0.1));
233 Optional<TestObject> other(first);
234
235 EXPECT_TRUE(!!other);
236 EXPECT_TRUE(other.value() == TestObject(3, 0.1));
237 EXPECT_TRUE(first == other);
238 }
239 }
240
TEST(OptionalTest,ValueConstructor)241 TEST(OptionalTest, ValueConstructor) {
242 {
243 constexpr float value = 0.1f;
244 constexpr Optional<float> o(value);
245
246 EXPECT_TRUE(o);
247 EXPECT_EQ(value, o.value());
248 }
249
250 {
251 std::string value("foo");
252 Optional<std::string> o(value);
253
254 EXPECT_TRUE(o);
255 EXPECT_EQ(value, o.value());
256 }
257
258 {
259 TestObject value(3, 0.1);
260 Optional<TestObject> o(value);
261
262 EXPECT_TRUE(o);
263 EXPECT_EQ(TestObject::State::COPY_CONSTRUCTED, o->state());
264 EXPECT_EQ(value, o.value());
265 }
266 }
267
TEST(OptionalTest,MoveConstructor)268 TEST(OptionalTest, MoveConstructor) {
269 {
270 constexpr Optional<float> first(0.1f);
271 constexpr Optional<float> second(std::move(first));
272
273 EXPECT_TRUE(second.has_value());
274 EXPECT_EQ(second.value(), 0.1f);
275
276 EXPECT_TRUE(first.has_value());
277 }
278
279 {
280 Optional<std::string> first("foo");
281 Optional<std::string> second(std::move(first));
282
283 EXPECT_TRUE(second.has_value());
284 EXPECT_EQ("foo", second.value());
285
286 EXPECT_TRUE(first.has_value());
287 }
288
289 {
290 Optional<TestObject> first(TestObject(3, 0.1));
291 Optional<TestObject> second(std::move(first));
292
293 EXPECT_TRUE(second.has_value());
294 EXPECT_EQ(TestObject::State::MOVE_CONSTRUCTED, second->state());
295 EXPECT_TRUE(TestObject(3, 0.1) == second.value());
296
297 EXPECT_TRUE(first.has_value());
298 EXPECT_EQ(TestObject::State::MOVED_FROM, first->state());
299 }
300
301 // Even if copy constructor is deleted, move constructor needs to work.
302 // Note that it couldn't be constexpr.
303 {
304 Optional<DeletedCopy> first(in_place, 42);
305 Optional<DeletedCopy> second(std::move(first));
306
307 EXPECT_TRUE(second.has_value());
308 EXPECT_EQ(42, second->foo());
309
310 EXPECT_TRUE(first.has_value());
311 }
312
313 {
314 Optional<DeletedMove> first(in_place, 42);
315 Optional<DeletedMove> second(std::move(first));
316
317 EXPECT_TRUE(second.has_value());
318 EXPECT_EQ(42, second->foo());
319
320 EXPECT_TRUE(first.has_value());
321 }
322
323 {
324 Optional<NonTriviallyDestructibleDeletedCopyConstructor> first(in_place,
325 42);
326 Optional<NonTriviallyDestructibleDeletedCopyConstructor> second(
327 std::move(first));
328
329 EXPECT_TRUE(second.has_value());
330 EXPECT_EQ(42, second->foo());
331
332 EXPECT_TRUE(first.has_value());
333 }
334 }
335
TEST(OptionalTest,MoveValueConstructor)336 TEST(OptionalTest, MoveValueConstructor) {
337 {
338 constexpr float value = 0.1f;
339 constexpr Optional<float> o(std::move(value));
340
341 EXPECT_TRUE(o);
342 EXPECT_EQ(0.1f, o.value());
343 }
344
345 {
346 float value = 0.1f;
347 Optional<float> o(std::move(value));
348
349 EXPECT_TRUE(o);
350 EXPECT_EQ(0.1f, o.value());
351 }
352
353 {
354 std::string value("foo");
355 Optional<std::string> o(std::move(value));
356
357 EXPECT_TRUE(o);
358 EXPECT_EQ("foo", o.value());
359 }
360
361 {
362 TestObject value(3, 0.1);
363 Optional<TestObject> o(std::move(value));
364
365 EXPECT_TRUE(o);
366 EXPECT_EQ(TestObject::State::MOVE_CONSTRUCTED, o->state());
367 EXPECT_EQ(TestObject(3, 0.1), o.value());
368 }
369 }
370
TEST(OptionalTest,ConvertingCopyConstructor)371 TEST(OptionalTest, ConvertingCopyConstructor) {
372 {
373 Optional<int> first(1);
374 Optional<double> second(first);
375 EXPECT_TRUE(second.has_value());
376 EXPECT_EQ(1.0, second.value());
377 }
378
379 // Make sure explicit is not marked for convertible case.
380 {
381 Optional<int> o(1);
382 ignore_result<Optional<double>>(o);
383 }
384 }
385
TEST(OptionalTest,ConvertingMoveConstructor)386 TEST(OptionalTest, ConvertingMoveConstructor) {
387 {
388 Optional<int> first(1);
389 Optional<double> second(std::move(first));
390 EXPECT_TRUE(second.has_value());
391 EXPECT_EQ(1.0, second.value());
392 }
393
394 // Make sure explicit is not marked for convertible case.
395 {
396 Optional<int> o(1);
397 ignore_result<Optional<double>>(std::move(o));
398 }
399
400 {
401 class Test1 {
402 public:
403 explicit Test1(int foo) : foo_(foo) {}
404
405 int foo() const { return foo_; }
406
407 private:
408 int foo_;
409 };
410
411 // Not copyable but convertible from Test1.
412 class Test2 {
413 public:
414 Test2(const Test2&) = delete;
415 explicit Test2(Test1&& other) : bar_(other.foo()) {}
416
417 double bar() const { return bar_; }
418
419 private:
420 double bar_;
421 };
422
423 Optional<Test1> first(in_place, 42);
424 Optional<Test2> second(std::move(first));
425 EXPECT_TRUE(second.has_value());
426 EXPECT_EQ(42.0, second->bar());
427 }
428 }
429
TEST(OptionalTest,ConstructorForwardArguments)430 TEST(OptionalTest, ConstructorForwardArguments) {
431 {
432 constexpr Optional<float> a(base::in_place, 0.1f);
433 EXPECT_TRUE(a);
434 EXPECT_EQ(0.1f, a.value());
435 }
436
437 {
438 Optional<float> a(base::in_place, 0.1f);
439 EXPECT_TRUE(a);
440 EXPECT_EQ(0.1f, a.value());
441 }
442
443 {
444 Optional<std::string> a(base::in_place, "foo");
445 EXPECT_TRUE(a);
446 EXPECT_EQ("foo", a.value());
447 }
448
449 {
450 Optional<TestObject> a(base::in_place, 0, 0.1);
451 EXPECT_TRUE(!!a);
452 EXPECT_TRUE(TestObject(0, 0.1) == a.value());
453 }
454 }
455
TEST(OptionalTest,ConstructorForwardInitListAndArguments)456 TEST(OptionalTest, ConstructorForwardInitListAndArguments) {
457 {
458 Optional<std::vector<int>> opt(in_place, {3, 1});
459 EXPECT_TRUE(opt);
460 EXPECT_THAT(*opt, ElementsAre(3, 1));
461 EXPECT_EQ(2u, opt->size());
462 }
463
464 {
465 Optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>());
466 EXPECT_TRUE(opt);
467 EXPECT_THAT(*opt, ElementsAre(3, 1));
468 EXPECT_EQ(2u, opt->size());
469 }
470 }
471
TEST(OptionalTest,ForwardConstructor)472 TEST(OptionalTest, ForwardConstructor) {
473 {
474 Optional<double> a(1);
475 EXPECT_TRUE(a.has_value());
476 EXPECT_EQ(1.0, a.value());
477 }
478
479 // Test that default type of 'U' is value_type.
480 {
481 struct TestData {
482 int a;
483 double b;
484 bool c;
485 };
486
487 Optional<TestData> a({1, 2.0, true});
488 EXPECT_TRUE(a.has_value());
489 EXPECT_EQ(1, a->a);
490 EXPECT_EQ(2.0, a->b);
491 EXPECT_TRUE(a->c);
492 }
493
494 // If T has a constructor with a param Optional<U>, and another ctor with a
495 // param U, then T(Optional<U>) should be used for Optional<T>(Optional<U>)
496 // constructor.
497 {
498 enum class ParamType {
499 DEFAULT_CONSTRUCTED,
500 COPY_CONSTRUCTED,
501 MOVE_CONSTRUCTED,
502 INT,
503 IN_PLACE,
504 OPTIONAL_INT,
505 };
506 struct Test {
507 Test() : param_type(ParamType::DEFAULT_CONSTRUCTED) {}
508 Test(const Test& param) : param_type(ParamType::COPY_CONSTRUCTED) {}
509 Test(Test&& param) : param_type(ParamType::MOVE_CONSTRUCTED) {}
510 explicit Test(int param) : param_type(ParamType::INT) {}
511 explicit Test(in_place_t param) : param_type(ParamType::IN_PLACE) {}
512 explicit Test(Optional<int> param)
513 : param_type(ParamType::OPTIONAL_INT) {}
514
515 ParamType param_type;
516 };
517
518 // Overload resolution with copy-conversion constructor.
519 {
520 const Optional<int> arg(in_place, 1);
521 Optional<Test> testee(arg);
522 EXPECT_EQ(ParamType::OPTIONAL_INT, testee->param_type);
523 }
524
525 // Overload resolution with move conversion constructor.
526 {
527 Optional<Test> testee(Optional<int>(in_place, 1));
528 EXPECT_EQ(ParamType::OPTIONAL_INT, testee->param_type);
529 }
530
531 // Default constructor should be used.
532 {
533 Optional<Test> testee(in_place);
534 EXPECT_EQ(ParamType::DEFAULT_CONSTRUCTED, testee->param_type);
535 }
536 }
537
538 {
539 struct Test {
540 Test(int a) {} // NOLINT(runtime/explicit)
541 };
542 // If T is convertible from U, it is not marked as explicit.
543 static_assert(std::is_convertible<int, Test>::value,
544 "Int should be convertible to Test.");
545 ([](Optional<Test> param) {})(1);
546 }
547 }
548
TEST(OptionalTest,NulloptConstructor)549 TEST(OptionalTest, NulloptConstructor) {
550 constexpr Optional<int> a(base::nullopt);
551 EXPECT_FALSE(a);
552 }
553
TEST(OptionalTest,AssignValue)554 TEST(OptionalTest, AssignValue) {
555 {
556 Optional<float> a;
557 EXPECT_FALSE(a);
558 a = 0.1f;
559 EXPECT_TRUE(a);
560
561 Optional<float> b(0.1f);
562 EXPECT_TRUE(a == b);
563 }
564
565 {
566 Optional<std::string> a;
567 EXPECT_FALSE(a);
568 a = std::string("foo");
569 EXPECT_TRUE(a);
570
571 Optional<std::string> b(std::string("foo"));
572 EXPECT_EQ(a, b);
573 }
574
575 {
576 Optional<TestObject> a;
577 EXPECT_FALSE(!!a);
578 a = TestObject(3, 0.1);
579 EXPECT_TRUE(!!a);
580
581 Optional<TestObject> b(TestObject(3, 0.1));
582 EXPECT_TRUE(a == b);
583 }
584
585 {
586 Optional<TestObject> a = TestObject(4, 1.0);
587 EXPECT_TRUE(!!a);
588 a = TestObject(3, 0.1);
589 EXPECT_TRUE(!!a);
590
591 Optional<TestObject> b(TestObject(3, 0.1));
592 EXPECT_TRUE(a == b);
593 }
594 }
595
TEST(OptionalTest,AssignObject)596 TEST(OptionalTest, AssignObject) {
597 {
598 Optional<float> a;
599 Optional<float> b(0.1f);
600 a = b;
601
602 EXPECT_TRUE(a);
603 EXPECT_EQ(a.value(), 0.1f);
604 EXPECT_EQ(a, b);
605 }
606
607 {
608 Optional<std::string> a;
609 Optional<std::string> b("foo");
610 a = b;
611
612 EXPECT_TRUE(a);
613 EXPECT_EQ(a.value(), "foo");
614 EXPECT_EQ(a, b);
615 }
616
617 {
618 Optional<TestObject> a;
619 Optional<TestObject> b(TestObject(3, 0.1));
620 a = b;
621
622 EXPECT_TRUE(!!a);
623 EXPECT_TRUE(a.value() == TestObject(3, 0.1));
624 EXPECT_TRUE(a == b);
625 }
626
627 {
628 Optional<TestObject> a(TestObject(4, 1.0));
629 Optional<TestObject> b(TestObject(3, 0.1));
630 a = b;
631
632 EXPECT_TRUE(!!a);
633 EXPECT_TRUE(a.value() == TestObject(3, 0.1));
634 EXPECT_TRUE(a == b);
635 }
636
637 {
638 Optional<DeletedMove> a(in_place, 42);
639 Optional<DeletedMove> b;
640 b = a;
641
642 EXPECT_TRUE(!!a);
643 EXPECT_TRUE(!!b);
644 EXPECT_EQ(a->foo(), b->foo());
645 }
646
647 {
648 Optional<DeletedMove> a(in_place, 42);
649 Optional<DeletedMove> b(in_place, 1);
650 b = a;
651
652 EXPECT_TRUE(!!a);
653 EXPECT_TRUE(!!b);
654 EXPECT_EQ(a->foo(), b->foo());
655 }
656
657 // Converting assignment.
658 {
659 Optional<int> a(in_place, 1);
660 Optional<double> b;
661 b = a;
662
663 EXPECT_TRUE(!!a);
664 EXPECT_TRUE(!!b);
665 EXPECT_EQ(1, a.value());
666 EXPECT_EQ(1.0, b.value());
667 }
668
669 {
670 Optional<int> a(in_place, 42);
671 Optional<double> b(in_place, 1);
672 b = a;
673
674 EXPECT_TRUE(!!a);
675 EXPECT_TRUE(!!b);
676 EXPECT_EQ(42, a.value());
677 EXPECT_EQ(42.0, b.value());
678 }
679
680 {
681 Optional<int> a;
682 Optional<double> b(in_place, 1);
683 b = a;
684 EXPECT_FALSE(!!a);
685 EXPECT_FALSE(!!b);
686 }
687 }
688
TEST(OptionalTest,AssignObject_rvalue)689 TEST(OptionalTest, AssignObject_rvalue) {
690 {
691 Optional<float> a;
692 Optional<float> b(0.1f);
693 a = std::move(b);
694
695 EXPECT_TRUE(a);
696 EXPECT_TRUE(b);
697 EXPECT_EQ(0.1f, a.value());
698 }
699
700 {
701 Optional<std::string> a;
702 Optional<std::string> b("foo");
703 a = std::move(b);
704
705 EXPECT_TRUE(a);
706 EXPECT_TRUE(b);
707 EXPECT_EQ("foo", a.value());
708 }
709
710 {
711 Optional<TestObject> a;
712 Optional<TestObject> b(TestObject(3, 0.1));
713 a = std::move(b);
714
715 EXPECT_TRUE(!!a);
716 EXPECT_TRUE(!!b);
717 EXPECT_TRUE(TestObject(3, 0.1) == a.value());
718
719 EXPECT_EQ(TestObject::State::MOVE_CONSTRUCTED, a->state());
720 EXPECT_EQ(TestObject::State::MOVED_FROM, b->state());
721 }
722
723 {
724 Optional<TestObject> a(TestObject(4, 1.0));
725 Optional<TestObject> b(TestObject(3, 0.1));
726 a = std::move(b);
727
728 EXPECT_TRUE(!!a);
729 EXPECT_TRUE(!!b);
730 EXPECT_TRUE(TestObject(3, 0.1) == a.value());
731
732 EXPECT_EQ(TestObject::State::MOVE_ASSIGNED, a->state());
733 EXPECT_EQ(TestObject::State::MOVED_FROM, b->state());
734 }
735
736 {
737 Optional<DeletedMove> a(in_place, 42);
738 Optional<DeletedMove> b;
739 b = std::move(a);
740
741 EXPECT_TRUE(!!a);
742 EXPECT_TRUE(!!b);
743 EXPECT_EQ(42, b->foo());
744 }
745
746 {
747 Optional<DeletedMove> a(in_place, 42);
748 Optional<DeletedMove> b(in_place, 1);
749 b = std::move(a);
750
751 EXPECT_TRUE(!!a);
752 EXPECT_TRUE(!!b);
753 EXPECT_EQ(42, b->foo());
754 }
755
756 // Converting assignment.
757 {
758 Optional<int> a(in_place, 1);
759 Optional<double> b;
760 b = std::move(a);
761
762 EXPECT_TRUE(!!a);
763 EXPECT_TRUE(!!b);
764 EXPECT_EQ(1.0, b.value());
765 }
766
767 {
768 Optional<int> a(in_place, 42);
769 Optional<double> b(in_place, 1);
770 b = std::move(a);
771
772 EXPECT_TRUE(!!a);
773 EXPECT_TRUE(!!b);
774 EXPECT_EQ(42.0, b.value());
775 }
776
777 {
778 Optional<int> a;
779 Optional<double> b(in_place, 1);
780 b = std::move(a);
781
782 EXPECT_FALSE(!!a);
783 EXPECT_FALSE(!!b);
784 }
785 }
786
TEST(OptionalTest,AssignNull)787 TEST(OptionalTest, AssignNull) {
788 {
789 Optional<float> a(0.1f);
790 Optional<float> b(0.2f);
791 a = base::nullopt;
792 b = base::nullopt;
793 EXPECT_EQ(a, b);
794 }
795
796 {
797 Optional<std::string> a("foo");
798 Optional<std::string> b("bar");
799 a = base::nullopt;
800 b = base::nullopt;
801 EXPECT_EQ(a, b);
802 }
803
804 {
805 Optional<TestObject> a(TestObject(3, 0.1));
806 Optional<TestObject> b(TestObject(4, 1.0));
807 a = base::nullopt;
808 b = base::nullopt;
809 EXPECT_TRUE(a == b);
810 }
811 }
812
TEST(OptionalTest,AssignOverload)813 TEST(OptionalTest, AssignOverload) {
814 struct Test1 {
815 enum class State {
816 CONSTRUCTED,
817 MOVED,
818 };
819 State state = State::CONSTRUCTED;
820 };
821
822 // Here, Optional<Test2> can be assigned from Optioanl<Test1>.
823 // In case of move, marks MOVED to Test1 instance.
824 struct Test2 {
825 enum class State {
826 DEFAULT_CONSTRUCTED,
827 COPY_CONSTRUCTED_FROM_TEST1,
828 MOVE_CONSTRUCTED_FROM_TEST1,
829 COPY_ASSIGNED_FROM_TEST1,
830 MOVE_ASSIGNED_FROM_TEST1,
831 };
832
833 Test2() = default;
834 explicit Test2(const Test1& test1)
835 : state(State::COPY_CONSTRUCTED_FROM_TEST1) {}
836 explicit Test2(Test1&& test1) : state(State::MOVE_CONSTRUCTED_FROM_TEST1) {
837 test1.state = Test1::State::MOVED;
838 }
839 Test2& operator=(const Test1& test1) {
840 state = State::COPY_ASSIGNED_FROM_TEST1;
841 return *this;
842 }
843 Test2& operator=(Test1&& test1) {
844 state = State::MOVE_ASSIGNED_FROM_TEST1;
845 test1.state = Test1::State::MOVED;
846 return *this;
847 }
848
849 State state = State::DEFAULT_CONSTRUCTED;
850 };
851
852 {
853 Optional<Test1> a(in_place);
854 Optional<Test2> b;
855
856 b = a;
857 EXPECT_TRUE(!!a);
858 EXPECT_TRUE(!!b);
859 EXPECT_EQ(Test1::State::CONSTRUCTED, a->state);
860 EXPECT_EQ(Test2::State::COPY_CONSTRUCTED_FROM_TEST1, b->state);
861 }
862
863 {
864 Optional<Test1> a(in_place);
865 Optional<Test2> b(in_place);
866
867 b = a;
868 EXPECT_TRUE(!!a);
869 EXPECT_TRUE(!!b);
870 EXPECT_EQ(Test1::State::CONSTRUCTED, a->state);
871 EXPECT_EQ(Test2::State::COPY_ASSIGNED_FROM_TEST1, b->state);
872 }
873
874 {
875 Optional<Test1> a(in_place);
876 Optional<Test2> b;
877
878 b = std::move(a);
879 EXPECT_TRUE(!!a);
880 EXPECT_TRUE(!!b);
881 EXPECT_EQ(Test1::State::MOVED, a->state);
882 EXPECT_EQ(Test2::State::MOVE_CONSTRUCTED_FROM_TEST1, b->state);
883 }
884
885 {
886 Optional<Test1> a(in_place);
887 Optional<Test2> b(in_place);
888
889 b = std::move(a);
890 EXPECT_TRUE(!!a);
891 EXPECT_TRUE(!!b);
892 EXPECT_EQ(Test1::State::MOVED, a->state);
893 EXPECT_EQ(Test2::State::MOVE_ASSIGNED_FROM_TEST1, b->state);
894 }
895
896 // Similar to Test2, but Test3 also has copy/move ctor and assign operators
897 // from Optional<Test1>, too. In this case, for a = b where a is
898 // Optional<Test3> and b is Optional<Test1>,
899 // Optional<T>::operator=(U&&) where U is Optional<Test1> should be used
900 // rather than Optional<T>::operator=(Optional<U>&&) where U is Test1.
901 struct Test3 {
902 enum class State {
903 DEFAULT_CONSTRUCTED,
904 COPY_CONSTRUCTED_FROM_TEST1,
905 MOVE_CONSTRUCTED_FROM_TEST1,
906 COPY_CONSTRUCTED_FROM_OPTIONAL_TEST1,
907 MOVE_CONSTRUCTED_FROM_OPTIONAL_TEST1,
908 COPY_ASSIGNED_FROM_TEST1,
909 MOVE_ASSIGNED_FROM_TEST1,
910 COPY_ASSIGNED_FROM_OPTIONAL_TEST1,
911 MOVE_ASSIGNED_FROM_OPTIONAL_TEST1,
912 };
913
914 Test3() = default;
915 explicit Test3(const Test1& test1)
916 : state(State::COPY_CONSTRUCTED_FROM_TEST1) {}
917 explicit Test3(Test1&& test1) : state(State::MOVE_CONSTRUCTED_FROM_TEST1) {
918 test1.state = Test1::State::MOVED;
919 }
920 explicit Test3(const Optional<Test1>& test1)
921 : state(State::COPY_CONSTRUCTED_FROM_OPTIONAL_TEST1) {}
922 explicit Test3(Optional<Test1>&& test1)
923 : state(State::MOVE_CONSTRUCTED_FROM_OPTIONAL_TEST1) {
924 // In the following senarios, given |test1| should always have value.
925 DCHECK(test1.has_value());
926 test1->state = Test1::State::MOVED;
927 }
928 Test3& operator=(const Test1& test1) {
929 state = State::COPY_ASSIGNED_FROM_TEST1;
930 return *this;
931 }
932 Test3& operator=(Test1&& test1) {
933 state = State::MOVE_ASSIGNED_FROM_TEST1;
934 test1.state = Test1::State::MOVED;
935 return *this;
936 }
937 Test3& operator=(const Optional<Test1>& test1) {
938 state = State::COPY_ASSIGNED_FROM_OPTIONAL_TEST1;
939 return *this;
940 }
941 Test3& operator=(Optional<Test1>&& test1) {
942 state = State::MOVE_ASSIGNED_FROM_OPTIONAL_TEST1;
943 // In the following senarios, given |test1| should always have value.
944 DCHECK(test1.has_value());
945 test1->state = Test1::State::MOVED;
946 return *this;
947 }
948
949 State state = State::DEFAULT_CONSTRUCTED;
950 };
951
952 {
953 Optional<Test1> a(in_place);
954 Optional<Test3> b;
955
956 b = a;
957 EXPECT_TRUE(!!a);
958 EXPECT_TRUE(!!b);
959 EXPECT_EQ(Test1::State::CONSTRUCTED, a->state);
960 EXPECT_EQ(Test3::State::COPY_CONSTRUCTED_FROM_OPTIONAL_TEST1, b->state);
961 }
962
963 {
964 Optional<Test1> a(in_place);
965 Optional<Test3> b(in_place);
966
967 b = a;
968 EXPECT_TRUE(!!a);
969 EXPECT_TRUE(!!b);
970 EXPECT_EQ(Test1::State::CONSTRUCTED, a->state);
971 EXPECT_EQ(Test3::State::COPY_ASSIGNED_FROM_OPTIONAL_TEST1, b->state);
972 }
973
974 {
975 Optional<Test1> a(in_place);
976 Optional<Test3> b;
977
978 b = std::move(a);
979 EXPECT_TRUE(!!a);
980 EXPECT_TRUE(!!b);
981 EXPECT_EQ(Test1::State::MOVED, a->state);
982 EXPECT_EQ(Test3::State::MOVE_CONSTRUCTED_FROM_OPTIONAL_TEST1, b->state);
983 }
984
985 {
986 Optional<Test1> a(in_place);
987 Optional<Test3> b(in_place);
988
989 b = std::move(a);
990 EXPECT_TRUE(!!a);
991 EXPECT_TRUE(!!b);
992 EXPECT_EQ(Test1::State::MOVED, a->state);
993 EXPECT_EQ(Test3::State::MOVE_ASSIGNED_FROM_OPTIONAL_TEST1, b->state);
994 }
995 }
996
TEST(OptionalTest,OperatorStar)997 TEST(OptionalTest, OperatorStar) {
998 {
999 Optional<float> a(0.1f);
1000 EXPECT_EQ(a.value(), *a);
1001 }
1002
1003 {
1004 Optional<std::string> a("foo");
1005 EXPECT_EQ(a.value(), *a);
1006 }
1007
1008 {
1009 Optional<TestObject> a(TestObject(3, 0.1));
1010 EXPECT_EQ(a.value(), *a);
1011 }
1012 }
1013
TEST(OptionalTest,OperatorStar_rvalue)1014 TEST(OptionalTest, OperatorStar_rvalue) {
1015 EXPECT_EQ(0.1f, *Optional<float>(0.1f));
1016 EXPECT_EQ(std::string("foo"), *Optional<std::string>("foo"));
1017 EXPECT_TRUE(TestObject(3, 0.1) == *Optional<TestObject>(TestObject(3, 0.1)));
1018 }
1019
TEST(OptionalTest,OperatorArrow)1020 TEST(OptionalTest, OperatorArrow) {
1021 Optional<TestObject> a(TestObject(3, 0.1));
1022 EXPECT_EQ(a->foo(), 3);
1023 }
1024
TEST(OptionalTest,Value_rvalue)1025 TEST(OptionalTest, Value_rvalue) {
1026 EXPECT_EQ(0.1f, Optional<float>(0.1f).value());
1027 EXPECT_EQ(std::string("foo"), Optional<std::string>("foo").value());
1028 EXPECT_TRUE(TestObject(3, 0.1) ==
1029 Optional<TestObject>(TestObject(3, 0.1)).value());
1030 }
1031
TEST(OptionalTest,ValueOr)1032 TEST(OptionalTest, ValueOr) {
1033 {
1034 Optional<float> a;
1035 EXPECT_EQ(0.0f, a.value_or(0.0f));
1036
1037 a = 0.1f;
1038 EXPECT_EQ(0.1f, a.value_or(0.0f));
1039
1040 a = base::nullopt;
1041 EXPECT_EQ(0.0f, a.value_or(0.0f));
1042 }
1043
1044 // value_or() can be constexpr.
1045 {
1046 constexpr Optional<int> a(in_place, 1);
1047 constexpr int value = a.value_or(10);
1048 EXPECT_EQ(1, value);
1049 }
1050 {
1051 constexpr Optional<int> a;
1052 constexpr int value = a.value_or(10);
1053 EXPECT_EQ(10, value);
1054 }
1055
1056 {
1057 Optional<std::string> a;
1058 EXPECT_EQ("bar", a.value_or("bar"));
1059
1060 a = std::string("foo");
1061 EXPECT_EQ(std::string("foo"), a.value_or("bar"));
1062
1063 a = base::nullopt;
1064 EXPECT_EQ(std::string("bar"), a.value_or("bar"));
1065 }
1066
1067 {
1068 Optional<TestObject> a;
1069 EXPECT_TRUE(a.value_or(TestObject(1, 0.3)) == TestObject(1, 0.3));
1070
1071 a = TestObject(3, 0.1);
1072 EXPECT_TRUE(a.value_or(TestObject(1, 0.3)) == TestObject(3, 0.1));
1073
1074 a = base::nullopt;
1075 EXPECT_TRUE(a.value_or(TestObject(1, 0.3)) == TestObject(1, 0.3));
1076 }
1077 }
1078
TEST(OptionalTest,Swap_bothNoValue)1079 TEST(OptionalTest, Swap_bothNoValue) {
1080 Optional<TestObject> a, b;
1081 a.swap(b);
1082
1083 EXPECT_FALSE(a);
1084 EXPECT_FALSE(b);
1085 EXPECT_TRUE(TestObject(42, 0.42) == a.value_or(TestObject(42, 0.42)));
1086 EXPECT_TRUE(TestObject(42, 0.42) == b.value_or(TestObject(42, 0.42)));
1087 }
1088
TEST(OptionalTest,Swap_inHasValue)1089 TEST(OptionalTest, Swap_inHasValue) {
1090 Optional<TestObject> a(TestObject(1, 0.3));
1091 Optional<TestObject> b;
1092 a.swap(b);
1093
1094 EXPECT_FALSE(a);
1095
1096 EXPECT_TRUE(!!b);
1097 EXPECT_TRUE(TestObject(42, 0.42) == a.value_or(TestObject(42, 0.42)));
1098 EXPECT_TRUE(TestObject(1, 0.3) == b.value_or(TestObject(42, 0.42)));
1099 }
1100
TEST(OptionalTest,Swap_outHasValue)1101 TEST(OptionalTest, Swap_outHasValue) {
1102 Optional<TestObject> a;
1103 Optional<TestObject> b(TestObject(1, 0.3));
1104 a.swap(b);
1105
1106 EXPECT_TRUE(!!a);
1107 EXPECT_FALSE(!!b);
1108 EXPECT_TRUE(TestObject(1, 0.3) == a.value_or(TestObject(42, 0.42)));
1109 EXPECT_TRUE(TestObject(42, 0.42) == b.value_or(TestObject(42, 0.42)));
1110 }
1111
TEST(OptionalTest,Swap_bothValue)1112 TEST(OptionalTest, Swap_bothValue) {
1113 Optional<TestObject> a(TestObject(0, 0.1));
1114 Optional<TestObject> b(TestObject(1, 0.3));
1115 a.swap(b);
1116
1117 EXPECT_TRUE(!!a);
1118 EXPECT_TRUE(!!b);
1119 EXPECT_TRUE(TestObject(1, 0.3) == a.value_or(TestObject(42, 0.42)));
1120 EXPECT_TRUE(TestObject(0, 0.1) == b.value_or(TestObject(42, 0.42)));
1121 EXPECT_EQ(TestObject::State::SWAPPED, a->state());
1122 EXPECT_EQ(TestObject::State::SWAPPED, b->state());
1123 }
1124
TEST(OptionalTest,Emplace)1125 TEST(OptionalTest, Emplace) {
1126 {
1127 Optional<float> a(0.1f);
1128 EXPECT_EQ(0.3f, a.emplace(0.3f));
1129
1130 EXPECT_TRUE(a);
1131 EXPECT_EQ(0.3f, a.value());
1132 }
1133
1134 {
1135 Optional<std::string> a("foo");
1136 EXPECT_EQ("bar", a.emplace("bar"));
1137
1138 EXPECT_TRUE(a);
1139 EXPECT_EQ("bar", a.value());
1140 }
1141
1142 {
1143 Optional<TestObject> a(TestObject(0, 0.1));
1144 EXPECT_EQ(TestObject(1, 0.2), a.emplace(TestObject(1, 0.2)));
1145
1146 EXPECT_TRUE(!!a);
1147 EXPECT_TRUE(TestObject(1, 0.2) == a.value());
1148 }
1149
1150 {
1151 Optional<std::vector<int>> a;
1152 auto& ref = a.emplace({2, 3});
1153 static_assert(std::is_same<std::vector<int>&, decltype(ref)>::value, "");
1154 EXPECT_TRUE(a);
1155 EXPECT_THAT(*a, ElementsAre(2, 3));
1156 EXPECT_EQ(&ref, &*a);
1157 }
1158
1159 {
1160 Optional<std::vector<int>> a;
1161 auto& ref = a.emplace({4, 5}, std::allocator<int>());
1162 static_assert(std::is_same<std::vector<int>&, decltype(ref)>::value, "");
1163 EXPECT_TRUE(a);
1164 EXPECT_THAT(*a, ElementsAre(4, 5));
1165 EXPECT_EQ(&ref, &*a);
1166 }
1167 }
1168
TEST(OptionalTest,Equals_TwoEmpty)1169 TEST(OptionalTest, Equals_TwoEmpty) {
1170 Optional<int> a;
1171 Optional<int> b;
1172
1173 EXPECT_TRUE(a == b);
1174 }
1175
TEST(OptionalTest,Equals_TwoEquals)1176 TEST(OptionalTest, Equals_TwoEquals) {
1177 Optional<int> a(1);
1178 Optional<int> b(1);
1179
1180 EXPECT_TRUE(a == b);
1181 }
1182
TEST(OptionalTest,Equals_OneEmpty)1183 TEST(OptionalTest, Equals_OneEmpty) {
1184 Optional<int> a;
1185 Optional<int> b(1);
1186
1187 EXPECT_FALSE(a == b);
1188 }
1189
TEST(OptionalTest,Equals_TwoDifferent)1190 TEST(OptionalTest, Equals_TwoDifferent) {
1191 Optional<int> a(0);
1192 Optional<int> b(1);
1193
1194 EXPECT_FALSE(a == b);
1195 }
1196
TEST(OptionalTest,Equals_DifferentType)1197 TEST(OptionalTest, Equals_DifferentType) {
1198 Optional<int> a(0);
1199 Optional<double> b(0);
1200
1201 EXPECT_TRUE(a == b);
1202 }
1203
TEST(OptionalTest,NotEquals_TwoEmpty)1204 TEST(OptionalTest, NotEquals_TwoEmpty) {
1205 Optional<int> a;
1206 Optional<int> b;
1207
1208 EXPECT_FALSE(a != b);
1209 }
1210
TEST(OptionalTest,NotEquals_TwoEquals)1211 TEST(OptionalTest, NotEquals_TwoEquals) {
1212 Optional<int> a(1);
1213 Optional<int> b(1);
1214
1215 EXPECT_FALSE(a != b);
1216 }
1217
TEST(OptionalTest,NotEquals_OneEmpty)1218 TEST(OptionalTest, NotEquals_OneEmpty) {
1219 Optional<int> a;
1220 Optional<int> b(1);
1221
1222 EXPECT_TRUE(a != b);
1223 }
1224
TEST(OptionalTest,NotEquals_TwoDifferent)1225 TEST(OptionalTest, NotEquals_TwoDifferent) {
1226 Optional<int> a(0);
1227 Optional<int> b(1);
1228
1229 EXPECT_TRUE(a != b);
1230 }
1231
TEST(OptionalTest,NotEquals_DifferentType)1232 TEST(OptionalTest, NotEquals_DifferentType) {
1233 Optional<int> a(0);
1234 Optional<double> b(0.0);
1235
1236 EXPECT_FALSE(a != b);
1237 }
1238
TEST(OptionalTest,Less_LeftEmpty)1239 TEST(OptionalTest, Less_LeftEmpty) {
1240 Optional<int> l;
1241 Optional<int> r(1);
1242
1243 EXPECT_TRUE(l < r);
1244 }
1245
TEST(OptionalTest,Less_RightEmpty)1246 TEST(OptionalTest, Less_RightEmpty) {
1247 Optional<int> l(1);
1248 Optional<int> r;
1249
1250 EXPECT_FALSE(l < r);
1251 }
1252
TEST(OptionalTest,Less_BothEmpty)1253 TEST(OptionalTest, Less_BothEmpty) {
1254 Optional<int> l;
1255 Optional<int> r;
1256
1257 EXPECT_FALSE(l < r);
1258 }
1259
TEST(OptionalTest,Less_BothValues)1260 TEST(OptionalTest, Less_BothValues) {
1261 {
1262 Optional<int> l(1);
1263 Optional<int> r(2);
1264
1265 EXPECT_TRUE(l < r);
1266 }
1267 {
1268 Optional<int> l(2);
1269 Optional<int> r(1);
1270
1271 EXPECT_FALSE(l < r);
1272 }
1273 {
1274 Optional<int> l(1);
1275 Optional<int> r(1);
1276
1277 EXPECT_FALSE(l < r);
1278 }
1279 }
1280
TEST(OptionalTest,Less_DifferentType)1281 TEST(OptionalTest, Less_DifferentType) {
1282 Optional<int> l(1);
1283 Optional<double> r(2.0);
1284
1285 EXPECT_TRUE(l < r);
1286 }
1287
TEST(OptionalTest,LessEq_LeftEmpty)1288 TEST(OptionalTest, LessEq_LeftEmpty) {
1289 Optional<int> l;
1290 Optional<int> r(1);
1291
1292 EXPECT_TRUE(l <= r);
1293 }
1294
TEST(OptionalTest,LessEq_RightEmpty)1295 TEST(OptionalTest, LessEq_RightEmpty) {
1296 Optional<int> l(1);
1297 Optional<int> r;
1298
1299 EXPECT_FALSE(l <= r);
1300 }
1301
TEST(OptionalTest,LessEq_BothEmpty)1302 TEST(OptionalTest, LessEq_BothEmpty) {
1303 Optional<int> l;
1304 Optional<int> r;
1305
1306 EXPECT_TRUE(l <= r);
1307 }
1308
TEST(OptionalTest,LessEq_BothValues)1309 TEST(OptionalTest, LessEq_BothValues) {
1310 {
1311 Optional<int> l(1);
1312 Optional<int> r(2);
1313
1314 EXPECT_TRUE(l <= r);
1315 }
1316 {
1317 Optional<int> l(2);
1318 Optional<int> r(1);
1319
1320 EXPECT_FALSE(l <= r);
1321 }
1322 {
1323 Optional<int> l(1);
1324 Optional<int> r(1);
1325
1326 EXPECT_TRUE(l <= r);
1327 }
1328 }
1329
TEST(OptionalTest,LessEq_DifferentType)1330 TEST(OptionalTest, LessEq_DifferentType) {
1331 Optional<int> l(1);
1332 Optional<double> r(2.0);
1333
1334 EXPECT_TRUE(l <= r);
1335 }
1336
TEST(OptionalTest,Greater_BothEmpty)1337 TEST(OptionalTest, Greater_BothEmpty) {
1338 Optional<int> l;
1339 Optional<int> r;
1340
1341 EXPECT_FALSE(l > r);
1342 }
1343
TEST(OptionalTest,Greater_LeftEmpty)1344 TEST(OptionalTest, Greater_LeftEmpty) {
1345 Optional<int> l;
1346 Optional<int> r(1);
1347
1348 EXPECT_FALSE(l > r);
1349 }
1350
TEST(OptionalTest,Greater_RightEmpty)1351 TEST(OptionalTest, Greater_RightEmpty) {
1352 Optional<int> l(1);
1353 Optional<int> r;
1354
1355 EXPECT_TRUE(l > r);
1356 }
1357
TEST(OptionalTest,Greater_BothValue)1358 TEST(OptionalTest, Greater_BothValue) {
1359 {
1360 Optional<int> l(1);
1361 Optional<int> r(2);
1362
1363 EXPECT_FALSE(l > r);
1364 }
1365 {
1366 Optional<int> l(2);
1367 Optional<int> r(1);
1368
1369 EXPECT_TRUE(l > r);
1370 }
1371 {
1372 Optional<int> l(1);
1373 Optional<int> r(1);
1374
1375 EXPECT_FALSE(l > r);
1376 }
1377 }
1378
TEST(OptionalTest,Greater_DifferentType)1379 TEST(OptionalTest, Greater_DifferentType) {
1380 Optional<int> l(1);
1381 Optional<double> r(2.0);
1382
1383 EXPECT_FALSE(l > r);
1384 }
1385
TEST(OptionalTest,GreaterEq_BothEmpty)1386 TEST(OptionalTest, GreaterEq_BothEmpty) {
1387 Optional<int> l;
1388 Optional<int> r;
1389
1390 EXPECT_TRUE(l >= r);
1391 }
1392
TEST(OptionalTest,GreaterEq_LeftEmpty)1393 TEST(OptionalTest, GreaterEq_LeftEmpty) {
1394 Optional<int> l;
1395 Optional<int> r(1);
1396
1397 EXPECT_FALSE(l >= r);
1398 }
1399
TEST(OptionalTest,GreaterEq_RightEmpty)1400 TEST(OptionalTest, GreaterEq_RightEmpty) {
1401 Optional<int> l(1);
1402 Optional<int> r;
1403
1404 EXPECT_TRUE(l >= r);
1405 }
1406
TEST(OptionalTest,GreaterEq_BothValue)1407 TEST(OptionalTest, GreaterEq_BothValue) {
1408 {
1409 Optional<int> l(1);
1410 Optional<int> r(2);
1411
1412 EXPECT_FALSE(l >= r);
1413 }
1414 {
1415 Optional<int> l(2);
1416 Optional<int> r(1);
1417
1418 EXPECT_TRUE(l >= r);
1419 }
1420 {
1421 Optional<int> l(1);
1422 Optional<int> r(1);
1423
1424 EXPECT_TRUE(l >= r);
1425 }
1426 }
1427
TEST(OptionalTest,GreaterEq_DifferentType)1428 TEST(OptionalTest, GreaterEq_DifferentType) {
1429 Optional<int> l(1);
1430 Optional<double> r(2.0);
1431
1432 EXPECT_FALSE(l >= r);
1433 }
1434
TEST(OptionalTest,OptNullEq)1435 TEST(OptionalTest, OptNullEq) {
1436 {
1437 Optional<int> opt;
1438 EXPECT_TRUE(opt == base::nullopt);
1439 }
1440 {
1441 Optional<int> opt(1);
1442 EXPECT_FALSE(opt == base::nullopt);
1443 }
1444 }
1445
TEST(OptionalTest,NullOptEq)1446 TEST(OptionalTest, NullOptEq) {
1447 {
1448 Optional<int> opt;
1449 EXPECT_TRUE(base::nullopt == opt);
1450 }
1451 {
1452 Optional<int> opt(1);
1453 EXPECT_FALSE(base::nullopt == opt);
1454 }
1455 }
1456
TEST(OptionalTest,OptNullNotEq)1457 TEST(OptionalTest, OptNullNotEq) {
1458 {
1459 Optional<int> opt;
1460 EXPECT_FALSE(opt != base::nullopt);
1461 }
1462 {
1463 Optional<int> opt(1);
1464 EXPECT_TRUE(opt != base::nullopt);
1465 }
1466 }
1467
TEST(OptionalTest,NullOptNotEq)1468 TEST(OptionalTest, NullOptNotEq) {
1469 {
1470 Optional<int> opt;
1471 EXPECT_FALSE(base::nullopt != opt);
1472 }
1473 {
1474 Optional<int> opt(1);
1475 EXPECT_TRUE(base::nullopt != opt);
1476 }
1477 }
1478
TEST(OptionalTest,OptNullLower)1479 TEST(OptionalTest, OptNullLower) {
1480 {
1481 Optional<int> opt;
1482 EXPECT_FALSE(opt < base::nullopt);
1483 }
1484 {
1485 Optional<int> opt(1);
1486 EXPECT_FALSE(opt < base::nullopt);
1487 }
1488 }
1489
TEST(OptionalTest,NullOptLower)1490 TEST(OptionalTest, NullOptLower) {
1491 {
1492 Optional<int> opt;
1493 EXPECT_FALSE(base::nullopt < opt);
1494 }
1495 {
1496 Optional<int> opt(1);
1497 EXPECT_TRUE(base::nullopt < opt);
1498 }
1499 }
1500
TEST(OptionalTest,OptNullLowerEq)1501 TEST(OptionalTest, OptNullLowerEq) {
1502 {
1503 Optional<int> opt;
1504 EXPECT_TRUE(opt <= base::nullopt);
1505 }
1506 {
1507 Optional<int> opt(1);
1508 EXPECT_FALSE(opt <= base::nullopt);
1509 }
1510 }
1511
TEST(OptionalTest,NullOptLowerEq)1512 TEST(OptionalTest, NullOptLowerEq) {
1513 {
1514 Optional<int> opt;
1515 EXPECT_TRUE(base::nullopt <= opt);
1516 }
1517 {
1518 Optional<int> opt(1);
1519 EXPECT_TRUE(base::nullopt <= opt);
1520 }
1521 }
1522
TEST(OptionalTest,OptNullGreater)1523 TEST(OptionalTest, OptNullGreater) {
1524 {
1525 Optional<int> opt;
1526 EXPECT_FALSE(opt > base::nullopt);
1527 }
1528 {
1529 Optional<int> opt(1);
1530 EXPECT_TRUE(opt > base::nullopt);
1531 }
1532 }
1533
TEST(OptionalTest,NullOptGreater)1534 TEST(OptionalTest, NullOptGreater) {
1535 {
1536 Optional<int> opt;
1537 EXPECT_FALSE(base::nullopt > opt);
1538 }
1539 {
1540 Optional<int> opt(1);
1541 EXPECT_FALSE(base::nullopt > opt);
1542 }
1543 }
1544
TEST(OptionalTest,OptNullGreaterEq)1545 TEST(OptionalTest, OptNullGreaterEq) {
1546 {
1547 Optional<int> opt;
1548 EXPECT_TRUE(opt >= base::nullopt);
1549 }
1550 {
1551 Optional<int> opt(1);
1552 EXPECT_TRUE(opt >= base::nullopt);
1553 }
1554 }
1555
TEST(OptionalTest,NullOptGreaterEq)1556 TEST(OptionalTest, NullOptGreaterEq) {
1557 {
1558 Optional<int> opt;
1559 EXPECT_TRUE(base::nullopt >= opt);
1560 }
1561 {
1562 Optional<int> opt(1);
1563 EXPECT_FALSE(base::nullopt >= opt);
1564 }
1565 }
1566
TEST(OptionalTest,ValueEq_Empty)1567 TEST(OptionalTest, ValueEq_Empty) {
1568 Optional<int> opt;
1569 EXPECT_FALSE(opt == 1);
1570 }
1571
TEST(OptionalTest,ValueEq_NotEmpty)1572 TEST(OptionalTest, ValueEq_NotEmpty) {
1573 {
1574 Optional<int> opt(0);
1575 EXPECT_FALSE(opt == 1);
1576 }
1577 {
1578 Optional<int> opt(1);
1579 EXPECT_TRUE(opt == 1);
1580 }
1581 }
1582
TEST(OptionalTest,ValueEq_DifferentType)1583 TEST(OptionalTest, ValueEq_DifferentType) {
1584 Optional<int> opt(0);
1585 EXPECT_TRUE(opt == 0.0);
1586 }
1587
TEST(OptionalTest,EqValue_Empty)1588 TEST(OptionalTest, EqValue_Empty) {
1589 Optional<int> opt;
1590 EXPECT_FALSE(1 == opt);
1591 }
1592
TEST(OptionalTest,EqValue_NotEmpty)1593 TEST(OptionalTest, EqValue_NotEmpty) {
1594 {
1595 Optional<int> opt(0);
1596 EXPECT_FALSE(1 == opt);
1597 }
1598 {
1599 Optional<int> opt(1);
1600 EXPECT_TRUE(1 == opt);
1601 }
1602 }
1603
TEST(OptionalTest,EqValue_DifferentType)1604 TEST(OptionalTest, EqValue_DifferentType) {
1605 Optional<int> opt(0);
1606 EXPECT_TRUE(0.0 == opt);
1607 }
1608
TEST(OptionalTest,ValueNotEq_Empty)1609 TEST(OptionalTest, ValueNotEq_Empty) {
1610 Optional<int> opt;
1611 EXPECT_TRUE(opt != 1);
1612 }
1613
TEST(OptionalTest,ValueNotEq_NotEmpty)1614 TEST(OptionalTest, ValueNotEq_NotEmpty) {
1615 {
1616 Optional<int> opt(0);
1617 EXPECT_TRUE(opt != 1);
1618 }
1619 {
1620 Optional<int> opt(1);
1621 EXPECT_FALSE(opt != 1);
1622 }
1623 }
1624
TEST(OPtionalTest,ValueNotEq_DifferentType)1625 TEST(OPtionalTest, ValueNotEq_DifferentType) {
1626 Optional<int> opt(0);
1627 EXPECT_FALSE(opt != 0.0);
1628 }
1629
TEST(OptionalTest,NotEqValue_Empty)1630 TEST(OptionalTest, NotEqValue_Empty) {
1631 Optional<int> opt;
1632 EXPECT_TRUE(1 != opt);
1633 }
1634
TEST(OptionalTest,NotEqValue_NotEmpty)1635 TEST(OptionalTest, NotEqValue_NotEmpty) {
1636 {
1637 Optional<int> opt(0);
1638 EXPECT_TRUE(1 != opt);
1639 }
1640 {
1641 Optional<int> opt(1);
1642 EXPECT_FALSE(1 != opt);
1643 }
1644 }
1645
TEST(OptionalTest,NotEqValue_DifferentType)1646 TEST(OptionalTest, NotEqValue_DifferentType) {
1647 Optional<int> opt(0);
1648 EXPECT_FALSE(0.0 != opt);
1649 }
1650
TEST(OptionalTest,ValueLess_Empty)1651 TEST(OptionalTest, ValueLess_Empty) {
1652 Optional<int> opt;
1653 EXPECT_TRUE(opt < 1);
1654 }
1655
TEST(OptionalTest,ValueLess_NotEmpty)1656 TEST(OptionalTest, ValueLess_NotEmpty) {
1657 {
1658 Optional<int> opt(0);
1659 EXPECT_TRUE(opt < 1);
1660 }
1661 {
1662 Optional<int> opt(1);
1663 EXPECT_FALSE(opt < 1);
1664 }
1665 {
1666 Optional<int> opt(2);
1667 EXPECT_FALSE(opt < 1);
1668 }
1669 }
1670
TEST(OPtionalTest,ValueLess_DifferentType)1671 TEST(OPtionalTest, ValueLess_DifferentType) {
1672 Optional<int> opt(0);
1673 EXPECT_TRUE(opt < 1.0);
1674 }
1675
TEST(OptionalTest,LessValue_Empty)1676 TEST(OptionalTest, LessValue_Empty) {
1677 Optional<int> opt;
1678 EXPECT_FALSE(1 < opt);
1679 }
1680
TEST(OptionalTest,LessValue_NotEmpty)1681 TEST(OptionalTest, LessValue_NotEmpty) {
1682 {
1683 Optional<int> opt(0);
1684 EXPECT_FALSE(1 < opt);
1685 }
1686 {
1687 Optional<int> opt(1);
1688 EXPECT_FALSE(1 < opt);
1689 }
1690 {
1691 Optional<int> opt(2);
1692 EXPECT_TRUE(1 < opt);
1693 }
1694 }
1695
TEST(OptionalTest,LessValue_DifferentType)1696 TEST(OptionalTest, LessValue_DifferentType) {
1697 Optional<int> opt(0);
1698 EXPECT_FALSE(0.0 < opt);
1699 }
1700
TEST(OptionalTest,ValueLessEq_Empty)1701 TEST(OptionalTest, ValueLessEq_Empty) {
1702 Optional<int> opt;
1703 EXPECT_TRUE(opt <= 1);
1704 }
1705
TEST(OptionalTest,ValueLessEq_NotEmpty)1706 TEST(OptionalTest, ValueLessEq_NotEmpty) {
1707 {
1708 Optional<int> opt(0);
1709 EXPECT_TRUE(opt <= 1);
1710 }
1711 {
1712 Optional<int> opt(1);
1713 EXPECT_TRUE(opt <= 1);
1714 }
1715 {
1716 Optional<int> opt(2);
1717 EXPECT_FALSE(opt <= 1);
1718 }
1719 }
1720
TEST(OptionalTest,ValueLessEq_DifferentType)1721 TEST(OptionalTest, ValueLessEq_DifferentType) {
1722 Optional<int> opt(0);
1723 EXPECT_TRUE(opt <= 0.0);
1724 }
1725
TEST(OptionalTest,LessEqValue_Empty)1726 TEST(OptionalTest, LessEqValue_Empty) {
1727 Optional<int> opt;
1728 EXPECT_FALSE(1 <= opt);
1729 }
1730
TEST(OptionalTest,LessEqValue_NotEmpty)1731 TEST(OptionalTest, LessEqValue_NotEmpty) {
1732 {
1733 Optional<int> opt(0);
1734 EXPECT_FALSE(1 <= opt);
1735 }
1736 {
1737 Optional<int> opt(1);
1738 EXPECT_TRUE(1 <= opt);
1739 }
1740 {
1741 Optional<int> opt(2);
1742 EXPECT_TRUE(1 <= opt);
1743 }
1744 }
1745
TEST(OptionalTest,LessEqValue_DifferentType)1746 TEST(OptionalTest, LessEqValue_DifferentType) {
1747 Optional<int> opt(0);
1748 EXPECT_TRUE(0.0 <= opt);
1749 }
1750
TEST(OptionalTest,ValueGreater_Empty)1751 TEST(OptionalTest, ValueGreater_Empty) {
1752 Optional<int> opt;
1753 EXPECT_FALSE(opt > 1);
1754 }
1755
TEST(OptionalTest,ValueGreater_NotEmpty)1756 TEST(OptionalTest, ValueGreater_NotEmpty) {
1757 {
1758 Optional<int> opt(0);
1759 EXPECT_FALSE(opt > 1);
1760 }
1761 {
1762 Optional<int> opt(1);
1763 EXPECT_FALSE(opt > 1);
1764 }
1765 {
1766 Optional<int> opt(2);
1767 EXPECT_TRUE(opt > 1);
1768 }
1769 }
1770
TEST(OptionalTest,ValueGreater_DifferentType)1771 TEST(OptionalTest, ValueGreater_DifferentType) {
1772 Optional<int> opt(0);
1773 EXPECT_FALSE(opt > 0.0);
1774 }
1775
TEST(OptionalTest,GreaterValue_Empty)1776 TEST(OptionalTest, GreaterValue_Empty) {
1777 Optional<int> opt;
1778 EXPECT_TRUE(1 > opt);
1779 }
1780
TEST(OptionalTest,GreaterValue_NotEmpty)1781 TEST(OptionalTest, GreaterValue_NotEmpty) {
1782 {
1783 Optional<int> opt(0);
1784 EXPECT_TRUE(1 > opt);
1785 }
1786 {
1787 Optional<int> opt(1);
1788 EXPECT_FALSE(1 > opt);
1789 }
1790 {
1791 Optional<int> opt(2);
1792 EXPECT_FALSE(1 > opt);
1793 }
1794 }
1795
TEST(OptionalTest,GreaterValue_DifferentType)1796 TEST(OptionalTest, GreaterValue_DifferentType) {
1797 Optional<int> opt(0);
1798 EXPECT_FALSE(0.0 > opt);
1799 }
1800
TEST(OptionalTest,ValueGreaterEq_Empty)1801 TEST(OptionalTest, ValueGreaterEq_Empty) {
1802 Optional<int> opt;
1803 EXPECT_FALSE(opt >= 1);
1804 }
1805
TEST(OptionalTest,ValueGreaterEq_NotEmpty)1806 TEST(OptionalTest, ValueGreaterEq_NotEmpty) {
1807 {
1808 Optional<int> opt(0);
1809 EXPECT_FALSE(opt >= 1);
1810 }
1811 {
1812 Optional<int> opt(1);
1813 EXPECT_TRUE(opt >= 1);
1814 }
1815 {
1816 Optional<int> opt(2);
1817 EXPECT_TRUE(opt >= 1);
1818 }
1819 }
1820
TEST(OptionalTest,ValueGreaterEq_DifferentType)1821 TEST(OptionalTest, ValueGreaterEq_DifferentType) {
1822 Optional<int> opt(0);
1823 EXPECT_TRUE(opt <= 0.0);
1824 }
1825
TEST(OptionalTest,GreaterEqValue_Empty)1826 TEST(OptionalTest, GreaterEqValue_Empty) {
1827 Optional<int> opt;
1828 EXPECT_TRUE(1 >= opt);
1829 }
1830
TEST(OptionalTest,GreaterEqValue_NotEmpty)1831 TEST(OptionalTest, GreaterEqValue_NotEmpty) {
1832 {
1833 Optional<int> opt(0);
1834 EXPECT_TRUE(1 >= opt);
1835 }
1836 {
1837 Optional<int> opt(1);
1838 EXPECT_TRUE(1 >= opt);
1839 }
1840 {
1841 Optional<int> opt(2);
1842 EXPECT_FALSE(1 >= opt);
1843 }
1844 }
1845
TEST(OptionalTest,GreaterEqValue_DifferentType)1846 TEST(OptionalTest, GreaterEqValue_DifferentType) {
1847 Optional<int> opt(0);
1848 EXPECT_TRUE(0.0 >= opt);
1849 }
1850
TEST(OptionalTest,NotEquals)1851 TEST(OptionalTest, NotEquals) {
1852 {
1853 Optional<float> a(0.1f);
1854 Optional<float> b(0.2f);
1855 EXPECT_NE(a, b);
1856 }
1857
1858 {
1859 Optional<std::string> a("foo");
1860 Optional<std::string> b("bar");
1861 EXPECT_NE(a, b);
1862 }
1863
1864 {
1865 Optional<int> a(1);
1866 Optional<double> b(2);
1867 EXPECT_NE(a, b);
1868 }
1869
1870 {
1871 Optional<TestObject> a(TestObject(3, 0.1));
1872 Optional<TestObject> b(TestObject(4, 1.0));
1873 EXPECT_TRUE(a != b);
1874 }
1875 }
1876
TEST(OptionalTest,NotEqualsNull)1877 TEST(OptionalTest, NotEqualsNull) {
1878 {
1879 Optional<float> a(0.1f);
1880 Optional<float> b(0.1f);
1881 b = base::nullopt;
1882 EXPECT_NE(a, b);
1883 }
1884
1885 {
1886 Optional<std::string> a("foo");
1887 Optional<std::string> b("foo");
1888 b = base::nullopt;
1889 EXPECT_NE(a, b);
1890 }
1891
1892 {
1893 Optional<TestObject> a(TestObject(3, 0.1));
1894 Optional<TestObject> b(TestObject(3, 0.1));
1895 b = base::nullopt;
1896 EXPECT_TRUE(a != b);
1897 }
1898 }
1899
TEST(OptionalTest,MakeOptional)1900 TEST(OptionalTest, MakeOptional) {
1901 {
1902 // Use qualified base::make_optional here and elsewhere to avoid the name
1903 // confliction to std::make_optional.
1904 // The name conflict happens only for types in std namespace, such as
1905 // std::string. The other qualified base::make_optional usages are just for
1906 // consistency.
1907 Optional<float> o = base::make_optional(32.f);
1908 EXPECT_TRUE(o);
1909 EXPECT_EQ(32.f, *o);
1910
1911 float value = 3.f;
1912 o = base::make_optional(std::move(value));
1913 EXPECT_TRUE(o);
1914 EXPECT_EQ(3.f, *o);
1915 }
1916
1917 {
1918 Optional<std::string> o = base::make_optional(std::string("foo"));
1919 EXPECT_TRUE(o);
1920 EXPECT_EQ("foo", *o);
1921
1922 std::string value = "bar";
1923 o = base::make_optional(std::move(value));
1924 EXPECT_TRUE(o);
1925 EXPECT_EQ(std::string("bar"), *o);
1926 }
1927
1928 {
1929 Optional<TestObject> o = base::make_optional(TestObject(3, 0.1));
1930 EXPECT_TRUE(!!o);
1931 EXPECT_TRUE(TestObject(3, 0.1) == *o);
1932
1933 TestObject value = TestObject(0, 0.42);
1934 o = base::make_optional(std::move(value));
1935 EXPECT_TRUE(!!o);
1936 EXPECT_TRUE(TestObject(0, 0.42) == *o);
1937 EXPECT_EQ(TestObject::State::MOVED_FROM, value.state());
1938 EXPECT_EQ(TestObject::State::MOVE_ASSIGNED, o->state());
1939
1940 EXPECT_EQ(TestObject::State::MOVE_CONSTRUCTED,
1941 base::make_optional(std::move(value))->state());
1942 }
1943
1944 {
1945 struct Test {
1946 Test(int a, double b, bool c) : a(a), b(b), c(c) {}
1947
1948 int a;
1949 double b;
1950 bool c;
1951 };
1952
1953 Optional<Test> o = base::make_optional<Test>(1, 2.0, true);
1954 EXPECT_TRUE(!!o);
1955 EXPECT_EQ(1, o->a);
1956 EXPECT_EQ(2.0, o->b);
1957 EXPECT_TRUE(o->c);
1958 }
1959
1960 {
1961 auto str1 = base::make_optional<std::string>({'1', '2', '3'});
1962 EXPECT_EQ("123", *str1);
1963
1964 auto str2 = base::make_optional<std::string>({'a', 'b', 'c'},
1965 std::allocator<char>());
1966 EXPECT_EQ("abc", *str2);
1967 }
1968 }
1969
TEST(OptionalTest,NonMemberSwap_bothNoValue)1970 TEST(OptionalTest, NonMemberSwap_bothNoValue) {
1971 Optional<TestObject> a, b;
1972 base::swap(a, b);
1973
1974 EXPECT_FALSE(!!a);
1975 EXPECT_FALSE(!!b);
1976 EXPECT_TRUE(TestObject(42, 0.42) == a.value_or(TestObject(42, 0.42)));
1977 EXPECT_TRUE(TestObject(42, 0.42) == b.value_or(TestObject(42, 0.42)));
1978 }
1979
TEST(OptionalTest,NonMemberSwap_inHasValue)1980 TEST(OptionalTest, NonMemberSwap_inHasValue) {
1981 Optional<TestObject> a(TestObject(1, 0.3));
1982 Optional<TestObject> b;
1983 base::swap(a, b);
1984
1985 EXPECT_FALSE(!!a);
1986 EXPECT_TRUE(!!b);
1987 EXPECT_TRUE(TestObject(42, 0.42) == a.value_or(TestObject(42, 0.42)));
1988 EXPECT_TRUE(TestObject(1, 0.3) == b.value_or(TestObject(42, 0.42)));
1989 }
1990
TEST(OptionalTest,NonMemberSwap_outHasValue)1991 TEST(OptionalTest, NonMemberSwap_outHasValue) {
1992 Optional<TestObject> a;
1993 Optional<TestObject> b(TestObject(1, 0.3));
1994 base::swap(a, b);
1995
1996 EXPECT_TRUE(!!a);
1997 EXPECT_FALSE(!!b);
1998 EXPECT_TRUE(TestObject(1, 0.3) == a.value_or(TestObject(42, 0.42)));
1999 EXPECT_TRUE(TestObject(42, 0.42) == b.value_or(TestObject(42, 0.42)));
2000 }
2001
TEST(OptionalTest,NonMemberSwap_bothValue)2002 TEST(OptionalTest, NonMemberSwap_bothValue) {
2003 Optional<TestObject> a(TestObject(0, 0.1));
2004 Optional<TestObject> b(TestObject(1, 0.3));
2005 base::swap(a, b);
2006
2007 EXPECT_TRUE(!!a);
2008 EXPECT_TRUE(!!b);
2009 EXPECT_TRUE(TestObject(1, 0.3) == a.value_or(TestObject(42, 0.42)));
2010 EXPECT_TRUE(TestObject(0, 0.1) == b.value_or(TestObject(42, 0.42)));
2011 EXPECT_EQ(TestObject::State::SWAPPED, a->state());
2012 EXPECT_EQ(TestObject::State::SWAPPED, b->state());
2013 }
2014
TEST(OptionalTest,Hash_OptionalReflectsInternal)2015 TEST(OptionalTest, Hash_OptionalReflectsInternal) {
2016 {
2017 std::hash<int> int_hash;
2018 std::hash<Optional<int>> opt_int_hash;
2019
2020 EXPECT_EQ(int_hash(1), opt_int_hash(Optional<int>(1)));
2021 }
2022
2023 {
2024 std::hash<std::string> str_hash;
2025 std::hash<Optional<std::string>> opt_str_hash;
2026
2027 EXPECT_EQ(str_hash(std::string("foobar")),
2028 opt_str_hash(Optional<std::string>(std::string("foobar"))));
2029 }
2030 }
2031
TEST(OptionalTest,Hash_NullOptEqualsNullOpt)2032 TEST(OptionalTest, Hash_NullOptEqualsNullOpt) {
2033 std::hash<Optional<int>> opt_int_hash;
2034 std::hash<Optional<std::string>> opt_str_hash;
2035
2036 EXPECT_EQ(opt_str_hash(Optional<std::string>()),
2037 opt_int_hash(Optional<int>()));
2038 }
2039
TEST(OptionalTest,Hash_UseInSet)2040 TEST(OptionalTest, Hash_UseInSet) {
2041 std::set<Optional<int>> setOptInt;
2042
2043 EXPECT_EQ(setOptInt.end(), setOptInt.find(42));
2044
2045 setOptInt.insert(Optional<int>(3));
2046 EXPECT_EQ(setOptInt.end(), setOptInt.find(42));
2047 EXPECT_NE(setOptInt.end(), setOptInt.find(3));
2048 }
2049
TEST(OptionalTest,HasValue)2050 TEST(OptionalTest, HasValue) {
2051 Optional<int> a;
2052 EXPECT_FALSE(a.has_value());
2053
2054 a = 42;
2055 EXPECT_TRUE(a.has_value());
2056
2057 a = nullopt;
2058 EXPECT_FALSE(a.has_value());
2059
2060 a = 0;
2061 EXPECT_TRUE(a.has_value());
2062
2063 a = Optional<int>();
2064 EXPECT_FALSE(a.has_value());
2065 }
2066
TEST(OptionalTest,Reset_int)2067 TEST(OptionalTest, Reset_int) {
2068 Optional<int> a(0);
2069 EXPECT_TRUE(a.has_value());
2070 EXPECT_EQ(0, a.value());
2071
2072 a.reset();
2073 EXPECT_FALSE(a.has_value());
2074 EXPECT_EQ(-1, a.value_or(-1));
2075 }
2076
TEST(OptionalTest,Reset_Object)2077 TEST(OptionalTest, Reset_Object) {
2078 Optional<TestObject> a(TestObject(0, 0.1));
2079 EXPECT_TRUE(a.has_value());
2080 EXPECT_EQ(TestObject(0, 0.1), a.value());
2081
2082 a.reset();
2083 EXPECT_FALSE(a.has_value());
2084 EXPECT_EQ(TestObject(42, 0.0), a.value_or(TestObject(42, 0.0)));
2085 }
2086
TEST(OptionalTest,Reset_NoOp)2087 TEST(OptionalTest, Reset_NoOp) {
2088 Optional<int> a;
2089 EXPECT_FALSE(a.has_value());
2090
2091 a.reset();
2092 EXPECT_FALSE(a.has_value());
2093 }
2094
TEST(OptionalTest,AssignFromRValue)2095 TEST(OptionalTest, AssignFromRValue) {
2096 Optional<TestObject> a;
2097 EXPECT_FALSE(a.has_value());
2098
2099 TestObject obj;
2100 a = std::move(obj);
2101 EXPECT_TRUE(a.has_value());
2102 EXPECT_EQ(1, a->move_ctors_count());
2103 }
2104
TEST(OptionalTest,DontCallDefaultCtor)2105 TEST(OptionalTest, DontCallDefaultCtor) {
2106 Optional<DeletedDefaultConstructor> a;
2107 EXPECT_FALSE(a.has_value());
2108
2109 a = base::make_optional<DeletedDefaultConstructor>(42);
2110 EXPECT_TRUE(a.has_value());
2111 EXPECT_EQ(42, a->foo());
2112 }
2113
TEST(OptionalTest,DontCallNewMemberFunction)2114 TEST(OptionalTest, DontCallNewMemberFunction) {
2115 Optional<DeleteNewOperators> a;
2116 EXPECT_FALSE(a.has_value());
2117
2118 a = DeleteNewOperators();
2119 EXPECT_TRUE(a.has_value());
2120 }
2121
TEST(OptionalTest,Noexcept)2122 TEST(OptionalTest, Noexcept) {
2123 // Trivial copy ctor, non-trivial move ctor, nothrow move assign.
2124 struct Test1 {
2125 Test1(const Test1&) = default;
2126 Test1(Test1&&) {}
2127 Test1& operator=(Test1&&) = default;
2128 };
2129 // Non-trivial copy ctor, trivial move ctor, throw move assign.
2130 struct Test2 {
2131 Test2(const Test2&) {}
2132 Test2(Test2&&) = default;
2133 Test2& operator=(Test2&&) { return *this; }
2134 };
2135 // Trivial copy ctor, non-trivial nothrow move ctor.
2136 struct Test3 {
2137 Test3(const Test3&) = default;
2138 Test3(Test3&&) noexcept {}
2139 };
2140 // Non-trivial copy ctor, non-trivial nothrow move ctor.
2141 struct Test4 {
2142 Test4(const Test4&) {}
2143 Test4(Test4&&) noexcept {}
2144 };
2145 // Non-trivial copy ctor, non-trivial move ctor.
2146 struct Test5 {
2147 Test5(const Test5&) {}
2148 Test5(Test5&&) {}
2149 };
2150
2151 static_assert(
2152 noexcept(Optional<int>(std::declval<Optional<int>>())),
2153 "move constructor for noexcept move-constructible T must be noexcept "
2154 "(trivial copy, trivial move)");
2155 static_assert(
2156 !noexcept(Optional<Test1>(std::declval<Optional<Test1>>())),
2157 "move constructor for non-noexcept move-constructible T must not be "
2158 "noexcept (trivial copy)");
2159 static_assert(
2160 noexcept(Optional<Test2>(std::declval<Optional<Test2>>())),
2161 "move constructor for noexcept move-constructible T must be noexcept "
2162 "(non-trivial copy, trivial move)");
2163 static_assert(
2164 noexcept(Optional<Test3>(std::declval<Optional<Test3>>())),
2165 "move constructor for noexcept move-constructible T must be noexcept "
2166 "(trivial copy, non-trivial move)");
2167 static_assert(
2168 noexcept(Optional<Test4>(std::declval<Optional<Test4>>())),
2169 "move constructor for noexcept move-constructible T must be noexcept "
2170 "(non-trivial copy, non-trivial move)");
2171 static_assert(
2172 !noexcept(Optional<Test5>(std::declval<Optional<Test5>>())),
2173 "move constructor for non-noexcept move-constructible T must not be "
2174 "noexcept (non-trivial copy)");
2175
2176 static_assert(
2177 noexcept(std::declval<Optional<int>>() = std::declval<Optional<int>>()),
2178 "move assign for noexcept move-constructible/move-assignable T "
2179 "must be noexcept");
2180 static_assert(
2181 !noexcept(std::declval<Optional<Test1>>() =
2182 std::declval<Optional<Test1>>()),
2183 "move assign for non-noexcept move-constructible T must not be noexcept");
2184 static_assert(
2185 !noexcept(std::declval<Optional<Test2>>() =
2186 std::declval<Optional<Test2>>()),
2187 "move assign for non-noexcept move-assignable T must not be noexcept");
2188 }
2189
2190 } // namespace base
2191