Skip to content

Commit 71191bc

Browse files
committed
Add test for VecDeque truncate
1 parent aa89353 commit 71191bc

File tree

1 file changed

+35
-0
lines changed
  • src/liballoc/collections/vec_deque

1 file changed

+35
-0
lines changed

src/liballoc/collections/vec_deque/tests.rs

+35
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,41 @@ fn test_clone_from() {
384384
}
385385
}
386386

387+
#[test]
388+
fn test_vec_deque_truncate_drop() {
389+
static mut DROPS: u32 = 0;
390+
#[derive(Clone)]
391+
struct Elem(i32);
392+
impl Drop for Elem {
393+
fn drop(&mut self) {
394+
unsafe {
395+
DROPS += 1;
396+
}
397+
}
398+
}
399+
400+
let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
401+
for push_front in 0..=v.len() {
402+
let v = v.clone();
403+
let mut tester = VecDeque::with_capacity(5);
404+
for (index, elem) in v.into_iter().enumerate() {
405+
if index < push_front {
406+
tester.push_front(elem);
407+
} else {
408+
tester.push_back(elem);
409+
}
410+
}
411+
assert_eq!(unsafe { DROPS }, 0);
412+
tester.truncate(3);
413+
assert_eq!(unsafe { DROPS }, 2);
414+
tester.truncate(0);
415+
assert_eq!(unsafe { DROPS }, 5);
416+
unsafe {
417+
DROPS = 0;
418+
}
419+
}
420+
}
421+
387422
#[test]
388423
fn issue_53529() {
389424
use crate::boxed::Box;

0 commit comments

Comments
 (0)