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