Skip to content

Commit 4b53f56

Browse files
committed
Add a test verifying the number of drop calls
1 parent d14c0d2 commit 4b53f56

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: library/alloc/tests/vec.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use core::alloc::{Allocator, Layout};
2+
use core::ptr::NonNull;
3+
use std::alloc::System;
14
use std::assert_matches::assert_matches;
25
use std::borrow::Cow;
36
use std::cell::Cell;
@@ -991,6 +994,27 @@ fn test_into_iter_advance_by() {
991994
assert_eq!(i.len(), 0);
992995
}
993996

997+
#[test]
998+
fn test_into_iter_drop_allocator() {
999+
struct ReferenceCountedAllocator<'a>(DropCounter<'a>);
1000+
1001+
unsafe impl Allocator for ReferenceCountedAllocator<'_> {
1002+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
1003+
System.allocate(layout)
1004+
}
1005+
1006+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
1007+
System.deallocate(ptr, layout)
1008+
}
1009+
}
1010+
1011+
let mut drop_count = 0;
1012+
let allocator = ReferenceCountedAllocator(DropCounter { count: &mut drop_count });
1013+
let _ = Vec::<u32, _>::new_in(allocator).into_iter();
1014+
1015+
assert_eq!(drop_count, 1);
1016+
}
1017+
9941018
#[test]
9951019
fn test_from_iter_specialization() {
9961020
let src: Vec<usize> = vec![0usize; 1];

0 commit comments

Comments
 (0)