Skip to content

Commit 2f3b3e1

Browse files
committed
Add pseudorandom filter test function to geo_filters
Also change the RNG in the test_rng harness to use chacha12 since that's portable.
1 parent 4b6bcfc commit 2f3b3e1

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

crates/geo_filters/src/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,14 @@ pub(crate) fn take_ref<I: Iterator>(iter: &mut I, n: usize) -> impl Iterator<Ite
353353

354354
#[cfg(test)]
355355
pub(crate) mod tests {
356-
use rand::{rngs::StdRng, RngCore};
356+
use rand::RngCore;
357+
use rand_chacha::ChaCha12Rng;
357358

358359
use crate::{Count, Method};
359360

360361
/// Runs estimation trials and returns the average precision and variance.
361362
pub(crate) fn test_estimate<M: Method, C: Count<M>>(
362-
rnd: &mut StdRng,
363+
rnd: &mut ChaCha12Rng,
363364
f: impl Fn() -> C,
364365
) -> (f32, f32) {
365366
let cnt = 10000usize;

crates/geo_filters/src/config/lookup.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ impl HashToBucketLookup {
4545

4646
#[cfg(test)]
4747
mod tests {
48-
use rand::{rngs::StdRng, RngCore};
48+
use rand::RngCore;
49+
use rand_chacha::ChaCha12Rng;
4950

5051
use crate::{
5152
config::{hash_to_bucket, phi_f64},
@@ -70,7 +71,7 @@ mod tests {
7071
});
7172
}
7273

73-
fn lookup_random_hashes_variance<const B: usize>(rnd: &mut StdRng, n: u64) -> f64 {
74+
fn lookup_random_hashes_variance<const B: usize>(rnd: &mut ChaCha12Rng, n: u64) -> f64 {
7475
let phi = phi_f64(B);
7576
let buckets = HashToBucketLookup::new(B);
7677

crates/geo_filters/src/diff_count.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ impl<'a, C: GeoConfig<Diff>> GeoDiffCount<'a, C> {
353353
pub fn iter_ones(&self) -> impl Iterator<Item = C::BucketType> + '_ {
354354
iter_ones(self.bit_chunks().peekable()).map(C::BucketType::from_usize)
355355
}
356+
357+
/// Generate a pseudo-random filter. For a given number of items
358+
#[cfg(test)]
359+
pub fn pseudorandom_filter(config: C, items: usize) -> Self {
360+
use rand::RngCore;
361+
use rand_chacha::rand_core::SeedableRng;
362+
363+
let mut rng = rand_chacha::ChaCha12Rng::seed_from_u64(items as u64);
364+
let mut filter = Self::new(config);
365+
for _ in 0..items {
366+
filter.push_hash(rng.next_u64());
367+
}
368+
filter
369+
}
356370
}
357371

358372
/// Applies a repeated bit mask to the underlying filter.
@@ -434,7 +448,8 @@ mod tests {
434448
use std::io::Write;
435449

436450
use itertools::Itertools;
437-
use rand::{rngs::StdRng, seq::IteratorRandom, RngCore};
451+
use rand::{seq::IteratorRandom, RngCore};
452+
use rand_chacha::ChaCha12Rng;
438453

439454
use crate::{
440455
build_hasher::UnstableDefaultBuildHasher,
@@ -658,7 +673,7 @@ mod tests {
658673
// This helper exists in order to easily test serializing types with different
659674
// bucket types in the MSB sparse bit field representation. See tests below.
660675
#[cfg(target_endian = "little")]
661-
fn serialization_round_trip<C: GeoConfig<Diff> + Default>(rnd: &mut StdRng) {
676+
fn serialization_round_trip<C: GeoConfig<Diff> + Default>(rnd: &mut ChaCha12Rng) {
662677
// Run 100 simulations of random values being put into
663678
// a diff counter. "Serializing" to a vector to emulate
664679
// writing to a disk, and then deserializing and asserting

crates/geo_filters/src/test_rng.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
22

3-
use rand::{rngs::StdRng, SeedableRng as _};
3+
use rand::SeedableRng as _;
4+
use rand_chacha::ChaCha12Rng;
45

56
/// Provides a seeded random number generator to tests which require some
67
/// degree of randomization. If the test panics the harness will print the
@@ -12,7 +13,7 @@ use rand::{rngs::StdRng, SeedableRng as _};
1213
/// is only ran once with this seed.
1314
pub fn prng_test_harness<F>(iterations: usize, mut test_fn: F)
1415
where
15-
F: FnMut(&mut StdRng),
16+
F: FnMut(&mut ChaCha12Rng),
1617
{
1718
let maybe_manual_seed = std::env::var("TEST_SEED")
1819
.map(|s| s.parse::<u64>().expect("Parse TEST_SEED to u64"))
@@ -21,12 +22,12 @@ where
2122
let maybe_panic = catch_unwind(AssertUnwindSafe(|| {
2223
if let Some(manual_seed) = maybe_manual_seed {
2324
seed = manual_seed;
24-
let mut rng = StdRng::seed_from_u64(seed);
25+
let mut rng = ChaCha12Rng::seed_from_u64(seed);
2526
test_fn(&mut rng);
2627
} else {
2728
for _ in 0..iterations {
2829
seed = rand::random();
29-
let mut rng = StdRng::seed_from_u64(seed);
30+
let mut rng = ChaCha12Rng::seed_from_u64(seed);
3031
test_fn(&mut rng);
3132
}
3233
}

0 commit comments

Comments
 (0)