36# include <gtest/gtest.h>
52TEST(StreamingAlgorithms, ReservoirSampling)
55 for (
int i = 0; i < 1000; ++i) data.
append(i);
60 ASSERT_EQ(sample.size(),
k);
63 for (
size_t i = 0; i < sample.size(); ++i)
65 EXPECT_GE(sample[i], 0);
66 EXPECT_LT(sample[i], 1000);
71 for (
size_t i = 0; i < sample.size(); ++i)
72 unique_vals.insert(sample[i]);
73 EXPECT_EQ(unique_vals.size(),
k);
76TEST(StreamingAlgorithms, CountMinSketch)
80 cms.update(
"apple", 10);
81 cms.update(
"banana", 5);
82 cms.update(
"cherry", 1);
84 for (
int i = 0; i < 100; ++i) cms.update(
"apple");
86 EXPECT_GE(cms.estimate(
"apple"), 110u);
87 EXPECT_GE(cms.estimate(
"banana"), 5u);
88 EXPECT_GE(cms.estimate(
"cherry"), 1u);
91 EXPECT_LE(cms.estimate(
"cherry"), 10u);
94 const size_t before = cms.estimate(
"dragonfruit");
95 cms.update(
"dragonfruit", 5);
96 EXPECT_GE(cms.estimate(
"dragonfruit"), before + 5);
109 for (
int i = 0; i < n; ++i)
113 EXPECT_NEAR(est, 1000.0, 1000.0 * 0.05);
126TEST(StreamingAlgorithms, MovedFromSketchesAreEmptyAndUsable)
129 for (
int i = 0; i < 1000; ++i)
133 EXPECT_DOUBLE_EQ(hll2.
estimate(), est);
135 EXPECT_DOUBLE_EQ(hll.
estimate(), 0.0);
140 small_hll = std::move(hll2);
141 EXPECT_DOUBLE_EQ(small_hll.
estimate(), est);
151 EXPECT_EQ(cms.
width(), 64u);
156 small_cms = std::move(cms2);
157 EXPECT_EQ(small_cms.
estimate(7), 5u);
158 EXPECT_EQ(cms2.
width(), 2u);
165 EXPECT_EQ(mh.
size(), 64u);
170 small_mh = std::move(mh2);
171 EXPECT_EQ(small_mh.
size(), 64u);
172 EXPECT_EQ(mh2.
size(), 2u);
177 for (
int i = 0; i < 4; ++i)
183 for (
int i = 0; i < 20; ++i)
187 small_rs = std::move(rs2);
190 for (
int i = 0; i < 5; ++i)
197 EXPECT_EQ(mh.
size(), expected_k);
199 EXPECT_EQ(sig.size(), expected_k);
203 EXPECT_EQ(mh.
size(), expected_k);
204 for (
size_t i = 0; i < expected_k; ++i)
205 EXPECT_EQ(sig[i], std::numeric_limits<uint64_t>::max());
210 constexpr size_t K = 256;
220 for (
int i = 0; i < 100; ++i)
226 ASSERT_EQ(set1.
size(), 100u);
227 ASSERT_EQ(set2.
size(), 100u);
228 ASSERT_EQ(set3.
size(), 100u);
230 for (
int i = 0; i < 100; ++i) mh1.
update(i);
231 for (
int i = 50; i < 150; ++i) mh2.
update(i);
232 for (
int i = 200; i < 300; ++i) mh3.
update(i);
235 EXPECT_EQ(mh1.
size(), K);
236 EXPECT_EQ(mh2.
size(), K);
237 EXPECT_EQ(mh3.
size(), K);
238 EXPECT_EQ(mh1_range.
size(), K);
241 EXPECT_DOUBLE_EQ(mh1.
similarity(mh1_range), 1.0);
245 EXPECT_NE(sig_ptr,
nullptr);
247 ASSERT_EQ(sig1.size(), K);
248 ASSERT_EQ(sig1_range.size(), K);
250 bool changed_after_update =
false;
251 for (
size_t i = 0; i < K; ++i)
253 EXPECT_EQ(sig1[i], sig1_range[i]);
254 if (sig1_range[i] != std::numeric_limits<uint64_t>::max())
255 changed_after_update =
true;
257 EXPECT_TRUE(changed_after_update);
262 EXPECT_NEAR(sim12, 1.0/3.0, 0.1);
263 EXPECT_NEAR(sim13, 0.0, 0.05);
268 for (
int i = 0; i < 150; ++i)
270 ASSERT_EQ(union12.
size(), 150u);
275 EXPECT_EQ(&merge_ret, &mh_merged);
276 EXPECT_EQ(mh_merged.
size(), K);
277 EXPECT_DOUBLE_EQ(mh_merged.
similarity(mh_union_expected), 1.0);
282 ASSERT_EQ(sig_merged.size(), K);
283 ASSERT_EQ(sig_union.size(), K);
284 for (
size_t i = 0; i < K; ++i)
286 EXPECT_EQ(sig_merged[i], std::min(sig1[i], sig2[i]));
287 EXPECT_EQ(sig_merged[i], sig_union[i]);
297TEST(StreamingAlgorithms, MinHashNewMethods)
304 for (
int i = 0; i < 50; ++i)
307 for (
int i = 0; i < 50; ++i)
311 EXPECT_DOUBLE_EQ(mhLoop.
similarity(mhRange), 1.0);
319 for (
int i = 0; i < 50; ++i) mhA.update(i);
320 for (
int i = 25; i < 75; ++i) mhB.update(i);
321 for (
int i = 0; i < 75; ++i) mhUnion.
update(i);
325 EXPECT_GT(mhA.similarity(mhUnion), 0.8);
332 EXPECT_EQ(&ref, &mhC);
335 EXPECT_EQ(mhA.size(), 64u);
337 EXPECT_EQ(mhA.size(), 64u);
340TEST(StreamingAlgorithms, MinHashPreconditions)
351 EXPECT_THROW((mh64.
similarity(mh32)), std::domain_error);
352 EXPECT_THROW((mh32.
similarity(mh64)), std::domain_error);
353 EXPECT_THROW(mh64.
merge(mh32), std::domain_error);
354 EXPECT_THROW(mh32.
merge(mh64), std::domain_error);
362 const DynArray<std::string> doc2 = {
"this",
"is",
"a",
"test",
"document",
"with",
"other",
"words"};
363 const DynArray<std::string> doc3 = {
"completely",
"unrelated",
"content",
"nothing",
"matches",
"here",
"at",
"all"};
365 for (
size_t i = 0; i < doc1.
size(); ++i) sh1.
update(doc1[i]);
366 for (
size_t i = 0; i < doc2.
size(); ++i) sh2.
update(doc2[i]);
367 for (
size_t i = 0; i < doc3.
size(); ++i) sh3.
update(doc3[i]);
376 EXPECT_GT(sim12, 0.8);
377 EXPECT_LT(sim13, 0.7);
380TEST(StreamingAlgorithms, EdgeCases)
389 ASSERT_EQ(s1.size(), 1u);
390 EXPECT_EQ(s1[0], 42);
401 EXPECT_THROW(cms.
merge(cms_other), std::domain_error);
405 EXPECT_NEAR(hll.
estimate(), 0.0, 0.1);
407 EXPECT_NEAR(hll.
estimate(), 1.0, 0.5);
411 EXPECT_EQ(mh1.similarity(mh2), 1.0);
413 EXPECT_LT(mh1.similarity(mh2), 0.1);
415 EXPECT_EQ(mh1.similarity(mh2), 1.0);
427TEST(ReservoirSampler, overflow_size_t)
431 for (
int i = 0; i < 5; ++i)
434 EXPECT_THROW(s.
update(1), std::overflow_error);
438 for (
int i = 0; i < 5; ++i)
442 EXPECT_THROW(s2.
update(one.
begin(), one.
end()), std::overflow_error);
445TEST(ReservoirSampler, overflow_rng_range)
451 for (
int i = 0; i < 3; ++i)
454 const uint64_t rng_range = s.
rng_range();
456 EXPECT_THROW(s.
update(99), std::runtime_error);
460 for (
int i = 0; i < 3; ++i)
464 EXPECT_THROW(s2.
update(one2.
begin(), one2.
end()), std::runtime_error);
467TEST(StreamingAlgorithms, CMSSketchMerge)
Count-Min Sketch frequency estimator.
void merge(const Count_Min_Sketch &other)
Merge another sketch into this one.
CounterT estimate(const T &val) const
Estimate frequency of an element.
static Count_Min_Sketch from_error_bounds(const double epsilon, const double delta)
Create a sketch with target error bounds.
void update(const T &val, CounterT count=1)
Increment count for an element.
size_t width() const noexcept
Number of counter columns (hash buckets per row).
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Doubly-linked list (defined in tpl_dynList.H).
HyperLogLog cardinality estimator.
double estimate() const noexcept
Estimate current cardinality.
void clear()
Reset all registers to zero.
void merge(const HyperLogLog &other)
Merge another HyperLogLog into this one (union of sets).
void update(const T &val)
Add an element to the set.
size_t num_registers() const noexcept
Number of HyperLogLog registers (2^b).
MinHash signature generator.
MinHash & merge(const MinHash &other)
Merge another MinHash signature into this one.
size_t size() const noexcept
Number of hash functions used in the signature.
void clear()
Reset the signature to the initial all-maximum state.
void update(const T &val)
Add an element to the set.
double similarity(const MinHash &other) const
Estimate Jaccard similarity with another signature.
const Array< uint64_t > & get_signature() const noexcept
Returns the current signature.
Incremental reservoir sampler (Algorithm R).
size_t sample_size() const noexcept
Target sample size.
void update(const T &val)
Process a new element from the stream.
const Array< T > & get_sample() const noexcept
Returns the current random sample.
uint64_t rng_range() const noexcept
RNG output range (max - min + 1).
void set_n_seen_for_testing(const size_t n)
Forcibly set the internal stream counter (for testing only).
size_t total_seen() const noexcept
Total elements processed so far.
SimHash fingerprint generator.
void update(const T &val, const double weight=1.0)
Add a feature to the set.
static double similarity(const uint64_t f1, const uint64_t f2) noexcept
Estimate similarity with another fingerprint.
uint64_t get_fingerprint() const noexcept
Returns the fingerprint.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Count-Min Sketch for frequency estimation in streams.
HyperLogLog for approximate cardinality estimation.
MinHash for estimating Jaccard similarity between sets.
Main namespace for Aleph-w library functions.
auto reservoir_sample(Itor beg, Sent end, size_t k, unsigned long seed=static_cast< unsigned long >(std::chrono::high_resolution_clock::now().time_since_epoch().count()))
Convenience function to sample k elements from a range.
Reservoir sampling for random selection from data streams.
SimHash for estimating cosine similarity between sets of features.
static void assertMinHashContracts(MinHash< int > &mh, size_t expected_k)
Lazy and scalable dynamic array implementation.
Alias for htlist.H (DynList implementation).
Unified hash table interface.