File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -29,27 +29,29 @@ mod tests {
29
29
use super :: * ;
30
30
use snarkvm_console_network_environment:: Console ;
31
31
32
- use std:: collections:: HashSet ;
32
+ use std:: collections:: HashMap ;
33
33
34
34
type CurrentEnvironment = Console ;
35
35
36
36
const ITERATIONS : usize = 100 ;
37
37
38
38
#[ test]
39
39
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 ) ;
42
42
43
43
let mut rng = TestRng :: default ( ) ;
44
44
45
- // Note: This test technically has a `(1 + 2 + ... + ITERATIONS) / MODULUS` probability of being flaky.
46
45
for _ in 0 ..ITERATIONS {
47
46
// Sample a random value.
48
47
let string: StringType < CurrentEnvironment > = Uniform :: rand ( & mut rng) ;
49
- assert ! ( !set. contains( & string) , "{}" , string) ;
50
48
51
49
// 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) ;
53
55
}
54
56
}
55
57
}
You can’t perform that action at this time.
0 commit comments