36#include <gtest/gtest.h>
57 static bool should_throw;
60 explicit Throwing_Copy(
int v) :
value(v) {}
61 Throwing_Copy(
const Throwing_Copy &other) :
value(other.
value)
64 throw std::runtime_error(
"Throwing_Copy: copy failed");
66 Throwing_Copy(Throwing_Copy &&) =
default;
67 Throwing_Copy &operator=(
const Throwing_Copy &) =
default;
68 Throwing_Copy &operator=(Throwing_Copy &&) =
default;
71 bool Throwing_Copy::should_throw =
false;
84 EXPECT_EQ(
m.
size(), 0u);
90 EXPECT_TRUE(
m.
insert(
"alpha", 1));
91 EXPECT_FALSE(
m.
insert(
"alpha", 99));
92 EXPECT_EQ(
m.
size(), 1u);
93 EXPECT_EQ(*
m.find_copy(
"alpha"), 1);
99 EXPECT_TRUE(
m.
insert(
"alpha", std::make_unique<int>(10)));
100 bool saw =
m.with_value(
"alpha", [](
const std::unique_ptr<int> &p)
102 ASSERT_NE(p,
nullptr);
112 EXPECT_FALSE(
m.find_copy(
"alpha").has_value());
116 auto v =
m.find_copy(
"alpha");
117 ASSERT_TRUE(v.has_value());
124 m.insert_or_assign(
"alpha", 1);
125 EXPECT_EQ(*
m.find_copy(
"alpha"), 1);
126 m.insert_or_assign(
"alpha", 2);
127 EXPECT_EQ(*
m.find_copy(
"alpha"), 2);
128 EXPECT_EQ(
m.
size(), 1u);
134 EXPECT_FALSE(
m.erase(
"nope"));
136 EXPECT_TRUE(
m.erase(
"alpha"));
137 EXPECT_FALSE(
m.erase(
"alpha"));
147 EXPECT_TRUE(
m.with_value(
"alpha", [&](
const int &v) { seen = v; }));
151 EXPECT_FALSE(
m.with_value(
"missing", [&](
const int &) { called = true; }));
152 EXPECT_FALSE(called);
159 EXPECT_TRUE(
m.with_value_mut(
"alpha", [](
int &v) { v += 5; }));
160 EXPECT_EQ(*
m.find_copy(
"alpha"), 15);
162 EXPECT_FALSE(
m.with_value_mut(
"missing", [](
int &v) { v = 999; }));
168 for (
int i = 0; i < 100; ++i)
169 m.
insert(
"key" + std::to_string(i), i);
170 EXPECT_EQ(
m.
size(), 100u);
174 EXPECT_EQ(
m.
size(), 0u);
183 auto snap =
m.snapshot();
184 ASSERT_EQ(snap.size(), 2u);
187 m.insert_or_assign(
"alpha", 999);
190 std::map<std::string, int> as_map;
191 for (
const auto &entry : snap)
192 as_map.
emplace(entry.first, entry.second);
193 ASSERT_EQ(as_map.size(), 2u);
194 EXPECT_EQ(as_map.at(
"alpha"), 1);
195 EXPECT_EQ(as_map.at(
"beta"), 2);
201 Throwing_Copy
value(1);
202 Throwing_Copy::should_throw =
false;
205 Throwing_Copy::should_throw =
true;
206 Throwing_Copy other(2);
207 EXPECT_THROW(
m.
insert(2, other), std::runtime_error);
208 Throwing_Copy::should_throw =
false;
210 EXPECT_EQ(
m.
size(), 1u);
221 constexpr size_t shards = 16;
226 for (
int i = 0; i < 200; ++i)
228 const int key =
static_cast<int>(idx) * 1000 + i;
233 EXPECT_EQ(
m.
size(), shards * 200);
238 constexpr int thread_count = 8;
239 constexpr int keys_per_thread = 2000;
244 const int base =
static_cast<int>(t) * keys_per_thread;
246 for (
int i = 0; i < keys_per_thread; ++i)
248 const int key = base + i;
251 auto v =
m.find_copy(key);
252 ASSERT_TRUE(v.has_value());
256 for (
int i = 0; i < keys_per_thread; ++i)
258 const int key = base + i;
259 EXPECT_TRUE(
m.with_value_mut(key, [](
int &v) { v *= 2; }));
262 for (
int i = 0; i < keys_per_thread; i += 2)
263 EXPECT_TRUE(
m.erase(base + i));
266 const size_t expected =
static_cast<size_t>(thread_count) * (keys_per_thread / 2);
269 for (
int t = 0; t < thread_count; ++t)
270 for (
int i = 0; i < keys_per_thread; ++i)
272 const int key = t * keys_per_thread + i;
273 auto v =
m.find_copy(key);
275 EXPECT_FALSE(v.has_value());
278 ASSERT_TRUE(v.has_value());
279 EXPECT_EQ(*v, i * 2);
287 4000, 0xBADC0FFEu, 200,
289 Trace_Operation_Kind::insert,
290 Trace_Operation_Kind::erase,
291 Trace_Operation_Kind::contains
294 std::map<size_t, size_t> reference;
297 auto apply_reference = [](std::map<size_t, size_t> &ref,
const Trace_Operation &op) ->
bool
301 case Trace_Operation_Kind::insert:
302 return ref.emplace(op.key, op.value).second;
303 case Trace_Operation_Kind::erase:
304 return ref.
erase(op.key) != 0;
305 case Trace_Operation_Kind::contains:
306 return ref.find(op.key) != ref.end();
316 case Trace_Operation_Kind::insert:
317 return s.
insert(op.key, op.value);
318 case Trace_Operation_Kind::erase:
319 return s.
erase(op.key);
320 case Trace_Operation_Kind::contains:
327 for (
size_t i = 0; i < trace.size(); ++i)
329 const bool ref_result = apply_reference(reference, trace[i]);
330 const bool subj_result = apply_subject(subject, trace[i]);
331 ASSERT_EQ(ref_result, subj_result) <<
"mismatch at operation " << i;
334 ASSERT_EQ(subject.
size(), reference.size());
335 for (
const auto &[key,
value] : reference)
338 ASSERT_TRUE(v.has_value()) <<
"missing key " << key;
339 EXPECT_EQ(*v,
value);
size_t size_t int32_t value
Sharded concurrent hash map: Shards independently-locked DynMapHashTable partitions,...
bool contains(const Key &key) const
Check whether key is present.
size_t size() const
Return the total number of entries across all shards.
std::optional< T > find_copy(const Key &key) const
Look up key and return a copy of its value, if present.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
bool erase(const Key &key)
Remove key if present.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Minimal std::expected-style result type for C++20.
void emplace(Args &&... args)
Appends a new element into the container by constructing it from the given args.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
void clear()
Empties the container.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Reusable helpers for concurrent data-structure tests.
std::vector< Trace_Operation > make_random_operation_trace(const size_t count, const uint32_t seed, const size_t key_range, const std::initializer_list< Trace_Operation_Kind > kinds={ Trace_Operation_Kind::insert, Trace_Operation_Kind::erase, Trace_Operation_Kind::contains })
Build a deterministic pseudo-random operation trace.
void run_workers(const size_t thread_count, Worker worker)
Launch thread_count workers with a deterministic simultaneous start, join every one of them,...
Main namespace for Aleph-w library functions.
One randomized operation trace entry.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Sharded concurrent hash map (Aleph::ConcurrentHashMap).