about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJörn Horstmann <git@jhorstmann.net>2022-03-25 13:00:49 +0100
committerJörn Horstmann <git@jhorstmann.net>2022-03-25 13:28:19 +0100
commit4b53f563bd5b0f0e9cad26dbf2514f9a72c3fa2f (patch)
tree0cefb35170faca7906d0d015ee243b8a5e077f3d
parentd14c0d2acb3ff1d0111920185109c9697a3cd460 (diff)
Add a test verifying the number of drop calls
-rw-r--r--library/alloc/tests/vec.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
index ca0fcc855c7..ab2414a6dc0 100644
--- a/library/alloc/tests/vec.rs
+++ b/library/alloc/tests/vec.rs
@@ -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;
@@ -992,6 +995,27 @@ fn test_into_iter_advance_by() {
 }
 
 #[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];
     let srcptr = src.as_ptr();