import itertools import unittest from fractions import Fraction from model import summarize, result class ObservationTests(unittest.TestCase): def test_published_counts(self): self.assertEqual([(r["high"], r["total"], r["fraction"]) for r in result()["rows"]], [(5, 10, "1/2"), (5, 6, "5/6"), (9, 10, "9/10")]) def test_independent_expansion_oracle(self): # Enumerate individual events instead of weighting aggregate slot counts. for states in itertools.product([False, True], repeat=4): for counts in itertools.product(range(3), repeat=4): streams = [list(states), [states[i] for i in range(4) if counts[i] != 0], [states[i] for i in range(4) for _ in range(counts[i])]] for row, stream in zip(summarize(states, counts), streams): self.assertEqual(row["total"], len(stream)) self.assertEqual(row["high"], stream.count(True)) self.assertEqual(row["fraction"], str(Fraction(stream.count(True), len(stream))) if stream else None) def test_rotation_and_repetition(self): states, counts = result()["states_high"], result()["arrivals_per_slot"] fractions = [r["fraction"] for r in summarize(states, counts)] for offset in range(10): rotated = summarize(states[offset:] + states[:offset], counts[offset:] + counts[:offset]) self.assertEqual([r["fraction"] for r in rotated], fractions) repeated = summarize(states * 7, counts * 7) self.assertEqual([r["fraction"] for r in repeated], fractions) def test_timestamp_deduplication_changes_target(self): states, counts = result()["states_high"], result()["arrivals_per_slot"] unique = summarize(states, [int(n > 0) for n in counts]) self.assertEqual(unique[2]["fraction"], "5/6") self.assertNotEqual(unique[2]["fraction"], result()["rows"][2]["fraction"]) def test_empty_event_population_has_no_fraction(self): rows = summarize([False, True], [0, 0]) self.assertEqual(rows[0]["fraction"], "1/2") self.assertIsNone(rows[1]["fraction"]) self.assertIsNone(rows[2]["fraction"]) def test_invalid_inputs(self): for states, counts in [([], []), ([True], []), ([1], [1]), ([True], [-1]), ([True], [True]), ([True], [1.5]), ([True], [float("nan")])]: with self.subTest(states=states, counts=counts): with self.assertRaises(ValueError): summarize(states, counts) if __name__ == "__main__": unittest.main()