diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-22 18:15:40 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | defa7e1b9e16c6276e17a39b61d019c1a116472b (patch) | |
tree | cd7534d45a567987030ad4ee602fd2cf96c4f79c /asl/tests/option_tests.cpp | |
parent | 58bb86fd2f0ecd740fc5bc8078de44221c12c70a (diff) |
Use niche in option
Diffstat (limited to 'asl/tests/option_tests.cpp')
-rw-r--r-- | asl/tests/option_tests.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/asl/tests/option_tests.cpp b/asl/tests/option_tests.cpp index 4c65892..8b98c99 100644 --- a/asl/tests/option_tests.cpp +++ b/asl/tests/option_tests.cpp @@ -5,6 +5,25 @@ class Base {};
class Derived : public Base {};
+struct NonZero
+{
+ int value;
+
+ constexpr explicit NonZero(int x) : value(x)
+ {
+ ASL_ASSERT(x != 0);
+ }
+
+ constexpr explicit NonZero(asl::niche) : value(0) {}
+
+ constexpr bool operator==(asl::niche) const { return value == 0; }
+};
+static_assert(asl::has_niche<NonZero>);
+static_assert(!asl::has_niche<int>);
+
+static_assert(sizeof(asl::option<int>) > sizeof(int));
+static_assert(sizeof(asl::option<NonZero>) == sizeof(NonZero));
+
static_assert(!asl::is_option<int>); static_assert(asl::is_option<asl::option<int>>);
static_assert(asl::is_option<const asl::option<int>>);
@@ -276,3 +295,17 @@ ASL_TEST(or_else) ASL_TEST_ASSERT(b2.has_value());
ASL_TEST_EXPECT(b2.value() == 12);
}
+
+ASL_TEST(niche)
+{
+ asl::option<NonZero> opt;
+ ASL_TEST_EXPECT(!opt.has_value());
+
+ asl::option<NonZero> opt2(2);
+ ASL_TEST_EXPECT(opt2.has_value());
+ ASL_TEST_EXPECT(opt2.value().value == 2);
+
+ opt = opt2;
+ ASL_TEST_EXPECT(opt2.has_value());
+ ASL_TEST_EXPECT(opt2.value().value == 2);
+}
|