pub const DOMAIN_FUNCTIONS: &[(&str, &[&str])] = &[
("arithmetic", crate::domains::ARITHMETIC_FUNCTIONS),
("scientific", crate::domains::SCIENTIFIC_FUNCTIONS),
("statistics", crate::domains::STATISTICS_FUNCTIONS),
("precision", &["precision"]),
("number_theory", crate::domains::NUMBER_THEORY_FUNCTIONS),
("combinatorics", crate::domains::COMBINATORICS_FUNCTIONS),
("polynomial", crate::domains::POLYNOMIAL_FUNCTIONS),
("complex", crate::domains::COMPLEX_FUNCTIONS),
("matrix", crate::domains::MATRIX_FUNCTIONS),
("vector", crate::domains::VECTOR_FUNCTIONS),
("symbolic", crate::domains::SYMBOLIC_FUNCTIONS),
#[cfg(feature = "time")]
("time", crate::domains::TIME_FUNCTIONS),
#[cfg(feature = "unit")]
("unit", crate::domains::UNIT_FUNCTIONS),
#[cfg(feature = "fx")]
("fx", crate::domains::FX_FUNCTIONS),
#[cfg(feature = "numerical")]
("numerical", crate::domains::NUMERICAL_FUNCTIONS),
];
pub fn all_function_names() -> Vec<&'static str> {
let mut names: Vec<&'static str> = Vec::new();
for (_, funcs) in DOMAIN_FUNCTIONS {
for f in *funcs {
if !names.contains(f) {
names.push(f);
}
}
}
names
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_catalog_covers_core_domains() {
let domains: Vec<&str> = DOMAIN_FUNCTIONS.iter().map(|(d, _)| *d).collect();
for required in [
"arithmetic",
"scientific",
"statistics",
"precision",
"number_theory",
"combinatorics",
"polynomial",
"complex",
"matrix",
"vector",
"symbolic",
] {
assert!(domains.contains(&required), "目录应含已注册域 {}", required);
}
}
#[test]
fn test_catalog_matches_routing_whitelists() {
for (domain, funcs) in DOMAIN_FUNCTIONS {
let expected: &[&str] = match *domain {
"arithmetic" => crate::domains::ARITHMETIC_FUNCTIONS,
"scientific" => crate::domains::SCIENTIFIC_FUNCTIONS,
"statistics" => crate::domains::STATISTICS_FUNCTIONS,
"number_theory" => crate::domains::NUMBER_THEORY_FUNCTIONS,
"combinatorics" => crate::domains::COMBINATORICS_FUNCTIONS,
"polynomial" => crate::domains::POLYNOMIAL_FUNCTIONS,
"complex" => crate::domains::COMPLEX_FUNCTIONS,
"matrix" => crate::domains::MATRIX_FUNCTIONS,
"vector" => crate::domains::VECTOR_FUNCTIONS,
"symbolic" => crate::domains::SYMBOLIC_FUNCTIONS,
#[cfg(feature = "time")]
"time" => crate::domains::TIME_FUNCTIONS,
#[cfg(feature = "unit")]
"unit" => crate::domains::UNIT_FUNCTIONS,
#[cfg(feature = "fx")]
"fx" => crate::domains::FX_FUNCTIONS,
#[cfg(feature = "numerical")]
"numerical" => crate::domains::NUMERICAL_FUNCTIONS,
"precision" => continue,
other => panic!("目录出现未知域 {},请同步 factory 注册与白名单", other),
};
assert_eq!(*funcs, expected, "{} 目录与路由白名单漂移", domain);
}
}
#[test]
fn test_all_function_names_deduped_and_nonempty() {
let names = all_function_names();
assert!(!names.is_empty());
let unique: std::collections::HashSet<_> = names.iter().collect();
assert_eq!(unique.len(), names.len(), "函数名应去重");
}
#[test]
fn test_catalog_includes_numeric_frontier_functions() {
let names = all_function_names();
for required in ["erfc", "lgamma", "polylog"] {
assert!(
names.contains(&required),
"函数目录缺少 numeric-frontier 新增函数 {}",
required
);
}
let scientific: Vec<&str> = DOMAIN_FUNCTIONS
.iter()
.find(|(d, _)| *d == "scientific")
.map(|(_, f)| f.to_vec())
.unwrap_or_default();
for required in ["erfc", "lgamma", "polylog"] {
assert!(
scientific.contains(&required),
"scientific 域白名单缺少 {}",
required
);
}
}
#[test]
fn test_scientific_catalog_names_are_dispatchable() {
let funcs = crate::domains::SCIENTIFIC_FUNCTIONS;
for name in funcs {
let mut last: Option<String> = None;
for probe in [format!("{name}(1)"), format!("{name}(1, 2)")] {
let cache = crate::core::CacheManager::new();
match crate::core::evaluate(&probe, &crate::core::EvalContext::new(), None, &cache)
{
Ok(_) => {
last = None;
break;
}
Err(e) => {
let msg = e.to_string();
assert!(
!msg.contains("unknown function"),
"目录函数 {} 未被路由分发(探测 {probe} 报 {msg})",
name
);
last = Some(msg);
}
}
}
let _ = last;
}
}
#[cfg(feature = "fx")]
#[test]
fn test_fx_domain_in_catalog_when_feature_on() {
let domains: Vec<&str> = DOMAIN_FUNCTIONS.iter().map(|(d, _)| *d).collect();
assert!(domains.contains(&"fx"));
assert!(all_function_names().contains(&"fx"));
}
}