Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Oct 20, 2024
1 parent c23ab6f commit 10ca6cb
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,85 @@ mod tests {
unsafe { allocator.deallocate(z2) };
}

#[test]
fn reusing_slots4() {
let allocator = SlabAllocator::<i32>::new(2);

let x = allocator.allocate();
let y = allocator.allocate();

unsafe { allocator.deallocate(y) };

let y2 = allocator.allocate();
assert_eq!(y2, y);

unsafe { allocator.reset() };

let x2 = allocator.allocate();
let y3 = allocator.allocate();
assert_eq!(x2, x);
assert_eq!(y3, y2);

unsafe { allocator.deallocate(y3) };
unsafe { allocator.deallocate(x2) };
}

#[test]
fn reusing_slots5() {
let allocator = SlabAllocator::<i32>::new(1);

let x = allocator.allocate();

unsafe { allocator.deallocate(x) };

let x2 = allocator.allocate();
assert_eq!(x, x2);

let y = allocator.allocate();
let z = allocator.allocate();

unsafe { allocator.reset() };

let z2 = allocator.allocate();
let y2 = allocator.allocate();
assert_eq!(z2, z);
assert_eq!(y2, y);

unsafe { allocator.deallocate(z2) };
unsafe { allocator.deallocate(y2) };
}

#[test]
fn reusing_slots6() {
let allocator = SlabAllocator::<i32>::new(2);

let x = allocator.allocate();
let y = allocator.allocate();

unsafe { allocator.reset() };

let x2 = allocator.allocate();
let y2 = allocator.allocate();
let z = allocator.allocate();
assert_eq!(x2, x);
assert_eq!(y2, y);

unsafe { allocator.deallocate(x2) };
unsafe { allocator.deallocate(z) };
unsafe { allocator.deallocate(y2) };
unsafe { allocator.reset() };

let z2 = allocator.allocate();
let _ = allocator.allocate();
let x3 = allocator.allocate();
let y3 = allocator.allocate();
assert_eq!(z2, z);
assert_eq!(x3, x2);
assert_eq!(y3, y2);

unsafe { allocator.reset() };
}

#[test]
fn same_slab() {
const MAX_DIFF: usize = 2 * mem::size_of::<Slot<i32>>();
Expand Down

0 comments on commit 10ca6cb

Please sign in to comment.