1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "hash_set.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <forward_list>
20*795d594fSAndroid Build Coastguard Worker #include <map>
21*795d594fSAndroid Build Coastguard Worker #include <sstream>
22*795d594fSAndroid Build Coastguard Worker #include <string>
23*795d594fSAndroid Build Coastguard Worker #include <string_view>
24*795d594fSAndroid Build Coastguard Worker #include <unordered_set>
25*795d594fSAndroid Build Coastguard Worker #include <vector>
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker #include "hash_map.h"
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker namespace art {
32*795d594fSAndroid Build Coastguard Worker
33*795d594fSAndroid Build Coastguard Worker struct IsEmptyFnString {
MakeEmptyart::IsEmptyFnString34*795d594fSAndroid Build Coastguard Worker void MakeEmpty(std::string& item) const {
35*795d594fSAndroid Build Coastguard Worker item.clear();
36*795d594fSAndroid Build Coastguard Worker }
IsEmptyart::IsEmptyFnString37*795d594fSAndroid Build Coastguard Worker bool IsEmpty(const std::string& item) const {
38*795d594fSAndroid Build Coastguard Worker return item.empty();
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker };
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker class HashSetTest : public testing::Test {
43*795d594fSAndroid Build Coastguard Worker public:
HashSetTest()44*795d594fSAndroid Build Coastguard Worker HashSetTest() : seed_(97421), unique_number_(0) {
45*795d594fSAndroid Build Coastguard Worker }
RandomString(size_t len)46*795d594fSAndroid Build Coastguard Worker std::string RandomString(size_t len) {
47*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
48*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < len; ++i) {
49*795d594fSAndroid Build Coastguard Worker oss << static_cast<char>('A' + PRand() % 64);
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker static_assert(' ' < 'A', "space must be less than a");
52*795d594fSAndroid Build Coastguard Worker oss << " " << unique_number_++; // Relies on ' ' < 'A'
53*795d594fSAndroid Build Coastguard Worker return oss.str();
54*795d594fSAndroid Build Coastguard Worker }
SetSeed(size_t seed)55*795d594fSAndroid Build Coastguard Worker void SetSeed(size_t seed) {
56*795d594fSAndroid Build Coastguard Worker seed_ = seed;
57*795d594fSAndroid Build Coastguard Worker }
PRand()58*795d594fSAndroid Build Coastguard Worker size_t PRand() { // Pseudo random.
59*795d594fSAndroid Build Coastguard Worker seed_ = seed_ * 1103515245 + 12345;
60*795d594fSAndroid Build Coastguard Worker return seed_;
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker private:
64*795d594fSAndroid Build Coastguard Worker size_t seed_;
65*795d594fSAndroid Build Coastguard Worker size_t unique_number_;
66*795d594fSAndroid Build Coastguard Worker };
67*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestSmoke)68*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestSmoke) {
69*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
70*795d594fSAndroid Build Coastguard Worker const std::string test_string = "hello world 1234";
71*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.empty());
72*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.size(), 0U);
73*795d594fSAndroid Build Coastguard Worker hash_set.insert(test_string);
74*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(test_string);
75*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it, test_string);
76*795d594fSAndroid Build Coastguard Worker auto after_it = hash_set.erase(it);
77*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(after_it == hash_set.end());
78*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.empty());
79*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.size(), 0U);
80*795d594fSAndroid Build Coastguard Worker it = hash_set.find(test_string);
81*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it == hash_set.end());
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestInsertAndErase)84*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestInsertAndErase) {
85*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
86*795d594fSAndroid Build Coastguard Worker static constexpr size_t count = 1000;
87*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings;
88*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
89*795d594fSAndroid Build Coastguard Worker // Insert a bunch of elements and make sure we can find them.
90*795d594fSAndroid Build Coastguard Worker strings.push_back(RandomString(10));
91*795d594fSAndroid Build Coastguard Worker hash_set.insert(strings[i]);
92*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(strings[i]);
93*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it != hash_set.end());
94*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it, strings[i]);
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(strings.size(), hash_set.size());
97*795d594fSAndroid Build Coastguard Worker // Try to erase the odd strings.
98*795d594fSAndroid Build Coastguard Worker for (size_t i = 1; i < count; i += 2) {
99*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(strings[i]);
100*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it != hash_set.end());
101*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it, strings[i]);
102*795d594fSAndroid Build Coastguard Worker hash_set.erase(it);
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker // Test removed.
105*795d594fSAndroid Build Coastguard Worker for (size_t i = 1; i < count; i += 2) {
106*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(strings[i]);
107*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it == hash_set.end());
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; i += 2) {
110*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(strings[i]);
111*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it != hash_set.end());
112*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it, strings[i]);
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestIterator)116*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestIterator) {
117*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
118*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.begin() == hash_set.end());
119*795d594fSAndroid Build Coastguard Worker static constexpr size_t count = 1000;
120*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings;
121*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
122*795d594fSAndroid Build Coastguard Worker // Insert a bunch of elements and make sure we can find them.
123*795d594fSAndroid Build Coastguard Worker strings.push_back(RandomString(10));
124*795d594fSAndroid Build Coastguard Worker hash_set.insert(strings[i]);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker // Make sure we visit each string exactly once.
127*795d594fSAndroid Build Coastguard Worker std::map<std::string, size_t> found_count;
128*795d594fSAndroid Build Coastguard Worker for (const std::string& s : hash_set) {
129*795d594fSAndroid Build Coastguard Worker ++found_count[s];
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
132*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(found_count[strings[i]], 1U);
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker found_count.clear();
135*795d594fSAndroid Build Coastguard Worker // Remove all the elements with iterator erase.
136*795d594fSAndroid Build Coastguard Worker for (auto it = hash_set.begin(); it != hash_set.end();) {
137*795d594fSAndroid Build Coastguard Worker ++found_count[*it];
138*795d594fSAndroid Build Coastguard Worker it = hash_set.erase(it);
139*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.Verify(), 0U);
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
142*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(found_count[strings[i]], 1U);
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker }
145*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestSwap)146*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestSwap) {
147*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_seta, hash_setb;
148*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings;
149*795d594fSAndroid Build Coastguard Worker static constexpr size_t count = 1000;
150*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
151*795d594fSAndroid Build Coastguard Worker strings.push_back(RandomString(10));
152*795d594fSAndroid Build Coastguard Worker hash_seta.insert(strings[i]);
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker std::swap(hash_seta, hash_setb);
155*795d594fSAndroid Build Coastguard Worker hash_seta.insert("TEST");
156*795d594fSAndroid Build Coastguard Worker hash_setb.insert("TEST2");
157*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
158*795d594fSAndroid Build Coastguard Worker strings.push_back(RandomString(10));
159*795d594fSAndroid Build Coastguard Worker hash_seta.insert(strings[i]);
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestShrink)163*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestShrink) {
164*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
165*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings = {"a", "b", "c", "d", "e", "f", "g"};
166*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < strings.size(); ++i) {
167*795d594fSAndroid Build Coastguard Worker // Insert some strings into the beginning of our hash set to establish an initial size
168*795d594fSAndroid Build Coastguard Worker hash_set.insert(strings[i]);
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker
171*795d594fSAndroid Build Coastguard Worker hash_set.ShrinkToMaximumLoad();
172*795d594fSAndroid Build Coastguard Worker const double initial_load = hash_set.CalculateLoadFactor();
173*795d594fSAndroid Build Coastguard Worker
174*795d594fSAndroid Build Coastguard Worker // Insert a bunch of random strings to guarantee that we grow the capacity.
175*795d594fSAndroid Build Coastguard Worker std::vector<std::string> random_strings;
176*795d594fSAndroid Build Coastguard Worker static constexpr size_t count = 1000;
177*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
178*795d594fSAndroid Build Coastguard Worker random_strings.push_back(RandomString(10));
179*795d594fSAndroid Build Coastguard Worker hash_set.insert(random_strings[i]);
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker
182*795d594fSAndroid Build Coastguard Worker // Erase all the extra strings which guarantees that our load factor will be really bad.
183*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
184*795d594fSAndroid Build Coastguard Worker hash_set.erase(hash_set.find(random_strings[i]));
185*795d594fSAndroid Build Coastguard Worker }
186*795d594fSAndroid Build Coastguard Worker
187*795d594fSAndroid Build Coastguard Worker const double bad_load = hash_set.CalculateLoadFactor();
188*795d594fSAndroid Build Coastguard Worker EXPECT_GT(initial_load, bad_load);
189*795d594fSAndroid Build Coastguard Worker
190*795d594fSAndroid Build Coastguard Worker // Shrink again, the load factor should be good again.
191*795d594fSAndroid Build Coastguard Worker hash_set.ShrinkToMaximumLoad();
192*795d594fSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(initial_load, hash_set.CalculateLoadFactor());
193*795d594fSAndroid Build Coastguard Worker
194*795d594fSAndroid Build Coastguard Worker // Make sure all the initial elements we had are still there
195*795d594fSAndroid Build Coastguard Worker for (const std::string& initial_string : strings) {
196*795d594fSAndroid Build Coastguard Worker EXPECT_NE(hash_set.end(), hash_set.find(initial_string))
197*795d594fSAndroid Build Coastguard Worker << "expected to find " << initial_string;
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker }
200*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestLoadFactor)201*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestLoadFactor) {
202*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
203*795d594fSAndroid Build Coastguard Worker static constexpr size_t kStringCount = 1000;
204*795d594fSAndroid Build Coastguard Worker static constexpr double kEpsilon = 0.01;
205*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < kStringCount; ++i) {
206*795d594fSAndroid Build Coastguard Worker hash_set.insert(RandomString(i % 10 + 1));
207*795d594fSAndroid Build Coastguard Worker }
208*795d594fSAndroid Build Coastguard Worker // Check that changing the load factor resizes the table to be within the target range.
209*795d594fSAndroid Build Coastguard Worker EXPECT_GE(hash_set.CalculateLoadFactor() + kEpsilon, hash_set.GetMinLoadFactor());
210*795d594fSAndroid Build Coastguard Worker EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
211*795d594fSAndroid Build Coastguard Worker hash_set.SetLoadFactor(0.1, 0.3);
212*795d594fSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(0.1, hash_set.GetMinLoadFactor());
213*795d594fSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(0.3, hash_set.GetMaxLoadFactor());
214*795d594fSAndroid Build Coastguard Worker EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
215*795d594fSAndroid Build Coastguard Worker hash_set.SetLoadFactor(0.6, 0.8);
216*795d594fSAndroid Build Coastguard Worker EXPECT_LE(hash_set.CalculateLoadFactor() - kEpsilon, hash_set.GetMaxLoadFactor());
217*795d594fSAndroid Build Coastguard Worker }
218*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestStress)219*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestStress) {
220*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
221*795d594fSAndroid Build Coastguard Worker std::unordered_set<std::string> std_set;
222*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings;
223*795d594fSAndroid Build Coastguard Worker static constexpr size_t string_count = 2000;
224*795d594fSAndroid Build Coastguard Worker static constexpr size_t operations = 100000;
225*795d594fSAndroid Build Coastguard Worker static constexpr size_t target_size = 5000;
226*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < string_count; ++i) {
227*795d594fSAndroid Build Coastguard Worker strings.push_back(RandomString(i % 10 + 1));
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker const size_t seed = time(nullptr);
230*795d594fSAndroid Build Coastguard Worker SetSeed(seed);
231*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Starting stress test with seed " << seed;
232*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < operations; ++i) {
233*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.size(), std_set.size());
234*795d594fSAndroid Build Coastguard Worker size_t delta = std::abs(static_cast<ssize_t>(target_size) -
235*795d594fSAndroid Build Coastguard Worker static_cast<ssize_t>(hash_set.size()));
236*795d594fSAndroid Build Coastguard Worker size_t n = PRand();
237*795d594fSAndroid Build Coastguard Worker if (n % target_size == 0) {
238*795d594fSAndroid Build Coastguard Worker hash_set.clear();
239*795d594fSAndroid Build Coastguard Worker std_set.clear();
240*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.empty());
241*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(std_set.empty());
242*795d594fSAndroid Build Coastguard Worker } else if (n % target_size < delta) {
243*795d594fSAndroid Build Coastguard Worker // Skew towards adding elements until we are at the desired size.
244*795d594fSAndroid Build Coastguard Worker const std::string& s = strings[PRand() % string_count];
245*795d594fSAndroid Build Coastguard Worker hash_set.insert(s);
246*795d594fSAndroid Build Coastguard Worker std_set.insert(s);
247*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*hash_set.find(s), *std_set.find(s));
248*795d594fSAndroid Build Coastguard Worker } else {
249*795d594fSAndroid Build Coastguard Worker const std::string& s = strings[PRand() % string_count];
250*795d594fSAndroid Build Coastguard Worker auto it1 = hash_set.find(s);
251*795d594fSAndroid Build Coastguard Worker auto it2 = std_set.find(s);
252*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(it1 == hash_set.end(), it2 == std_set.end());
253*795d594fSAndroid Build Coastguard Worker if (it1 != hash_set.end()) {
254*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it1, *it2);
255*795d594fSAndroid Build Coastguard Worker hash_set.erase(it1);
256*795d594fSAndroid Build Coastguard Worker std_set.erase(it2);
257*795d594fSAndroid Build Coastguard Worker }
258*795d594fSAndroid Build Coastguard Worker }
259*795d594fSAndroid Build Coastguard Worker }
260*795d594fSAndroid Build Coastguard Worker }
261*795d594fSAndroid Build Coastguard Worker
262*795d594fSAndroid Build Coastguard Worker struct IsEmptyStringPair {
MakeEmptyart::IsEmptyStringPair263*795d594fSAndroid Build Coastguard Worker void MakeEmpty(std::pair<std::string, int>& pair) const {
264*795d594fSAndroid Build Coastguard Worker pair.first.clear();
265*795d594fSAndroid Build Coastguard Worker }
IsEmptyart::IsEmptyStringPair266*795d594fSAndroid Build Coastguard Worker bool IsEmpty(const std::pair<std::string, int>& pair) const {
267*795d594fSAndroid Build Coastguard Worker return pair.first.empty();
268*795d594fSAndroid Build Coastguard Worker }
269*795d594fSAndroid Build Coastguard Worker };
270*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestHashMap)271*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestHashMap) {
272*795d594fSAndroid Build Coastguard Worker HashMap<std::string, int, IsEmptyStringPair> hash_map;
273*795d594fSAndroid Build Coastguard Worker hash_map.insert(std::make_pair(std::string("abcd"), 123));
274*795d594fSAndroid Build Coastguard Worker hash_map.insert(std::make_pair(std::string("abcd"), 124));
275*795d594fSAndroid Build Coastguard Worker hash_map.insert(std::make_pair(std::string("bags"), 444));
276*795d594fSAndroid Build Coastguard Worker auto it = hash_map.find(std::string("abcd"));
277*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(it->second, 123);
278*795d594fSAndroid Build Coastguard Worker hash_map.erase(it);
279*795d594fSAndroid Build Coastguard Worker it = hash_map.find(std::string("abcd"));
280*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(it, hash_map.end());
281*795d594fSAndroid Build Coastguard Worker }
282*795d594fSAndroid Build Coastguard Worker
283*795d594fSAndroid Build Coastguard Worker struct IsEmptyFnVectorInt {
MakeEmptyart::IsEmptyFnVectorInt284*795d594fSAndroid Build Coastguard Worker void MakeEmpty(std::vector<int>& item) const {
285*795d594fSAndroid Build Coastguard Worker item.clear();
286*795d594fSAndroid Build Coastguard Worker }
IsEmptyart::IsEmptyFnVectorInt287*795d594fSAndroid Build Coastguard Worker bool IsEmpty(const std::vector<int>& item) const {
288*795d594fSAndroid Build Coastguard Worker return item.empty();
289*795d594fSAndroid Build Coastguard Worker }
290*795d594fSAndroid Build Coastguard Worker };
291*795d594fSAndroid Build Coastguard Worker
292*795d594fSAndroid Build Coastguard Worker template <typename T>
HashIntSequence(T begin,T end)293*795d594fSAndroid Build Coastguard Worker size_t HashIntSequence(T begin, T end) {
294*795d594fSAndroid Build Coastguard Worker size_t hash = 0;
295*795d594fSAndroid Build Coastguard Worker for (auto iter = begin; iter != end; ++iter) {
296*795d594fSAndroid Build Coastguard Worker hash = hash * 2 + *iter;
297*795d594fSAndroid Build Coastguard Worker }
298*795d594fSAndroid Build Coastguard Worker return hash;
299*795d594fSAndroid Build Coastguard Worker }
300*795d594fSAndroid Build Coastguard Worker
301*795d594fSAndroid Build Coastguard Worker struct VectorIntHashEquals {
operator ()art::VectorIntHashEquals302*795d594fSAndroid Build Coastguard Worker std::size_t operator()(const std::vector<int>& item) const {
303*795d594fSAndroid Build Coastguard Worker return HashIntSequence(item.begin(), item.end());
304*795d594fSAndroid Build Coastguard Worker }
305*795d594fSAndroid Build Coastguard Worker
operator ()art::VectorIntHashEquals306*795d594fSAndroid Build Coastguard Worker std::size_t operator()(const std::forward_list<int>& item) const {
307*795d594fSAndroid Build Coastguard Worker return HashIntSequence(item.begin(), item.end());
308*795d594fSAndroid Build Coastguard Worker }
309*795d594fSAndroid Build Coastguard Worker
operator ()art::VectorIntHashEquals310*795d594fSAndroid Build Coastguard Worker bool operator()(const std::vector<int>& a, const std::vector<int>& b) const {
311*795d594fSAndroid Build Coastguard Worker return a == b;
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker
operator ()art::VectorIntHashEquals314*795d594fSAndroid Build Coastguard Worker bool operator()(const std::vector<int>& a, const std::forward_list<int>& b) const {
315*795d594fSAndroid Build Coastguard Worker auto aiter = a.begin();
316*795d594fSAndroid Build Coastguard Worker auto biter = b.begin();
317*795d594fSAndroid Build Coastguard Worker while (aiter != a.end() && biter != b.end()) {
318*795d594fSAndroid Build Coastguard Worker if (*aiter != *biter) {
319*795d594fSAndroid Build Coastguard Worker return false;
320*795d594fSAndroid Build Coastguard Worker }
321*795d594fSAndroid Build Coastguard Worker aiter++;
322*795d594fSAndroid Build Coastguard Worker biter++;
323*795d594fSAndroid Build Coastguard Worker }
324*795d594fSAndroid Build Coastguard Worker return (aiter == a.end() && biter == b.end());
325*795d594fSAndroid Build Coastguard Worker }
326*795d594fSAndroid Build Coastguard Worker };
327*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestLookupByAlternateKeyType)328*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestLookupByAlternateKeyType) {
329*795d594fSAndroid Build Coastguard Worker HashSet<std::vector<int>, IsEmptyFnVectorInt, VectorIntHashEquals, VectorIntHashEquals> hash_set;
330*795d594fSAndroid Build Coastguard Worker hash_set.insert(std::vector<int>({1, 2, 3, 4}));
331*795d594fSAndroid Build Coastguard Worker hash_set.insert(std::vector<int>({4, 2}));
332*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.end(), hash_set.find(std::vector<int>({1, 1, 1, 1})));
333*795d594fSAndroid Build Coastguard Worker ASSERT_NE(hash_set.end(), hash_set.find(std::vector<int>({1, 2, 3, 4})));
334*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(hash_set.end(), hash_set.find(std::forward_list<int>({1, 1, 1, 1})));
335*795d594fSAndroid Build Coastguard Worker ASSERT_NE(hash_set.end(), hash_set.find(std::forward_list<int>({1, 2, 3, 4})));
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,TestReserve)338*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, TestReserve) {
339*795d594fSAndroid Build Coastguard Worker HashSet<std::string, IsEmptyFnString> hash_set;
340*795d594fSAndroid Build Coastguard Worker std::vector<size_t> sizes = {1, 10, 25, 55, 128, 1024, 4096};
341*795d594fSAndroid Build Coastguard Worker for (size_t size : sizes) {
342*795d594fSAndroid Build Coastguard Worker hash_set.reserve(size);
343*795d594fSAndroid Build Coastguard Worker const size_t buckets_before = hash_set.NumBuckets();
344*795d594fSAndroid Build Coastguard Worker // Check that we expanded enough.
345*795d594fSAndroid Build Coastguard Worker CHECK_GE(hash_set.ElementsUntilExpand(), size);
346*795d594fSAndroid Build Coastguard Worker // Try inserting elements until we are at our reserve size and ensure the hash set did not
347*795d594fSAndroid Build Coastguard Worker // expand.
348*795d594fSAndroid Build Coastguard Worker while (hash_set.size() < size) {
349*795d594fSAndroid Build Coastguard Worker hash_set.insert(std::to_string(hash_set.size()));
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker CHECK_EQ(hash_set.NumBuckets(), buckets_before);
352*795d594fSAndroid Build Coastguard Worker }
353*795d594fSAndroid Build Coastguard Worker // Check the behaviour for shrinking, it does not necessarily resize down.
354*795d594fSAndroid Build Coastguard Worker constexpr size_t size = 100;
355*795d594fSAndroid Build Coastguard Worker hash_set.reserve(size);
356*795d594fSAndroid Build Coastguard Worker CHECK_GE(hash_set.ElementsUntilExpand(), size);
357*795d594fSAndroid Build Coastguard Worker }
358*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,IteratorConversion)359*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, IteratorConversion) {
360*795d594fSAndroid Build Coastguard Worker const char* test_string = "test string";
361*795d594fSAndroid Build Coastguard Worker HashSet<std::string> hash_set;
362*795d594fSAndroid Build Coastguard Worker HashSet<std::string>::iterator it = hash_set.insert(test_string).first;
363*795d594fSAndroid Build Coastguard Worker HashSet<std::string>::const_iterator cit = it;
364*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it == cit);
365*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(*it, *cit);
366*795d594fSAndroid Build Coastguard Worker }
367*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,StringSearchStringView)368*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, StringSearchStringView) {
369*795d594fSAndroid Build Coastguard Worker const char* test_string = "test string";
370*795d594fSAndroid Build Coastguard Worker HashSet<std::string> hash_set;
371*795d594fSAndroid Build Coastguard Worker HashSet<std::string>::iterator insert_pos = hash_set.insert(test_string).first;
372*795d594fSAndroid Build Coastguard Worker HashSet<std::string>::iterator it = hash_set.find(std::string_view(test_string));
373*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(it == insert_pos);
374*795d594fSAndroid Build Coastguard Worker }
375*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,DoubleInsert)376*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, DoubleInsert) {
377*795d594fSAndroid Build Coastguard Worker const char* test_string = "test string";
378*795d594fSAndroid Build Coastguard Worker HashSet<std::string> hash_set;
379*795d594fSAndroid Build Coastguard Worker hash_set.insert(test_string);
380*795d594fSAndroid Build Coastguard Worker hash_set.insert(test_string);
381*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(1u, hash_set.size());
382*795d594fSAndroid Build Coastguard Worker }
383*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,Preallocated)384*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, Preallocated) {
385*795d594fSAndroid Build Coastguard Worker static const size_t kBufferSize = 64;
386*795d594fSAndroid Build Coastguard Worker uint32_t buffer[kBufferSize];
387*795d594fSAndroid Build Coastguard Worker HashSet<uint32_t> hash_set(buffer, kBufferSize);
388*795d594fSAndroid Build Coastguard Worker size_t max_without_resize = kBufferSize * hash_set.GetMaxLoadFactor();
389*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i != max_without_resize; ++i) {
390*795d594fSAndroid Build Coastguard Worker hash_set.insert(i);
391*795d594fSAndroid Build Coastguard Worker }
392*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(hash_set.owns_data_);
393*795d594fSAndroid Build Coastguard Worker hash_set.insert(max_without_resize);
394*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.owns_data_);
395*795d594fSAndroid Build Coastguard Worker }
396*795d594fSAndroid Build Coastguard Worker
397*795d594fSAndroid Build Coastguard Worker class SmallIndexEmptyFn {
398*795d594fSAndroid Build Coastguard Worker public:
MakeEmpty(uint16_t & item) const399*795d594fSAndroid Build Coastguard Worker void MakeEmpty(uint16_t& item) const {
400*795d594fSAndroid Build Coastguard Worker item = std::numeric_limits<uint16_t>::max();
401*795d594fSAndroid Build Coastguard Worker }
IsEmpty(const uint16_t & item) const402*795d594fSAndroid Build Coastguard Worker bool IsEmpty(const uint16_t& item) const {
403*795d594fSAndroid Build Coastguard Worker return item == std::numeric_limits<uint16_t>::max();
404*795d594fSAndroid Build Coastguard Worker }
405*795d594fSAndroid Build Coastguard Worker };
406*795d594fSAndroid Build Coastguard Worker
407*795d594fSAndroid Build Coastguard Worker class StatefulHashFn {
408*795d594fSAndroid Build Coastguard Worker public:
StatefulHashFn(const std::vector<std::string> * strings)409*795d594fSAndroid Build Coastguard Worker explicit StatefulHashFn(const std::vector<std::string>* strings)
410*795d594fSAndroid Build Coastguard Worker : strings_(strings) {}
411*795d594fSAndroid Build Coastguard Worker
operator ()(const uint16_t & index) const412*795d594fSAndroid Build Coastguard Worker size_t operator() (const uint16_t& index) const {
413*795d594fSAndroid Build Coastguard Worker CHECK_LT(index, strings_->size());
414*795d594fSAndroid Build Coastguard Worker return (*this)((*strings_)[index]);
415*795d594fSAndroid Build Coastguard Worker }
416*795d594fSAndroid Build Coastguard Worker
operator ()(std::string_view s) const417*795d594fSAndroid Build Coastguard Worker size_t operator() (std::string_view s) const {
418*795d594fSAndroid Build Coastguard Worker return DataHash()(s);
419*795d594fSAndroid Build Coastguard Worker }
420*795d594fSAndroid Build Coastguard Worker
421*795d594fSAndroid Build Coastguard Worker private:
422*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>* strings_;
423*795d594fSAndroid Build Coastguard Worker };
424*795d594fSAndroid Build Coastguard Worker
425*795d594fSAndroid Build Coastguard Worker class StatefulPred {
426*795d594fSAndroid Build Coastguard Worker public:
StatefulPred(const std::vector<std::string> * strings)427*795d594fSAndroid Build Coastguard Worker explicit StatefulPred(const std::vector<std::string>* strings)
428*795d594fSAndroid Build Coastguard Worker : strings_(strings) {}
429*795d594fSAndroid Build Coastguard Worker
operator ()(const uint16_t & lhs,const uint16_t & rhs) const430*795d594fSAndroid Build Coastguard Worker bool operator() (const uint16_t& lhs, const uint16_t& rhs) const {
431*795d594fSAndroid Build Coastguard Worker CHECK_LT(rhs, strings_->size());
432*795d594fSAndroid Build Coastguard Worker return (*this)(lhs, (*strings_)[rhs]);
433*795d594fSAndroid Build Coastguard Worker }
434*795d594fSAndroid Build Coastguard Worker
operator ()(const uint16_t & lhs,std::string_view rhs) const435*795d594fSAndroid Build Coastguard Worker bool operator() (const uint16_t& lhs, std::string_view rhs) const {
436*795d594fSAndroid Build Coastguard Worker CHECK_LT(lhs, strings_->size());
437*795d594fSAndroid Build Coastguard Worker return (*strings_)[lhs] == rhs;
438*795d594fSAndroid Build Coastguard Worker }
439*795d594fSAndroid Build Coastguard Worker
440*795d594fSAndroid Build Coastguard Worker private:
441*795d594fSAndroid Build Coastguard Worker const std::vector<std::string>* strings_;
442*795d594fSAndroid Build Coastguard Worker };
443*795d594fSAndroid Build Coastguard Worker
TEST_F(HashSetTest,StatefulHashSet)444*795d594fSAndroid Build Coastguard Worker TEST_F(HashSetTest, StatefulHashSet) {
445*795d594fSAndroid Build Coastguard Worker std::vector<std::string> strings{
446*795d594fSAndroid Build Coastguard Worker "duplicate",
447*795d594fSAndroid Build Coastguard Worker "a",
448*795d594fSAndroid Build Coastguard Worker "b",
449*795d594fSAndroid Build Coastguard Worker "xyz",
450*795d594fSAndroid Build Coastguard Worker "___",
451*795d594fSAndroid Build Coastguard Worker "123",
452*795d594fSAndroid Build Coastguard Worker "placeholder",
453*795d594fSAndroid Build Coastguard Worker "duplicate"
454*795d594fSAndroid Build Coastguard Worker };
455*795d594fSAndroid Build Coastguard Worker const size_t duplicateFirstIndex = 0;
456*795d594fSAndroid Build Coastguard Worker const size_t duplicateSecondIndex = strings.size() - 1u;
457*795d594fSAndroid Build Coastguard Worker const size_t otherIndex = 1u;
458*795d594fSAndroid Build Coastguard Worker
459*795d594fSAndroid Build Coastguard Worker StatefulHashFn hashfn(&strings);
460*795d594fSAndroid Build Coastguard Worker StatefulPred pred(&strings);
461*795d594fSAndroid Build Coastguard Worker HashSet<uint16_t, SmallIndexEmptyFn, StatefulHashFn, StatefulPred> hash_set(hashfn, pred);
462*795d594fSAndroid Build Coastguard Worker for (size_t index = 0, size = strings.size(); index != size; ++index) {
463*795d594fSAndroid Build Coastguard Worker bool inserted = hash_set.insert(index).second;
464*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(index != duplicateSecondIndex, inserted) << index;
465*795d594fSAndroid Build Coastguard Worker }
466*795d594fSAndroid Build Coastguard Worker
467*795d594fSAndroid Build Coastguard Worker // Check search by string.
468*795d594fSAndroid Build Coastguard Worker for (size_t index = 0, size = strings.size(); index != size; ++index) {
469*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(strings[index]);
470*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(it == hash_set.end());
471*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(index == duplicateSecondIndex ? duplicateFirstIndex : index, *it) << index;
472*795d594fSAndroid Build Coastguard Worker }
473*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(hash_set.find("missing") == hash_set.end());
474*795d594fSAndroid Build Coastguard Worker
475*795d594fSAndroid Build Coastguard Worker // Check search by index.
476*795d594fSAndroid Build Coastguard Worker for (size_t index = 0, size = strings.size(); index != size; ++index) {
477*795d594fSAndroid Build Coastguard Worker auto it = hash_set.find(index);
478*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(it == hash_set.end());
479*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(index == duplicateSecondIndex ? duplicateFirstIndex : index, *it) << index;
480*795d594fSAndroid Build Coastguard Worker }
481*795d594fSAndroid Build Coastguard Worker // Note: Searching for index >= strings.size() is not supported by Stateful{HashFn,Pred}.
482*795d594fSAndroid Build Coastguard Worker
483*795d594fSAndroid Build Coastguard Worker // Test removal and search by missing index.
484*795d594fSAndroid Build Coastguard Worker auto remove_it = hash_set.find(otherIndex);
485*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(remove_it == hash_set.end());
486*795d594fSAndroid Build Coastguard Worker hash_set.erase(remove_it);
487*795d594fSAndroid Build Coastguard Worker auto search_it = hash_set.find(otherIndex);
488*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(search_it == hash_set.end());
489*795d594fSAndroid Build Coastguard Worker }
490*795d594fSAndroid Build Coastguard Worker
491*795d594fSAndroid Build Coastguard Worker } // namespace art
492