use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
use sparse_hash_map::SparseMap;
struct CountingAlloc;
static ALLOCS: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for CountingAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCS.fetch_add(1, Ordering::Relaxed);
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout);
}
}
#[global_allocator]
static GLOBAL: CountingAlloc = CountingAlloc;
#[test]
fn test_allocations_routed_and_bounded() {
let before = ALLOCS.load(Ordering::Relaxed);
let mut map: SparseMap<i64, i64> = SparseMap::new();
let nb = 1000i64;
for i in 0..nb {
map.insert(i, i * 2);
}
assert_eq!(map.len(), nb as usize);
let after = ALLOCS.load(Ordering::Relaxed);
let allocations = after - before;
assert!(allocations > 0, "map should allocate");
assert!(
allocations < nb as usize * 3,
"allocations {allocations} should stay bounded relative to {nb}"
);
}