Update TestNullable.cpp

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

View File

@@ -33,8 +33,8 @@ TEST_CASE("Nullable")
MoveOnly(const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) = default;
// MoveOnly& operator=(const MoveOnly&) = delete;
// MoveOnly& operator=(MoveOnly&&) = default;
MoveOnly& operator=(const MoveOnly&) = delete;
MoveOnly& operator=(MoveOnly&&) = default;
};
Nullable<MoveOnly> a;
@@ -48,7 +48,6 @@ TEST_CASE("Nullable")
SECTION("Bool Type")
{
Nullable<bool> a;
REQUIRE(a.HasValue() == false);
@@ -83,57 +82,67 @@ TEST_CASE("Nullable")
};
Nullable<DoNotConstruct> a;
a.Reset();
}
SECTION("Lifetime")
{
int constructed = 0, destructed = 0;
struct Lifetime
{
int& constructed;
int& destructed;
int* _constructed;
int* _destructed;
Lifetime(int& constructed, int& destructed)
: constructed(constructed)
, destructed(destructed)
Lifetime(int* constructed, int* destructed)
: _constructed(constructed)
, _destructed(destructed)
{
constructed++;
++(*_constructed);
}
Lifetime(const Lifetime& other)
: constructed(other.constructed)
, destructed(other.destructed)
Lifetime(Lifetime&& other) noexcept
: _constructed(other._constructed)
, _destructed(other._destructed)
{
constructed++;
++(*_constructed);
}
Lifetime() = delete;
Lifetime& operator=(const Lifetime&) = delete;
Lifetime& operator=(Lifetime&&) = delete;
~Lifetime()
{
destructed++;
++(*_destructed);
}
};
{
Nullable<Lifetime> a = Lifetime(constructed, destructed);
int constructed = 0, destructed = 0;
REQUIRE(constructed == destructed);
REQUIRE(constructed == 1);
REQUIRE(destructed == 0);
{
Nullable<Lifetime> a = Lifetime(&constructed, &destructed);
REQUIRE(a.HasValue());
REQUIRE(constructed == destructed + 1);
a.Reset();
REQUIRE(constructed == 1);
REQUIRE(destructed == 1);
REQUIRE(!a.HasValue());
REQUIRE(constructed == destructed);
}
REQUIRE(constructed == destructed);
{
Nullable<Lifetime> a = Lifetime(constructed, destructed);
Nullable<Lifetime> b = Lifetime(&constructed, &destructed);
REQUIRE(constructed == destructed + 1);
}
REQUIRE(constructed == destructed);
REQUIRE(constructed == 2);
REQUIRE(destructed == 2);
{
Nullable<Lifetime> c = Lifetime(&constructed, &destructed);
Nullable<Lifetime> d = MoveTemp(c);
REQUIRE(constructed == destructed + 1);
}
REQUIRE(constructed == destructed);
}
SECTION("Matching")
@@ -142,12 +151,12 @@ TEST_CASE("Nullable")
Nullable<int> b = 2;
a.Match(
[](int value) { FAIL("Null nullable must not match value handler."); },
[](int) { FAIL("Null nullable must not match value handler."); },
[]() {}
);
b.Match(
[](int value) {},
[](int) {},
[]() { FAIL("Nullable with valid value must not match null handler."); }
);
}