use std::sync::atomic::Ordering;
use crate::cap_inspect::CapInspect;
use crate::registry;
use crate::{TrackedVec, TrackedVecDeque};
fn creation_count(name: &str) -> u64 {
let mut total = 0u64;
registry::registry().scan(|_, stats| {
if stats.name == name {
total += stats.creation_count.load(Ordering::Relaxed);
}
});
total
}
fn total_observed(name: &str) -> u64 {
let mut total = 0u64;
registry::registry().scan(|_, stats| {
if stats.name == name {
total += stats.samples.total_observed();
}
});
total
}
#[cfg(not(debug_assertions))]
fn has_entry(name: &str) -> bool {
let mut found = false;
registry::registry().scan(|_, stats| {
if stats.name == name {
found = true;
}
});
found
}
#[test]
fn safe_only_binding_creation_count_equals_one() {
let name = "creation_count_tests:safe_only";
let before_cc = creation_count(name);
let before_to = total_observed(name);
{
let mut v = TrackedVec::wrap_from(
Vec::<u32>::with_capacity(32),
name,
file!(),
line!(),
column!(),
);
v.push(1u32);
v.push(2);
let _len = v.len();
let _ref: &[u32] = &v;
}
assert_eq!(
creation_count(name) - before_cc,
1,
"wrap_from must increment creation_count exactly once"
);
assert_eq!(
total_observed(name) - before_to,
1,
"safe-only binding: exactly one Drop sample"
);
}
#[test]
fn mixed_binding_creation_count_stays_one() {
let name = "creation_count_tests:mixed";
let (ctor_file, ctor_line, ctor_col) = (file!(), line!(), column!());
registry::record_creation(name, ctor_file, ctor_line, ctor_col);
let before_cc = creation_count(name);
let before_to = total_observed(name);
let v: Vec<u32> = Vec::with_capacity(16);
CapInspect::cap_inspect_at(&v, name, ctor_file, ctor_line, ctor_col);
CapInspect::cap_inspect_at(&v, name, ctor_file, ctor_line, ctor_col);
CapInspect::cap_inspect_at(&v, name, ctor_file, ctor_line, ctor_col);
assert_eq!(
creation_count(name) - before_cc,
0,
"cap_inspect_at must not increment creation_count; it was already set by record_creation"
);
assert_eq!(
total_observed(name) - before_to,
3,
"3 cap_inspect_at calls must produce 3 samples"
);
registry::record_sample(ctor_file, ctor_line, ctor_col, v.capacity());
assert_eq!(
total_observed(name) - before_to,
4,
"3 cap_inspect + 1 Drop = 4 total observations"
);
assert_eq!(
creation_count(name) - before_cc,
0,
"creation_count must remain unchanged after all samples"
);
}
#[test]
#[cfg(not(debug_assertions))]
fn cap_inspect_only_site_does_not_create_orphan_entry() {
let orphan_name = "creation_count_tests:orphan_never_registered";
let orphan_file = "orphan_src/not_a_real_file.rs";
let orphan_line: u32 = 99_999;
let orphan_col: u32 = 1;
assert!(
!has_entry(orphan_name),
"precondition: orphan site must not be in the registry"
);
let v: Vec<u32> = Vec::with_capacity(8);
CapInspect::cap_inspect_at(&v, orphan_name, orphan_file, orphan_line, orphan_col);
assert!(
!has_entry(orphan_name),
"cap_inspect_at on unregistered site must not create a registry entry"
);
assert_eq!(
creation_count(orphan_name),
0,
"orphan cap_inspect must not affect creation_count"
);
assert_eq!(
total_observed(orphan_name),
0,
"orphan cap_inspect must not record any observations"
);
}
#[test]
fn mixed_consumption_samples_in_reservoir() {
let name = "creation_count_tests:reservoir_samples";
let (ctor_file, ctor_line, ctor_col) = (file!(), line!(), column!());
registry::record_creation(name, ctor_file, ctor_line, ctor_col);
let before_to = total_observed(name);
let caps: &[usize] = &[8, 16, 32, 64];
for &cap in caps {
let v: Vec<u8> = Vec::with_capacity(cap);
CapInspect::cap_inspect_at(&v, name, ctor_file, ctor_line, ctor_col);
}
assert_eq!(
total_observed(name) - before_to,
caps.len() as u64,
"total_observed must equal the number of cap_inspect_at calls"
);
let mut snapshot_max = 0usize;
registry::registry().scan(|_, stats| {
if stats.name == name {
let snap = stats.samples.snapshot();
if let Some(&m) = snap.iter().max() {
snapshot_max = snapshot_max.max(m);
}
}
});
assert!(
snapshot_max >= 64,
"reservoir must contain the largest cap_inspect sample (64), got {snapshot_max}"
);
}
#[test]
fn wrap_from_construction_count_one() {
let name = "creation_count_tests:wrap_from_count_one";
let before = creation_count(name);
let inner: Vec<u32> = Vec::with_capacity(64);
let _t = TrackedVec::wrap_from(inner, name, file!(), line!(), column!());
assert_eq!(
creation_count(name) - before,
1,
"wrap_from must increment creation_count exactly once"
);
}
#[test]
fn with_capacity_named_construction_count_one() {
let name = "creation_count_tests:with_capacity_named_count_one";
let before = creation_count(name);
let _t: TrackedVecDeque<u32> =
TrackedVecDeque::with_capacity_named(16, name, file!(), line!(), column!());
assert_eq!(
creation_count(name) - before,
1,
"with_capacity_named must increment creation_count exactly once"
);
}