1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/url_pattern_index/closed_hash_map.h"
#include <string>
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "components/url_pattern_index/uint64_hasher.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace url_pattern_index {
namespace {
template <typename MapType>
void ExpectHashMapIntegrity(const MapType& map, uint32_t min_capacity = 0) {
EXPECT_EQ(map.entries().size(), map.size());
EXPECT_EQ(map.hash_table().size(), map.table_size());
EXPECT_LE(map.size() * 2, map.table_size());
EXPECT_LE(min_capacity * 2, map.table_size());
std::vector<bool> entry_is_referenced(map.size());
for (uint32_t i = 0; i < map.table_size(); ++i) {
SCOPED_TRACE(testing::Message() << "Hash-table slot: " << i);
const uint32_t entry_index = map.hash_table()[i];
if (static_cast<uint32_t>(entry_index) >= map.size())
continue;
EXPECT_FALSE(entry_is_referenced[entry_index]);
entry_is_referenced[entry_index] = true;
}
for (uint32_t i = 0; i < map.size(); ++i) {
SCOPED_TRACE(testing::Message() << "Hash-table entry index: " << i);
EXPECT_TRUE(entry_is_referenced[i]);
}
}
template <typename MapType>
void ExpectEmptyMap(const MapType& map, uint32_t min_capacity) {
ExpectHashMapIntegrity(map, min_capacity);
EXPECT_EQ(0u, map.size());
}
} // namespace
TEST(ClosedHashMapTest, EmptyMapDefault) {
HashMap<int, int> hm;
ExpectEmptyMap(hm, 0);
EXPECT_EQ(nullptr, hm.Get(0));
EXPECT_EQ(nullptr, hm.Get(100500));
EXPECT_GT(hm.table_size(), 0u);
}
TEST(ClosedHashMapTest, EmptyMapWithCapacity) {
HashMap<int, int> hm(100);
ExpectEmptyMap(hm, 100);
EXPECT_EQ(nullptr, hm.Get(0));
EXPECT_EQ(nullptr, hm.Get(100500));
}
TEST(ClosedHashMapTest, InsertDistinctAndGet) {
HashMap<int, int> hm;
static const int kKeys[] = {1, 5, 10, 3, -100500};
for (int key : kKeys) {
EXPECT_TRUE(hm.Insert(key, -key));
ExpectHashMapIntegrity(hm);
}
for (int key : kKeys) {
const int* value = hm.Get(key);
ASSERT_NE(nullptr, value);
EXPECT_EQ(-key, *value);
}
EXPECT_EQ(nullptr, hm.Get(1234567));
}
TEST(ClosedHashMapTest, InsertExistingAndGet) {
HashMap<int, int> hm;
EXPECT_TRUE(hm.Insert(123, -123));
EXPECT_FALSE(hm.Insert(123, -124));
ExpectHashMapIntegrity(hm);
const int* value = hm.Get(123);
ASSERT_NE(nullptr, value);
EXPECT_EQ(-123, *value);
}
TEST(ClosedHashMapTest, InsertManyKeysWithCustomHasher) {
using CustomProber = SimpleQuadraticProber<uint64_t, Uint64ToUint32Hasher>;
ClosedHashMap<uint64_t, std::string, CustomProber> hm;
ExpectEmptyMap(hm, 0);
std::vector<std::pair<uint64_t, std::string>> entries;
for (int key = 10, i = 0; key < 1000000; key += ++i) {
entries.push_back(std::make_pair(key, base::NumberToString(key)));
}
uint32_t expected_size = 0;
for (const auto& entry : entries) {
EXPECT_TRUE(hm.Insert(entry.first, entry.second));
EXPECT_FALSE(hm.Insert(entry.first, "-1"));
++expected_size;
EXPECT_EQ(expected_size, hm.size());
EXPECT_LE(expected_size * 2, hm.table_size());
}
ExpectHashMapIntegrity(hm);
for (const auto& entry : entries) {
const std::string* value = hm.Get(entry.first);
ASSERT_NE(nullptr, value);
EXPECT_EQ(entry.second, *value);
}
}
TEST(ClosedHashMapTest, OperatorBrackets) {
HashMap<int, int> hm;
for (int i = 0; i < 5; ++i) {
const uint32_t expected_size = i ? 1 : 0;
EXPECT_EQ(expected_size, hm.size());
int expected_value = (i + 1) * 10;
hm[123] = expected_value;
EXPECT_EQ(1u, hm.size());
const int* value_ptr = hm.Get(123);
ASSERT_NE(nullptr, value_ptr);
EXPECT_EQ(expected_value, *value_ptr);
EXPECT_EQ(expected_value, hm[123]);
EXPECT_EQ(1u, hm.size());
expected_value *= 100;
hm[123] = expected_value;
EXPECT_EQ(expected_value, hm[123]);
}
}
TEST(ClosedHashMapTest, ManualRehash) {
HashMap<int, int> hm(3);
const uint32_t expected_table_size = hm.table_size();
static const int kKeys[] = {1, 5, 10};
for (int key : kKeys) {
EXPECT_TRUE(hm.Insert(key, -key));
}
// No rehashing occurred.
EXPECT_EQ(expected_table_size, hm.table_size());
for (int i = 1; i <= 2; ++i) {
for (int key : kKeys) {
const int* value = hm.Get(key);
ASSERT_NE(nullptr, value);
EXPECT_EQ(-key, *value);
}
hm.Rehash(100 * i);
ExpectHashMapIntegrity(hm, 100 * i);
}
}
} // namespace url_pattern_index
|