Skip to content

Commit fcc5e97

Browse files
authored
Add more context to polycool unit test failure messages (#595)
1 parent 11cb7f1 commit fcc5e97

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

polycool/src/arbitrary.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ mod tests {
164164
arbtest::arbtest(|u| {
165165
let r = float_in_unit_interval(u)?;
166166
let p: Poly<6> = poly_with_planted_root(u, r, 1e-6)?;
167-
assert!(p.eval(r).abs() <= 1e-12 * p.max_abs_coefficient().max(1.0));
167+
let threshold = 1e-12 * p.max_abs_coefficient().max(1.0);
168+
let y = p.eval(r);
169+
assert!(
170+
y.abs() <= threshold,
171+
"poly {p:?} had planted root {r} evaluate to {y:?}, but expected |y| <= {threshold:?}"
172+
);
168173
Ok(())
169174
});
170175
}

polycool/src/poly.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,28 @@ mod tests {
452452
}
453453
let roots = poly.roots_between(-2.0, 2.0, 1e-13);
454454

455-
assert!(roots.iter().all(|r| r.is_finite()));
455+
assert!(
456+
roots.iter().all(|r| r.is_finite()),
457+
"found non-finite roots: {roots:?}"
458+
);
456459

457460
// Check that the roots are sorted.
458-
assert!(roots.is_sorted());
459-
assert!(roots.iter().all(|r| (-2.0..=2.0).contains(r)));
461+
assert!(
462+
roots.is_sorted(),
463+
"roots are not sorted: {roots:?}"
464+
);
465+
assert!(
466+
roots.iter().all(|r| (-2.0..=2.0).contains(r)),
467+
"found roots out of range [-2.0, 2.0]: {roots:?}"
468+
);
460469

461470
// We can't expect great accuracy for huge coefficients, because the
462471
// evaluations during Newton iteration are subject to error.
463472
let error = poly.max_abs_coefficient().max(1.0) * 1e-12;
464-
assert!(roots.iter().any(|r| (r - planted_root).abs() <= error));
473+
assert!(
474+
roots.iter().any(|r| (r - planted_root).abs() <= error),
475+
"could not find something close to planted_root {planted_root} in found {roots:?} with allowed error {error} for poly {poly:?}"
476+
);
465477
Ok(())
466478
})
467479
.budget_ms(5_000);

polycool/src/quadratic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ mod tests {
125125
let r_magnitude = r.abs().max(1.0);
126126
let threshold = r_magnitude * 1e-14 * r_magnitude * magnitude;
127127
if y.is_finite() && threshold.is_finite() {
128-
assert!(y.abs() <= threshold);
128+
assert!(
129+
y.abs() <= threshold,
130+
"quadratic {q:?} had root {r} evaluate to {y:?}, but expected |y| <= {threshold:?}"
131+
);
129132
}
130133
}
131134
Ok(())

0 commit comments

Comments
 (0)