rust/tests/run-pass/btreemap.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

// compile-flags: -Zmiri-strict-provenance
2020-09-11 05:20:08 -05:00
#![feature(btree_drain_filter)]
2019-02-13 10:22:06 -06:00
use std::collections::{BTreeMap, BTreeSet};
2020-09-11 05:20:08 -05:00
use std::mem;
2019-02-13 10:22:06 -06:00
2017-08-28 07:10:59 -05:00
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub enum Foo {
A(&'static str),
_B,
_C,
}
2020-09-10 01:38:43 -05:00
// Gather all references from a mutable iterator and make sure Miri notices if
// using them is dangerous.
fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
// Gather all those references.
let mut refs: Vec<&mut T> = iter.collect();
// Use them all. Twice, to be sure we got all interleavings.
for r in refs.iter_mut() {
std::mem::swap(dummy, r);
}
for r in refs {
std::mem::swap(dummy, r);
}
}
2017-08-28 07:10:59 -05:00
pub fn main() {
2019-02-13 10:22:06 -06:00
let mut b = BTreeSet::new();
2017-08-28 07:10:59 -05:00
b.insert(Foo::A("\'"));
b.insert(Foo::A("/="));
b.insert(Foo::A("#"));
b.insert(Foo::A("0o"));
assert!(b.remove(&Foo::A("/=")));
assert!(!b.remove(&Foo::A("/=")));
2019-02-13 10:22:06 -06:00
// Also test a lower-alignment type, where the NodeHeader overlaps with
// the keys.
let mut b = BTreeSet::new();
2020-09-10 01:38:43 -05:00
b.insert(1024u16);
b.insert(7u16);
2019-02-13 10:22:06 -06:00
let mut b = BTreeMap::new();
2020-09-10 01:38:43 -05:00
b.insert(format!("bar"), 1024);
b.insert(format!("baz"), 7);
for i in 0..60 {
b.insert(format!("key{}", i), i);
}
test_all_refs(&mut 13, b.values_mut());
2020-09-11 05:20:08 -05:00
// Test forgetting the drain.
let mut d = b.drain_filter(|_, i| *i < 30);
d.next().unwrap();
mem::forget(d);
2017-08-28 07:10:59 -05:00
}