use std::fmt;
pub const ALPHA: f64 = 0.01;
#[derive(Debug, Clone)]
pub struct TestResult {
pub name: &'static str,
pub p_value: f64,
pub note: Option<String>,
}
impl TestResult {
pub fn new(name: &'static str, p_value: f64) -> Self {
Self {
name,
p_value,
note: None,
}
}
pub fn with_note(name: &'static str, p_value: f64, note: impl Into<String>) -> Self {
Self {
name,
p_value,
note: Some(note.into()),
}
}
pub fn insufficient(name: &'static str, reason: &str) -> Self {
Self {
name,
p_value: f64::NAN,
note: Some(reason.to_owned()),
}
}
pub fn passed(&self) -> bool {
self.p_value >= ALPHA
}
pub fn skipped(&self) -> bool {
self.p_value.is_nan()
}
}
impl fmt::Display for TestResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let status = if self.skipped() {
"SKIP"
} else if self.passed() {
"PASS"
} else {
"FAIL"
};
if self.skipped() {
write!(f, "[{status}] {:<48} p = N/A", self.name)?;
} else {
write!(f, "[{status}] {:<48} p = {:.6}", self.name, self.p_value)?;
}
if let Some(n) = &self.note {
write!(f, " ({n})")?;
}
Ok(())
}
}