about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/vecdeque.rs
blob: ccecf3d30a4147523e5773834be32b91a8413f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//@revisions: stack tree tree_uniq
//@compile-flags: -Zmiri-strict-provenance
//@[tree]compile-flags: -Zmiri-tree-borrows
//@[tree_uniq]compile-flags: -Zmiri-tree-borrows -Zmiri-unique-is-unique

use std::collections::VecDeque;

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() {
    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);
    for a in dst.iter() {
        assert_eq!(**a, 2);
    }

    // Regression test for Debug impl's
    println!("{:?} {:?}", dst, dst.iter());
    println!("{:?}", VecDeque::<u32>::new().iter());

    for a in dst {
        assert_eq!(*a, 2);
    }

    // # 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());
}