#![cfg(feature = "dhat-compat")]
use mod_alloc::ModAlloc;
#[global_allocator]
static GLOBAL: ModAlloc = ModAlloc::new();
#[inline(never)]
fn workload() {
for _ in 0..32 {
let _v: Vec<u8> = Vec::with_capacity(256);
}
}
fn assert_required_top_level_keys(json: &str) {
for expected in [
"\"dhatFileVersion\":2",
"\"mode\":\"rust-heap\"",
"\"verb\":\"Allocated\"",
"\"bklt\":false",
"\"bkacc\":false",
"\"bu\":\"byte\"",
"\"bsu\":\"bytes\"",
"\"bksu\":\"blocks\"",
"\"tu\":\"instrs\"",
"\"Mtu\":\"Minstr\"",
"\"tuth\":0",
"\"cmd\":\"",
"\"pid\":",
"\"tg\":0",
"\"te\":0",
"\"pps\":[",
"\"ftbl\":[",
"\"[root]\"",
] {
assert!(
json.contains(expected),
"missing required JSON fragment: {expected}\nfull JSON:\n{json}"
);
}
}
#[test]
fn json_starts_and_ends_with_object_braces() {
let s = GLOBAL.dhat_json_string();
assert!(s.starts_with('{'), "JSON must start with {{; got: {s}");
assert!(s.ends_with('}'), "JSON must end with }}; got: {s}");
}
#[test]
fn json_contains_all_required_top_level_keys_after_workload() {
workload();
let s = GLOBAL.dhat_json_string();
assert_required_top_level_keys(&s);
}
#[test]
fn pps_grows_after_workload() {
let _ = GLOBAL.dhat_json_string(); workload();
let after = GLOBAL.dhat_json_string();
assert!(
after.contains("\"tb\":") || after.contains("\"tbk\":"),
"expected at least one program point in: {after}"
);
let ftbl_idx = after.find("\"ftbl\":[").expect("ftbl present");
let ftbl_chunk = &after[ftbl_idx..];
assert!(
ftbl_chunk.contains("\"[root]\""),
"ftbl missing root entry: {ftbl_chunk}"
);
}
#[test]
fn write_dhat_json_round_trips() {
workload();
let target = std::env::temp_dir().join(format!(
"mod-alloc-dhat-test-{}-{}.json",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0),
));
GLOBAL.write_dhat_json(&target).expect("write_dhat_json");
let written = std::fs::read_to_string(&target).expect("read written file");
let _ = std::fs::remove_file(&target);
assert!(written.starts_with('{') && written.ends_with('}'));
assert_required_top_level_keys(&written);
}