#29 — log_evidence and log_evidence_for took &mut self while mutating nothing. Loosening them to &self is not source-breaking for ordinary callers (a &mut reborrows as & transparently) and brings them in line with the filtered_* accessors added last week. Not the mechanical change it looked like: under the rayon feature the closure in log_evidence_internal captured all of &self rather than just the competitor store, which drags KeyTable<K> in and demands K: Sync from every caller. That compiled while the method took &mut self and stopped compiling the moment it did not. Binding `let agents = &self.agents;` before the closure narrows the capture; the comment there says why, because the next person to inline it will reintroduce the bound. #31 — TimeSlice::add_events constructed Skill with ..Default::default() while filtered_step spells every field out. The design relies on a new Skill field being a compile error at construction sites rather than a silent default, and that tripwire only fired at one of the two. Now both. #28 — log_evidence_internal's `forward` flag is a genuine forward-only quantity only on a history that has never been converged, because iteration alternates sweeps and the likelihood feeding the forward message absorbs backward information from the second iteration onward. Documented, with a pointer to filtered_log_evidence for the quantity that survives convergence. That trap is one function away from the one #19 was about. #23 — color_greedy carried #[allow(dead_code)] despite being called by recompute_color_groups: a mute button on a live function, which is the specific complaint in that issue. #27 was already fixed — the guard landed inf4e2922and the issue was filed against7742b2b, which merge-base confirms predates it — but nothing pinned it. Added the issue's own reproduction, which matters because the two profiles fail differently and a debug-only test would miss the release path. Removing both guards reproduces the issue verbatim: "attempt to subtract with overflow" in debug, "index out of bounds: the len is 0 but the index is 18446744073709551615" in release. Also amended the filtered-estimates spec (#30): the tolerance-not-bit-identity caveat is conservative. Forcing the scratch onto the sequential sweep instead of the grouped one — a far larger perturbation than a permuted event order — still agrees within 1e-8 under tight convergence. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T5SYDExxL4vZgvunrcNSMc
273 lines
7.9 KiB
Rust
273 lines
7.9 KiB
Rust
//! Degenerate, boundary, and error-path coverage.
|
|
//!
|
|
//! 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.
|
|
|
|
use trueskill_tt::{
|
|
ConstantDrift, ConvergenceOptions, Game, GameOptions, Gaussian, History, InferenceError,
|
|
NullObserver, Outcome, Rating,
|
|
};
|
|
|
|
type R = Rating<i64, ConstantDrift>;
|
|
|
|
fn rating() -> R {
|
|
R::new(
|
|
Gaussian::from_ms(25.0, 25.0 / 3.0),
|
|
25.0 / 6.0,
|
|
ConstantDrift(25.0 / 300.0),
|
|
)
|
|
}
|
|
|
|
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();
|
|
let err = h.record_draw(&"a", &"b", 1).unwrap_err();
|
|
assert!(matches!(
|
|
err,
|
|
InferenceError::TieWithoutDrawProbability { .. }
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn builder_draw_without_draw_probability_is_rejected() {
|
|
let mut h = History::default();
|
|
let err = h
|
|
.event(1)
|
|
.team(["a"])
|
|
.team(["b"])
|
|
.draw()
|
|
.commit()
|
|
.unwrap_err();
|
|
assert!(matches!(
|
|
err,
|
|
InferenceError::TieWithoutDrawProbability { .. }
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn draw_with_positive_draw_probability_is_finite() {
|
|
let mut h = History::builder().p_draw(0.25).build();
|
|
h.record_draw(&"a", &"b", 1).unwrap();
|
|
let report = h.converge().unwrap();
|
|
|
|
assert_finite(h.current_skill("a").unwrap(), "drawn competitor skill");
|
|
assert_finite(h.current_skill("b").unwrap(), "drawn competitor skill");
|
|
assert!(report.log_evidence.is_finite());
|
|
assert!(report.converged);
|
|
}
|
|
|
|
#[test]
|
|
fn game_ranked_rejects_tie_without_draw_probability() {
|
|
let a = [rating()];
|
|
let b = [rating()];
|
|
let teams: Vec<&[R]> = vec![&a, &b];
|
|
let err = Game::ranked(&teams, Outcome::draw(2), &GameOptions::default()).unwrap_err();
|
|
assert!(matches!(
|
|
err,
|
|
InferenceError::TieWithoutDrawProbability { .. }
|
|
));
|
|
}
|
|
|
|
/// `Outcome::winner(w, n)` ties every loser, so any n >= 3 free-for-all hits
|
|
/// the tie path even though the caller never asked for a draw.
|
|
#[test]
|
|
fn winner_of_three_or_more_requires_draw_probability() {
|
|
let a = [rating()];
|
|
let b = [rating()];
|
|
let c = [rating()];
|
|
let teams: Vec<&[R]> = vec![&a, &b, &c];
|
|
|
|
let err = Game::ranked(&teams, Outcome::winner(0, 3), &GameOptions::default()).unwrap_err();
|
|
assert!(matches!(
|
|
err,
|
|
InferenceError::TieWithoutDrawProbability { .. }
|
|
));
|
|
|
|
let opts = GameOptions {
|
|
p_draw: 0.1,
|
|
..GameOptions::default()
|
|
};
|
|
let game = Game::ranked(&teams, Outcome::winner(0, 3), &opts).unwrap();
|
|
for team in game.posteriors() {
|
|
for skill in team {
|
|
assert_finite(skill, "3-team winner posterior");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn full_ranking_without_ties_needs_no_draw_probability() {
|
|
let a = [rating()];
|
|
let b = [rating()];
|
|
let c = [rating()];
|
|
let teams: Vec<&[R]> = vec![&a, &b, &c];
|
|
let game = Game::ranked(&teams, Outcome::ranking([0, 1, 2]), &GameOptions::default()).unwrap();
|
|
|
|
for team in game.posteriors() {
|
|
for skill in team {
|
|
assert_finite(skill, "strict ranking posterior");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn empty_history_converges_trivially() {
|
|
let mut h = History::default();
|
|
let report = h.converge().unwrap();
|
|
assert_eq!(report.iterations, 0);
|
|
assert!(report.converged);
|
|
}
|
|
|
|
/// Issue #27's exact reproduction: a non-default key type reaching `converge`
|
|
/// with no events at all. The underflow it reported trapped in debug and
|
|
/// indexed out of bounds in release, so this must run in both profiles.
|
|
#[test]
|
|
fn converge_on_an_empty_history_with_owned_keys() {
|
|
let mut history: History<i64, ConstantDrift, NullObserver, String> =
|
|
History::builder_with_key().score_sigma(5.0).build();
|
|
|
|
let report = history.converge().unwrap();
|
|
|
|
assert_eq!(report.iterations, 0);
|
|
assert!(report.converged);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_event_stream_then_converge() {
|
|
let mut h = History::default();
|
|
h.add_events(std::iter::empty()).unwrap();
|
|
let report = h.converge().unwrap();
|
|
assert_eq!(report.iterations, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_history_queries_do_not_panic() {
|
|
let h = History::default();
|
|
assert!(h.learning_curves().is_empty());
|
|
assert!(h.learning_curve("nobody").is_empty());
|
|
assert!(h.current_skill("nobody").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn single_event_history_converges() {
|
|
let mut h = History::default();
|
|
h.record_winner(&"a", &"b", 1).unwrap();
|
|
let report = h.converge().unwrap();
|
|
assert!(report.converged);
|
|
assert_finite(h.current_skill("a").unwrap(), "single-event skill");
|
|
}
|
|
|
|
#[test]
|
|
fn scored_event_rejects_non_positive_sigma() {
|
|
let mut h = History::builder().score_sigma(2.0).build();
|
|
let err = h
|
|
.event(1)
|
|
.team(["a"])
|
|
.team(["b"])
|
|
.scores_with_sigma([3.0, 1.0], f64::NAN)
|
|
.commit()
|
|
.unwrap_err();
|
|
assert!(matches!(
|
|
err,
|
|
InferenceError::InvalidParameter {
|
|
name: "score_sigma",
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn convergence_reports_are_finite_across_many_teams() {
|
|
let opts = GameOptions {
|
|
p_draw: 0.1,
|
|
convergence: ConvergenceOptions::default(),
|
|
..GameOptions::default()
|
|
};
|
|
let holders: Vec<[R; 1]> = (0..12).map(|_| [rating()]).collect();
|
|
let teams: Vec<&[R]> = holders.iter().map(|t| t.as_slice()).collect();
|
|
let game = Game::ranked(&teams, Outcome::ranking(0..12), &opts).unwrap();
|
|
|
|
assert!(
|
|
game.log_evidence().is_finite(),
|
|
"12-team log-evidence must be finite, got {}",
|
|
game.log_evidence()
|
|
);
|
|
for team in game.posteriors() {
|
|
for skill in team {
|
|
assert_finite(skill, "12-team posterior");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A long diff chain underflows a linear evidence product: each link
|
|
/// contributes a probability in (0, 1], so ~1000 links flush the product to
|
|
/// exactly 0.0 and `ln(0.0)` is `-inf`. Accumulating in log space keeps it
|
|
/// finite.
|
|
#[test]
|
|
fn log_evidence_survives_a_long_diff_chain() {
|
|
let holders: Vec<[R; 1]> = (0..1200).map(|_| [rating()]).collect();
|
|
let teams: Vec<&[R]> = holders.iter().map(|t| t.as_slice()).collect();
|
|
let game = Game::ranked(
|
|
&teams,
|
|
Outcome::ranking(0..holders.len() as u32),
|
|
&GameOptions::default(),
|
|
)
|
|
.unwrap();
|
|
|
|
let log_evidence = game.log_evidence();
|
|
assert!(
|
|
log_evidence.is_finite(),
|
|
"1200-team log-evidence must be finite, got {log_evidence}"
|
|
);
|
|
assert!(
|
|
log_evidence < 0.0,
|
|
"log-evidence of a probability must be negative, got {log_evidence}"
|
|
);
|
|
}
|
|
|
|
/// A near-certain outcome rounds the losing tail to exactly zero in the
|
|
/// `erfc` approximation; the evidence floor keeps `ln` finite.
|
|
#[test]
|
|
fn log_evidence_finite_for_near_certain_outcome() {
|
|
let overwhelming = R::new(Gaussian::from_ms(5_000.0, 0.5), 1.0, ConstantDrift(0.0));
|
|
let hopeless = R::new(Gaussian::from_ms(-5_000.0, 0.5), 1.0, ConstantDrift(0.0));
|
|
let a = [overwhelming];
|
|
let b = [hopeless];
|
|
let teams: Vec<&[R]> = vec![&a, &b];
|
|
|
|
let game = Game::ranked(&teams, Outcome::winner(0, 2), &GameOptions::default()).unwrap();
|
|
assert!(
|
|
game.log_evidence().is_finite(),
|
|
"got {}",
|
|
game.log_evidence()
|
|
);
|
|
|
|
// And the reverse — a colossal upset — must also stay finite.
|
|
let upset = Game::ranked(&teams, Outcome::winner(1, 2), &GameOptions::default()).unwrap();
|
|
assert!(
|
|
upset.log_evidence().is_finite(),
|
|
"upset log-evidence must be finite, got {}",
|
|
upset.log_evidence()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_history_has_no_filtered_estimates() {
|
|
let history: History = History::builder().build();
|
|
|
|
assert_eq!(history.filtered_log_evidence(), 0.0);
|
|
|
|
assert!(history.filtered_learning_curves().is_empty());
|
|
|
|
assert!(history.filtered_learning_curve("nobody").is_empty());
|
|
}
|