const SINGLE_DEFINITION: &[&str] = &[
"ln_gamma",
"ln_beta",
"regularized_incomplete_beta",
"regularized_upper_gamma",
"chi_squared_survival",
"f_distribution_sf",
"normal_quantile",
"erf",
"erfc",
];
fn crate_sources() -> Vec<(std::path::PathBuf, String)> {
fn walk(dir: &std::path::Path, out: &mut Vec<(std::path::PathBuf, String)>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
walk(&p, out);
} else if p.extension().is_some_and(|x| x == "rs")
&& let Ok(t) = std::fs::read_to_string(&p)
{
out.push((p, t));
}
}
}
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut out = Vec::new();
walk(&root, &mut out);
assert!(!out.is_empty(), "found no sources under {}", root.display());
out
}
#[test]
fn every_numeric_primitive_has_exactly_one_definition() {
let sources = crate_sources();
let mut offenders = Vec::new();
for name in SINGLE_DEFINITION {
let mut sites = Vec::new();
for (path, text) in &sources {
for (i, line) in text.lines().enumerate() {
let t = line.trim_start();
for prefix in ["fn ", "pub fn ", "pub(crate) fn ", "pub(super) fn "] {
if let Some(rest) = t.strip_prefix(prefix)
&& (rest.starts_with(&format!("{name}("))
|| rest.starts_with(&format!("{name}<")))
{
sites.push(format!("{}:{}", path.display(), i + 1));
}
}
}
}
if sites.len() > 1 {
offenders.push(format!(
"`{name}` is defined {} times:\n {}",
sites.len(),
sites.join("\n ")
));
}
}
assert!(
offenders.is_empty(),
"a numeric primitive has more than one implementation in this crate. \
Two of these have already shipped wrong, each with a correct sibling \
one module away — a second implementation of a mathematical constant \
can only agree or be wrong, and nothing makes it agree:\n\n {}",
offenders.join("\n ")
);
}
#[test]
fn the_scan_can_see_a_definition() {
let sources = crate_sources();
let found = sources.iter().any(|(_, t)| {
t.lines().any(|l| {
let t = l.trim_start();
t.starts_with("fn ln_gamma(") || t.starts_with("pub(crate) fn ln_gamma(")
})
});
assert!(
found,
"the scanner found no `ln_gamma` definition at all — it has stopped \
matching the source layout rather than proving anything"
);
}