"""Tests for recompute_entry01.py against the released entry 01 files (offline, Python 3 and NumPy only).""" import contextlib import copy import io import os import unittest import recompute_entry01 as R ENTRY_DIR = R.DEFAULT_ENTRY_DIR class RecomputesTheReleasedTable(unittest.TestCase): @classmethod def setUpClass(cls): cls.files = R.load_entry(ENTRY_DIR) cls.table = R.recompute(cls.files) def test_comparators_match_the_published_values(self): self.assertAlmostEqual(self.table["N"], 334.1976, places=4) self.assertAlmostEqual(self.table["Df_bar"], 547.7121, places=4) self.assertAlmostEqual(self.table["T"], 36.7536, places=4) self.assertAlmostEqual(self.table["threshold"], 510.9585, places=4) def test_told_mean_and_paired_differences_match_the_published_values(self): self.assertAlmostEqual(self.table["told_mean"], 562.9894, places=4) self.assertAlmostEqual(self.table["paired"][2], 45.9015, places=4) self.assertAlmostEqual(self.table["paired"][3], -0.5978, places=4) self.assertAlmostEqual(self.table["paired"][4], 0.5283, places=4) def test_identity_arm_reproduces_the_saved_block_exactly(self): self.assertTrue(self.table["identity_check"]["reproduces"]) self.assertEqual(self.table["identity_check"]["max_abs_diff"], 0.0) def test_outcome_and_record_agree(self): self.assertEqual(self.table["outcome"], "MATCHES IN THIS SCREEN") self.assertEqual(R.disagreements(self.table, self.files["record"]), []) def test_learned_arm_cost_comes_from_the_released_records(self): for s in R.SEEDS: self.assertEqual(self.table["Df_cost"][s]["decisions"], 20000) self.assertEqual(self.table["Df_cost"][s]["gradient_updates"], 19000) self.assertEqual([round(self.table["Df_cost"][s]["adapt_seconds"]) for s in R.SEEDS], [5757, 6124, 5782]) def test_main_exits_zero_on_the_released_files(self): printed = io.StringIO() with contextlib.redirect_stdout(printed): self.assertEqual(R.main(["recompute_entry01.py", ENTRY_DIR]), 0) self.assertIn("AGREES with told2.json", printed.getvalue()) class RefusesInconsistentInputs(unittest.TestCase): def setUp(self): self.files = copy.deepcopy(R.load_entry(ENTRY_DIR)) def test_a_df_record_that_contradicts_the_gate_a_reading_is_refused(self): self.files["df"][3]["after"]["walker-run"]["episodes"][0] += 10.0 with self.assertRaises(ValueError): R.recompute(self.files) def test_a_tampered_told_episode_is_reported_as_a_disagreement(self): self.files["told"]["result"]["episodes"][0] += 1.0 found = R.disagreements(R.recompute(self.files), self.files["record"]) self.assertTrue(any(f.startswith("told mean") for f in found)) self.assertTrue(any(f.startswith("paired seed 2") for f in found)) def test_a_missing_input_file_raises(self): with self.assertRaises(FileNotFoundError): R.load_entry(os.path.join(ENTRY_DIR, "no-such-dir")) if __name__ == "__main__": unittest.main()