add a test for shrink_in_place

This commit is contained in:
gnzlbg 2018-11-06 12:07:13 +01:00 committed by gnzlbg
parent 698010b8bc
commit d32993869a
1 changed files with 35 additions and 0 deletions

35
tests/shrink_in_place.rs Normal file
View File

@ -0,0 +1,35 @@
#![cfg_attr(feature = "alloc_trait", feature(allocator_api))]
extern crate jemallocator;
use jemallocator::Jemalloc;
#[global_allocator]
static A: Jemalloc = Jemalloc;
#[test]
#[cfg(feature = "alloc_trait")]
fn shrink_in_place() {
unsafe {
use std::alloc::{Alloc, Layout};
// allocate a "large" block of memory:
let orig_sz = 10 * 4096;
let orig_l = Layout::from_size_align(orig_sz, 1).unwrap();
let ptr = Jemalloc.alloc(orig_l).unwrap();
// try to shrink it in place to 1 byte - if this succeeds,
// the size-class of the new allocation should be different
// than that of the original allocation:
let new_sz = 1;
if let Ok(()) = Jemalloc.shrink_in_place(ptr, orig_l, new_sz) {
// test that deallocating with the new layout succeeds:
let new_l = Layout::from_size_align(new_sz, 1).unwrap();
Jemalloc.dealloc(ptr, new_l);
} else {
assert!(false);
// if shrink in place failed - deallocate with the old layout
Jemalloc.dealloc(ptr, orig_l);
}
}
}