Skip to content

Commit

Permalink
Add a test verifying the number of drop calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorstmann committed Mar 25, 2022
1 parent d14c0d2 commit 4b53f56
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 4b53f56

Please sign in to comment.