Brian Silverman | da86135 | 2019-02-02 16:42:28 -0800 | [diff] [blame^] | 1 | #include "catch.hpp" |
| 2 | #include "optional.hpp" |
| 3 | |
| 4 | struct move_detector { |
| 5 | move_detector() = default; |
| 6 | move_detector(move_detector &&rhs) { rhs.been_moved = true; } |
| 7 | bool been_moved = false; |
| 8 | }; |
| 9 | |
| 10 | TEST_CASE("Observers", "[observers]") { |
| 11 | tl::optional<int> o1 = 42; |
| 12 | tl::optional<int> o2; |
| 13 | const tl::optional<int> o3 = 42; |
| 14 | |
| 15 | REQUIRE(*o1 == 42); |
| 16 | REQUIRE(*o1 == o1.value()); |
| 17 | REQUIRE(o2.value_or(42) == 42); |
| 18 | REQUIRE(o3.value() == 42); |
| 19 | auto success = std::is_same<decltype(o1.value()), int &>::value; |
| 20 | REQUIRE(success); |
| 21 | success = std::is_same<decltype(o3.value()), const int &>::value; |
| 22 | REQUIRE(success); |
| 23 | success = std::is_same<decltype(std::move(o1).value()), int &&>::value; |
| 24 | REQUIRE(success); |
| 25 | |
| 26 | #ifndef TL_OPTIONAL_NO_CONSTRR |
| 27 | success = std::is_same<decltype(std::move(o3).value()), const int &&>::value; |
| 28 | REQUIRE(success); |
| 29 | #endif |
| 30 | |
| 31 | tl::optional<move_detector> o4{tl::in_place}; |
| 32 | move_detector o5 = std::move(o4).value(); |
| 33 | REQUIRE(o4->been_moved); |
| 34 | REQUIRE(!o5.been_moved); |
| 35 | } |