use std::alloc::System;
use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM};
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
fn allocs_for(f: impl Fn()) -> usize {
f(); let region = Region::new(GLOBAL);
f();
region.change().allocations
}
#[test]
fn preset_per_call_allocations_are_bounded() {
let canon_input = "Ηеllо\u{202E}\u{200B}Wоrld";
let canon = allocs_for(|| {
let _ = disarm::api::canonicalize(canon_input);
});
assert!(
canon <= 6,
"canonicalize allocated {canon} times/call (expected <=6 after ping-pong)"
);
let strict = allocs_for(|| {
let _ = disarm::api::canonicalize_strict(canon_input);
});
assert!(
strict <= 6,
"canonicalize_strict allocated {strict} times/call (expected <=6)"
);
let key_input = "CAFÉ\u{200B} ИМЯ";
let key = allocs_for(|| {
let _ = disarm::api::search_key(key_input, None);
});
assert!(
key <= 4,
"search_key allocated {key} times/call (expected <=4 after ping-pong)"
);
let sort_input = "Über ИМЯ Война";
let sort = allocs_for(|| {
let _ = disarm::api::sort_key(sort_input, None);
});
assert!(
sort <= 6,
"sort_key allocated {sort} times/call (expected <=6)"
);
let benign = "The quick brown fox jumps over the lazy dog. Hello world.";
for (name, n) in [
(
"canonicalize",
allocs_for(|| {
let _ = disarm::api::canonicalize(benign);
}),
),
(
"strip_obfuscation",
allocs_for(|| {
let _ = disarm::api::strip_obfuscation(benign);
}),
),
(
"search_key",
allocs_for(|| {
let _ = disarm::api::search_key("the quick brown fox jumps over", None);
}),
),
] {
assert_eq!(
n, 0,
"{name} on benign ASCII allocated {n} times/call (Cow fast path expected 0)"
);
}
let benign_nonascii = "日本語漢字 한국어 café";
let n = allocs_for(|| {
let _ = disarm::api::canonicalize(benign_nonascii);
});
assert_eq!(
n, 0,
"canonicalize on benign non-ASCII allocated {n} times/call (Option D expected 0)"
);
let ws_dirty = "the quick brown fox jumps over the lazy dog. hello world. ";
for (name, n) in [
(
"canonicalize",
allocs_for(|| {
let _ = disarm::api::canonicalize(ws_dirty);
}),
),
(
"strip_obfuscation",
allocs_for(|| {
let _ = disarm::api::strip_obfuscation(ws_dirty);
}),
),
] {
assert!(
n <= 1,
"{name} on whitespace-only-dirty ASCII allocated {n} times/call (#464 expected <=1)"
);
}
}