#include "asl/option.hpp" #include "asl/tests/test_types.hpp" #include "asl/testing/testing.hpp" class Base {}; class Derived : public Base {}; static_assert(!asl::is_option); static_assert(asl::is_option>); static_assert(asl::is_option>); static_assert(asl::trivially_destructible>); static_assert(!asl::trivially_destructible>); static_assert(asl::copy_constructible>); static_assert(asl::copy_constructible>); static_assert(!asl::copy_constructible>); static_assert(!asl::copy_constructible>); static_assert(asl::move_constructible>); static_assert(asl::move_constructible>); static_assert(asl::move_constructible>); static_assert(!asl::move_constructible>); static_assert(asl::copy_assignable>); static_assert(asl::copy_assignable>); static_assert(!asl::copy_assignable>); static_assert(!asl::copy_assignable>); static_assert(asl::move_assignable>); static_assert(asl::move_assignable>); static_assert(asl::move_assignable>); static_assert(!asl::move_assignable>); static_assert(asl::assignable&, asl::option>); static_assert(!asl::assignable&, asl::option>); static_assert(!asl::convertible, asl::option>); static_assert(asl::convertible, asl::option>); class ExplicitConversion { public: explicit ExplicitConversion(int) {} }; class ImplicitConversion { public: ImplicitConversion(int) {} }; // NOLINT static_assert(!asl::convertible); static_assert(asl::convertible); static_assert(!asl::convertible>); static_assert(asl::convertible>); static_assert(!asl::convertible, asl::option>); static_assert(asl::convertible, asl::option>); static_assert(asl::trivially_copy_constructible>); static_assert(!asl::trivially_copy_constructible>); static_assert(asl::trivially_move_constructible>); static_assert(!asl::trivially_move_constructible>); static_assert(asl::trivially_copy_assignable>); static_assert(!asl::trivially_copy_assignable>); static_assert(asl::trivially_move_assignable>); static_assert(!asl::trivially_move_assignable>); ASL_TEST(make_null) { asl::option a; asl::option b = asl::nullopt; ASL_TEST_EXPECT(!a.has_value()); ASL_TEST_EXPECT(!b.has_value()); } ASL_TEST(make_value) { asl::option a = 48; ASL_TEST_EXPECT(a.has_value()); } ASL_TEST(reset) { asl::option b = 48; ASL_TEST_EXPECT(b.has_value()); b.reset(); ASL_TEST_EXPECT(!b.has_value()); } ASL_TEST(call_destructor) { bool destroyed = false; { DestructorObserver obs(&destroyed); asl::option opt(ASL_MOVE(obs)); ASL_TEST_EXPECT(!destroyed); asl::option opt2 = ASL_MOVE(opt); ASL_TEST_EXPECT(!destroyed); } ASL_TEST_EXPECT(destroyed); } ASL_TEST(call_destructor_on_reset) { bool destroyed = false; asl::option opt(&destroyed); ASL_TEST_EXPECT(!destroyed); opt.reset(); ASL_TEST_EXPECT(destroyed); } ASL_TEST(value) { asl::option a = 1; asl::option b = 2; asl::option c = a; ASL_TEST_EXPECT(a.value() == 1); ASL_TEST_EXPECT(b.value() == 2); ASL_TEST_EXPECT(c.value() == 1); c = b; ASL_TEST_EXPECT(c.value() == 2); } ASL_TEST(value_move) { bool destroyed = false; asl::option opt(&destroyed); ASL_TEST_EXPECT(!destroyed); { auto x = ASL_MOVE(opt).value(); ASL_TEST_EXPECT(!destroyed); } ASL_TEST_EXPECT(destroyed); } ASL_TEST(deduction_guide) { asl::option opt(45); ASL_TEST_EXPECT(opt.value() == 45); } ASL_TEST(convert_copy) { asl::option opt8 = uint8_t{8}; asl::option opt16 = opt8; ASL_TEST_EXPECT(opt16.has_value()); ASL_TEST_EXPECT(opt16.value() == 8); opt8 = uint8_t{10}; ASL_TEST_EXPECT(opt8.has_value()); ASL_TEST_EXPECT(opt8.value() == 10); opt16 = asl::nullopt; ASL_TEST_EXPECT(!opt16.has_value()); opt16 = opt8; ASL_TEST_EXPECT(opt16.has_value()); ASL_TEST_EXPECT(opt16.value() == 10); } ASL_TEST(convert_move) { asl::option opt8 = uint8_t{8}; asl::option opt16 = ASL_MOVE(opt8); ASL_TEST_EXPECT(opt16.has_value()); ASL_TEST_EXPECT(opt16.value() == 8); opt8 = ASL_MOVE(uint8_t{10}); ASL_TEST_EXPECT(opt8.has_value()); ASL_TEST_EXPECT(opt8.value() == 10); opt16 = asl::nullopt; ASL_TEST_EXPECT(!opt16.has_value()); opt16 = ASL_MOVE(opt8); ASL_TEST_EXPECT(opt16.has_value()); ASL_TEST_EXPECT(opt16.value() == 10); }