Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fix unit test string::test_random #2363

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions console/types/string/src/random.rs
Original file line number Diff line number Diff line change
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);
}
}
}