#![allow(clippy::expect_used, clippy::unwrap_used)]
use std::sync::Arc;
use zerodds_flatdata::{FlatStruct, FlatWriter, InMemorySlotAllocator};
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[derive(Copy, Clone)]
#[repr(C)]
struct Pose1k {
bytes: [u8; 1024],
}
unsafe impl FlatStruct for Pose1k {
const TYPE_HASH: [u8; 16] = [0x42; 16];
}
#[test]
fn write_flat_is_heap_allocation_free() {
let _profiler = dhat::Profiler::builder().testing().build();
const N: usize = 1000;
let alloc = Arc::new(InMemorySlotAllocator::new(0, N + 16, Pose1k::WIRE_SIZE));
let writer = FlatWriter::<Pose1k>::new(Arc::clone(&alloc), 0b1);
let payload = Pose1k {
bytes: [0xAA; 1024],
};
writer.write(&payload).expect("warmup write");
let before = dhat::HeapStats::get();
for _ in 0..N {
writer.write(&payload).expect("write");
}
let after = dhat::HeapStats::get();
let blocks = after.total_blocks - before.total_blocks;
let bytes = after.total_bytes - before.total_bytes;
assert_eq!(
blocks, 0,
"write_flat must be heap-allocation-free: {blocks} blocks / {bytes} bytes \
allocated across {N} writes (reserve_slot + commit_slot copy into a \
pre-allocated slot — no heap)"
);
}