use crate::command_verdict_in;
use crate::handler_property_tests::{
ABSTRACTION_CONTEXTS, ABSTRACTION_ROOTS, ABSTRACTION_SUBS, abstraction_suffixes, abstraction_witnesses, workspace,
};
#[test]
fn substitution_is_never_more_permissive_than_a_path_it_could_produce() {
let suffixes = abstraction_suffixes();
let per_context: Vec<(usize, Vec<String>)> = std::thread::scope(|scope| {
let handles: Vec<_> = ABSTRACTION_CONTEXTS.iter().map(|ctx| scope.spawn(|| check_context(ctx, &suffixes))).collect();
handles.into_iter().map(|h| h.join().expect("a context's check panicked")).collect()
});
let constrained: usize = per_context.iter().map(|(n, _)| n).sum();
let violations: Vec<String> = per_context.into_iter().flat_map(|(_, v)| v).collect();
assert!(constrained > 0, "no witness was refused — the property is vacuous");
assert!(
violations.is_empty(),
"a substitution was more permissive than a path it could produce ({} cases):\n{}",
violations.len(),
violations.join("\n"),
);
}
fn check_context(ctx: &str, suffixes: &[String]) -> (usize, Vec<String>) {
let mut violations = Vec::new();
let mut constrained = 0usize;
for root in ABSTRACTION_ROOTS {
for suffix in suffixes {
let refused: Vec<String> = abstraction_witnesses(root)
.into_iter()
.map(|witness| ctx.replace("{}", &format!("{witness}{suffix}")))
.filter(|concrete| !command_verdict_in(concrete, workspace()).is_allowed())
.collect();
if refused.is_empty() {
continue;
}
constrained += refused.len();
let allowed: Vec<String> = ABSTRACTION_SUBS
.iter()
.map(|sub| ctx.replace("{}", &format!("{}{suffix}", sub.replace("{root}", root))))
.filter(|abstracted| command_verdict_in(abstracted, workspace()).is_allowed())
.collect();
for concrete in &refused {
for abstracted in &allowed {
violations.push(format!(" `{concrete}` denies but `{abstracted}` allows"));
}
}
}
}
(constrained, violations)
}