Skip to content

Commit

Permalink
bloom: Fix tests that require a random hash
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Aug 18, 2023
1 parent 39b5adf commit fd77bc2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions bloom/src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,16 @@ mod test {
);
}

fn generate_random_hash() -> Hash {
let mut rng = rand::thread_rng();
let mut hash = [0u8; solana_sdk::hash::HASH_BYTES];
rng.fill(&mut hash);
Hash::new_from_array(hash)
}

#[test]
fn test_atomic_bloom() {
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
let hash_values: Vec<_> = std::iter::repeat_with(generate_random_hash)
.take(1200)
.collect();
let bloom: AtomicBloom<_> = Bloom::<Hash>::random(1287, 0.1, 7424).into();
Expand All @@ -327,7 +334,7 @@ mod test {
for hash_value in hash_values {
assert!(bloom.contains(&hash_value));
}
let false_positive = std::iter::repeat_with(Hash::new_unique)
let false_positive = std::iter::repeat_with(generate_random_hash)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand All @@ -339,7 +346,7 @@ mod test {
let mut rng = rand::thread_rng();
let keys: Vec<_> = std::iter::repeat_with(|| rng.gen()).take(5).collect();
let mut bloom = Bloom::<Hash>::new(9731, keys.clone());
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
let hash_values: Vec<_> = std::iter::repeat_with(generate_random_hash)
.take(1000)
.collect();
for hash_value in &hash_values {
Expand Down Expand Up @@ -374,7 +381,7 @@ mod test {
assert!(bloom.contains(hash_value));
}
// Round trip, inserting new hash values.
let more_hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
let more_hash_values: Vec<_> = std::iter::repeat_with(generate_random_hash)
.take(1000)
.collect();
let bloom: AtomicBloom<_> = bloom.into();
Expand All @@ -389,7 +396,7 @@ mod test {
for hash_value in &more_hash_values {
assert!(bloom.contains(hash_value));
}
let false_positive = std::iter::repeat_with(Hash::new_unique)
let false_positive = std::iter::repeat_with(generate_random_hash)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand All @@ -408,7 +415,7 @@ mod test {
for hash_value in &more_hash_values {
assert!(bloom.contains(hash_value));
}
let false_positive = std::iter::repeat_with(Hash::new_unique)
let false_positive = std::iter::repeat_with(generate_random_hash)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand Down

0 comments on commit fd77bc2

Please sign in to comment.