add test checking that Vec push/pop does not invalidate pointers

This commit is contained in:
Ralf Jung 2019-05-25 10:11:00 +02:00
parent 46805805ab
commit 9d82826e55
1 changed files with 21 additions and 0 deletions

View File

@ -1152,3 +1152,24 @@ fn test_try_reserve_exact() {
}
}
#[test]
fn test_stable_push_pop() {
// Test that, if we reserved enough space, adding and removing elements does not
// invalidate references into the vector (such as `v0`). This test also
// runs in Miri, which would detect such problems.
let mut v = Vec::with_capacity(10);
v.push(13);
// laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
let v0 = unsafe { &*(&v[0] as *const _) };
// Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
v.push(1);
v.push(2);
v.insert(1, 1);
assert_eq!(*v0, 13);
v.remove(1);
v.pop().unwrap();
assert_eq!(*v0, 13);
}