Update TestNullable.cpp

This commit is contained in:
Mateusz Karbowiak
2024-10-07 12:24:09 +02:00
parent f56207f1a4
commit eda4f433d0

View File

@@ -7,7 +7,7 @@ TEST_CASE("Nullable")
{ {
SECTION("Trivial Type") SECTION("Trivial Type")
{ {
Nullable<int> a; Nullable<int> a;
REQUIRE(a.HasValue() == false); REQUIRE(a.HasValue() == false);
REQUIRE(a.GetValueOr(2) == 2); REQUIRE(a.GetValueOr(2) == 2);
@@ -25,130 +25,139 @@ TEST_CASE("Nullable")
SECTION("Move-Only Type") SECTION("Move-Only Type")
{ {
struct MoveOnly struct MoveOnly
{ {
MoveOnly() = default; MoveOnly() = default;
~MoveOnly() = default; ~MoveOnly() = default;
MoveOnly(const MoveOnly&) = delete; MoveOnly(const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) = default; MoveOnly(MoveOnly&&) = default;
// MoveOnly& operator=(const MoveOnly&) = delete; MoveOnly& operator=(const MoveOnly&) = delete;
// MoveOnly& operator=(MoveOnly&&) = default; MoveOnly& operator=(MoveOnly&&) = default;
}; };
Nullable<MoveOnly> a; Nullable<MoveOnly> a;
REQUIRE(a.HasValue() == false); REQUIRE(a.HasValue() == false);
a = MoveOnly(); a = MoveOnly();
REQUIRE(a.HasValue() == true); REQUIRE(a.HasValue() == true);
} }
SECTION("Bool Type") SECTION("Bool Type")
{ {
Nullable<bool> a;
Nullable<bool> a;
REQUIRE(a.HasValue() == false); REQUIRE(a.HasValue() == false);
REQUIRE(a.GetValueOr(true) == true); REQUIRE(a.GetValueOr(true) == true);
REQUIRE(a.IsTrue() == false); REQUIRE(a.IsTrue() == false);
REQUIRE(a.IsFalse() == false); REQUIRE(a.IsFalse() == false);
a = false; a = false;
REQUIRE(a.HasValue() == true); REQUIRE(a.HasValue() == true);
REQUIRE(a.GetValue() == false); REQUIRE(a.GetValue() == false);
REQUIRE(a.GetValueOr(true) == false); REQUIRE(a.GetValueOr(true) == false);
REQUIRE(a.IsTrue() == false); REQUIRE(a.IsTrue() == false);
REQUIRE(a.IsFalse() == true); REQUIRE(a.IsFalse() == true);
a = true; a = true;
REQUIRE(a.IsTrue() == true); REQUIRE(a.IsTrue() == true);
REQUIRE(a.IsFalse() == false); REQUIRE(a.IsFalse() == false);
a.Reset(); a.Reset();
REQUIRE(a.HasValue() == false); REQUIRE(a.HasValue() == false);
} }
SECTION("Lifetime (No Construction)") SECTION("Lifetime (No Construction)")
{ {
struct DoNotConstruct struct DoNotConstruct
{ {
DoNotConstruct() { FAIL("DoNotConstruct must not be constructed."); } DoNotConstruct() { FAIL("DoNotConstruct must not be constructed."); }
}; };
Nullable<DoNotConstruct> a; Nullable<DoNotConstruct> a;
a.Reset(); a.Reset();
} }
SECTION("Lifetime") SECTION("Lifetime")
{ {
int constructed = 0, destructed = 0;
struct Lifetime struct Lifetime
{ {
int& constructed; int* _constructed;
int& destructed; int* _destructed;
Lifetime(int& constructed, int& destructed) Lifetime(int* constructed, int* destructed)
: constructed(constructed) : _constructed(constructed)
, destructed(destructed) , _destructed(destructed)
{ {
constructed++; ++(*_constructed);
} }
Lifetime(const Lifetime& other) Lifetime(Lifetime&& other) noexcept
: constructed(other.constructed) : _constructed(other._constructed)
, destructed(other.destructed) , _destructed(other._destructed)
{ {
constructed++; ++(*_constructed);
} }
~Lifetime() Lifetime() = delete;
{ Lifetime& operator=(const Lifetime&) = delete;
destructed++; Lifetime& operator=(Lifetime&&) = delete;
}
};
{ ~Lifetime()
Nullable<Lifetime> a = Lifetime(constructed, destructed); {
++(*_destructed);
}
};
REQUIRE(constructed == 1); int constructed = 0, destructed = 0;
REQUIRE(destructed == 0); REQUIRE(constructed == destructed);
a.Reset(); {
REQUIRE(constructed == 1); Nullable<Lifetime> a = Lifetime(&constructed, &destructed);
REQUIRE(destructed == 1); REQUIRE(a.HasValue());
} REQUIRE(constructed == destructed + 1);
{ a.Reset();
Nullable<Lifetime> a = Lifetime(constructed, destructed); REQUIRE(!a.HasValue());
} REQUIRE(constructed == destructed);
}
REQUIRE(constructed == destructed);
REQUIRE(constructed == 2); {
REQUIRE(destructed == 2); Nullable<Lifetime> b = Lifetime(&constructed, &destructed);
} REQUIRE(constructed == destructed + 1);
}
REQUIRE(constructed == destructed);
{
Nullable<Lifetime> c = Lifetime(&constructed, &destructed);
Nullable<Lifetime> d = MoveTemp(c);
REQUIRE(constructed == destructed + 1);
}
REQUIRE(constructed == destructed);
}
SECTION("Matching") SECTION("Matching")
{ {
Nullable<int> a; Nullable<int> a;
Nullable<int> b = 2; Nullable<int> b = 2;
a.Match( a.Match(
[](int value) { FAIL("Null nullable must not match value handler."); }, [](int) { FAIL("Null nullable must not match value handler."); },
[]() {} []() {}
); );
b.Match( b.Match(
[](int value) {}, [](int) {},
[]() { FAIL("Nullable with valid value must not match null handler."); } []() { FAIL("Nullable with valid value must not match null handler."); }
); );
} }
}; };