]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/btreemap.rs
Merge commit '598f0909568a51de8a2d1148f55a644fd8dffad0' into sync_cg_clif-2023-01-24
[rust.git] / src / tools / miri / tests / pass / btreemap.rs
1 //@compile-flags: -Zmiri-strict-provenance
2 #![feature(btree_drain_filter)]
3 use std::collections::{BTreeMap, BTreeSet};
4 use std::mem;
5
6 #[derive(PartialEq, Eq, PartialOrd, Ord)]
7 pub enum Foo {
8     A(&'static str),
9     _B,
10     _C,
11 }
12
13 // Gather all references from a mutable iterator and make sure Miri notices if
14 // using them is dangerous.
15 fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
16     // Gather all those references.
17     let mut refs: Vec<&mut T> = iter.collect();
18     // Use them all. Twice, to be sure we got all interleavings.
19     for r in refs.iter_mut() {
20         std::mem::swap(dummy, r);
21     }
22     for r in refs {
23         std::mem::swap(dummy, r);
24     }
25 }
26
27 pub fn main() {
28     let mut b = BTreeSet::new();
29     b.insert(Foo::A("\'"));
30     b.insert(Foo::A("/="));
31     b.insert(Foo::A("#"));
32     b.insert(Foo::A("0o"));
33     assert!(b.remove(&Foo::A("/=")));
34     assert!(!b.remove(&Foo::A("/=")));
35
36     // Also test a lower-alignment type, where the NodeHeader overlaps with
37     // the keys.
38     let mut b = BTreeSet::new();
39     b.insert(1024u16);
40     b.insert(7u16);
41
42     let mut b = BTreeMap::new();
43     b.insert(format!("bar"), 1024);
44     b.insert(format!("baz"), 7);
45     for i in 0..60 {
46         b.insert(format!("key{}", i), i);
47     }
48     test_all_refs(&mut 13, b.values_mut());
49
50     // Test forgetting the drain.
51     let mut d = b.drain_filter(|_, i| *i < 30);
52     d.next().unwrap();
53     mem::forget(d);
54 }