use sheng::price::{Calibration, MACOS_AARCH64, MINTED, UNMEASURED};
use sheng::prior::{DEFAULT_CHAINS, SOURCE_BYTES};
use sheng::shuffle::Kernel;
use sheng::{BuildError, Gate, Policy, Sieve};
const ARMING: &[&str] = &[
r"(?-u)WalletService",
r"(?-u)foo[^\n]*bar",
r"(?-u)<[^>]*>",
r"(?-u)\{[^\n]*\}",
];
fn with(calibration: Calibration) -> Policy<'static> {
Policy {
calibration,
..Policy::default()
}
}
#[test]
fn an_unmeasured_machine_declines_instead_of_guessing() {
let policy = with(UNMEASURED);
for pattern in ARMING {
match Sieve::with(pattern, &policy) {
Err(BuildError::Uncalibrated { arch, kernel }) => {
assert_eq!(arch, std::env::consts::ARCH);
assert_eq!(kernel, sheng::shuffle::kernel());
},
other => panic!("{pattern:?} must decline on an unmeasured machine, got {other:?}"),
}
}
}
#[test]
fn waiving_the_gate_still_builds_on_an_unmeasured_machine() {
let policy = Policy {
gate: Gate::Ungated,
..with(UNMEASURED)
};
for pattern in ARMING {
let sieve = Sieve::with(pattern, &policy).expect("the quotient does not need a price");
assert!(sieve.conjuncts() > 0);
assert!(
sieve.cost().sieve.is_infinite(),
"an unmeasured sieve must not report a finite price"
);
}
}
#[test]
fn a_caller_can_price_a_machine_the_crate_never_measured() {
let flat = Calibration {
arch: "hypothetical",
dfa_skip: 1.274907,
..MACOS_AARCH64
};
let (mut moved, mut same) = (0, 0);
for pattern in [
r"(?-u)(alpha|beta|gamma)",
r"(?-u)#[0-9a-fA-F]{6}",
r"(?-u)e[^\n]*q",
] {
let shipped = Sieve::with(pattern, &with(MACOS_AARCH64));
let theirs = Sieve::with(pattern, &with(flat));
if shipped.is_err() && theirs.is_ok() {
moved += 1;
}
if shipped.is_ok() == theirs.is_ok() {
same += 1;
}
}
assert!(
moved > 0,
"no pattern changed verdict between two machines — the calibration is inert"
);
assert!(
moved + same == 3,
"a slower memchr must never *un*-arm a pattern"
);
}
#[test]
fn the_prior_reaches_the_decision() {
let source = [sheng::prior::Prior::Source.chain()];
let uniform = [sheng::prior::Prior::Uniform.chain()];
let modeled = |pattern: &str, chains: &[sheng::prior::Chain]| {
Sieve::with(
pattern,
&Policy {
chains,
gate: Gate::Ungated,
..Policy::default()
},
)
.map(|s| s.fallthrough())
};
let mut disagreed = 0;
for pattern in ARMING {
let (Ok(one), Ok(other), Ok(swept)) = (
modeled(pattern, &source),
modeled(pattern, &uniform),
modeled(pattern, &DEFAULT_CHAINS),
) else {
continue;
};
assert!(
swept >= one.max(other) - 1e-12,
"{pattern:?}: sweeping chains must dominate each alone \
({swept:.3e} vs {one:.3e} / {other:.3e})"
);
if (one - other).abs() > 1e-9 {
disagreed += 1;
}
}
assert!(
disagreed > 0,
"no pattern priced differently under source text than under uniform noise — \
the chains in a Policy are not reaching the model"
);
}
#[test]
fn longer_documents_are_harder_to_justify() {
if !sheng::price::active().is_measured() {
return; }
let mut judged = 0;
for pattern in ARMING {
let Ok(long) = Sieve::with(pattern, &Policy::default()) else {
continue;
};
let short = Sieve::with(
pattern,
&Policy {
len: 8.0,
gate: Gate::Ungated,
..Policy::default()
},
)
.expect("ungated always builds");
let (a, b) = (long.cost().speedup(), short.cost().speedup());
judged += 1;
assert!(
b >= a,
"{pattern:?}: a 4 KiB document cannot be a better deal than 8 bytes \
({a:.3}x vs {b:.3}x) — survival only grows with length"
);
}
assert!(
judged > 0,
"no pattern armed at the nominal length, so nothing above was actually \
compared — this test has gone vacuous again"
);
}
#[test]
fn every_shipped_row_names_a_real_machine() {
for cal in MINTED {
assert!(!cal.arch.is_empty() && cal.arch != "unmeasured");
assert!(
cal.host.len() > 8,
"{}: host is not a description",
cal.arch
);
assert_eq!(cal.minted.len(), 10, "{}: minted is not a date", cal.arch);
assert_ne!(cal.kernel, Kernel::Scalar);
}
let total: f64 = SOURCE_BYTES.iter().sum();
assert!((total - 1.0).abs() < 1e-6, "byte marginals must sum to 1");
assert!(
SOURCE_BYTES[b' ' as usize] > SOURCE_BYTES[b'~' as usize],
"source text has more spaces than tildes, or this is not source text"
);
}