36#include <gtest/gtest.h>
52TEST(QuotientFilterCtor, valid_parameters)
58 EXPECT_EQ(qf.
size(), 0u);
62TEST(QuotientFilterCtor, rejects_invalid_q)
68TEST(QuotientFilterCtor, rejects_invalid_r)
74TEST(QuotientFilterCtor, rejects_q_plus_r_exceeds_64)
79TEST(QuotientFilterCtor, from_capacity_creates_valid_filter)
82 EXPECT_GE(qf.capacity(), 1000u);
83 EXPECT_LE(qf.false_positive_rate(), 0.02);
86TEST(QuotientFilterCtor, from_capacity_rejects_bad_args)
100TEST(QuotientFilterBasic, insert_and_contains_no_false_negatives)
104 for (
int i = 0; i < 100; ++i)
107 EXPECT_EQ(qf.
size(), 100u);
109 for (
int i = 0; i < 100; ++i)
110 EXPECT_TRUE(qf.
contains(i)) <<
"False negative for i=" << i;
113TEST(QuotientFilterBasic, absent_elements_usually_not_found)
117 for (
int i = 0; i < 100; ++i)
120 size_t false_positives = 0;
121 for (
int i = 1000; i < 2000; ++i)
127 EXPECT_LT(false_positives, 20u)
128 <<
"Too many false positives: " << false_positives;
131TEST(QuotientFilterBasic, duplicate_insert_is_idempotent)
136 EXPECT_EQ(qf.
size(), 1u);
139 EXPECT_EQ(qf.
size(), 1u);
144TEST(QuotientFilterBasic, string_keys)
156 EXPECT_EQ(qf.
size(), 3u);
163TEST(QuotientFilterDelete, remove_existing_element)
170 EXPECT_EQ(qf.
size(), 3u);
172 EXPECT_TRUE(qf.
remove(20));
173 EXPECT_EQ(qf.
size(), 2u);
179TEST(QuotientFilterDelete, remove_nonexistent_is_noop)
184 EXPECT_FALSE(qf.
remove(999));
185 EXPECT_EQ(qf.
size(), 1u);
189TEST(QuotientFilterDelete, remove_all_makes_empty)
193 for (
int i = 0; i < 50; ++i)
196 for (
int i = 0; i < 50; ++i)
197 EXPECT_TRUE(qf.
remove(i)) <<
"Failed to remove i=" << i;
199 EXPECT_EQ(qf.
size(), 0u);
203TEST(QuotientFilterDelete, insert_after_remove)
213 EXPECT_EQ(qf.
size(), 1u);
220TEST(QuotientFilterCapacity, load_factor_computed_correctly)
225 for (
int i = 0; i < 8; ++i)
231TEST(QuotientFilterCapacity, overflow_throws)
235 for (
int i = 0; i < 4; ++i)
242 for (
int i = 100; i < 200; ++i)
245 catch (
const std::overflow_error &) { threw =
true;
break; }
254TEST(QuotientFilterIntrospection, memory_usage)
260TEST(QuotientFilterIntrospection, false_positive_rate)
267TEST(QuotientFilterIntrospection, estimate_returns_valid_params)
271 EXPECT_GE(r_val, 10);
272 EXPECT_LE(q_val + r_val, 64);
279TEST(QuotientFilterSemantics, copy_preserves_state)
282 for (
int i = 0; i < 30; ++i)
287 EXPECT_EQ(
copy.q(), qf.
q());
288 EXPECT_EQ(
copy.r(), qf.
r());
290 for (
int i = 0; i < 30; ++i)
291 EXPECT_TRUE(
copy.contains(i));
294TEST(QuotientFilterSemantics, move_transfers_state)
297 for (
int i = 0; i < 30; ++i)
300 size_t orig_size = qf.
size();
303 EXPECT_EQ(moved.
size(), orig_size);
304 for (
int i = 0; i < 30; ++i)
308TEST(QuotientFilterSemantics, swap_works)
325TEST(QuotientFilterClear, clears_all_state)
328 for (
int i = 0; i < 50; ++i)
332 EXPECT_EQ(qf.
size(), 0u);
335 for (
int i = 0; i < 50; ++i)
343TEST(QuotientFilterMerge, merge_combines_elements)
348 for (
int i = 0; i < 50; ++i)
350 for (
int i = 50; i < 100; ++i)
355 for (
int i = 0; i < 100; ++i)
356 EXPECT_TRUE(a.
contains(i)) <<
"Missing i=" << i;
359TEST(QuotientFilterMerge, merge_rejects_mismatched_params)
364 EXPECT_THROW(a.
merge(b), std::domain_error);
367TEST(QuotientFilterMerge, merge_rejects_mismatched_seed)
372 EXPECT_THROW(a.
merge(b), std::domain_error);
379TEST(QuotientFilterFPRate, empirical_fp_rate_within_bounds)
381 const size_t n = 500;
384 for (
size_t i = 0; i < n; ++i)
385 qf.
insert(
static_cast<int>(i));
387 size_t test_count = 10000;
389 for (
size_t i = n + 10000; i < n + 10000 + test_count; ++i)
390 if (qf.
contains(
static_cast<int>(i)))
393 double empirical =
static_cast<double>(fps) / test_count;
397 EXPECT_LT(empirical, theoretical * 5.0 + 0.01)
398 <<
"Empirical FP rate " << empirical
399 <<
" exceeds 5x theoretical " << theoretical;
Quotient filter for probabilistic set membership with deletion.
Quotient_Filter & insert(const T &item)
Insert an element.
static Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
Construct from desired capacity and false positive rate.
double load_factor() const noexcept
Current load factor.
size_t memory_usage() const noexcept
Memory usage in bytes.
uint8_t q() const noexcept
Quotient bits.
bool is_empty() const noexcept
True if empty.
double false_positive_rate() const noexcept
Theoretical FP probability ~ 2^{-r}.
size_t capacity() const noexcept
Number of slots (2^q).
uint8_t r() const noexcept
Remainder bits.
void merge(const Quotient_Filter &other)
Merge another filter (same q, r, seed).
size_t size() const noexcept
Number of elements.
bool contains(const T &item) const noexcept
Test membership.
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.
bool remove(const T &item) noexcept
Remove an element.
void clear() noexcept
Remove all elements.
void swap(Quotient_Filter &other) noexcept
Swap in O(1).
Minimal std::expected-style result type for C++20.
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.