]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/hashmap.rs
tweak entropy tests a bit
[rust.git] / tests / run-pass / hashmap.rs
1 // compile-flags: -Zmiri-seed=0000000000000000
2
3 use std::collections::{self, HashMap};
4 use std::hash::{BuildHasherDefault, BuildHasher};
5
6 fn test_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
7     map.insert(0, 0);
8     assert_eq!(map.values().fold(0, |x, y| x+y), 0);
9
10     let table_base = map.get(&0).unwrap() as *const _;
11
12     let num = 22; // large enough to trigger a resize
13     for i in 1..num {
14         map.insert(i, i);
15     }
16     assert!(table_base != map.get(&0).unwrap() as *const _); // make sure relocation happened
17     assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2); // check the right things are in the table now
18
19     // Inserting again replaces the existing entries
20     for i in 0..num {
21         map.insert(i, num-1-i);
22     }
23     assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2);
24
25     // TODO: Test Entry API, Iterators, ...
26
27 }
28
29 fn main() {
30     if cfg!(not(target_os = "macos")) {
31         let map: HashMap<i32, i32> = HashMap::default();
32         test_map(map);
33     } else {
34         // TODO: Implement random number generation on OS X.
35         // Until then, use a deterministic map.
36         let map : HashMap<i32, i32, BuildHasherDefault<collections::hash_map::DefaultHasher>> = HashMap::default();
37         test_map(map);
38     }
39 }