]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/hashmap.rs
Rollup merge of #105784 - yanns:update_stdarch, r=Amanieu
[rust.git] / src / tools / miri / tests / pass / hashmap.rs
1 use std::collections::HashMap;
2 use std::hash::BuildHasher;
3
4 // Gather all references from a mutable iterator and make sure Miri notices if
5 // using them is dangerous.
6 fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
7     // Gather all those references.
8     let mut refs: Vec<&mut T> = iter.collect();
9     // Use them all. Twice, to be sure we got all interleavings.
10     for r in refs.iter_mut() {
11         std::mem::swap(dummy, r);
12     }
13     for r in refs {
14         std::mem::swap(dummy, r);
15     }
16 }
17
18 fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
19     map.insert(0, 0);
20     assert_eq!(map.values().fold(0, |x, y| x + y), 0);
21
22     let num = 25;
23     for i in 1..num {
24         map.insert(i, i);
25     }
26     assert_eq!(map.values().fold(0, |x, y| x + y), num * (num - 1) / 2); // check the right things are in the table now
27
28     // Inserting again replaces the existing entries
29     for i in 0..num {
30         map.insert(i, num - 1 - i);
31     }
32     assert_eq!(map.values().fold(0, |x, y| x + y), num * (num - 1) / 2);
33
34     test_all_refs(&mut 13, map.values_mut());
35 }
36
37 fn main() {
38     // hashbrown uses Miri on its own CI; we just do a smoketest here.
39     smoketest_map(HashMap::new());
40 }