Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash_function_allocation_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
47# include <gtest/gtest.h>
48# include <gtest/gtest-spi.h>
49
50# include "allocation_failure.H"
51
52# include <cstddef>
53# include <new>
54# include <string>
55# include <utility>
56
57# include <tpl_dynMapOhash.H>
58# include <tpl_dynSetHash.H>
59# include <tpl_lhash.H>
60# include <tpl_odhash.H>
61# include <tpl_olhash.H>
62
63using namespace Aleph;
64
65namespace
66{
67 constexpr int Keys = 300;
68
69 size_t new_hash(const int & k) noexcept
70 {
71 return static_cast<size_t>(k) * 40503u + 17;
72 }
73
74 size_t new_pair_hash(const std::pair<int, int> & p) noexcept
75 {
76 return new_hash(p.first);
77 }
78
79 // Makes each allocation of set(c) fail in turn, on a container with Keys
80 // keys, until set(c) completes: after each failure, which must be a
81 // std::bad_alloc that reaches the caller, holds(c, k) must still find
82 // every key. Returns how many allocations failed.
83 template <class Container, class Insert, class Set, class Holds>
84 size_t fail_every_allocation(Insert insert, Set set, Holds holds)
85 {
86 Container c;
87 for (int k = 0; k < Keys; ++k)
88 insert(c, k);
89
90 size_t failures = 0;
91 for (size_t n = 1; n < 1000; ++n)
92 {
93 SCOPED_TRACE("the allocation " + std::to_string(n) + " fails");
94 bool threw = false;
95 {
96 const Fail_At failing(n);
97 try
98 {
99 set(c);
100 }
101 catch (const std::bad_alloc &)
102 {
103 threw = true;
104 }
105 }
106
107 for (int k = 0; k < Keys; ++k)
108 if (not holds(c, k))
109 {
110 ADD_FAILURE() << "key " << k << " is no longer found";
111 return failures;
112 }
113
114 if (not threw)
115 {
116 // an allocation failed and set(c) completed: it caught it
117 if (refused_allocations > 0)
118 ADD_FAILURE() << "an allocation failed, but no std::bad_alloc reached the caller";
119 return failures;
120 }
121 ++failures;
122 }
123
124 ADD_FAILURE() << "set_hash_fct() made more than 1000 allocations";
125 return failures;
126 }
127
128 // Volatile, so that the compiler keeps the allocations of
129 // allocate_and_free(), which it could otherwise leave out
130 int * volatile allocated = nullptr;
131
132 void allocate_and_free()
133 {
134 allocated = new int[4];
135 delete [] allocated;
136 }
137
138 // The harness: an operation that lets the failure of its first
139 // allocation through, and catches that of the second one and goes on,
140 // completes when the second one fails; that is a failure that did not
141 // reach the caller, not the end of the allocations to fail
142 TEST(HashFunctionAllocation, AnAllocationThatTheOperationCatchesFailsTheTest)
143 {
145 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
146
147 EXPECT_NONFATAL_FAILURE(fail_every_allocation<LhashTable<int>>(
148 [] (auto & c, int k) { c.insert(new LhashBucket<int>(k)); },
149 [] (auto &)
150 {
151 allocate_and_free();
152 try
153 {
154 allocate_and_free();
155 }
156 catch (const std::bad_alloc &)
157 {
158 // caught: the operation goes on
159 }
160 },
161 [] (auto & c, int k) { return c.search(k) != nullptr; }),
162 "no std::bad_alloc reached the caller");
163 }
164
165 TEST(HashFunctionAllocation, AChainedTableKeepsItsFunction)
166 {
168 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
169
170 const size_t failures = fail_every_allocation<LhashTable<int>>(
171 [] (auto & c, int k) { c.insert(new LhashBucket<int>(k)); },
172 [] (auto & c) { c.set_hash_fct(new_hash); },
173 [] (auto & c, int k) { return c.search(k) != nullptr; });
174 EXPECT_GE(failures, 1u);
175 }
176
177 TEST(HashFunctionAllocation, AnOpenTableKeepsItsFunctions)
178 {
180 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
181
182 auto insert = [] (auto & c, int k) { (void) c.insert(k); };
183 auto holds = [] (auto & c, int k) { return c.search(k) != nullptr; };
184 EXPECT_GE(fail_every_allocation<OLhashTable<int>>(
185 insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
186 EXPECT_GE(fail_every_allocation<ODhashTable<int>>(
187 insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
188 EXPECT_GE(fail_every_allocation<ODhashTable<int>>(
189 insert, [] (auto & c) { c.set_second_hash_fct(new_hash); }, holds), 1u);
190 }
191
192 TEST(HashFunctionAllocation, AMapKeepsItsFunction)
193 {
195 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
196
197 auto insert = [] (auto & c, int k) { (void) c.insert(k, 10 * k); };
198 auto holds = [] (auto & c, int k)
199 {
200 const auto * p = c.search(k);
201 return p != nullptr and p->second == 10 * k;
202 };
203 using Map = DynMapHash<int, int>;
204 using Open_Map = MapOLhash<int, int>;
205 EXPECT_GE(fail_every_allocation<Map>(
206 insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
207 EXPECT_GE(fail_every_allocation<Map>(
208 insert, [] (auto & c) { c.set_hash_fct(new_pair_hash); }, holds), 1u);
209 EXPECT_GE(fail_every_allocation<Open_Map>(
210 insert, [] (auto & c) { c.set_hash_fct(new_pair_hash); }, holds), 1u);
211 }
212 // A chained table that resizes itself, with a load factor at its upper
213 // threshold or above, as one filled while it did not resize itself:
214 // placing its keys again, with set_hash_fct(), with resize() or with
215 // the growth of an insertion, grew it again in the middle, and a failure
216 // of that allocation left keys out of the table and lost its lists. The
217 // capacity that it grows to is allocated at once now
218 TEST(HashFunctionAllocation, AChainedTableAboveItsLoadKeepsItsKeys)
219 {
221 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
222
223 // Keys keys in the 101 lists of a table built by default: a load of 3
224 auto filled = [] (auto & c, int k, auto add)
225 {
226 c.set_with_resize(false);
227 add(c, k);
228 c.set_with_resize(true);
229 };
230 auto insert = [&filled] (auto & c, int k)
231 {
232 filled(c, k, [] (auto & t, int key) { t.insert(new LhashBucket<int>(key)); });
233 };
234 auto holds = [] (auto & c, int k) { return c.search(k) != nullptr; };
235 using Table = LhashTable<int>;
236 EXPECT_GE(fail_every_allocation<Table>(
237 insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
238 EXPECT_GE(fail_every_allocation<Table>(
239 insert, [] (auto & c) { (void) c.resize(53); }, holds), 1u);
240 int next = Keys; // a key that is not in the table, for each try
241 EXPECT_GE(fail_every_allocation<Table>(
242 insert, [&next] (auto & c)
243 {
244 auto * bucket = new LhashBucket<int>(next);
245 ++next;
246 if (c.insert(bucket) == nullptr)
247 delete bucket;
248 }, holds), 1u);
249
250 auto set_insert = [&filled] (auto & c, int k)
251 {
252 filled(c, k, [] (auto & s, int key) { (void) s.insert(key); });
253 };
254 EXPECT_GE(fail_every_allocation<DynSetHash<int>>(
255 set_insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
256
257 auto map_insert = [&filled] (auto & c, int k)
258 {
259 filled(c, k, [] (auto & m, int key) { (void) m.insert(key, 10 * key); });
260 };
261 using Map = DynMapHash<int, int>;
262 EXPECT_GE(fail_every_allocation<Map>(
263 map_insert, [] (auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
264 }
265
266 // The case of the review: 190 keys in 211 lists, the last one left there
267 // by the growth of its insertion, which failed, and set_hash_fct() with
268 // each of its allocations failing in turn
269 TEST(HashFunctionAllocation, SetHashFctAfterAFailedGrowthKeepsItsKeys)
270 {
272 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
273
274 LhashTable<int> table(211);
275 const size_t len = table.capacity();
276 int keys = 0;
277 for (bool failed = false; not failed; ++keys)
278 {
279 auto * bucket = new LhashBucket<int>(keys);
280 const bool grows = (keys + 1.0) / len >= table.get_upper_alpha();
281 const Fail_At failing(grows ? 1 : 1000000);
282 try
283 {
284 ASSERT_NE(table.insert(bucket), nullptr);
285 }
286 catch (const std::bad_alloc &)
287 {
288 failed = true; // the bucket stays in the table
289 }
290 ASSERT_EQ(table.capacity(), len);
291 }
292 ASSERT_GE(table.current_alpha(), table.get_upper_alpha());
293
294 size_t failures = 0;
295 for (size_t n = 1; n < 100; ++n)
296 {
297 SCOPED_TRACE("the allocation " + std::to_string(n) + " fails");
298 bool threw = false;
299 {
300 const Fail_At failing(n);
301 try
302 {
303 table.set_hash_fct(new_hash);
304 }
305 catch (const std::bad_alloc &)
306 {
307 threw = true;
308 }
309 }
310 EXPECT_EQ(table.size(), static_cast<size_t>(keys));
311 for (int k = 0; k < keys; ++k)
312 ASSERT_NE(table.search(k), nullptr) << "key " << k;
313 if (not threw)
314 break;
315 ++failures;
316 }
317 EXPECT_EQ(failures, 1u) << "set_hash_fct() made more than the allocation of its lists";
318 }
319
320} // namespace
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Self-adjusting dynamic hash table.
Generic linear hash table.
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
#define TEST(name)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
double add(double a, double b)
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Definition tpl_lhash.H:1008
Open addressing hash map using linear probing.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
int keys[]
static int * k
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Hash tables with collision resolution by separate chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.