From fd77bc2ba9f95ace8dca5283d37581b6d188f4c0 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Fri, 18 Aug 2023 17:43:45 +0200 Subject: [PATCH] bloom: Fix tests that require a random hash --- bloom/src/bloom.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/bloom/src/bloom.rs b/bloom/src/bloom.rs index b76b6f43b01b16..78f985b1b4c7b3 100644 --- a/bloom/src/bloom.rs +++ b/bloom/src/bloom.rs @@ -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::::random(1287, 0.1, 7424).into(); @@ -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(); @@ -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::::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 { @@ -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(); @@ -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(); @@ -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();