#![allow(dead_code)]
use std::process::ExitCode;
#[derive(Default)]
pub struct Checks {
failed: usize,
total: usize,
}
impl Checks {
pub fn compare(&mut self, what: &str, got: f64, printed: f64, tolerance: f64) {
let ok = (got - printed).abs() <= tolerance;
self.row(
ok,
format!(" {what:<34} {got:>12.6} paper {printed:<10} ± {tolerance:e}"),
);
}
pub fn count(&mut self, what: &str, got: usize, printed: usize) {
self.row(
got == printed,
format!(" {what:<34} {got:>12} paper {printed}"),
);
}
fn row(&mut self, ok: bool, text: String) {
self.total += 1;
if !ok {
self.failed += 1;
}
println!("{text:<80} {}", if ok { "ok" } else { "FAILED" });
}
pub fn finish(self) -> ExitCode {
if self.failed == 0 {
println!("all {} within tolerance of the paper", self.total);
ExitCode::SUCCESS
} else {
println!("{} of {} outside tolerance", self.failed, self.total);
ExitCode::FAILURE
}
}
}