]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/hashmap.rs
b29b681939794e19a029e46b80c1913c93862265
[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     // TODO: Implement random number generation on OS X
31     if cfg!(not(target_os = "macos")) {
32         let map_normal: HashMap<i32, i32> = HashMap::new();
33         test_map(map_normal);
34     } else {
35         let map : HashMap<i32, i32, BuildHasherDefault<collections::hash_map::DefaultHasher>> = Default::default();
36         test_map(map);
37     }
38 }