rust/tests/run-pass/vecdeque.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

// compile-flags: -Zmiri-track-raw-pointers
// ignore-windows (FIXME: tracking raw pointers does not work on Windows)
2018-08-24 12:50:31 -05:00
use std::collections::VecDeque;
2020-09-19 04:33:16 -05:00
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);
}
}
2018-08-24 12:50:31 -05:00
fn main() {
let mut dst = VecDeque::new();
dst.push_front(Box::new(1));
dst.push_front(Box::new(2));
dst.pop_back();
let mut src = VecDeque::new();
src.push_front(Box::new(2));
dst.append(&mut src);
2019-02-15 03:41:12 -06:00
for a in dst.iter() {
assert_eq!(**a, 2);
}
// Regression test for Debug and Diaplay impl's
println!("{:?} {:?}", dst, dst.iter());
println!("{:?}", VecDeque::<u32>::new().iter());
2018-08-24 12:50:31 -05:00
for a in dst {
2020-09-19 04:33:16 -05:00
assert_eq!(*a, 2);
2018-08-24 12:50:31 -05:00
}
2020-09-19 04:33:16 -05:00
// # Aliasing tests.
let mut v = std::collections::VecDeque::new();
v.push_back(1);
v.push_back(2);
// Test `fold` bad aliasing.
let mut it = v.iter_mut();
let ref0 = it.next().unwrap();
let sum = it.fold(0, |x, y| x + *y);
assert_eq!(*ref0 + sum, 3);
// Test general iterator aliasing.
v.push_front(0);
test_all_refs(&mut 0, v.iter_mut());
2018-08-24 12:50:31 -05:00
}