Auto merge of #1541 - RalfJung:rustup, r=RalfJung

Rustup, expand collection tests
This commit is contained in:
bors 2020-09-10 06:45:44 +00:00
commit a6741b0592
6 changed files with 93 additions and 8 deletions

View File

@ -1 +1 @@
e114d6228b948ce056de0bcdec2603c8e89d3727 a1894e4afe1a39f718cc27232a5a2f0d02b501f6

View File

@ -1,7 +1,7 @@
use std::collections::BinaryHeap; use std::collections::BinaryHeap;
use std::iter::Iterator; use std::iter::Iterator;
fn main() { fn zero_sized_push() {
const N: usize = 8; const N: usize = 8;
for len in 0..N { for len in 0..N {
@ -16,3 +16,22 @@ fn main() {
tester.clear(); tester.clear();
} }
} }
fn drain() {
let mut heap = (0..128i32).collect::<BinaryHeap<_>>();
assert!(!heap.is_empty());
let mut sum = 0;
for x in heap.drain() {
sum += x;
}
assert_eq!(sum, 127*128/2);
assert!(heap.is_empty());
}
fn main() {
zero_sized_push();
drain();
}

View File

@ -7,6 +7,20 @@ pub enum Foo {
_C, _C,
} }
// 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);
}
}
pub fn main() { pub fn main() {
let mut b = BTreeSet::new(); let mut b = BTreeSet::new();
b.insert(Foo::A("\'")); b.insert(Foo::A("\'"));
@ -19,11 +33,14 @@ pub fn main() {
// Also test a lower-alignment type, where the NodeHeader overlaps with // Also test a lower-alignment type, where the NodeHeader overlaps with
// the keys. // the keys.
let mut b = BTreeSet::new(); let mut b = BTreeSet::new();
b.insert(1024); b.insert(1024u16);
b.insert(7); b.insert(7u16);
let mut b = BTreeMap::new(); let mut b = BTreeMap::new();
b.insert("bar", 1024); b.insert(format!("bar"), 1024);
b.insert("baz", 7); b.insert(format!("baz"), 7);
for _val in b.iter_mut() {} for i in 0..60 {
b.insert(format!("key{}", i), i);
}
test_all_refs(&mut 13, b.values_mut());
} }

View File

@ -1,6 +1,20 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::BuildHasher; use std::hash::BuildHasher;
// 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);
}
}
fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) { fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
map.insert(0, 0); map.insert(0, 0);
assert_eq!(map.values().fold(0, |x, y| x+y), 0); assert_eq!(map.values().fold(0, |x, y| x+y), 0);
@ -16,6 +30,8 @@ fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
map.insert(i, num-1-i); map.insert(i, num-1-i);
} }
assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2); assert_eq!(map.values().fold(0, |x, y| x+y), num*(num-1)/2);
test_all_refs(&mut 13, map.values_mut());
} }
fn main() { fn main() {

View File

@ -4,7 +4,21 @@
fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> { fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> {
v.iter().cloned().collect() v.iter().cloned().collect()
} }
// 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);
}
}
fn main() { fn main() {
let mut m = list_from(&[0, 2, 4, 6, 8]); let mut m = list_from(&[0, 2, 4, 6, 8]);
let len = m.len(); let len = m.len();
@ -30,6 +44,9 @@ fn main() {
} }
assert_eq!(m.len(), 3 + len * 2); assert_eq!(m.len(), 3 + len * 2);
let mut m2 = m.clone();
assert_eq!(m.into_iter().collect::<Vec<_>>(), assert_eq!(m.into_iter().collect::<Vec<_>>(),
[-10, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99]); [-10, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99]);
test_all_refs(&mut 13, m2.iter_mut());
} }

View File

@ -1,3 +1,17 @@
// 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);
}
}
fn make_vec() -> Vec<u8> { fn make_vec() -> Vec<u8> {
let mut v = Vec::with_capacity(4); let mut v = Vec::with_capacity(4);
v.push(1); v.push(1);
@ -53,6 +67,8 @@ fn vec_iter_and_mut() {
*i += 1; *i += 1;
} }
assert_eq!(v.iter().sum::<i32>(), 2+3+4+5); assert_eq!(v.iter().sum::<i32>(), 2+3+4+5);
test_all_refs(&mut 13, v.iter_mut());
} }
fn vec_iter_and_mut_rev() { fn vec_iter_and_mut_rev() {