36#include <gtest/gtest.h>
55TEST(CompactQuotientFilterCtor, valid_parameters)
61 EXPECT_EQ(qf.
size(), 0u);
65TEST(CompactQuotientFilterCtor, rejects_invalid_q)
71TEST(CompactQuotientFilterCtor, rejects_invalid_r)
77TEST(CompactQuotientFilterCtor, rejects_q_plus_r_exceeds_64)
82TEST(CompactQuotientFilterCtor, from_capacity_creates_valid_filter)
85 EXPECT_GE(qf.capacity(), 1000u);
86 EXPECT_LE(qf.false_positive_rate(), 0.02);
89TEST(CompactQuotientFilterCtor, from_capacity_rejects_bad_args)
96TEST(CompactQuotientFilterCtor, memory_usage_is_compact)
99 size_t expected_bytes = (1024 * 11 + 7) / 8;
111TEST(CompactQuotientFilterBasic, insert_and_contains_no_false_negatives)
115 for (
int i = 0; i < 100; ++i)
118 EXPECT_EQ(qf.
size(), 100u);
120 for (
int i = 0; i < 100; ++i)
121 EXPECT_TRUE(qf.
contains(i)) <<
"False negative for i=" << i;
124TEST(CompactQuotientFilterBasic, absent_elements_usually_not_found)
128 for (
int i = 0; i < 100; ++i)
131 size_t false_positives = 0;
132 for (
int i = 1000; i < 2000; ++i)
138 EXPECT_LT(false_positives, 20u) <<
"Too many false positives: " << false_positives;
141TEST(CompactQuotientFilterBasic, duplicate_insert_is_idempotent)
146 EXPECT_EQ(qf.
size(), 1u);
149 EXPECT_EQ(qf.
size(), 1u);
154TEST(CompactQuotientFilterBasic, string_keys)
166 EXPECT_EQ(qf.
size(), 3u);
169TEST(CompactQuotientFilterBasic, small_remainder_bits)
174 for (
int i = 0; i < 50; ++i)
177 for (
int i = 0; i < 50; ++i)
184TEST(CompactQuotientFilterBasic, large_remainder_bits)
189 for (
int i = 0; i < 50; ++i)
192 for (
int i = 0; i < 50; ++i)
203TEST(CompactQuotientFilterDelete, remove_existing_element)
210 EXPECT_EQ(qf.
size(), 3u);
212 EXPECT_TRUE(qf.
remove(20));
213 EXPECT_EQ(qf.
size(), 2u);
219TEST(CompactQuotientFilterDelete, remove_nonexistent_is_noop)
224 EXPECT_FALSE(qf.
remove(999));
225 EXPECT_EQ(qf.
size(), 1u);
229TEST(CompactQuotientFilterDelete, remove_all_makes_empty)
233 for (
int i = 0; i < 50; ++i)
236 for (
int i = 0; i < 50; ++i)
237 EXPECT_TRUE(qf.
remove(i)) <<
"Failed to remove i=" << i;
239 EXPECT_EQ(qf.
size(), 0u);
243TEST(CompactQuotientFilterDelete, insert_after_remove)
253 EXPECT_EQ(qf.
size(), 1u);
256TEST(CompactQuotientFilterDelete, interleaved_insert_remove)
260 for (
int i = 0; i < 100; ++i)
264 for (
int i = 0; i < 100; i += 2)
265 EXPECT_TRUE(qf.
remove(i));
267 EXPECT_EQ(qf.
size(), 50u);
270 for (
int i = 1; i < 100; i += 2)
271 EXPECT_TRUE(qf.
contains(i)) <<
"Lost i=" << i;
274 for (
int i = 0; i < 100; i += 2)
275 EXPECT_FALSE(qf.
contains(i)) <<
"Still has i=" << i;
282TEST(CompactQuotientFilterCapacity, load_factor_computed_correctly)
287 for (
int i = 0; i < 8; ++i)
293TEST(CompactQuotientFilterCapacity, overflow_throws)
297 for (
int i = 0; i < 4; ++i)
301 for (
int i = 100; i < 200; ++i)
307 catch (
const std::overflow_error &)
316TEST(CompactQuotientFilterCapacity, high_load_factor_still_works)
321 for (
int i = 0; i < 230; ++i)
327 for (
int i = 0; i < 230; ++i)
328 EXPECT_TRUE(qf.
contains(i)) <<
"Lost i=" << i;
335TEST(CompactQuotientFilterIntrospection, memory_usage)
338 size_t expected = (1024 * 11 + 7) / 8;
342TEST(CompactQuotientFilterIntrospection, false_positive_rate)
349TEST(CompactQuotientFilterIntrospection, estimate_returns_valid_params)
353 EXPECT_GE(r_val, 10);
354 EXPECT_LE(q_val + r_val, 64);
361TEST(CompactQuotientFilterSemantics, copy_preserves_state)
364 for (
int i = 0; i < 30; ++i)
369 EXPECT_EQ(
copy.q(), qf.
q());
370 EXPECT_EQ(
copy.r(), qf.
r());
372 for (
int i = 0; i < 30; ++i)
373 EXPECT_TRUE(
copy.contains(i));
376TEST(CompactQuotientFilterSemantics, move_transfers_state)
379 for (
int i = 0; i < 30; ++i)
382 size_t orig_size = qf.
size();
385 EXPECT_EQ(moved.
size(), orig_size);
386 for (
int i = 0; i < 30; ++i)
390TEST(CompactQuotientFilterSemantics, swap_works)
407TEST(CompactQuotientFilterClear, clears_all_state)
410 for (
int i = 0; i < 50; ++i)
414 EXPECT_EQ(qf.
size(), 0u);
417 for (
int i = 0; i < 50; ++i)
421TEST(CompactQuotientFilterClear, can_reuse_after_clear)
425 for (
int i = 0; i < 20; ++i)
430 for (
int i = 100; i < 120; ++i)
433 EXPECT_EQ(qf.
size(), 20u);
434 for (
int i = 100; i < 120; ++i)
442TEST(CompactQuotientFilterMerge, merge_combines_elements)
447 for (
int i = 0; i < 50; ++i)
449 for (
int i = 50; i < 100; ++i)
454 for (
int i = 0; i < 100; ++i)
455 EXPECT_TRUE(a.
contains(i)) <<
"Missing i=" << i;
458TEST(CompactQuotientFilterMerge, merge_rejects_mismatched_params)
463 EXPECT_THROW(a.
merge(b), std::domain_error);
466TEST(CompactQuotientFilterMerge, merge_rejects_mismatched_seed)
471 EXPECT_THROW(a.
merge(b), std::domain_error);
474TEST(CompactQuotientFilterMerge, merge_with_overlapping_elements)
479 for (
int i = 0; i < 60; ++i)
481 for (
int i = 40; i < 100; ++i)
487 for (
int i = 0; i < 100; ++i)
488 EXPECT_TRUE(a.
contains(i)) <<
"Missing i=" << i;
495TEST(CompactQuotientFilterFPRate, empirical_fp_rate_within_bounds)
497 const size_t n = 500;
500 for (
size_t i = 0; i < n; ++i)
501 qf.
insert(
static_cast<int>(i));
503 size_t test_count = 10000;
505 for (
size_t i = n + 10000; i < n + 10000 + test_count; ++i)
506 if (qf.
contains(
static_cast<int>(i)))
509 double empirical =
static_cast<double>(fps) / test_count;
513 EXPECT_LT(empirical, theoretical * 5.0 + 0.01)
514 <<
"Empirical FP rate " << empirical <<
" exceeds 5x theoretical " << theoretical;
517TEST(CompactQuotientFilterFPRate, low_fp_rate_with_large_r)
521 for (
int i = 0; i < 200; ++i)
525 for (
int i = 10000; i < 20000; ++i)
537TEST(CompactQuotientFilterStress, random_insertions_and_lookups)
540 std::mt19937
rng(42);
541 std::uniform_int_distribution<int> dist(0, 1000000);
543 std::set<int> inserted;
546 for (
int i = 0; i < 5000; ++i)
554 for (
int val : inserted)
555 EXPECT_TRUE(qf.
contains(val)) <<
"Lost value " << val;
558TEST(CompactQuotientFilterStress, sequential_insert_remove_cycles)
562 for (
int cycle = 0; cycle < 5; ++cycle)
567 for (
int i = 0; i < 200; ++i)
568 qf.
insert(cycle * 1000 + i);
571 for (
int i = 0; i < 100; ++i)
572 qf.
remove(cycle * 1000 + i);
575 for (
int i = 100; i < 200; ++i)
576 EXPECT_TRUE(qf.
contains(cycle * 1000 + i)) <<
"Cycle " << cycle <<
", lost i=" << i;
580TEST(CompactQuotientFilterStress, collision_heavy_workload)
585 for (
int i = 0; i < 40; ++i)
588 for (
int i = 0; i < 40; ++i)
589 EXPECT_TRUE(qf.
contains(i)) <<
"Lost i=" << i;
592 for (
int i = 0; i < 20; ++i)
593 EXPECT_TRUE(qf.
remove(i));
596 for (
int i = 20; i < 40; ++i)
604TEST(CompactQuotientFilterEdge, single_slot_filter)
616TEST(CompactQuotientFilterEdge, minimal_remainder_bits)
620 for (
int i = 0; i < 50; ++i)
623 for (
int i = 0; i < 50; ++i)
627TEST(CompactQuotientFilterEdge, maximal_remainder_bits)
636TEST(CompactQuotientFilterEdge, wrap_around_behavior)
641 for (
int i = 0; i < 16; ++i)
644 for (
int i = 0; i < 16; ++i)
645 EXPECT_TRUE(qf.
contains(i)) <<
"Lost i=" << i;
652TEST(CompactQuotientFilterComparison, same_behavior_as_standard)
654 const uint32_t
seed = 12345;
658 std::vector<int> values = {1, 5, 10, 42, 99, 123, 456, 789};
660 for (
int val : values)
667 for (
int val : values)
669 EXPECT_TRUE(standard.
contains(val));
674 EXPECT_EQ(standard.
size(), compact.
size());
677TEST(CompactQuotientFilterComparison, memory_savings_verified)
686 EXPECT_LT(compact_mem, standard_mem / 5);
689 EXPECT_EQ(standard_mem, 8192u);
692 EXPECT_EQ(compact_mem, 1408u);
Memory-optimized quotient filter with bit-packed slots.
double load_factor() const noexcept
Current load factor.
size_t memory_usage() const noexcept
Memory usage in bytes.
void swap(Compact_Quotient_Filter &other) noexcept
Swap in O(1).
static std::pair< uint8_t, uint8_t > estimate(size_t n, double fp_rate)
The quotient and the remainder bits of a filter for n elements and a target FP rate.
uint8_t q() const noexcept
Quotient bits.
size_t capacity() const noexcept
Number of slots (2^q).
static Compact_Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
Construct from desired capacity and false positive rate.
bool is_empty() const noexcept
True if empty.
void clear() noexcept
Remove all elements.
Compact_Quotient_Filter & insert(const T &item)
Insert an element.
size_t size() const noexcept
Number of elements.
void merge(const Compact_Quotient_Filter &other)
Merge another filter (same q, r, seed).
bool remove(const T &item) noexcept
Remove an element.
uint8_t r() const noexcept
Remainder bits.
bool contains(const T &item) const noexcept
Test membership.
double false_positive_rate() const noexcept
Theoretical FP probability ~ 2^{-r}.
Quotient filter for probabilistic set membership with deletion.
Quotient_Filter & insert(const T &item)
Insert an element.
size_t memory_usage() const noexcept
Memory usage in bytes.
size_t size() const noexcept
Number of elements.
bool contains(const T &item) const noexcept
Test membership.
Minimal std::expected-style result type for C++20.
Memory-optimized quotient filter with bit-packed slots.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Quotient filter: a cache-friendly probabilistic set with deletion.