Add a test verifying the number of drop calls

This commit is contained in:
Jörn Horstmann 2022-03-25 13:00:49 +01:00
parent d14c0d2acb
commit 4b53f563bd
1 changed files with 24 additions and 0 deletions

View File

@ -1,3 +1,6 @@
use core::alloc::{Allocator, Layout};
use core::ptr::NonNull;
use std::alloc::System;
use std::assert_matches::assert_matches;
use std::borrow::Cow;
use std::cell::Cell;
@ -991,6 +994,27 @@ fn test_into_iter_advance_by() {
assert_eq!(i.len(), 0);
}
#[test]
fn test_into_iter_drop_allocator() {
struct ReferenceCountedAllocator<'a>(DropCounter<'a>);
unsafe impl Allocator for ReferenceCountedAllocator<'_> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
System.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
System.deallocate(ptr, layout)
}
}
let mut drop_count = 0;
let allocator = ReferenceCountedAllocator(DropCounter { count: &mut drop_count });
let _ = Vec::<u32, _>::new_in(allocator).into_iter();
assert_eq!(drop_count, 1);
}
#[test]
fn test_from_iter_specialization() {
let src: Vec<usize> = vec![0usize; 1];