mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 17:52:26 -05:00
151e6a1818
We were *copying* the other Optional's value and then marking it as not having a value. This prevented us from ever destroying the original.
43 lines
849 B
C++
43 lines
849 B
C++
#include <AK/TestSuite.h>
|
|
|
|
#include <AK/AKString.h>
|
|
#include <AK/Optional.h>
|
|
|
|
TEST_CASE(basic_optional)
|
|
{
|
|
Optional<int> x;
|
|
EXPECT_EQ(x.has_value(), false);
|
|
x = 3;
|
|
EXPECT_EQ(x.has_value(), true);
|
|
EXPECT_EQ(x.value(), 3);
|
|
}
|
|
|
|
TEST_CASE(move_optional)
|
|
{
|
|
Optional<int> x;
|
|
EXPECT_EQ(x.has_value(), false);
|
|
x = 3;
|
|
EXPECT_EQ(x.has_value(), true);
|
|
EXPECT_EQ(x.value(), 3);
|
|
|
|
Optional<int> y;
|
|
y = move(x);
|
|
EXPECT_EQ(y.has_value(), true);
|
|
EXPECT_EQ(y.value(), 3);
|
|
EXPECT_EQ(x.has_value(), false);
|
|
}
|
|
|
|
TEST_CASE(optional_leak_1)
|
|
{
|
|
struct Structure {
|
|
Optional<String> str;
|
|
};
|
|
|
|
// This used to leak, it does not anymore.
|
|
Vector<Structure> vec;
|
|
vec.append({ "foo" });
|
|
EXPECT_EQ(vec[0].str.has_value(), true);
|
|
EXPECT_EQ(vec[0].str.value(), "foo");
|
|
}
|
|
|
|
TEST_MAIN(Optional)
|