use nativeview_codegen::{generate_all_abi_files, generate_c_abi_header, generate_table_files};
use nativeview_tables::tables::CORE_TABLE_DESCS;
use nativeview_test_support::{AllocStats, measure_allocs};
nativeview_test_support::install_counting_allocator!();
fn assert_density(what: &str, allocs: usize, out: usize, min_bytes_per_alloc: usize) {
assert!(
allocs * min_bytes_per_alloc <= out,
"{what} took {allocs} allocations for {out} output bytes \
({} bytes/allocation, minimum {min_bytes_per_alloc})",
out / allocs.max(1)
);
}
#[test]
fn c_abi_header_allocation_ceiling() {
let _ = generate_c_abi_header(); let (header, AllocStats { allocs, bytes, .. }) = measure_allocs(generate_c_abi_header);
let header = header.expect("C header");
eprintln!(
"generate_c_abi_header: {allocs} allocations, {bytes} bytes for {} output bytes",
header.len()
);
assert_density("C header", allocs, header.len(), 23);
assert!(
bytes <= 5 * header.len(),
"C header allocated {bytes} bytes for {} output bytes",
header.len()
);
}
#[test]
fn table_files_allocation_ceiling() {
let (files, AllocStats { allocs, bytes, .. }) =
measure_allocs(|| generate_table_files(CORE_TABLE_DESCS));
let out: usize = files.expect("tables").iter().map(|f| f.content.len()).sum();
eprintln!(
"generate_table_files(17 core): {allocs} allocations, {bytes} bytes for {out} output bytes"
);
assert_density("tables", allocs, out, 29);
assert!(
bytes <= 5 * out,
"tables allocated {bytes} bytes for {out} output bytes"
);
}
#[test]
fn every_host_file_allocation_ceiling() {
let (files, AllocStats { allocs, bytes, .. }) =
measure_allocs(|| generate_all_abi_files(CORE_TABLE_DESCS));
let out: usize = files
.expect("all files")
.iter()
.map(|f| f.content.len())
.sum();
eprintln!("generate_all_abi_files: {allocs} allocations, {bytes} bytes for {out} output bytes");
assert_density("all files", allocs, out, 26);
assert!(
bytes <= 5 * out,
"all files allocated {bytes} bytes for {out} output bytes"
);
}