use std::alloc::Layout;
use std::ptr as StdPtr;
use super::{LeafNode15, SeizeAllocator};
use crate::alloc_trait::TreeAllocator;
use crate::internode::InternodeNode;
use crate::node_pool;
use crate::nodeversion::NodeVersion;
use crate::policy::{BoxPolicy, InlinePolicy};
#[test]
fn test_seize_allocator_new() {
let _alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
}
#[test]
fn test_seize_allocator_alloc_leaf_direct() {
let alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
let ptr = alloc.alloc_leaf_direct(false, false);
assert!(!ptr.is_null());
unsafe {
assert!((*ptr).is_empty());
}
unsafe {
StdPtr::drop_in_place(ptr);
let layout = Layout::new::<LeafNode15<BoxPolicy<u64>>>();
node_pool::pool_dealloc(ptr.cast(), layout);
}
}
#[test]
fn test_seize_allocator_teardown_single_leaf() {
let alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
let ptr = alloc.alloc_leaf_direct(true, false);
assert!(!ptr.is_null());
alloc.teardown_tree(ptr.cast());
}
const INTERNODE_MAX_DEPTH: u32 = 15;
const WIDE_TREE_NUM_KEYS: u8 = 14;
#[test]
fn test_seize_allocator_teardown_deep_internode_tree() {
let alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
let leaf_ptr = alloc.alloc_leaf_direct(false, false);
let mut child_ptr: *mut u8 = leaf_ptr.cast();
for height in 0..INTERNODE_MAX_DEPTH {
let inode_ptr = alloc.alloc_internode_direct(height);
let inode: &InternodeNode = unsafe { &*inode_ptr.cast::<InternodeNode>() };
inode.set_child(0, child_ptr);
inode.set_nkeys(0);
unsafe {
let version_ptr = child_ptr.cast::<NodeVersion>();
if (*version_ptr).is_leaf() {
let leaf: &LeafNode15<BoxPolicy<u64>> = &*child_ptr.cast();
leaf.set_parent(inode_ptr);
} else {
let child_inode: &InternodeNode = &*child_ptr.cast();
child_inode.set_parent(inode_ptr);
}
}
child_ptr = inode_ptr;
}
let root_ptr = child_ptr;
unsafe {
let inode: &InternodeNode = &*root_ptr.cast();
inode.version().mark_root();
}
alloc.teardown_tree(root_ptr);
}
#[test]
fn test_seize_allocator_teardown_wide_internode_tree() {
let alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
let root_ptr = alloc.alloc_internode_direct_root(0);
let root: &InternodeNode = unsafe { &*root_ptr.cast::<InternodeNode>() };
root.set_nkeys(WIDE_TREE_NUM_KEYS);
for i in 0..=usize::from(WIDE_TREE_NUM_KEYS) {
let leaf_ptr = alloc.alloc_leaf_direct(false, false);
unsafe {
root.set_child(i, leaf_ptr.cast());
(*leaf_ptr).set_parent(root_ptr);
}
}
for i in 0..usize::from(WIDE_TREE_NUM_KEYS) {
root.set_ikey(i, (i as u64 + 1) * 100);
}
alloc.teardown_tree(root_ptr);
}
#[test]
fn test_seize_allocator_inline_new() {
let _alloc: SeizeAllocator<InlinePolicy<u64>> = SeizeAllocator::new();
}
#[test]
fn test_seize_allocator_inline_alloc_leaf_direct() {
let alloc: SeizeAllocator<InlinePolicy<u64>> = SeizeAllocator::new();
let ptr = alloc.alloc_leaf_direct(false, false);
assert!(!ptr.is_null());
unsafe {
assert!((*ptr).is_empty());
}
unsafe {
StdPtr::drop_in_place(ptr);
let layout = Layout::new::<LeafNode15<InlinePolicy<u64>>>();
node_pool::pool_dealloc(ptr.cast(), layout);
}
}
#[test]
fn test_seize_allocator_inline_teardown_single_leaf() {
let alloc: SeizeAllocator<InlinePolicy<u64>> = SeizeAllocator::new();
let ptr = alloc.alloc_leaf_direct(true, false);
assert!(!ptr.is_null());
alloc.teardown_tree(ptr.cast());
}
#[test]
fn test_seize_allocator_inline_teardown_deep_tree() {
let alloc: SeizeAllocator<InlinePolicy<u64>> = SeizeAllocator::new();
let leaf_ptr = alloc.alloc_leaf_direct(false, false);
let mut child_ptr: *mut u8 = leaf_ptr.cast();
for height in 0..INTERNODE_MAX_DEPTH {
let inode_ptr = alloc.alloc_internode_direct(height);
let inode: &InternodeNode = unsafe { &*inode_ptr.cast::<InternodeNode>() };
inode.set_child(0, child_ptr);
inode.set_nkeys(0);
unsafe {
let version_ptr = child_ptr.cast::<NodeVersion>();
if (*version_ptr).is_leaf() {
let leaf: &LeafNode15<InlinePolicy<u64>> = &*child_ptr.cast();
leaf.set_parent(inode_ptr);
} else {
let child_inode: &InternodeNode = &*child_ptr.cast();
child_inode.set_parent(inode_ptr);
}
}
child_ptr = inode_ptr;
}
let root_ptr = child_ptr;
unsafe {
let inode: &InternodeNode = &*root_ptr.cast();
inode.version().mark_root();
}
alloc.teardown_tree(root_ptr);
}
#[test]
fn test_seize_allocator_teardown_null() {
let alloc: SeizeAllocator<BoxPolicy<u64>> = SeizeAllocator::new();
alloc.teardown_tree(StdPtr::null_mut());
}
#[test]
fn test_seize_allocator_inline_teardown_null() {
let alloc: SeizeAllocator<InlinePolicy<u64>> = SeizeAllocator::new();
alloc.teardown_tree(StdPtr::null_mut());
}