2020-05-09 15:06:31 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-09 15:06:31 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-25 08:53:23 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-02-26 10:25:24 +03:00
|
|
|
|
2023-02-02 18:48:11 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/Concepts.h>
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
#include <AK/String.h>
|
2021-02-23 10:33:03 +03:00
|
|
|
#include <AK/StringUtils.h>
|
2021-11-10 13:05:21 +03:00
|
|
|
#include <AK/Vector.h>
|
2021-02-23 10:33:03 +03:00
|
|
|
|
2023-02-02 18:48:11 +03:00
|
|
|
TEST_CASE(hash_compatible)
|
|
|
|
{
|
|
|
|
static_assert(AK::Concepts::HashCompatible<String, StringView>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<String, FlyString>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<StringView, String>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<StringView, FlyString>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<FlyString, String>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<FlyString, StringView>);
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
static_assert(AK::Concepts::HashCompatible<ByteString, StringView>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<ByteString, DeprecatedFlyString>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<StringView, ByteString>);
|
2023-02-02 18:48:11 +03:00
|
|
|
static_assert(AK::Concepts::HashCompatible<StringView, DeprecatedFlyString>);
|
2023-12-16 17:19:34 +03:00
|
|
|
static_assert(AK::Concepts::HashCompatible<DeprecatedFlyString, ByteString>);
|
2023-02-02 18:48:11 +03:00
|
|
|
static_assert(AK::Concepts::HashCompatible<DeprecatedFlyString, StringView>);
|
|
|
|
|
|
|
|
static_assert(AK::Concepts::HashCompatible<StringView, ByteBuffer>);
|
|
|
|
static_assert(AK::Concepts::HashCompatible<ByteBuffer, StringView>);
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:25:24 +03:00
|
|
|
TEST_CASE(matches_null)
|
|
|
|
{
|
|
|
|
EXPECT(AK::StringUtils::matches(StringView(), StringView()));
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(!AK::StringUtils::matches(StringView(), ""sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches(StringView(), "*"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches(StringView(), "?"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches(StringView(), "a"sv));
|
2020-02-26 10:25:24 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(!AK::StringUtils::matches(""sv, StringView()));
|
|
|
|
EXPECT(!AK::StringUtils::matches("a"sv, StringView()));
|
2020-02-26 10:25:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(matches_empty)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches(""sv, ""sv));
|
2020-02-26 10:25:24 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches(""sv, "*"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches(""sv, "?"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches(""sv, "a"sv));
|
2020-02-26 10:25:24 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(!AK::StringUtils::matches("a"sv, ""sv));
|
2020-02-26 10:25:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(matches_case_sensitive)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("a"sv, "a"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::matches("a"sv, "A"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::matches("A"sv, "a"sv, CaseSensitivity::CaseSensitive));
|
2020-02-26 10:25:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(matches_case_insensitive)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(!AK::StringUtils::matches("aa"sv, "a"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("aa"sv, "*"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches("cb"sv, "?a"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("adceb"sv, "a*b"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches("acdcb"sv, "a*c?b"sv));
|
2020-02-26 10:25:24 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 08:34:39 +03:00
|
|
|
TEST_CASE(matches_with_positions)
|
|
|
|
{
|
|
|
|
Vector<AK::MaskSpan> spans;
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("abbb"sv, "a*"sv, CaseSensitivity::CaseSensitive, &spans));
|
2020-10-25 08:34:39 +03:00
|
|
|
EXPECT(spans == Vector<AK::MaskSpan>({ { 1, 3 } }));
|
|
|
|
|
|
|
|
spans.clear();
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("abbb"sv, "?*"sv, CaseSensitivity::CaseSensitive, &spans));
|
2020-10-25 08:34:39 +03:00
|
|
|
EXPECT_EQ(spans, Vector<AK::MaskSpan>({ { 0, 1 }, { 1, 3 } }));
|
|
|
|
|
|
|
|
spans.clear();
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("acdcxb"sv, "a*c?b"sv, CaseSensitivity::CaseSensitive, &spans));
|
2020-10-25 08:34:39 +03:00
|
|
|
EXPECT_EQ(spans, Vector<AK::MaskSpan>({ { 1, 2 }, { 4, 1 } }));
|
|
|
|
|
|
|
|
spans.clear();
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("aaaa"sv, "A*"sv, CaseSensitivity::CaseInsensitive, &spans));
|
2020-10-25 08:34:39 +03:00
|
|
|
EXPECT_EQ(spans, Vector<AK::MaskSpan>({ { 1, 3 } }));
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:34:12 +03:00
|
|
|
// #4607
|
|
|
|
TEST_CASE(matches_trailing)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::matches("ab"sv, "ab*"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("ab"sv, "ab****"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("ab"sv, "*ab****"sv));
|
2020-12-28 23:34:12 +03:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:20:53 +03:00
|
|
|
TEST_CASE(match_backslash_escape)
|
|
|
|
{
|
|
|
|
EXPECT(AK::StringUtils::matches("ab*"sv, "ab\\*"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches("abc"sv, "ab\\*"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches("abcd"sv, "ab\\*"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("ab?"sv, "ab\\?"sv));
|
|
|
|
EXPECT(!AK::StringUtils::matches("abc"sv, "ab\\?"sv));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(match_trailing_backslash)
|
|
|
|
{
|
|
|
|
EXPECT(AK::StringUtils::matches("x\\"sv, "x\\"sv));
|
|
|
|
EXPECT(AK::StringUtils::matches("x\\"sv, "x\\\\"sv));
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:19:33 +03:00
|
|
|
TEST_CASE(convert_to_int)
|
|
|
|
{
|
2020-06-12 22:07:52 +03:00
|
|
|
auto value = AK::StringUtils::convert_to_int(StringView());
|
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_int(""sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_int("a"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_int("+"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_int("-"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual = AK::StringUtils::convert_to_int("0"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("+1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("-1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), -1);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("01"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("12345"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 12345);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int("-12345"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), -12345);
|
2020-11-16 21:30:22 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_int(" \t-12345 \n\n"sv);
|
2020-11-16 21:30:22 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), -12345);
|
2020-12-20 08:30:22 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_i8 = AK::StringUtils::convert_to_int<i8>("-1"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_i8.has_value());
|
|
|
|
EXPECT_EQ(actual_i8.value(), -1);
|
|
|
|
EXPECT_EQ(sizeof(actual_i8.value()), (size_t)1);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_i8 = AK::StringUtils::convert_to_int<i8>("128"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_i8.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_i16 = AK::StringUtils::convert_to_int<i16>("-1"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_i16.has_value());
|
|
|
|
EXPECT_EQ(actual_i16.value(), -1);
|
|
|
|
EXPECT_EQ(sizeof(actual_i16.value()), (size_t)2);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_i16 = AK::StringUtils::convert_to_int<i16>("32768"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_i16.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_i32 = AK::StringUtils::convert_to_int<i32>("-1"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_i32.has_value());
|
|
|
|
EXPECT_EQ(actual_i32.value(), -1);
|
|
|
|
EXPECT_EQ(sizeof(actual_i32.value()), (size_t)4);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_i32 = AK::StringUtils::convert_to_int<i32>("2147483648"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_i32.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_i64 = AK::StringUtils::convert_to_int<i64>("-1"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_i64.has_value());
|
|
|
|
EXPECT_EQ(actual_i64.value(), -1);
|
|
|
|
EXPECT_EQ(sizeof(actual_i64.value()), (size_t)8);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_i64 = AK::StringUtils::convert_to_int<i64>("9223372036854775808"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_i64.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(convert_to_uint)
|
|
|
|
{
|
2020-06-12 22:07:52 +03:00
|
|
|
auto value = AK::StringUtils::convert_to_uint(StringView());
|
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint(""sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint("a"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint("+"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint("-"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint("+1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint("-1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT(!value.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual = AK::StringUtils::convert_to_uint("0"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0u);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint("1"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1u);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint("01"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1u);
|
2020-03-02 16:19:33 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint("12345"sv);
|
2020-06-12 22:07:52 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 12345u);
|
2020-11-16 21:30:22 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint(" \t12345 \n\n"sv);
|
2020-11-16 21:30:22 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 12345u);
|
2020-12-20 08:30:22 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_u8 = AK::StringUtils::convert_to_uint<u8>("255"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_u8.has_value());
|
|
|
|
EXPECT_EQ(actual_u8.value(), 255u);
|
|
|
|
EXPECT_EQ(sizeof(actual_u8.value()), (size_t)1);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_u8 = AK::StringUtils::convert_to_uint<u8>("256"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_u8.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_u16 = AK::StringUtils::convert_to_uint<u16>("65535"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_u16.has_value());
|
|
|
|
EXPECT_EQ(actual_u16.value(), 65535u);
|
|
|
|
EXPECT_EQ(sizeof(actual_u16.value()), (size_t)2);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_u16 = AK::StringUtils::convert_to_uint<u16>("65536"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_u16.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_u32 = AK::StringUtils::convert_to_uint<u32>("4294967295"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_u32.has_value());
|
|
|
|
EXPECT_EQ(actual_u32.value(), 4294967295ul);
|
|
|
|
EXPECT_EQ(sizeof(actual_u32.value()), (size_t)4);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_u32 = AK::StringUtils::convert_to_uint<u32>("4294967296"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_u32.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual_u64 = AK::StringUtils::convert_to_uint<u64>("18446744073709551615"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(actual_u64.has_value());
|
|
|
|
EXPECT_EQ(actual_u64.value(), 18446744073709551615ull);
|
|
|
|
EXPECT_EQ(sizeof(actual_u64.value()), (size_t)8);
|
2022-07-11 20:32:29 +03:00
|
|
|
actual_u64 = AK::StringUtils::convert_to_uint<u64>("18446744073709551616"sv);
|
2020-12-20 08:30:22 +03:00
|
|
|
EXPECT(!actual_u64.has_value());
|
2020-03-02 16:19:33 +03:00
|
|
|
}
|
|
|
|
|
2021-12-20 23:06:54 +03:00
|
|
|
TEST_CASE(convert_to_uint_from_octal)
|
|
|
|
{
|
|
|
|
auto value = AK::StringUtils::convert_to_uint_from_octal<u16>(StringView());
|
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>(""sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("a"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("+"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("-"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("+1"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("-1"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
value = AK::StringUtils::convert_to_uint_from_octal<u16>("8"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!value.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
auto actual = AK::StringUtils::convert_to_uint_from_octal<u16>("77777777"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT(!actual.has_value());
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>("0"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0u);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>("1"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 1u);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>("0755"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0755u);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>("755"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0755u);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>(" \t644 \n\n"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0644u);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
actual = AK::StringUtils::convert_to_uint_from_octal<u16>("177777"sv);
|
2021-12-20 23:06:54 +03:00
|
|
|
EXPECT_EQ(actual.has_value(), true);
|
|
|
|
EXPECT_EQ(actual.value(), 0177777u);
|
|
|
|
}
|
|
|
|
|
2023-10-14 11:42:59 +03:00
|
|
|
TEST_CASE(convert_to_floating_point)
|
|
|
|
{
|
|
|
|
auto number_string = " 123.45 "sv;
|
|
|
|
auto maybe_number = AK::StringUtils::convert_to_floating_point<float>(number_string, TrimWhitespace::Yes);
|
|
|
|
EXPECT_APPROXIMATE(maybe_number.value(), 123.45f);
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:31:56 +03:00
|
|
|
TEST_CASE(ends_with)
|
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString test_string = "ABCDEF";
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::ends_with(test_string, "DEF"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::ends_with(test_string, "ABCDEF"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::ends_with(test_string, "ABCDE"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::ends_with(test_string, "ABCDEFG"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::ends_with(test_string, "def"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::ends_with(test_string, "def"sv, CaseSensitivity::CaseSensitive));
|
2020-05-26 13:31:56 +03:00
|
|
|
}
|
|
|
|
|
2020-07-18 19:59:38 +03:00
|
|
|
TEST_CASE(starts_with)
|
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString test_string = "ABCDEF";
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::starts_with(test_string, "ABC"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::starts_with(test_string, "ABCDEF"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::starts_with(test_string, "BCDEF"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::starts_with(test_string, "ABCDEFG"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::starts_with(test_string, "abc"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::starts_with(test_string, "abc"sv, CaseSensitivity::CaseSensitive));
|
2020-07-18 19:59:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 00:07:03 +03:00
|
|
|
TEST_CASE(contains)
|
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString test_string = "ABCDEFABCXYZ";
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "ABC"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "ABC"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "AbC"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "BCX"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "BCX"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "BcX"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(test_string, "xyz"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, "xyz"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(test_string, "EFG"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(test_string, "EfG"sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, ""sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(test_string, ""sv, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(""sv, test_string, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(""sv, test_string, CaseSensitivity::CaseInsensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(test_string, "L"sv, CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(!AK::StringUtils::contains(test_string, "L"sv, CaseSensitivity::CaseInsensitive));
|
2022-03-18 21:02:07 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString command_palette_bug_string = "Go Go Back";
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::contains(command_palette_bug_string, "Go Back"sv, AK::CaseSensitivity::CaseSensitive));
|
|
|
|
EXPECT(AK::StringUtils::contains(command_palette_bug_string, "gO bAcK"sv, AK::CaseSensitivity::CaseInsensitive));
|
2020-10-21 00:07:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 02:26:02 +03:00
|
|
|
TEST_CASE(is_whitespace)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT(AK::StringUtils::is_whitespace(""sv));
|
|
|
|
EXPECT(AK::StringUtils::is_whitespace(" "sv));
|
|
|
|
EXPECT(AK::StringUtils::is_whitespace(" \t"sv));
|
|
|
|
EXPECT(AK::StringUtils::is_whitespace(" \t\n"sv));
|
|
|
|
EXPECT(AK::StringUtils::is_whitespace(" \t\n\r\v"sv));
|
|
|
|
EXPECT(!AK::StringUtils::is_whitespace(" a "sv));
|
|
|
|
EXPECT(!AK::StringUtils::is_whitespace("a\t"sv));
|
2021-01-03 02:26:02 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 16:38:09 +03:00
|
|
|
TEST_CASE(trim)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("aaa.a."sv, "."sv, TrimMode::Right), "aaa.a"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("...aaa"sv, "."sv, TrimMode::Left), "aaa"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("...aaa.a..."sv, "."sv, TrimMode::Both), "aaa.a"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("."sv, "."sv, TrimMode::Right), ""sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("."sv, "."sv, TrimMode::Left), ""sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("."sv, "."sv, TrimMode::Both), ""sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::trim("..."sv, "."sv, TrimMode::Both), ""sv);
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:58:45 +03:00
|
|
|
TEST_CASE(find)
|
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString test_string = "1234567";
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "1"sv).value_or(1), 0u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "2"sv).value_or(2), 1u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "3"sv).value_or(3), 2u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "4"sv).value_or(4), 3u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "5"sv).value_or(5), 4u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "34"sv).value_or(3), 2u);
|
|
|
|
EXPECT_EQ(AK::StringUtils::find(test_string, "78"sv).has_value(), false);
|
2021-01-12 22:58:45 +03:00
|
|
|
}
|
|
|
|
|
2021-02-21 00:39:22 +03:00
|
|
|
TEST_CASE(to_snakecase)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("foobar"sv), "foobar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("Foobar"sv), "foobar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("FOOBAR"sv), "foobar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("fooBar"sv), "foo_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("FooBar"sv), "foo_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("fooBAR"sv), "foo_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("FOOBar"sv), "foo_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("foo_bar"sv), "foo_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("FBar"sv), "f_bar");
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_snakecase("FooB"sv), "foo_b");
|
2021-02-21 00:39:22 +03:00
|
|
|
}
|
2021-08-26 20:55:41 +03:00
|
|
|
|
|
|
|
TEST_CASE(to_titlecase)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase(""sv), ""sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("f"sv), "F"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("foobar"sv), "Foobar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("Foobar"sv), "Foobar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("FOOBAR"sv), "Foobar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bAR"sv), "Foo Bar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("foo bar"sv), "Foo Bar"sv);
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase(" foo bar "sv), " Foo Bar "sv);
|
2022-10-20 15:44:18 +03:00
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("\xc3\xa7"sv), "\xc3\xa7"sv); // U+00E7 LATIN SMALL LETTER C WITH CEDILLA
|
|
|
|
EXPECT_EQ(AK::StringUtils::to_titlecase("\xe1\x80\x80"sv), "\xe1\x80\x80"sv); // U+1000 MYANMAR LETTER KA
|
2021-08-26 20:55:41 +03:00
|
|
|
}
|