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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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 "hash_set.h"
#include <forward_list>
#include <map>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
#include <gtest/gtest.h>
#include "hash_map.h"
namespace art {
struct IsEmptyFnString {
void MakeEmpty(std::string& item) const {
item.clear();
}
bool IsEmpty(const std::string& item) const {
return item.empty();
}
};
class HashSetTest : public testing::Test {
public:
HashSetTest() : seed_(97421), unique_number_(0) {
}
std::string RandomString(size_t len) {
std::ostringstream oss;
for (size_t i = 0; i < len; ++i) {
oss << static_cast<char>('A' + PRand() % 64);
}
static_assert(' ' < 'A', "space must be less than a");
oss << " " << unique_number_++; // Relies on ' ' < 'A'
return oss.str();
}
void SetSeed(size_t seed) {
seed_ = seed;
}
size_t PRand() { // Pseudo random.
seed_ = seed_ * 1103515245 + 12345;
return seed_;
}
private:
size_t seed_;
size_t unique_number_;
};
TEST_F(HashSetTest, TestSmoke) {
HashSet<std::string, IsEmptyFnString> hash_set;
const std::string test_string = "hello world 1234";
ASSERT_TRUE(hash_set.empty());
ASSERT_EQ(hash_set.size(), 0U);
hash_set.insert(test_string);
auto it = hash_set.find(test_string);
ASSERT_EQ(*it, test_string);
auto after_it = hash_set.erase(it);
ASSERT_TRUE(after_it == hash_set.end());
ASSERT_TRUE(hash_set.empty());
ASSERT_EQ(hash_set.size(), 0U);
it = hash_set.find(test_string);
ASSERT_TRUE(it == hash_set.end());
}
TEST_F(HashSetTest, TestInsertAndErase) {
HashSet<std::string, IsEmptyFnString> hash_set;
static constexpr size_t count = 1000;
std::vector<std::string> strings;
for (size_t i = 0; i < count; ++i) {
// Insert a bunch of elements and make sure we can find them.
strings.push_back(RandomString(10));
hash_set.insert(strings[i]);
auto it = hash_set.find(strings[i]);
ASSERT_TRUE(it != hash_set.end());
ASSERT_EQ(*it, strings[i]);
}
ASSERT_EQ(strings.size(), hash_set.size());
// Try to erase the odd strings.
for (size_t i = 1; i < count; i += 2) {
auto it = hash_set.find(strings[i]);
ASSERT_TRUE(it != hash_set.end());
ASSERT_EQ(*it, strings[i]);
hash_set.erase(it);
}
// Test removed.
for (size_t i = 1; i < count; i += 2) {
auto it = hash_set.find(strings[i]);
ASSERT_TRUE(it == hash_set.end());
}
for (size_t i = 0; i < count; i += 2) {
auto it = hash_set.find(strings[i]);
ASSERT_TRUE(it != hash_set.end());
ASSERT_EQ(*it, strings[i]);
}
}
TEST_F(HashSetTest, TestIterator) {
HashSet<std::string, IsEmptyFnString> hash_set;
ASSERT_TRUE(hash_set.begin() == hash_set.end());
static constexpr size_t count = 1000;
std::vector<std::string> strings;
for (size_t i = 0; i < count; ++i) {
// Insert a bunch of elements and make sure we can find them.
strings.push_back(RandomString(10));
hash_set.insert(strings[i]);
}
// Make sure we visit each string exactly once.
std::map<std::string, size_t> found_count;
for (const std::string& s : hash_set) {
++found_count[s];
}
for (size_t i = 0; i < count; ++i) {
ASSERT_EQ(found_count[strings[i]], 1U);
}
found_count.clear();
// Remove all the elements with iterator erase.
for (auto it = hash_set.begin(); it != hash_set.end();) {
++found_count[*it];
it = hash_set.erase(it);
ASSERT_EQ(hash_set.Verify(), 0U);
}
for (size_t i = 0; i < count; ++i) {
ASSERT_EQ(found_count[strings[i]], 1U);
}
}
TEST_F(HashSetTest, TestSwap) {
HashSet<std::string, IsEmptyFnString> hash_seta, hash_setb;
std::vector<std::string> strings;
static constexpr size_t count = 1000;
for (size_t i = 0; i < count; ++i) {
strings.push_back(RandomString(10));
hash_seta.insert(strings[i]);
}
std::swap(hash_seta, hash_setb);
hash_seta.insert("TEST");
hash_setb.insert("TEST2");
for (size_t i = 0; i < count; ++i) {
strings.push_back(RandomString(10));
hash_seta.insert(strings[i]);
}
}
TEST_F(HashSetTest, TestShrink) {
HashSet<std::string, IsEmptyFnString> hash_set;
std::vector<std::string> strings = {"a", "b", "c", "d", "e", "f", "g"};
for (size_t i = 0; i < strings.size(); ++i) {
// Insert some strings into the beginning of our hash set to establish an initial size
hash_set.insert(strings[i]);
}
hash_set.ShrinkToMaximumLoad();
const double initial_load = hash_set.CalculateLoadFactor();
// Insert a bunch of random strings to guarantee that we grow the capacity.
std::vector<std::string> random_strings;
static constexpr size_t count = 1000;
for (size_t i = 0; i < count; ++i) {
random_strings.push_back(RandomString(10));
hash_set.insert(random_strings[i]);
}
// Erase all the extra strings which guarantees that our load factor will be really bad.
for (size_t i = 0; i < count; ++i) {
hash_set.erase(hash_set.find(random_strings[i]));
}
const double bad_load = hash_set.CalculateLoadFactor();
EXPECT_GT(initial_load, bad_load);
// Shrink again, the load factor should be good again.
hash_set.ShrinkToMaximumLoad();
EXPECT_DOUBLE_EQ(initial_load, hash_set.CalculateLoadFactor());
// Make sure all the initial elements we had are still there
for (const std::string& initial_string : strings) {
EXPECT_NE(hash_set.end(), hash_set.find(initial_string))
<< "expected to find " << initial_string;
}
}
TEST_F(HashSetTest, TestLoadFactor) {
HashSet<std::string, IsEmptyFnString> hash_set;
static constexpr size_t kStringCount = 1000;
static constexpr double kEpsilon = 0.01;
for (size_t i = 0; i < kStringCount; ++i) {
hash_set.insert(RandomString(i % 10 + 1));
}
// Check that changing the load factor resizes the table to be within the target range.
EXPECT_GE(hash_set.CalculateLoadFactor() + kEpsilon, hash_set.GetMinLoadFactor());
EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
hash_set.SetLoadFactor(0.1, 0.3);
EXPECT_DOUBLE_EQ(0.1, hash_set.GetMinLoadFactor());
EXPECT_DOUBLE_EQ(0.3, hash_set.GetMaxLoadFactor());
EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
hash_set.SetLoadFactor(0.6, 0.8);
EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
}
TEST_F(HashSetTest, TestStress) {
HashSet<std::string, IsEmptyFnString> hash_set;
std::unordered_set<std::string> std_set;
std::vector<std::string> strings;
static constexpr size_t string_count = 2000;
static constexpr size_t operations = 100000;
static constexpr size_t target_size = 5000;
for (size_t i = 0; i < string_count; ++i) {
strings.push_back(RandomString(i % 10 + 1));
}
const size_t seed = time(nullptr);
SetSeed(seed);
LOG(INFO) << "Starting stress test with seed " << seed;
for (size_t i = 0; i < operations; ++i) {
ASSERT_EQ(hash_set.size(), std_set.size());
size_t delta = std::abs(static_cast<ssize_t>(target_size) -
static_cast<ssize_t>(hash_set.size()));
size_t n = PRand();
if (n % target_size == 0) {
hash_set.clear();
std_set.clear();
ASSERT_TRUE(hash_set.empty());
ASSERT_TRUE(std_set.empty());
} else if (n % target_size < delta) {
// Skew towards adding elements until we are at the desired size.
const std::string& s = strings[PRand() % string_count];
hash_set.insert(s);
std_set.insert(s);
ASSERT_EQ(*hash_set.find(s), *std_set.find(s));
} else {
const std::string& s = strings[PRand() % string_count];
auto it1 = hash_set.find(s);
auto it2 = std_set.find(s);
ASSERT_EQ(it1 == hash_set.end(), it2 == std_set.end());
if (it1 != hash_set.end()) {
ASSERT_EQ(*it1, *it2);
hash_set.erase(it1);
std_set.erase(it2);
}
}
}
}
struct IsEmptyStringPair {
void MakeEmpty(std::pair<std::string, int>& pair) const {
pair.first.clear();
}
bool IsEmpty(const std::pair<std::string, int>& pair) const {
return pair.first.empty();
}
};
TEST_F(HashSetTest, TestHashMap) {
HashMap<std::string, int, IsEmptyStringPair> hash_map;
hash_map.insert(std::make_pair(std::string("abcd"), 123));
hash_map.insert(std::make_pair(std::string("abcd"), 124));
hash_map.insert(std::make_pair(std::string("bags"), 444));
auto it = hash_map.find(std::string("abcd"));
ASSERT_EQ(it->second, 123);
hash_map.erase(it);
it = hash_map.find(std::string("abcd"));
ASSERT_EQ(it, hash_map.end());
}
struct IsEmptyFnVectorInt {
void MakeEmpty(std::vector<int>& item) const {
item.clear();
}
bool IsEmpty(const std::vector<int>& item) const {
return item.empty();
}
};
template <typename T>
size_t HashIntSequence(T begin, T end) {
size_t hash = 0;
for (auto iter = begin; iter != end; ++iter) {
hash = hash * 2 + *iter;
}
return hash;
}
struct VectorIntHashEquals {
std::size_t operator()(const std::vector<int>& item) const {
return HashIntSequence(item.begin(), item.end());
}
std::size_t operator()(const std::forward_list<int>& item) const {
return HashIntSequence(item.begin(), item.end());
}
bool operator()(const std::vector<int>& a, const std::vector<int>& b) const {
return a == b;
}
bool operator()(const std::vector<int>& a, const std::forward_list<int>& b) const {
auto aiter = a.begin();
auto biter = b.begin();
while (aiter != a.end() && biter != b.end()) {
if (*aiter != *biter) {
return false;
}
aiter++;
biter++;
}
return (aiter == a.end() && biter == b.end());
}
};
TEST_F(HashSetTest, TestLookupByAlternateKeyType) {
HashSet<std::vector<int>, IsEmptyFnVectorInt, VectorIntHashEquals, VectorIntHashEquals> hash_set;
hash_set.insert(std::vector<int>({1, 2, 3, 4}));
hash_set.insert(std::vector<int>({4, 2}));
ASSERT_EQ(hash_set.end(), hash_set.find(std::vector<int>({1, 1, 1, 1})));
ASSERT_NE(hash_set.end(), hash_set.find(std::vector<int>({1, 2, 3, 4})));
ASSERT_EQ(hash_set.end(), hash_set.find(std::forward_list<int>({1, 1, 1, 1})));
ASSERT_NE(hash_set.end(), hash_set.find(std::forward_list<int>({1, 2, 3, 4})));
}
TEST_F(HashSetTest, TestReserve) {
HashSet<std::string, IsEmptyFnString> hash_set;
std::vector<size_t> sizes = {1, 10, 25, 55, 128, 1024, 4096};
for (size_t size : sizes) {
hash_set.reserve(size);
const size_t buckets_before = hash_set.NumBuckets();
// Check that we expanded enough.
CHECK_GE(hash_set.ElementsUntilExpand(), size);
// Try inserting elements until we are at our reserve size and ensure the hash set did not
// expand.
while (hash_set.size() < size) {
hash_set.insert(std::to_string(hash_set.size()));
}
CHECK_EQ(hash_set.NumBuckets(), buckets_before);
}
// Check the behaviour for shrinking, it does not necessarily resize down.
constexpr size_t size = 100;
hash_set.reserve(size);
CHECK_GE(hash_set.ElementsUntilExpand(), size);
}
TEST_F(HashSetTest, IteratorConversion) {
const char* test_string = "dummy";
HashSet<std::string> hash_set;
HashSet<std::string>::iterator it = hash_set.insert(test_string).first;
HashSet<std::string>::const_iterator cit = it;
ASSERT_TRUE(it == cit);
ASSERT_EQ(*it, *cit);
}
TEST_F(HashSetTest, StringSearchStringView) {
const char* test_string = "dummy";
HashSet<std::string> hash_set;
HashSet<std::string>::iterator insert_pos = hash_set.insert(test_string).first;
HashSet<std::string>::iterator it = hash_set.find(std::string_view(test_string));
ASSERT_TRUE(it == insert_pos);
}
TEST_F(HashSetTest, DoubleInsert) {
const char* test_string = "dummy";
HashSet<std::string> hash_set;
hash_set.insert(test_string);
hash_set.insert(test_string);
ASSERT_EQ(1u, hash_set.size());
}
} // namespace art
|