use super::{EIKONAL_TOLERANCE, FieldBoundReport, field_bound_report};
use crate::fields::FieldBound;
const N: u32 = 16;
#[test]
fn every_reference_field_meets_its_declared_bound() {
let mut rows = alloc::vec::Vec::new();
crate::for_each_reference_field!(f64, |name, field| {
let report = field_bound_report(&field, N);
assert!(report.samples > 1000, "{name}: {} samples", report.samples);
assert!(
!report.violates(0.05),
"{name} declares {:?} but ‖∇f‖ reaches {:.4}",
report.declared,
report.sup
);
rows.push((name, report));
});
assert_eq!(rows.len(), 8, "the reference field set changed");
for (name, r) in &rows {
std::println!(
"measured: {name:<16} {:<28} sup {:>7.3} inf {:>6.3} eikonal {:>5.1}%",
alloc::format!("{:?}", r.declared),
r.sup,
r.inf,
100.0 * r.eikonal_fraction
);
}
for (name, r) in &rows {
if r.declared == FieldBound::Exact {
assert!(
r.eikonal_fraction > 0.99,
"{name} declares Exact but is eikonal on only {:.1}% of samples",
100.0 * r.eikonal_fraction
);
}
}
let csg = rows
.iter()
.find(|(name, _)| *name == "csg_difference")
.map(|(_, r)| *r)
.expect("csg_difference is a reference field");
assert!(
matches!(csg.declared, FieldBound::Underestimate { .. }),
"csg_difference no longer declares an underestimate"
);
assert!(
csg.eikonal_fraction > 0.99,
"csg_difference is no longer eikonal almost everywhere, so the reason \
`q` is separate from `l` needs restating: {:.3}",
csg.eikonal_fraction
);
}
#[test]
fn tightening_a_declaration_by_one_step_is_caught() {
let mut tightened = 0usize;
let mut already_tightest = 0usize;
crate::for_each_reference_field!(f64, |name, field| {
let real = field_bound_report(&field, N);
match real.declared {
FieldBound::Exact => {
already_tightest += 1;
}
FieldBound::Underestimate { .. } => {
already_tightest += 1;
}
FieldBound::Lipschitz { .. } | FieldBound::Unbounded => {
let claimed = FieldBoundReport {
declared: FieldBound::Exact,
..real
};
assert!(
claimed.violates(0.05),
"{name}: claiming Exact was not caught, though ‖∇f‖ reaches {:.4}",
real.sup
);
tightened += 1;
}
}
});
assert!(
tightened >= 3,
"only {tightened} fields could be tightened, too few to mean anything"
);
assert!(
already_tightest >= 4,
"{already_tightest} fields were already tightest"
);
std::println!(
"measured: {tightened} declarations tightened and caught, \
{already_tightest} already at their tightest"
);
}
#[test]
fn a_loosened_declaration_is_never_a_violation() {
crate::for_each_reference_field!(f64, |name, field| {
let real = field_bound_report(&field, N);
let loosened = FieldBoundReport {
declared: FieldBound::Lipschitz {
l: real.sup * 10.0 + 1.0,
},
..real
};
assert!(
!loosened.violates(0.0),
"{name}: a loose bound was rejected"
);
let unbounded = FieldBoundReport {
declared: FieldBound::Unbounded,
..real
};
assert!(
!unbounded.violates(0.0),
"{name}: Unbounded claims nothing and cannot be violated"
);
});
}
#[test]
fn the_eikonal_tolerance_is_loose_enough_to_survive_a_crease() {
const _: () = assert!(
EIKONAL_TOLERANCE >= 0.01,
"a band this tight measures creases rather than the field"
);
let report = field_bound_report(&crate::fields::BoxExact::<f64>::canonical(), N);
assert!(
report.eikonal_fraction > 0.95,
"an exact box should be eikonal almost everywhere, got {:.3}",
report.eikonal_fraction
);
}
#[test]
fn a_directional_bound_helps_only_where_the_global_one_is_loose() {
use crate::Sdf;
use crate::fields::ReferenceField;
fn steps<F: Sdf<Scalar = f64>>(field: &F, origin: [f64; 3], lambda: f64, far: f64) -> u32 {
let mut t = 0.0;
for step in 0..4096u32 {
let p = [origin[0] + t, origin[1], origin[2]];
let d = field.sample(p).abs();
if d < 1e-4 {
return step;
}
t += (d / lambda).max(1e-6);
if t > far {
return step;
}
}
4096
}
crate::for_each_reference_field!(f64, |name, field| {
if let Some(global) = field.bound().lipschitz() {
let directional = if name == "gyroid" { 2.0 } else { global };
let (lo, hi) = field.domain();
let far = hi[0] - lo[0];
let mut total_global = 0u64;
let mut total_directional = 0u64;
for a in 0..6 {
for b in 0..6 {
let at = |v: i32, l: f64, h: f64| l + (h - l) * (f64::from(v) + 0.5) / 6.0;
let origin = [lo[0], at(a, lo[1], hi[1]), at(b, lo[2], hi[2])];
total_global += u64::from(steps(&field, origin, global, far));
total_directional += u64::from(steps(&field, origin, directional, far));
}
}
let gain = total_global as f64 / total_directional as f64;
std::println!(
"measured: {name:<16} global λ {global:>6.3} directional {directional:>6.3} \
steps {total_global:>6} → {total_directional:>6} ({gain:.2}×)"
);
if (directional - global).abs() < 1e-12 {
assert_eq!(
total_global, total_directional,
"{name}: identical bounds must produce identical marches"
);
} else {
assert!(
gain > 1.2,
"{name}: a √3-tighter bound bought only {gain:.2}×, so the tightness \
is not where the cost is"
);
}
}
});
}