36#include <gtest/gtest.h>
55TEST(CompactCuckooFilterCtor, default_construction)
59 EXPECT_EQ(cf.
size(), 0u);
63TEST(CompactCuckooFilterCtor, custom_fingerprint_bits)
69TEST(CompactCuckooFilterCtor, custom_entries_per_bucket)
75TEST(CompactCuckooFilterCtor, memory_usage_is_compact)
81 size_t num_buckets = cf.
capacity() / 4;
82 size_t expected_bytes = (num_buckets * 4 * 8 + 7) / 8;
94TEST(CompactCuckooFilterBasic, insert_and_contains_no_false_negatives)
98 for (
int i = 0; i < 100; ++i)
101 EXPECT_EQ(cf.
size(), 100u);
103 for (
int i = 0; i < 100; ++i)
104 EXPECT_TRUE(cf.
contains(i)) <<
"False negative for i=" << i;
107TEST(CompactCuckooFilterBasic, absent_elements_usually_not_found)
111 for (
int i = 0; i < 100; ++i)
114 size_t false_positives = 0;
115 for (
int i = 1000; i < 2000; ++i)
121 EXPECT_LT(false_positives, 60u) <<
"Too many false positives: " << false_positives;
124TEST(CompactCuckooFilterBasic, duplicate_insert_stores_twice)
128 EXPECT_TRUE(cf.
insert(42));
129 EXPECT_EQ(cf.
size(), 1u);
132 EXPECT_TRUE(cf.
insert(42));
133 EXPECT_EQ(cf.
size(), 2u);
138TEST(CompactCuckooFilterBasic, string_keys)
150 EXPECT_EQ(cf.
size(), 3u);
153TEST(CompactCuckooFilterBasic, small_fingerprint_bits)
158 for (
int i = 0; i < 50; ++i)
161 for (
int i = 0; i < 50; ++i)
165TEST(CompactCuckooFilterBasic, large_fingerprint_bits)
170 for (
int i = 0; i < 50; ++i)
173 for (
int i = 0; i < 50; ++i)
181TEST(CompactCuckooFilterDelete, remove_existing_element)
188 EXPECT_EQ(cf.
size(), 3u);
190 EXPECT_TRUE(cf.
remove(20));
191 EXPECT_EQ(cf.
size(), 2u);
197TEST(CompactCuckooFilterDelete, remove_nonexistent_is_noop)
202 EXPECT_FALSE(cf.
remove(999));
203 EXPECT_EQ(cf.
size(), 1u);
207TEST(CompactCuckooFilterDelete, remove_duplicate_removes_one_copy)
213 EXPECT_EQ(cf.
size(), 2u);
215 EXPECT_TRUE(cf.
remove(5));
216 EXPECT_EQ(cf.
size(), 1u);
219 EXPECT_TRUE(cf.
remove(5));
220 EXPECT_EQ(cf.
size(), 0u);
224TEST(CompactCuckooFilterDelete, insert_after_remove)
234 EXPECT_EQ(cf.
size(), 1u);
237TEST(CompactCuckooFilterDelete, interleaved_insert_remove)
241 for (
int i = 0; i < 100; ++i)
245 for (
int i = 0; i < 100; i += 2)
246 EXPECT_TRUE(cf.
remove(i));
248 EXPECT_EQ(cf.
size(), 50u);
251 for (
int i = 1; i < 100; i += 2)
252 EXPECT_TRUE(cf.
contains(i)) <<
"Lost i=" << i;
255 for (
int i = 0; i < 100; i += 2)
256 EXPECT_FALSE(cf.
contains(i)) <<
"Still has i=" << i;
263TEST(CompactCuckooFilterCapacity, load_factor_computed_correctly)
269 for (
int i = 0; i < 50; ++i)
278TEST(CompactCuckooFilterCapacity, high_load_factor_still_works)
282 std::set<int> inserted_set;
284 for (
int i = 0; i < 1000; ++i)
286 inserted_set.insert(i);
288 EXPECT_GT(inserted_set.size(), 800u);
291 for (
int val : inserted_set)
292 EXPECT_TRUE(cf.
contains(val)) <<
"Lost val=" << val;
295TEST(CompactCuckooFilterCapacity, insert_fails_when_full)
300 for (
int i = 0; i < 200; ++i)
305 EXPECT_GT(successful, 80);
306 EXPECT_LT(successful, 200);
313TEST(CompactCuckooFilterIntrospection, memory_usage)
318 size_t num_buckets = cf.
capacity() / 4;
319 size_t expected = (num_buckets * 4 * 8 + 7) / 8;
324TEST(CompactCuckooFilterIntrospection, load_factor)
328 for (
int i = 0; i < 100; ++i)
340TEST(CompactCuckooFilterSemantics, copy_preserves_state)
343 for (
int i = 0; i < 30; ++i)
350 for (
int i = 0; i < 30; ++i)
351 EXPECT_TRUE(
copy.contains(i));
354TEST(CompactCuckooFilterSemantics, move_transfers_state)
357 for (
int i = 0; i < 30; ++i)
360 size_t orig_size = cf.
size();
363 EXPECT_EQ(moved.
size(), orig_size);
364 for (
int i = 0; i < 30; ++i)
372TEST(CompactCuckooFilterClear, clears_all_state)
375 for (
int i = 0; i < 50; ++i)
379 EXPECT_EQ(cf.
size(), 0u);
382 for (
int i = 0; i < 50; ++i)
386TEST(CompactCuckooFilterClear, can_reuse_after_clear)
390 for (
int i = 0; i < 20; ++i)
395 for (
int i = 100; i < 120; ++i)
398 EXPECT_EQ(cf.
size(), 20u);
399 for (
int i = 100; i < 120; ++i)
407TEST(CompactCuckooFilterFPRate, empirical_fp_rate_within_bounds)
409 const size_t n = 500;
412 for (
size_t i = 0; i < n; ++i)
413 cf.
insert(
static_cast<int>(i));
415 size_t test_count = 10000;
417 for (
size_t i = n + 10000; i < n + 10000 + test_count; ++i)
418 if (cf.
contains(
static_cast<int>(i)))
421 double empirical =
static_cast<double>(fps) / test_count;
425 EXPECT_LT(empirical, 0.10)
426 <<
"Empirical FP rate " << empirical <<
" too high";
429TEST(CompactCuckooFilterFPRate, low_fp_rate_with_large_fingerprint)
433 for (
int i = 0; i < 200; ++i)
437 for (
int i = 10000; i < 20000; ++i)
449TEST(CompactCuckooFilterStress, random_insertions_and_lookups)
452 std::mt19937
rng(42);
453 std::uniform_int_distribution<int> dist(0, 1000000);
455 std::set<int> inserted;
458 for (
int i = 0; i < 2000; ++i)
462 inserted.insert(val);
466 for (
int val : inserted)
467 EXPECT_TRUE(cf.
contains(val)) <<
"Lost value " << val;
470TEST(CompactCuckooFilterStress, sequential_insert_remove_cycles)
474 for (
int cycle = 0; cycle < 5; ++cycle)
479 for (
int i = 0; i < 200; ++i)
480 cf.
insert(cycle * 1000 + i);
483 for (
int i = 0; i < 100; ++i)
484 cf.
remove(cycle * 1000 + i);
487 for (
int i = 100; i < 200; ++i)
488 EXPECT_TRUE(cf.
contains(cycle * 1000 + i)) <<
"Cycle " << cycle <<
", lost i=" << i;
492TEST(CompactCuckooFilterStress, many_duplicates)
497 int successful_inserts = 0;
498 for (
int i = 0; i < 10; ++i)
500 ++successful_inserts;
502 EXPECT_GE(successful_inserts, 5);
503 EXPECT_EQ(cf.
size(),
static_cast<size_t>(successful_inserts));
507 int removals = successful_inserts / 2;
508 for (
int i = 0; i < removals; ++i)
509 EXPECT_TRUE(cf.
remove(42));
511 EXPECT_EQ(cf.
size(),
static_cast<size_t>(successful_inserts - removals));
512 if (successful_inserts > removals)
520TEST(CompactCuckooFilterEdge, tiny_filter)
532TEST(CompactCuckooFilterEdge, minimal_fingerprint_bits)
536 for (
int i = 0; i < 50; ++i)
539 for (
int i = 0; i < 50; ++i)
543TEST(CompactCuckooFilterEdge, maximal_fingerprint_bits)
552TEST(CompactCuckooFilterEdge, single_entry_per_bucket)
556 for (
int i = 0; i < 50; ++i)
559 for (
int i = 0; i < 50; ++i)
567TEST(CompactCuckooFilterComparison, same_behavior_as_standard)
572 std::vector<int> values = {1, 5, 10, 42, 99, 123, 456, 789};
574 for (
int val : values)
581 for (
int val : values)
583 EXPECT_TRUE(standard.
contains(val));
588 EXPECT_EQ(standard.
size(), compact.
size());
591TEST(CompactCuckooFilterComparison, memory_savings_verified)
599 size_t num_buckets = compact.
capacity() / 4;
600 size_t expected_compact = (num_buckets * 4 * 8 + 7) / 8;
601 size_t expected_standard = num_buckets * 16;
604 EXPECT_LE(compact_mem, expected_compact + 100);
607 EXPECT_LT(compact_mem, expected_standard / 3);
610TEST(CompactCuckooFilterComparison, performance_characteristics)
616 for (
int i = 0; i < 1000; ++i)
623 EXPECT_EQ(standard.
size(), 1000u);
624 EXPECT_EQ(compact.
size(), 1000u);
627 for (
int i = 0; i < 1000; ++i)
Memory-optimized Cuckoo Filter with bit-packed fingerprints.
bool contains(const T &item) const
Test membership.
bool is_empty() const noexcept
Synonym for empty() in traditional Aleph style.
size_t size() const noexcept
Return the number of items currently in the filter.
void clear()
Clear the filter.
bool remove(const T &item)
Remove an item from the filter.
bool insert(const T &item)
Insert an item into the filter.
double load_factor() const noexcept
Return the load factor of the filter (relative to capacity()).
size_t memory_usage() const noexcept
Return approximate memory usage in bytes.
size_t capacity() const noexcept
Return the total number of fingerprint slots (including stash).
Industrial-grade Cuckoo Filter implementation.
bool contains(const T &item) const
Test membership.
size_t size() const noexcept
Return the number of items currently in the filter.
bool insert(const T &item)
Insert an item into the filter.
Minimal std::expected-style result type for C++20.
Memory-optimized Cuckoo filter with bit-packed fingerprints.
Probabilistic set membership with Cuckoo filters.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.