Skip to content

Truly independent web browser

10.6 KB
Raw Blame
/*
 * Copyright (c) 2025, Tim Flynn <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/Array.h>
#include <AK/FFIHelpers.h>
#include <AK/Utf16FlyString.h>
#include <AK/Vector.h>
#include <LibThreading/Thread.h>

static_assert(AK::Concepts::HashCompatible<Utf16String, Utf16FlyString>);
static_assert(AK::Concepts::HashCompatible<Utf16FlyString, Utf16String>);

static_assert(AK::Concepts::HashCompatible<Utf16View, Utf16FlyString>);
static_assert(AK::Concepts::HashCompatible<Utf16FlyString, Utf16View>);

TEST_CASE(ffi_constructors_use_the_native_intern_table)
{
    auto first = Utf16FlyString::adopt_raw(ladybird_utf16_fly_string_from_utf8(
        reinterpret_cast<u8 const*>("this is a shared fly string"), 27));
    auto second = Utf16FlyString::adopt_raw(ladybird_utf16_fly_string_from_utf16(
        reinterpret_cast<u16 const*>(u"this is a shared fly string"), 27));

    EXPECT_EQ(first, second);
    EXPECT_EQ(first.raw_identity(), second.raw_identity());
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    EXPECT_EQ(ladybird_utf16_fly_string_from_utf8(nullptr, 0), Utf16FlyString {}.raw_identity());
    EXPECT_DEATH("Passing an invalid UTF-8 range", (void)ladybird_utf16_fly_string_from_utf8(nullptr, 1));

    auto empty_raw = ladybird_utf16_fly_string_from_utf16(nullptr, 0);
    EXPECT_EQ(empty_raw, Utf16FlyString {}.raw_identity());

    auto empty = Utf16FlyString::adopt_raw(empty_raw);
    EXPECT(empty.is_empty());
    EXPECT(empty.to_utf16_string().has_short_ascii_storage());
    EXPECT_EQ(empty.to_raw_leaked(), empty_raw);

    Optional<Utf16FlyString> optional_empty { empty };
    EXPECT(optional_empty.has_value());
}

TEST_CASE(short_ascii_literal_is_constexpr)
{
    // The _utf16_fly_string UDL folds short ASCII literals to compile-time
    // constants, both for char and char16_t inputs.
    static constexpr Utf16FlyString from_char = "foo"_utf16_fly_string;
    static constexpr Utf16FlyString from_char16 = u"bar"_utf16_fly_string;
    EXPECT_EQ(from_char, "foo"sv);
    EXPECT_EQ(from_char16, "bar"sv);
    EXPECT_EQ(from_char.length_in_code_units(), 3u);
    EXPECT_EQ(from_char16.length_in_code_units(), 3u);
}

TEST_CASE(empty_string)
{
    Utf16FlyString fly {};
    EXPECT(fly.is_empty());
    EXPECT_EQ(fly, ""sv);

    // Short strings do not get stored in the fly string table.
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 0u);
}

TEST_CASE(short_string)
{
    Utf16FlyString fly1 { "foo"_utf16 };
    EXPECT_EQ(fly1, "foo"sv);

    Utf16FlyString fly2 { "foo"_utf16 };
    EXPECT_EQ(fly2, "foo"sv);

    Utf16FlyString fly3 { "bar"_utf16 };
    EXPECT_EQ(fly3, "bar"sv);

    EXPECT_EQ(fly1, fly2);
    EXPECT_NE(fly1, fly3);
    EXPECT_NE(fly2, fly3);

    EXPECT(fly1.to_utf16_string().has_short_ascii_storage());
    EXPECT(fly2.to_utf16_string().has_short_ascii_storage());
    EXPECT(fly3.to_utf16_string().has_short_ascii_storage());

    // Short strings do not get stored in the fly string table.
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 0u);
}

TEST_CASE(long_string)
{
    Utf16FlyString fly1 { "thisisdefinitelymorethan7bytes"_utf16 };
    EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    Utf16FlyString fly2 { "thisisdefinitelymorethan7bytes"_utf16 };
    EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    Utf16FlyString fly3 { "thisisalsoforsuremorethan7bytes"_utf16 };
    EXPECT_EQ(fly3, "thisisalsoforsuremorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 2u);

    EXPECT_EQ(fly1, fly2);
    EXPECT_NE(fly1, fly3);
    EXPECT_NE(fly2, fly3);

    EXPECT(fly1.to_utf16_string().has_long_ascii_storage());
    EXPECT(fly2.to_utf16_string().has_long_ascii_storage());
    EXPECT(fly3.to_utf16_string().has_long_ascii_storage());
}

TEST_CASE(user_defined_literal)
{
    auto fly1 = "thisisdefinitelymorethan7bytes"_utf16_fly_string;
    EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    auto fly2 = "thisisdefinitelymorethan7bytes"_utf16_fly_string;
    EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    auto fly3 = u"thisisdefinitelymorethan7bytes"_utf16_fly_string;
    EXPECT_EQ(fly3, u"thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    auto fly4 = "foo"_utf16_fly_string;
    EXPECT_EQ(fly4, "foo"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    EXPECT_EQ(fly1, fly2);
    EXPECT_EQ(fly1, fly3);
    EXPECT_EQ(fly3, fly3);

    EXPECT_NE(fly1, fly4);
    EXPECT_NE(fly2, fly4);
    EXPECT_NE(fly3, fly4);
}

TEST_CASE(fly_string_keep_string_data_alive)
{
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 0u);
    {
        Utf16FlyString fly {};
        {
            auto string = "thisisdefinitelymorethan7bytes"_utf16;
            fly = Utf16FlyString { string };
            EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);
        }

        EXPECT_EQ(fly, "thisisdefinitelymorethan7bytes"sv);
        EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);
    }

    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 0u);
}

TEST_CASE(moved_fly_string_becomes_empty)
{
    Utf16FlyString fly1 {};
    EXPECT(fly1.is_empty());

    Utf16FlyString fly2 { "thisisdefinitelymorethan7bytes"_utf16 };
    EXPECT_EQ(fly2, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);

    fly1 = move(fly2);

    EXPECT(fly2.is_empty());
    EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv);
    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), 1u);
}

TEST_CASE(raw_ownership_transfer)
{
    {
        auto string = "short"_utf16_fly_string;
        auto raw = move(string).into_raw();

        EXPECT(string.is_empty());

        auto adopted_string = Utf16FlyString::adopt_raw(raw);
        EXPECT_EQ(adopted_string, "short"sv);
        EXPECT_EQ(adopted_string.raw_identity(), raw);
    }

    {
        auto string = "this is a long fly string"_utf16_fly_string;
        auto raw = string.raw_identity();
        auto const* header = reinterpret_cast<AK::Detail::Utf16StringDataHeader const*>(raw);

        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 1u);

        EXPECT_EQ(move(string).into_raw(), raw);
        EXPECT(string.is_empty());
        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 1u);

        auto adopted_string = Utf16FlyString::adopt_raw(raw);
        EXPECT_EQ(adopted_string, "this is a long fly string"sv);
        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 1u);

        Utf16FlyString::ref_raw(raw);
        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 2u);
        Utf16FlyString::unref_raw(raw);
        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 1u);

        {
            auto copied_string = Utf16FlyString::from_raw(raw);
            EXPECT_EQ(copied_string, adopted_string);
            EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 2u);
        }
        EXPECT_EQ(AK::atomic_load(&header->reference_count, AK::memory_order_relaxed), 1u);
    }
}

TEST_CASE(is_one_of)
{
    auto foo = Utf16FlyString::from_utf8("foo"sv);
    auto bar = Utf16FlyString::from_utf16(u"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));
}

TEST_CASE(optional)
{
    static_assert(AssertSize<Optional<Utf16FlyString>, sizeof(Utf16FlyString)>());

    Optional<Utf16FlyString> string;
    EXPECT(!string.has_value());

    string = "ascii"_utf16_fly_string;
    EXPECT(string.has_value());
    EXPECT_EQ(string.value(), "ascii"sv);

    auto released = string.release_value();
    EXPECT(!string.has_value());
    EXPECT_EQ(released, "ascii"sv);

    string = u"well πŸ˜€ hello"_utf16_fly_string;
    EXPECT(string.has_value());
    EXPECT_EQ(string.value(), u"well πŸ˜€ hello"sv);

    released = string.release_value();
    EXPECT(!string.has_value());
    EXPECT_EQ(released, u"well πŸ˜€ hello"sv);
}

TEST_CASE(interning_the_same_strings_on_several_threads)
{
    static constexpr size_t thread_count = 8;
    static constexpr size_t string_count = 1000;
    auto initial_fly_string_count = Utf16FlyString::number_of_utf16_fly_strings();

    IGNORE_USE_IN_ESCAPING_LAMBDA Array<Vector<Utf16FlyString>, thread_count> interned;
    Vector<NonnullRefPtr<Threading::Thread>> threads;
    for (size_t i = 0; i < thread_count; ++i) {
        auto thread = Threading::Thread::construct("StringInterner"sv, [&interned, i]() {
            for (size_t j = 0; j < string_count; ++j)
                interned[i].append(Utf16FlyString { Utf16String::formatted("A long string interned on several threads {}", j) });
            return 0;
        });
        thread->start();
        threads.append(move(thread));
    }
    for (auto& thread : threads)
        (void)thread->join();

    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), initial_fly_string_count + string_count);
    for (size_t i = 1; i < thread_count; ++i) {
        for (size_t j = 0; j < string_count; ++j)
            EXPECT_EQ(interned[i][j], interned[0][j]);
    }
}

TEST_CASE(looking_up_strings_while_other_threads_drop_them)
{
    auto initial_fly_string_count = Utf16FlyString::number_of_utf16_fly_strings();

    Vector<NonnullRefPtr<Threading::Thread>> threads;
    for (size_t i = 0; i < 8; ++i) {
        auto thread = Threading::Thread::construct("StringChurner"sv, []() {
            for (size_t j = 0; j < 20'000; ++j) {
                // A string that is alive must stay findable while an equal string that is being destroyed removes itself.
                auto fly_string = Utf16FlyString::from_utf8("A long string that is interned and dropped repeatedly"sv);
                auto same_fly_string = Utf16FlyString::from_utf8("A long string that is interned and dropped repeatedly"sv);
                EXPECT_EQ(fly_string, same_fly_string);
            }
            return 0;
        });
        thread->start();
        threads.append(move(thread));
    }
    for (auto& thread : threads)
        (void)thread->join();

    EXPECT_EQ(Utf16FlyString::number_of_utf16_fly_strings(), initial_fly_string_count);
}