2023-01-11 13:26:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Try.h>
|
|
|
|
|
|
|
|
TEST_CASE(empty_string)
|
|
|
|
{
|
|
|
|
FlyString fly {};
|
|
|
|
EXPECT(fly.is_empty());
|
|
|
|
EXPECT_EQ(fly, ""sv);
|
|
|
|
|
|
|
|
// Short strings do not get stored in the fly string table.
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(short_string)
|
|
|
|
{
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly1 { "foo"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly1, "foo"sv);
|
|
|
|
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly2 { "foo"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly2, "foo"sv);
|
|
|
|
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly3 { "bar"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly3, "bar"sv);
|
|
|
|
|
|
|
|
EXPECT_EQ(fly1, fly2);
|
|
|
|
EXPECT_NE(fly1, fly3);
|
|
|
|
EXPECT_NE(fly2, fly3);
|
|
|
|
|
|
|
|
EXPECT(fly1.to_string().is_short_string());
|
|
|
|
EXPECT(fly2.to_string().is_short_string());
|
|
|
|
EXPECT(fly3.to_string().is_short_string());
|
|
|
|
|
|
|
|
// Short strings do not get stored in the fly string table.
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(long_string)
|
|
|
|
{
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly1 { "thisisdefinitelymorethan7bytes"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly2 { "thisisdefinitelymorethan7bytes"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly3 { "thisisalsoforsuremorethan7bytes"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly3, "thisisalsoforsuremorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 2u);
|
|
|
|
|
|
|
|
EXPECT_EQ(fly1, fly2);
|
|
|
|
EXPECT_NE(fly1, fly3);
|
|
|
|
EXPECT_NE(fly2, fly3);
|
|
|
|
|
|
|
|
EXPECT(!fly1.to_string().is_short_string());
|
|
|
|
EXPECT(!fly2.to_string().is_short_string());
|
|
|
|
EXPECT(!fly3.to_string().is_short_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(from_string_view)
|
|
|
|
{
|
2023-08-07 10:07:35 +00:00
|
|
|
auto fly1 = "thisisdefinitelymorethan7bytes"_fly_string;
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
2023-08-07 10:07:35 +00:00
|
|
|
auto fly2 = "thisisdefinitelymorethan7bytes"_fly_string;
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
2023-08-07 10:07:35 +00:00
|
|
|
auto fly3 = "foo"_fly_string;
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly3, "foo"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(fly1, fly2);
|
|
|
|
EXPECT_NE(fly1, fly3);
|
|
|
|
EXPECT_NE(fly2, fly3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(fly_string_keep_string_data_alive)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 0u);
|
|
|
|
{
|
|
|
|
FlyString fly {};
|
|
|
|
{
|
2023-08-07 09:12:38 +00:00
|
|
|
auto string = "thisisdefinitelymorethan7bytes"_string;
|
2023-01-11 13:26:49 +00:00
|
|
|
fly = FlyString { string };
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(fly, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(moved_fly_string_becomes_empty)
|
|
|
|
{
|
|
|
|
FlyString fly1 {};
|
|
|
|
EXPECT(fly1.is_empty());
|
|
|
|
|
2023-08-07 09:12:38 +00:00
|
|
|
FlyString fly2 { "thisisdefinitelymorethan7bytes"_string };
|
2023-01-11 13:26:49 +00:00
|
|
|
EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
|
|
|
|
fly1 = move(fly2);
|
|
|
|
|
|
|
|
EXPECT(fly2.is_empty());
|
|
|
|
EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
|
|
|
|
EXPECT_EQ(FlyString::number_of_fly_strings(), 1u);
|
|
|
|
}
|
2023-03-24 17:25:10 +00:00
|
|
|
|
|
|
|
TEST_CASE(is_one_of)
|
|
|
|
{
|
|
|
|
auto foo = MUST(FlyString::from_utf8("foo"sv));
|
|
|
|
auto bar = MUST(FlyString::from_utf8("bar"sv));
|
|
|
|
|
|
|
|
EXPECT(foo.is_one_of(foo));
|
|
|
|
EXPECT(foo.is_one_of(foo, bar));
|
|
|
|
EXPECT(foo.is_one_of(bar, foo));
|
|
|
|
EXPECT(!foo.is_one_of(bar));
|
|
|
|
|
|
|
|
EXPECT(!bar.is_one_of("foo"sv));
|
|
|
|
EXPECT(bar.is_one_of("foo"sv, "bar"sv));
|
|
|
|
EXPECT(bar.is_one_of("bar"sv, "foo"sv));
|
|
|
|
EXPECT(bar.is_one_of("bar"sv));
|
|
|
|
}
|