Skip to content

Commit e7202ad

Browse files
committed
Fix test string::test_random
1 parent bb54fc4 commit e7202ad

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

console/types/string/src/random.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ mod tests {
2929
use super::*;
3030
use snarkvm_console_network_environment::Console;
3131

32-
use std::collections::HashSet;
32+
use std::collections::HashMap;
3333

3434
type CurrentEnvironment = Console;
3535

3636
const ITERATIONS: usize = 100;
3737

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

4343
let mut rng = TestRng::default();
4444

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

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

0 commit comments

Comments
 (0)