render-parity: a NaN tolerance fails its check instead of switching it off

This commit is contained in:
FredApps
2026-11-16 13:45:33 +01:00
parent 99e0f2be89
commit 7ed3163f35
2 changed files with 21 additions and 4 deletions
+10 -4
View File
@@ -112,7 +112,7 @@ pub struct Report {
}
impl Report {
/// Whether every measure is within `tolerance`.
/// Whether every measure is within `tolerance`. A NaN limit is never met.
pub fn within(&self, tolerance: &Tolerance) -> bool {
self.exceeded(tolerance).is_empty()
}
@@ -120,13 +120,13 @@ impl Report {
/// The measures over `tolerance`, each with its value and limit, for a test failure message.
pub fn exceeded(&self, tolerance: &Tolerance) -> Vec<String> {
let mut over = Vec::new();
if self.max_delta_e > tolerance.max_delta_e {
if exceeds(self.max_delta_e, tolerance.max_delta_e) {
over.push(format!("max ΔE2000 {:.4} > {}", self.max_delta_e, tolerance.max_delta_e));
}
if self.mean_delta_e > tolerance.mean_delta_e {
if exceeds(self.mean_delta_e, tolerance.mean_delta_e) {
over.push(format!("mean ΔE2000 {:.4} > {}", self.mean_delta_e, tolerance.mean_delta_e));
}
if self.max_channel > tolerance.max_channel {
if exceeds(f64::from(self.max_channel), f64::from(tolerance.max_channel)) {
over.push(format!("max channel difference {:.6} > {}", self.max_channel, tolerance.max_channel));
}
over
@@ -179,6 +179,12 @@ pub fn compare(expected: &Image<'_>, actual: &Image<'_>) -> Result<Report, Parit
Ok(report)
}
/// Whether `value` exceeds `limit`. A NaN limit (or value) exceeds it: an unordered comparison never
/// passes, so an invalid tolerance cannot switch a check off.
fn exceeds(value: f64, limit: f64) -> bool {
!matches!(value.partial_cmp(&limit), Some(std::cmp::Ordering::Less | std::cmp::Ordering::Equal))
}
fn finite(px: [f32; 3]) -> Option<[f32; 3]> {
px.iter().all(|v| v.is_finite()).then_some(px)
}
+11
View File
@@ -87,3 +87,14 @@ fn bad_inputs_are_errors_not_panics() {
let e: Box<dyn std::error::Error> = Box::new(e);
assert!(e.source().is_none());
}
#[test]
fn a_nan_tolerance_fails_every_check() {
let rgb = gray_ramp(2, 2);
let image = Image::new(2, 2, &rgb).unwrap();
let report = compare(&image, &image).unwrap();
let nan = Tolerance { max_delta_e: f64::NAN, mean_delta_e: f64::NAN, max_channel: f32::NAN };
assert_eq!(report.exceeded(&nan).len(), 3);
assert!(!report.within(&nan));
assert!(report.within(&Tolerance { max_delta_e: f64::INFINITY, ..Tolerance::BASELINE }));
}