Skip to content

test Vec::extend #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6050e523bae6de61de4e060facc43dc512adaccd
e6cef0445779724b469ab7b9a8d3c05d9e848ca8
File renamed without changes.
38 changes: 38 additions & 0 deletions tests/run-pass/vecs.rs → tests/run-pass/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,39 @@ fn vec_reallocate() -> Vec<u8> {
v
}

fn vec_push_ptr_stable() {
let mut v = Vec::with_capacity(10);
v.push(0);
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
v.push(1);
let _val = *v0;
}

fn vec_extend_ptr_stable() {
let mut v = Vec::with_capacity(10);
v.push(0);
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
// `slice::Iter` (with `T: Copy`) specialization
v.extend(&[1]);
let _val = *v0;
// `vec::IntoIter` specialization
v.extend(vec![2]);
let _val = *v0;
// `TrustedLen` specialization
v.extend(std::iter::once(3));
let _val = *v0;
// base case
v.extend(std::iter::once(3).filter(|_| true));
let _val = *v0;
}

fn vec_truncate_ptr_stable() {
let mut v = vec![0; 10];
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
v.truncate(5);
let _val = *v0;
}

fn main() {
assert_eq!(vec_reallocate().len(), 5);

Expand All @@ -89,4 +122,9 @@ fn main() {
// Test interesting empty slice comparison
// (one is a real pointer, one an integer pointer).
assert_eq!((200..-5).step_by(1).collect::<Vec<isize>>(), []);

// liballoc has a more extensive test of this, but let's at least do a smoke test here.
vec_push_ptr_stable();
vec_extend_ptr_stable();
vec_truncate_ptr_stable();
}