Skip to content

Commit

Permalink
Merge pull request #2363 from kpp/fix_test_string_random
Browse files Browse the repository at this point in the history
Fix test string::test_random
  • Loading branch information
howardwu committed Mar 2, 2024
2 parents cc68b0a + e7202ad commit d487c56
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions console/types/string/src/random.rs
Expand Up @@ -29,27 +29,29 @@ mod tests {
use super::*;
use snarkvm_console_network_environment::Console;

use std::collections::HashSet;
use std::collections::HashMap;

type CurrentEnvironment = Console;

const ITERATIONS: usize = 100;

#[test]
fn test_random() {
// Initialize a set to store all seen random elements.
let mut set = HashSet::with_capacity(ITERATIONS);
// Initialize a map[string]=>occurences to store all seen random elements.
let mut map = HashMap::with_capacity(ITERATIONS);

let mut rng = TestRng::default();

// Note: This test technically has a `(1 + 2 + ... + ITERATIONS) / MODULUS` probability of being flaky.
for _ in 0..ITERATIONS {
// Sample a random value.
let string: StringType<CurrentEnvironment> = Uniform::rand(&mut rng);
assert!(!set.contains(&string), "{}", string);

// Add the new random value to the set.
set.insert(string);
map.entry(string).and_modify(|count| *count += 1).or_insert(1);
}
for (string, count) in map {
let allowed_occurences = 1 + ITERATIONS / (string.len() * 10);
assert!(count <= allowed_occurences, "Encountered an element with a count of {}: {}", count, string);
}
}
}

0 comments on commit d487c56

Please sign in to comment.