# SPDX-License-Identifier: CC0-1.0 from collections import defaultdict from fractions import Fraction as F from itertools import product import unittest import warmup as m class WarmupTests(unittest.TestCase): def test_report(self): self.assertEqual([r["nextHitFraction"] for r in m.report()["rows"]], ["5/9", "4/9", "1/2", "5/9"]) self.assertEqual([r["expectedHitFraction"] for r in m.report()["windows"]], ["4/9", "1/2", "49/90", "499/900"]) def test_reject_invalid_probabilities(self): for p in [True, False, 0, 1, -1, F(3, 2), 0.5, "2/3", None]: with self.subTest(p=p), self.assertRaises(ValueError): m.stationary_hit(p) def test_reject_invalid_counts(self): for n in [True, 0, -1, 2.5, "2", None]: with self.subTest(n=n), self.assertRaises(ValueError): m.stopped_window_hit(F(2, 3), n) with self.subTest(n=n), self.assertRaises(ValueError): m.uncollected_mass(F(2, 3), n) with self.assertRaises(ValueError): m.collection_atom(F(2, 3), 1) def test_independent_enumeration(self): # Full histories, not the closed-form geometric terms in the model. for p in [F(1, 5), F(1, 2), F(2, 3), F(9, 10)]: for horizon in range(2, 9): stopped = defaultdict(F) tail = F(0) fixed_a = F(0) for path in product("AB", repeat=horizon): weight = F(1) seen = set() first = None for i, label in enumerate(path, 1): weight *= p if label == "A" else 1 - p seen.add(label) if len(seen) == 2 and first is None: first = i fixed_a += weight * (path[-1] == "A") if first is None: tail += weight else: stopped[first, path[first - 1]] += weight self.assertEqual(fixed_a, p) self.assertEqual(tail, m.uncollected_mass(p, horizon)) for t in range(2, horizon + 1): atom = m.collection_atom(p, t) self.assertEqual(stopped[t, "A"], atom["lastA"]) self.assertEqual(stopped[t, "B"], atom["lastB"]) def test_exact_tail_completion(self): for numerator in range(1, 10): p = F(numerator, 10) q = 1 - p for horizon in range(2, 31): a = sum((m.collection_atom(p, t)["lastA"] for t in range(2, horizon + 1)), F(0)) b = sum((m.collection_atom(p, t)["lastB"] for t in range(2, horizon + 1)), F(0)) # Remaining all-B/all-A histories eventually switch with probability one. self.assertEqual(a + q ** horizon, q) self.assertEqual(b + p ** horizon, p) self.assertEqual(a + b + m.uncollected_mass(p, horizon), 1) def test_independent_post_stop_transition_rewards(self): for numerator in range(1, 10): p = F(numerator, 10) probs = {"A": p, "B": 1 - p} distribution = {"A": 1 - p, "B": p} reward = F(0) for count in range(1, 101): next_distribution = defaultdict(F) for resident, mass in distribution.items(): for request, chance in probs.items(): reward += mass * chance * (resident == request) next_distribution[request] += mass * chance distribution = next_distribution self.assertEqual(reward / count, m.stopped_window_hit(p, count)) self.assertEqual(distribution, probs) def test_gap_and_label_symmetry(self): for numerator in range(1, 100): p = F(numerator, 100) self.assertEqual(m.stationary_hit(p) - m.stopped_window_hit(p, 1), (2 * p - 1) ** 2) self.assertEqual(m.stopped_window_hit(p, 7), m.stopped_window_hit(1 - p, 7)) self.assertEqual(m.stopped_window_hit(F(1, 2), 1), F(1, 2)) def test_conditional_stationarity_fails(self): atom = m.collection_atom(F(2, 3), 2) self.assertEqual(atom["lastA"] / atom["mass"], F(1, 2)) self.assertNotEqual(atom["lastA"] / atom["mass"], F(2, 3)) if __name__ == "__main__": unittest.main()