2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-02 07:23:03 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-06-27 13:57:49 +00:00
|
|
|
#include <AK/HashMap.h>
|
2021-05-19 12:35:34 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2023-02-02 15:55:50 +00:00
|
|
|
#include <AK/String.h>
|
2019-06-27 13:57:49 +00:00
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(construct)
|
2019-06-27 13:57:49 +00:00
|
|
|
{
|
2020-11-11 22:21:01 +00:00
|
|
|
using IntIntMap = HashMap<int, int>;
|
2019-06-27 13:57:49 +00:00
|
|
|
EXPECT(IntIntMap().is_empty());
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(IntIntMap().size(), 0u);
|
2019-07-16 08:08:39 +00:00
|
|
|
}
|
2019-06-27 13:57:49 +00:00
|
|
|
|
2020-12-28 11:49:16 +00:00
|
|
|
TEST_CASE(construct_from_initializer_list)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<int, DeprecatedString> number_to_string {
|
2020-12-28 11:49:16 +00:00
|
|
|
{ 1, "One" },
|
|
|
|
{ 2, "Two" },
|
|
|
|
{ 3, "Three" },
|
|
|
|
};
|
|
|
|
EXPECT_EQ(number_to_string.is_empty(), false);
|
|
|
|
EXPECT_EQ(number_to_string.size(), 3u);
|
|
|
|
}
|
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(populate)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<int, DeprecatedString> number_to_string;
|
2019-06-27 13:57:49 +00:00
|
|
|
number_to_string.set(1, "One");
|
|
|
|
number_to_string.set(2, "Two");
|
|
|
|
number_to_string.set(3, "Three");
|
2019-07-16 08:08:39 +00:00
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
EXPECT_EQ(number_to_string.is_empty(), false);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(number_to_string.size(), 3u);
|
2019-07-16 08:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(range_loop)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<int, DeprecatedString> number_to_string;
|
2020-10-15 21:59:31 +00:00
|
|
|
EXPECT_EQ(number_to_string.set(1, "One"), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(number_to_string.set(2, "Two"), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(number_to_string.set(3, "Three"), AK::HashSetResult::InsertedNewEntry);
|
2019-06-27 13:57:49 +00:00
|
|
|
|
|
|
|
int loop_counter = 0;
|
|
|
|
for (auto& it : number_to_string) {
|
2019-07-16 08:08:39 +00:00
|
|
|
EXPECT_EQ(it.value.is_null(), false);
|
2019-06-27 13:57:49 +00:00
|
|
|
++loop_counter;
|
|
|
|
}
|
2019-07-16 08:08:39 +00:00
|
|
|
EXPECT_EQ(loop_counter, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(map_remove)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<int, DeprecatedString> number_to_string;
|
2020-10-15 21:59:31 +00:00
|
|
|
EXPECT_EQ(number_to_string.set(1, "One"), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(number_to_string.set(2, "Two"), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(number_to_string.set(3, "Three"), AK::HashSetResult::InsertedNewEntry);
|
2019-06-27 13:57:49 +00:00
|
|
|
|
2020-10-15 21:59:31 +00:00
|
|
|
EXPECT_EQ(number_to_string.remove(1), true);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(number_to_string.size(), 2u);
|
2019-06-27 14:36:31 +00:00
|
|
|
EXPECT(number_to_string.find(1) == number_to_string.end());
|
|
|
|
|
2020-10-15 21:59:31 +00:00
|
|
|
EXPECT_EQ(number_to_string.remove(3), true);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(number_to_string.size(), 1u);
|
2019-06-27 14:36:31 +00:00
|
|
|
EXPECT(number_to_string.find(3) == number_to_string.end());
|
2019-07-16 08:08:39 +00:00
|
|
|
EXPECT(number_to_string.find(2) != number_to_string.end());
|
|
|
|
}
|
2019-06-27 14:36:31 +00:00
|
|
|
|
2022-01-05 15:53:24 +00:00
|
|
|
TEST_CASE(remove_all_matching)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<int, DeprecatedString> map;
|
2022-01-05 15:53:24 +00:00
|
|
|
|
|
|
|
map.set(1, "One");
|
|
|
|
map.set(2, "Two");
|
|
|
|
map.set(3, "Three");
|
|
|
|
map.set(4, "Four");
|
|
|
|
|
|
|
|
EXPECT_EQ(map.size(), 4u);
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(map.remove_all_matching([&](int key, DeprecatedString const& value) { return key == 1 || value == "Two"; }), true);
|
2022-01-06 16:06:59 +00:00
|
|
|
EXPECT_EQ(map.size(), 2u);
|
2022-01-05 15:53:24 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(map.remove_all_matching([&](int, DeprecatedString const&) { return false; }), false);
|
2022-01-05 15:53:24 +00:00
|
|
|
EXPECT_EQ(map.size(), 2u);
|
2022-01-06 16:06:59 +00:00
|
|
|
|
2022-01-05 15:53:24 +00:00
|
|
|
EXPECT(map.contains(3));
|
|
|
|
EXPECT(map.contains(4));
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(map.remove_all_matching([&](int, DeprecatedString const&) { return true; }), true);
|
|
|
|
EXPECT_EQ(map.remove_all_matching([&](int, DeprecatedString const&) { return false; }), false);
|
2022-01-05 15:53:24 +00:00
|
|
|
|
|
|
|
EXPECT(map.is_empty());
|
2022-01-05 15:57:45 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(map.remove_all_matching([&](int, DeprecatedString const&) { return true; }), false);
|
2022-01-05 15:53:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(case_insensitive)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, int, CaseInsensitiveStringTraits> casemap;
|
|
|
|
EXPECT_EQ(DeprecatedString("nickserv").to_lowercase(), DeprecatedString("NickServ").to_lowercase());
|
2020-10-15 16:29:21 +00:00
|
|
|
EXPECT_EQ(casemap.set("nickserv", 3), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(casemap.set("NickServ", 3), AK::HashSetResult::ReplacedExistingEntry);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(casemap.size(), 1u);
|
2019-06-27 13:57:49 +00:00
|
|
|
}
|
2019-07-16 08:08:39 +00:00
|
|
|
|
2023-01-09 17:51:21 +00:00
|
|
|
TEST_CASE(case_insensitive_stringview)
|
|
|
|
{
|
2023-03-14 11:40:53 +00:00
|
|
|
HashMap<StringView, int, CaseInsensitiveASCIIStringViewTraits> casemap;
|
2023-01-09 17:51:21 +00:00
|
|
|
EXPECT_EQ(casemap.set("nickserv"sv, 3), AK::HashSetResult::InsertedNewEntry);
|
|
|
|
EXPECT_EQ(casemap.set("NickServ"sv, 3), AK::HashSetResult::ReplacedExistingEntry);
|
|
|
|
EXPECT_EQ(casemap.size(), 1u);
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:31:48 +00:00
|
|
|
TEST_CASE(hashmap_of_nonnullownptr_get)
|
2019-08-02 07:24:15 +00:00
|
|
|
{
|
|
|
|
struct Object {
|
2022-12-04 18:02:33 +00:00
|
|
|
Object(DeprecatedString const& s)
|
2020-08-21 16:31:48 +00:00
|
|
|
: string(s)
|
2019-08-02 07:24:15 +00:00
|
|
|
{
|
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString string;
|
2019-08-14 09:47:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HashMap<int, NonnullOwnPtr<Object>> objects;
|
|
|
|
objects.set(1, make<Object>("One"));
|
|
|
|
objects.set(2, make<Object>("Two"));
|
|
|
|
objects.set(3, make<Object>("Three"));
|
|
|
|
|
|
|
|
{
|
|
|
|
auto x = objects.get(2);
|
|
|
|
EXPECT_EQ(x.has_value(), true);
|
|
|
|
EXPECT_EQ(x.value()->string, "Two");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Do it again to make sure that peeking into the map above didn't
|
|
|
|
// remove the value from the map.
|
|
|
|
auto x = objects.get(2);
|
|
|
|
EXPECT_EQ(x.has_value(), true);
|
|
|
|
EXPECT_EQ(x.value()->string, "Two");
|
|
|
|
}
|
|
|
|
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(objects.size(), 3u);
|
2019-08-14 09:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:29:21 +00:00
|
|
|
TEST_CASE(many_strings)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, int> strings;
|
2020-10-15 16:29:21 +00:00
|
|
|
for (int i = 0; i < 999; ++i) {
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(strings.set(DeprecatedString::number(i), i), AK::HashSetResult::InsertedNewEntry);
|
2020-10-15 16:29:21 +00:00
|
|
|
}
|
2020-10-15 21:59:31 +00:00
|
|
|
EXPECT_EQ(strings.size(), 999u);
|
2020-10-15 16:29:21 +00:00
|
|
|
for (auto& it : strings) {
|
|
|
|
EXPECT_EQ(it.key.to_int().value(), it.value);
|
|
|
|
}
|
2020-10-15 21:59:31 +00:00
|
|
|
for (int i = 0; i < 999; ++i) {
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(strings.remove(DeprecatedString::number(i)), true);
|
2020-10-15 21:59:31 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(strings.is_empty(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(basic_remove)
|
|
|
|
{
|
|
|
|
HashMap<int, int> map;
|
|
|
|
map.set(1, 10);
|
|
|
|
map.set(2, 20);
|
|
|
|
map.set(3, 30);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(3), true);
|
|
|
|
EXPECT_EQ(map.remove(3), false);
|
|
|
|
EXPECT_EQ(map.size(), 2u);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(1), true);
|
|
|
|
EXPECT_EQ(map.remove(1), false);
|
|
|
|
EXPECT_EQ(map.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(2), true);
|
|
|
|
EXPECT_EQ(map.remove(2), false);
|
|
|
|
EXPECT_EQ(map.size(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(basic_contains)
|
|
|
|
{
|
|
|
|
HashMap<int, int> map;
|
|
|
|
map.set(1, 10);
|
|
|
|
map.set(2, 20);
|
|
|
|
map.set(3, 30);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.contains(1), true);
|
|
|
|
EXPECT_EQ(map.contains(2), true);
|
|
|
|
EXPECT_EQ(map.contains(3), true);
|
|
|
|
EXPECT_EQ(map.contains(4), false);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(3), true);
|
|
|
|
EXPECT_EQ(map.contains(3), false);
|
|
|
|
EXPECT_EQ(map.contains(1), true);
|
|
|
|
EXPECT_EQ(map.contains(2), true);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(2), true);
|
|
|
|
EXPECT_EQ(map.contains(2), false);
|
|
|
|
EXPECT_EQ(map.contains(3), false);
|
|
|
|
EXPECT_EQ(map.contains(1), true);
|
|
|
|
|
|
|
|
EXPECT_EQ(map.remove(1), true);
|
|
|
|
EXPECT_EQ(map.contains(1), false);
|
2020-10-15 16:29:21 +00:00
|
|
|
}
|
2022-06-22 18:06:28 +00:00
|
|
|
|
|
|
|
TEST_CASE(in_place_rehashing_ordered_loop_bug)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
OrderedHashMap<DeprecatedString, DeprecatedString> map;
|
2022-06-22 18:06:28 +00:00
|
|
|
map.set("yt.innertube::nextId", "");
|
|
|
|
map.set("yt.innertube::requests", "");
|
|
|
|
map.remove("yt.innertube::nextId");
|
|
|
|
map.set("yt.innertube::nextId", "");
|
|
|
|
VERIFY(map.keys().size() == 2);
|
|
|
|
}
|
2023-02-02 15:55:50 +00:00
|
|
|
|
|
|
|
TEST_CASE(take)
|
|
|
|
{
|
|
|
|
HashMap<String, int> map;
|
|
|
|
|
|
|
|
EXPECT(!map.take("foo"sv).has_value());
|
|
|
|
EXPECT(!map.take("bar"sv).has_value());
|
2023-08-08 02:26:17 +00:00
|
|
|
EXPECT(!map.take("baz"_string).has_value());
|
2023-02-02 15:55:50 +00:00
|
|
|
|
2023-08-08 02:26:17 +00:00
|
|
|
map.set("foo"_string, 1);
|
|
|
|
map.set("bar"_string, 2);
|
|
|
|
map.set("baz"_string, 3);
|
2023-02-02 15:55:50 +00:00
|
|
|
|
|
|
|
auto foo = map.take("foo"sv);
|
|
|
|
EXPECT_EQ(foo, 1);
|
|
|
|
|
|
|
|
foo = map.take("foo"sv);
|
|
|
|
EXPECT(!foo.has_value());
|
|
|
|
|
|
|
|
auto bar = map.take("bar"sv);
|
|
|
|
EXPECT_EQ(bar, 2);
|
|
|
|
|
|
|
|
bar = map.take("bar"sv);
|
|
|
|
EXPECT(!bar.has_value());
|
|
|
|
|
2023-08-08 02:26:17 +00:00
|
|
|
auto baz = map.take("baz"_string);
|
2023-02-02 15:55:50 +00:00
|
|
|
EXPECT_EQ(baz, 3);
|
|
|
|
|
2023-08-08 02:26:17 +00:00
|
|
|
baz = map.take("baz"_string);
|
2023-02-02 15:55:50 +00:00
|
|
|
EXPECT(!baz.has_value());
|
|
|
|
}
|
2023-05-13 18:25:22 +00:00
|
|
|
|
|
|
|
TEST_CASE(clone_same_template_args)
|
|
|
|
{
|
|
|
|
HashMap<int, int> orig;
|
|
|
|
orig.set(1, 10);
|
|
|
|
orig.set(2, 20);
|
|
|
|
orig.set(3, 30);
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>(20));
|
|
|
|
|
|
|
|
auto second = TRY_OR_FAIL(orig.clone());
|
|
|
|
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>(20));
|
|
|
|
EXPECT_EQ(second.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(second.get(2), Optional<int>(20));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(clone_different_traits)
|
|
|
|
{
|
|
|
|
HashMap<StringView, StringView> orig;
|
|
|
|
orig.set("Well"sv, "hello friends!"sv);
|
|
|
|
orig.set("Thank"sv, "you, very cool!"sv);
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(2));
|
|
|
|
EXPECT_EQ(orig.get("Well"sv), Optional<StringView>("hello friends!"sv));
|
|
|
|
EXPECT_EQ(orig.get("weLL"sv), Optional<StringView>());
|
|
|
|
|
|
|
|
auto second = TRY_OR_FAIL(orig.clone<CaseInsensitiveASCIIStringViewTraits>());
|
|
|
|
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(2));
|
|
|
|
EXPECT_EQ(orig.get("Well"sv), Optional<StringView>("hello friends!"sv));
|
|
|
|
EXPECT_EQ(orig.get("weLL"sv), Optional<StringView>());
|
|
|
|
EXPECT_EQ(second.size(), static_cast<size_t>(2));
|
|
|
|
EXPECT_EQ(second.get("Well"sv), Optional<StringView>("hello friends!"sv));
|
|
|
|
EXPECT_EQ(second.get("weLL"sv), Optional<StringView>("hello friends!"sv));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(move_construct)
|
|
|
|
{
|
|
|
|
HashMap<int, int> orig;
|
|
|
|
orig.set(1, 10);
|
|
|
|
orig.set(2, 20);
|
|
|
|
orig.set(3, 30);
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>(20));
|
|
|
|
|
|
|
|
HashMap<int, int> second = move(orig);
|
|
|
|
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(0));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>());
|
|
|
|
EXPECT_EQ(second.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(second.get(2), Optional<int>(20));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(move_assign)
|
|
|
|
{
|
|
|
|
HashMap<int, int> orig;
|
|
|
|
HashMap<int, int> second;
|
|
|
|
orig.set(1, 10);
|
|
|
|
orig.set(2, 20);
|
|
|
|
orig.set(3, 30);
|
|
|
|
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>(20));
|
|
|
|
EXPECT_EQ(second.size(), static_cast<size_t>(0));
|
|
|
|
EXPECT_EQ(second.get(2), Optional<int>());
|
|
|
|
|
|
|
|
// 'Hashtable::operator=(Hashtable&&)' allocates temporarily an empty table,
|
|
|
|
// so we can't use NoAllocationGuard here. :(
|
|
|
|
second = move(orig);
|
|
|
|
|
|
|
|
EXPECT_EQ(orig.size(), static_cast<size_t>(0));
|
|
|
|
EXPECT_EQ(orig.get(2), Optional<int>());
|
|
|
|
EXPECT_EQ(second.size(), static_cast<size_t>(3));
|
|
|
|
EXPECT_EQ(second.get(2), Optional<int>(20));
|
|
|
|
}
|