test: add property-based tests, a shared finiteness helper, and boundary inputs
Most of what remained on #26. **Property tests (`tests/properties.rs`, proptest as a dev-dependency).** Four invariants over generated 1v1 schedules rather than hand-written fixtures, which is where this crate's shipped defects actually hid — a linear evidence product that underflowed only past ~1000 teams, and a batching path no golden exercised because every golden ingests in one call: - converged posteriors are always finite with positive sigma - log-evidence, batch and filtered, is finite and never above zero - filtered evidence is invariant to whether `converge` has run - one-at-a-time ingestion reaches the same fixed point as batched The invariance property was mutation-proved: making `filtered_step` read `skill.forward` instead of the carried message fails it with `-1.1038430064192069 -> -1.1135747072822761`. **Shared finiteness helper (`tests/common/mod.rs`).** `assert_finite` was local to `degenerate_inputs.rs`. It now also rejects a non-positive sigma, which the old version let through — `Gaussian::sigma` reports a non-positive precision as improper rather than trapping, so a collapsed posterior would have passed a finite-only check. **Boundary inputs.** Zero and negative weights, out-of-order timestamps, and extreme beta/sigma combinations. Worth recording that zero weight reaches `(m - performance.exclude(..)) * (1.0 / w)` — a division by zero — and the posterior comes out finite anyway; the test pins that rather than asserting what ought to happen. The weight tests `expect()` the commit rather than returning early on error, because an early return would have made them vacuous the moment validation changed. I checked that specifically by turning the return into a failure and confirming it did not fire. Not done, and left on #26: benchmark regression gating. Nothing fails on a regression today; making it fail needs a threshold chosen against how noisy the shared runner is, which is a policy call rather than a mechanical one. 60 test binaries, up from 56. MSRV 1.85 verified with proptest in the graph. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T5SYDExxL4vZgvunrcNSMc
This commit is contained in:
+112
-9
@@ -3,6 +3,9 @@
|
||||
//! These run in both debug and release: the defects they pin were all
|
||||
//! guarded only by `debug_assert!`, so a debug-only suite never saw them.
|
||||
|
||||
mod common;
|
||||
|
||||
use common::assert_finite;
|
||||
use trueskill_tt::{
|
||||
ConstantDrift, ConvergenceOptions, Game, GameOptions, Gaussian, History, InferenceError,
|
||||
NullObserver, Outcome, Rating,
|
||||
@@ -18,15 +21,6 @@ fn rating() -> R {
|
||||
)
|
||||
}
|
||||
|
||||
fn assert_finite(g: Gaussian, what: &str) {
|
||||
assert!(
|
||||
g.mu().is_finite() && g.sigma().is_finite(),
|
||||
"{what} must be finite, got mu={} sigma={}",
|
||||
g.mu(),
|
||||
g.sigma()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_draw_without_draw_probability_is_rejected() {
|
||||
let mut h = History::default();
|
||||
@@ -318,3 +312,112 @@ fn empty_history_has_no_filtered_estimates() {
|
||||
|
||||
assert!(history.filtered_learning_curve("nobody").is_empty());
|
||||
}
|
||||
|
||||
// --- Boundary inputs (#26) ----------------------------------------------
|
||||
|
||||
fn tight() -> ConvergenceOptions {
|
||||
ConvergenceOptions {
|
||||
max_iter: 2_000,
|
||||
epsilon: 1e-12,
|
||||
..ConvergenceOptions::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_curve_finite(h: &History, keys: &[&str], what: &str) {
|
||||
for key in keys {
|
||||
for (time, g) in h.learning_curve(*key) {
|
||||
assert!(
|
||||
g.mu().is_finite() && g.sigma().is_finite(),
|
||||
"{what}: non-finite posterior for {key} at t={time} (mu={} sigma={})",
|
||||
g.mu(),
|
||||
g.sigma()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A zero weight reaches `(m - performance.exclude(..)) * (1.0 / w)`, i.e. a
|
||||
/// division by zero. The commit is accepted today, so this pins that the
|
||||
/// resulting posterior is still finite rather than quietly NaN.
|
||||
#[test]
|
||||
fn zero_weight_does_not_produce_a_non_finite_posterior() {
|
||||
let mut h = History::builder().build();
|
||||
|
||||
h.event(1)
|
||||
.team(["a"])
|
||||
.weights([0.0])
|
||||
.team(["b"])
|
||||
.winner(0)
|
||||
.commit()
|
||||
.expect("a zero weight is accepted today; update this test if that changes");
|
||||
|
||||
h.converge().unwrap();
|
||||
|
||||
assert_curve_finite(&h, &["a", "b"], "zero weight");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_weight_does_not_produce_a_non_finite_posterior() {
|
||||
let mut h = History::builder().build();
|
||||
|
||||
h.event(1)
|
||||
.team(["a"])
|
||||
.weights([-1.0])
|
||||
.team(["b"])
|
||||
.winner(0)
|
||||
.commit()
|
||||
.expect("a negative weight is accepted today; update this test if that changes");
|
||||
|
||||
h.converge().unwrap();
|
||||
|
||||
assert_curve_finite(&h, &["a", "b"], "negative weight");
|
||||
}
|
||||
|
||||
/// Events supplied newest-first must land in the same slices as oldest-first:
|
||||
/// ingestion sorts by time rather than trusting arrival order.
|
||||
#[test]
|
||||
fn out_of_order_timestamps_converge_to_the_same_answer() {
|
||||
fn build(descending: bool) -> History {
|
||||
let mut h = History::builder().convergence(tight()).build();
|
||||
|
||||
let mut times: Vec<i64> = (1..=6).collect();
|
||||
if descending {
|
||||
times.reverse();
|
||||
}
|
||||
|
||||
for time in times {
|
||||
h.record_winner(&"a", &"b", time).unwrap();
|
||||
}
|
||||
|
||||
h.converge().unwrap();
|
||||
h
|
||||
}
|
||||
|
||||
let ascending = build(false);
|
||||
let descending = build(true);
|
||||
|
||||
let one = ascending.current_skill("a").unwrap();
|
||||
let other = descending.current_skill("a").unwrap();
|
||||
|
||||
assert!(
|
||||
(one.mu() - other.mu()).abs() < 1e-8 && (one.sigma() - other.sigma()).abs() < 1e-8,
|
||||
"arrival order changed the answer: ascending mu={} sigma={}, descending mu={} sigma={}",
|
||||
one.mu(),
|
||||
one.sigma(),
|
||||
other.mu(),
|
||||
other.sigma()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extreme_beta_and_sigma_stay_finite() {
|
||||
for (beta, sigma) in [(1e-6, 1e-6), (1e6, 1e6), (1e-6, 1e6), (1e6, 1e-6)] {
|
||||
let mut h = History::builder().beta(beta).sigma(sigma).build();
|
||||
|
||||
h.record_winner(&"a", &"b", 1).unwrap();
|
||||
h.record_winner(&"a", &"b", 2).unwrap();
|
||||
h.converge().unwrap();
|
||||
|
||||
assert_curve_finite(&h, &["a", "b"], &format!("beta={beta} sigma={sigma}"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user