| // Copyright 2022 The Tint Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "src/tint/utils/containers/hashmap.h" |
| |
| #include <array> |
| #include <random> |
| #include <string> |
| #include <tuple> |
| #include <unordered_map> |
| |
| #include "gmock/gmock.h" |
| |
| namespace tint { |
| namespace { |
| |
| constexpr std::array kPrimes{ |
| 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, |
| 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, |
| 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, |
| 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, |
| 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, |
| }; |
| |
| TEST(Hashmap, Empty) { |
| Hashmap<std::string, int, 8> map; |
| EXPECT_EQ(map.Count(), 0u); |
| } |
| |
| TEST(Hashmap, AddRemove) { |
| Hashmap<std::string, std::string, 8> map; |
| EXPECT_TRUE(map.Add("hello", "world")); |
| EXPECT_EQ(map.Get("hello"), "world"); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_TRUE(map.Contains("hello")); |
| EXPECT_FALSE(map.Contains("world")); |
| EXPECT_FALSE(map.Add("hello", "cat")); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_TRUE(map.Remove("hello")); |
| EXPECT_EQ(map.Count(), 0u); |
| EXPECT_FALSE(map.Contains("hello")); |
| EXPECT_FALSE(map.Contains("world")); |
| } |
| |
| TEST(Hashmap, ReplaceRemove) { |
| Hashmap<std::string, std::string, 8> map; |
| map.Replace("hello", "world"); |
| EXPECT_EQ(map.Get("hello"), "world"); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_TRUE(map.Contains("hello")); |
| EXPECT_FALSE(map.Contains("world")); |
| map.Replace("hello", "cat"); |
| EXPECT_EQ(map.Get("hello"), "cat"); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_TRUE(map.Remove("hello")); |
| EXPECT_EQ(map.Count(), 0u); |
| EXPECT_FALSE(map.Contains("hello")); |
| EXPECT_FALSE(map.Contains("world")); |
| } |
| |
| TEST(Hashmap, Generation) { |
| Hashmap<int, std::string, 8> map; |
| EXPECT_EQ(map.Generation(), 0u); |
| map.Add(1, "one"); |
| EXPECT_EQ(map.Generation(), 1u); |
| map.Add(1, "uno"); |
| EXPECT_EQ(map.Generation(), 1u); |
| map.Replace(1, "une"); |
| EXPECT_EQ(map.Generation(), 2u); |
| map.Add(2, "dos"); |
| EXPECT_EQ(map.Generation(), 3u); |
| map.Remove(1); |
| EXPECT_EQ(map.Generation(), 4u); |
| map.Clear(); |
| EXPECT_EQ(map.Generation(), 5u); |
| map.Find(2); |
| EXPECT_EQ(map.Generation(), 5u); |
| map.Get(2); |
| EXPECT_EQ(map.Generation(), 5u); |
| } |
| |
| TEST(Hashmap, Index) { |
| Hashmap<int, std::string, 4> map; |
| auto zero = map.Find(0); |
| EXPECT_FALSE(zero); |
| |
| map.Add(3, "three"); |
| auto three = map.Find(3); |
| map.Add(2, "two"); |
| auto two = map.Find(2); |
| map.Add(4, "four"); |
| auto four = map.Find(4); |
| map.Add(8, "eight"); |
| auto eight = map.Find(8); |
| |
| EXPECT_FALSE(zero); |
| ASSERT_TRUE(three); |
| ASSERT_TRUE(two); |
| ASSERT_TRUE(four); |
| ASSERT_TRUE(eight); |
| |
| EXPECT_EQ(*three, "three"); |
| EXPECT_EQ(*two, "two"); |
| EXPECT_EQ(*four, "four"); |
| EXPECT_EQ(*eight, "eight"); |
| |
| map.Add(0, "zero"); // Note: Find called before Add() is okay! |
| |
| map.Add(5, "five"); |
| auto five = map.Find(5); |
| map.Add(6, "six"); |
| auto six = map.Find(6); |
| map.Add(1, "one"); |
| auto one = map.Find(1); |
| map.Add(7, "seven"); |
| auto seven = map.Find(7); |
| |
| ASSERT_TRUE(zero); |
| ASSERT_TRUE(three); |
| ASSERT_TRUE(two); |
| ASSERT_TRUE(four); |
| ASSERT_TRUE(eight); |
| ASSERT_TRUE(five); |
| ASSERT_TRUE(six); |
| ASSERT_TRUE(one); |
| ASSERT_TRUE(seven); |
| |
| EXPECT_EQ(*zero, "zero"); |
| EXPECT_EQ(*three, "three"); |
| EXPECT_EQ(*two, "two"); |
| EXPECT_EQ(*four, "four"); |
| EXPECT_EQ(*eight, "eight"); |
| EXPECT_EQ(*five, "five"); |
| EXPECT_EQ(*six, "six"); |
| EXPECT_EQ(*one, "one"); |
| EXPECT_EQ(*seven, "seven"); |
| |
| map.Remove(2); |
| map.Remove(8); |
| map.Remove(1); |
| |
| EXPECT_FALSE(two); |
| EXPECT_FALSE(eight); |
| EXPECT_FALSE(one); |
| } |
| |
| TEST(Hashmap, StringKeys) { |
| Hashmap<std::string, int, 4> map; |
| EXPECT_FALSE(map.Find("zero")); |
| EXPECT_FALSE(map.Find(std::string("zero"))); |
| EXPECT_FALSE(map.Find(std::string_view("zero"))); |
| |
| map.Add("three", 3); |
| auto three_cstr = map.Find("three"); |
| auto three_str = map.Find(std::string("three")); |
| auto three_sv = map.Find(std::string_view("three")); |
| map.Add(std::string("two"), 2); |
| auto two_cstr = map.Find("two"); |
| auto two_str = map.Find(std::string("two")); |
| auto two_sv = map.Find(std::string_view("two")); |
| map.Add("four", 4); |
| auto four_cstr = map.Find("four"); |
| auto four_str = map.Find(std::string("four")); |
| auto four_sv = map.Find(std::string_view("four")); |
| map.Add(std::string("eight"), 8); |
| auto eight_cstr = map.Find("eight"); |
| auto eight_str = map.Find(std::string("eight")); |
| auto eight_sv = map.Find(std::string_view("eight")); |
| |
| ASSERT_TRUE(three_cstr); |
| ASSERT_TRUE(three_str); |
| ASSERT_TRUE(three_sv); |
| ASSERT_TRUE(two_cstr); |
| ASSERT_TRUE(two_str); |
| ASSERT_TRUE(two_sv); |
| ASSERT_TRUE(four_cstr); |
| ASSERT_TRUE(four_str); |
| ASSERT_TRUE(four_sv); |
| ASSERT_TRUE(eight_cstr); |
| ASSERT_TRUE(eight_str); |
| ASSERT_TRUE(eight_sv); |
| |
| EXPECT_EQ(*three_cstr, 3); |
| EXPECT_EQ(*three_str, 3); |
| EXPECT_EQ(*three_sv, 3); |
| EXPECT_EQ(*two_cstr, 2); |
| EXPECT_EQ(*two_str, 2); |
| EXPECT_EQ(*two_sv, 2); |
| EXPECT_EQ(*four_cstr, 4); |
| EXPECT_EQ(*four_str, 4); |
| EXPECT_EQ(*four_sv, 4); |
| EXPECT_EQ(*eight_cstr, 8); |
| EXPECT_EQ(*eight_str, 8); |
| EXPECT_EQ(*eight_sv, 8); |
| |
| map.Add("zero", 0); // Note: Find called before Add() is okay! |
| auto zero_cstr = map.Find("zero"); |
| auto zero_str = map.Find(std::string("zero")); |
| auto zero_sv = map.Find(std::string_view("zero")); |
| |
| map.Add(std::string("five"), 5); |
| auto five_cstr = map.Find("five"); |
| auto five_str = map.Find(std::string("five")); |
| auto five_sv = map.Find(std::string_view("five")); |
| map.Add("six", 6); |
| auto six_cstr = map.Find("six"); |
| auto six_str = map.Find(std::string("six")); |
| auto six_sv = map.Find(std::string_view("six")); |
| map.Add("one", 1); |
| auto one_cstr = map.Find("one"); |
| auto one_str = map.Find(std::string("one")); |
| auto one_sv = map.Find(std::string_view("one")); |
| map.Add(std::string("seven"), 7); |
| auto seven_cstr = map.Find("seven"); |
| auto seven_str = map.Find(std::string("seven")); |
| auto seven_sv = map.Find(std::string_view("seven")); |
| |
| ASSERT_TRUE(zero_cstr); |
| ASSERT_TRUE(zero_str); |
| ASSERT_TRUE(zero_sv); |
| ASSERT_TRUE(three_cstr); |
| ASSERT_TRUE(three_str); |
| ASSERT_TRUE(three_sv); |
| ASSERT_TRUE(two_cstr); |
| ASSERT_TRUE(two_str); |
| ASSERT_TRUE(two_sv); |
| ASSERT_TRUE(four_cstr); |
| ASSERT_TRUE(four_str); |
| ASSERT_TRUE(four_sv); |
| ASSERT_TRUE(eight_cstr); |
| ASSERT_TRUE(eight_str); |
| ASSERT_TRUE(eight_sv); |
| ASSERT_TRUE(five_cstr); |
| ASSERT_TRUE(five_str); |
| ASSERT_TRUE(five_sv); |
| ASSERT_TRUE(six_cstr); |
| ASSERT_TRUE(six_str); |
| ASSERT_TRUE(six_sv); |
| ASSERT_TRUE(one_cstr); |
| ASSERT_TRUE(one_str); |
| ASSERT_TRUE(one_sv); |
| ASSERT_TRUE(seven_cstr); |
| ASSERT_TRUE(seven_str); |
| ASSERT_TRUE(seven_sv); |
| |
| EXPECT_EQ(*zero_cstr, 0); |
| EXPECT_EQ(*zero_str, 0); |
| EXPECT_EQ(*zero_sv, 0); |
| EXPECT_EQ(*three_cstr, 3); |
| EXPECT_EQ(*three_str, 3); |
| EXPECT_EQ(*three_sv, 3); |
| EXPECT_EQ(*two_cstr, 2); |
| EXPECT_EQ(*two_str, 2); |
| EXPECT_EQ(*two_sv, 2); |
| EXPECT_EQ(*four_cstr, 4); |
| EXPECT_EQ(*four_str, 4); |
| EXPECT_EQ(*four_sv, 4); |
| EXPECT_EQ(*eight_cstr, 8); |
| EXPECT_EQ(*eight_str, 8); |
| EXPECT_EQ(*eight_sv, 8); |
| EXPECT_EQ(*five_cstr, 5); |
| EXPECT_EQ(*five_str, 5); |
| EXPECT_EQ(*five_sv, 5); |
| EXPECT_EQ(*six_cstr, 6); |
| EXPECT_EQ(*six_str, 6); |
| EXPECT_EQ(*six_sv, 6); |
| EXPECT_EQ(*one_cstr, 1); |
| EXPECT_EQ(*one_str, 1); |
| EXPECT_EQ(*one_sv, 1); |
| EXPECT_EQ(*seven_cstr, 7); |
| EXPECT_EQ(*seven_str, 7); |
| EXPECT_EQ(*seven_sv, 7); |
| } |
| |
| TEST(Hashmap, Iterator) { |
| using Map = Hashmap<int, std::string, 8>; |
| using Entry = typename Map::Entry; |
| Map map; |
| map.Add(1, "one"); |
| map.Add(4, "four"); |
| map.Add(3, "three"); |
| map.Add(2, "two"); |
| EXPECT_THAT(map, testing::UnorderedElementsAre(Entry{1, "one"}, Entry{2, "two"}, |
| Entry{3, "three"}, Entry{4, "four"})); |
| } |
| |
| TEST(Hashmap, MutableIterator) { |
| using Map = Hashmap<int, std::string, 8>; |
| using Entry = typename Map::Entry; |
| Map map; |
| map.Add(1, "one"); |
| map.Add(4, "four"); |
| map.Add(3, "three"); |
| map.Add(2, "two"); |
| for (auto pair : map) { |
| pair.value += "!"; |
| } |
| EXPECT_THAT(map, testing::UnorderedElementsAre(Entry{1, "one!"}, Entry{2, "two!"}, |
| Entry{3, "three!"}, Entry{4, "four!"})); |
| } |
| |
| TEST(Hashmap, KeysValues) { |
| using Map = Hashmap<int, std::string, 8>; |
| Map map; |
| map.Add(1, "one"); |
| map.Add(4, "four"); |
| map.Add(3, "three"); |
| map.Add(2, "two"); |
| EXPECT_THAT(map.Keys(), testing::UnorderedElementsAre(1, 2, 3, 4)); |
| EXPECT_THAT(map.Values(), testing::UnorderedElementsAre("one", "two", "three", "four")); |
| } |
| |
| TEST(Hashmap, AddMany) { |
| Hashmap<int, std::string, 8> map; |
| for (size_t i = 0; i < kPrimes.size(); i++) { |
| int prime = kPrimes[i]; |
| ASSERT_TRUE(map.Add(prime, std::to_string(prime))) << "i: " << i; |
| ASSERT_FALSE(map.Add(prime, std::to_string(prime))) << "i: " << i; |
| ASSERT_EQ(map.Count(), i + 1); |
| } |
| ASSERT_EQ(map.Count(), kPrimes.size()); |
| for (int prime : kPrimes) { |
| ASSERT_TRUE(map.Contains(prime)) << prime; |
| ASSERT_EQ(map.Get(prime), std::to_string(prime)) << prime; |
| } |
| } |
| |
| TEST(Hashmap, GetOrCreate) { |
| Hashmap<int, std::string, 8> map; |
| std::optional<std::string> value_of_key_0_at_create; |
| EXPECT_EQ(map.GetOrCreate(0, |
| [&] { |
| value_of_key_0_at_create = map.Get(0); |
| return "zero"; |
| }), |
| "zero"); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_EQ(map.Get(0), "zero"); |
| EXPECT_EQ(value_of_key_0_at_create, ""); |
| |
| bool create_called = false; |
| EXPECT_EQ(map.GetOrCreate(0, |
| [&] { |
| create_called = true; |
| return "oh noes"; |
| }), |
| "zero"); |
| EXPECT_FALSE(create_called); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_EQ(map.Get(0), "zero"); |
| |
| EXPECT_EQ(map.GetOrCreate(1, [&] { return "one"; }), "one"); |
| EXPECT_EQ(map.Count(), 2u); |
| EXPECT_EQ(map.Get(1), "one"); |
| } |
| |
| TEST(Hashmap, GetOrCreate_CreateModifiesMap) { |
| Hashmap<int, std::string, 8> map; |
| EXPECT_EQ(map.GetOrCreate(0, |
| [&] { |
| map.Add(3, "three"); |
| map.Add(1, "one"); |
| map.Add(2, "two"); |
| return "zero"; |
| }), |
| "zero"); |
| EXPECT_EQ(map.Count(), 4u); |
| EXPECT_EQ(map.Get(0), "zero"); |
| EXPECT_EQ(map.Get(1), "one"); |
| EXPECT_EQ(map.Get(2), "two"); |
| EXPECT_EQ(map.Get(3), "three"); |
| |
| bool create_called = false; |
| EXPECT_EQ(map.GetOrCreate(0, |
| [&] { |
| create_called = true; |
| return "oh noes"; |
| }), |
| "zero"); |
| EXPECT_FALSE(create_called); |
| EXPECT_EQ(map.Count(), 4u); |
| EXPECT_EQ(map.Get(0), "zero"); |
| EXPECT_EQ(map.Get(1), "one"); |
| EXPECT_EQ(map.Get(2), "two"); |
| EXPECT_EQ(map.Get(3), "three"); |
| |
| EXPECT_EQ(map.GetOrCreate(4, |
| [&] { |
| map.Add(6, "six"); |
| map.Add(5, "five"); |
| map.Add(7, "seven"); |
| return "four"; |
| }), |
| "four"); |
| EXPECT_EQ(map.Count(), 8u); |
| EXPECT_EQ(map.Get(0), "zero"); |
| EXPECT_EQ(map.Get(1), "one"); |
| EXPECT_EQ(map.Get(2), "two"); |
| EXPECT_EQ(map.Get(3), "three"); |
| EXPECT_EQ(map.Get(4), "four"); |
| EXPECT_EQ(map.Get(5), "five"); |
| EXPECT_EQ(map.Get(6), "six"); |
| EXPECT_EQ(map.Get(7), "seven"); |
| } |
| |
| TEST(Hashmap, GetOrCreate_CreateAddsSameKeyedValue) { |
| Hashmap<int, std::string, 8> map; |
| EXPECT_EQ(map.GetOrCreate(42, |
| [&] { |
| map.Add(42, "should-be-replaced"); |
| return "expected-value"; |
| }), |
| "expected-value"); |
| EXPECT_EQ(map.Count(), 1u); |
| EXPECT_EQ(map.Get(42), "expected-value"); |
| } |
| |
| TEST(Hashmap, Soak) { |
| std::mt19937 rnd; |
| std::unordered_map<std::string, std::string> reference; |
| Hashmap<std::string, std::string, 8> map; |
| for (size_t i = 0; i < 1000000; i++) { |
| std::string key = std::to_string(rnd() & 64); |
| std::string value = "V" + key; |
| switch (rnd() % 7) { |
| case 0: { // Add |
| auto expected = reference.emplace(key, value).second; |
| EXPECT_EQ(map.Add(key, value), expected) << "i:" << i; |
| EXPECT_EQ(map.Get(key), value) << "i:" << i; |
| EXPECT_TRUE(map.Contains(key)) << "i:" << i; |
| break; |
| } |
| case 1: { // Replace |
| reference[key] = value; |
| map.Replace(key, value); |
| EXPECT_EQ(map.Get(key), value) << "i:" << i; |
| EXPECT_TRUE(map.Contains(key)) << "i:" << i; |
| break; |
| } |
| case 2: { // Remove |
| auto expected = reference.erase(key) != 0; |
| EXPECT_EQ(map.Remove(key), expected) << "i:" << i; |
| EXPECT_FALSE(map.Get(key).has_value()) << "i:" << i; |
| EXPECT_FALSE(map.Contains(key)) << "i:" << i; |
| break; |
| } |
| case 3: { // Contains |
| auto expected = reference.count(key) != 0; |
| EXPECT_EQ(map.Contains(key), expected) << "i:" << i; |
| break; |
| } |
| case 4: { // Get |
| if (reference.count(key) != 0) { |
| auto expected = reference[key]; |
| EXPECT_EQ(map.Get(key), expected) << "i:" << i; |
| } else { |
| EXPECT_FALSE(map.Get(key).has_value()) << "i:" << i; |
| } |
| break; |
| } |
| case 5: { // Copy / Move |
| Hashmap<std::string, std::string, 8> tmp(map); |
| map = std::move(tmp); |
| break; |
| } |
| case 6: { // Clear |
| reference.clear(); |
| map.Clear(); |
| break; |
| } |
| } |
| } |
| } |
| |
| TEST(Hashmap, EqualitySameSize) { |
| Hashmap<int, std::string, 8> a; |
| Hashmap<int, std::string, 8> b; |
| EXPECT_EQ(a, b); |
| a.Add(1, "one"); |
| EXPECT_NE(a, b); |
| b.Add(2, "two"); |
| EXPECT_NE(a, b); |
| a.Add(2, "two"); |
| EXPECT_NE(a, b); |
| b.Add(1, "one"); |
| EXPECT_EQ(a, b); |
| } |
| |
| TEST(Hashmap, EqualityDifferentSize) { |
| Hashmap<int, std::string, 8> a; |
| Hashmap<int, std::string, 4> b; |
| EXPECT_EQ(a, b); |
| a.Add(1, "one"); |
| EXPECT_NE(a, b); |
| b.Add(2, "two"); |
| EXPECT_NE(a, b); |
| a.Add(2, "two"); |
| EXPECT_NE(a, b); |
| b.Add(1, "one"); |
| EXPECT_EQ(a, b); |
| } |
| |
| TEST(Hashmap, HashSameSize) { |
| Hashmap<int, std::string, 8> a; |
| Hashmap<int, std::string, 8> b; |
| EXPECT_EQ(Hash(a), Hash(b)); |
| a.Add(1, "one"); |
| b.Add(2, "two"); |
| a.Add(2, "two"); |
| b.Add(1, "one"); |
| EXPECT_EQ(Hash(a), Hash(b)); |
| } |
| |
| TEST(Hashmap, HashDifferentSize) { |
| Hashmap<int, std::string, 8> a; |
| Hashmap<int, std::string, 4> b; |
| EXPECT_EQ(Hash(a), Hash(b)); |
| a.Add(1, "one"); |
| b.Add(2, "two"); |
| a.Add(2, "two"); |
| b.Add(1, "one"); |
| EXPECT_EQ(Hash(a), Hash(b)); |
| } |
| |
| } // namespace |
| } // namespace tint |