File size: 5,403 Bytes
7ffaec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Tests for recompute_entry02.py against the released entry 02 files (offline, Python 3 and NumPy only)."""
import contextlib
import copy
import io
import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))   # runnable from the package root too

import recompute_entry02 as R

ENTRY_DIR = R.DEFAULT_ENTRY_DIR


class RecomputesTheReleasedEntry(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.files = R.load_entry(ENTRY_DIR)
        cls.table = R.recompute(cls.files)
        cls.ind = cls.table["independent"]

    def test_upright_distances_and_interval_match_the_published_values(self):
        self.assertAlmostEqual(self.ind["mean_H"], 34.507331, places=6)
        self.assertAlmostEqual(self.ind["mean_Z"], 46.001543, places=6)
        self.assertAlmostEqual(self.ind["half_H"], 17.253666, places=6)
        self.assertAlmostEqual(self.ind["consequence_ci_m"][0], -20.323184, places=6)
        self.assertAlmostEqual(self.ind["consequence_ci_m"][1], -1.230142, places=6)

    def test_both_consequence_conditions_fail_so_the_class_is_not_consequential(self):
        self.assertFalse(self.ind["Z_condition"])
        self.assertFalse(self.ind["interval_condition"])
        self.assertEqual(self.ind["outcome"], "NOT CONSEQUENTIAL")
        self.assertEqual(self.table["registered"]["outcome"], "NOT CONSEQUENTIAL")

    def test_walkability_comes_from_the_fallback_not_the_cpu_reference(self):
        self.assertAlmostEqual(self.ind["walk_bar_m"], 8.626833, places=6)
        self.assertAlmostEqual(self.ind["walk_cpu_mean"], 1.373488, places=6)
        self.assertLess(self.ind["walk_cpu_mean"], self.ind["walk_bar_m"])
        self.assertEqual(self.ind["fallback_best_step"], 30000)
        self.assertAlmostEqual(self.ind["fallback_selection_means"][30000], 42.453677, places=6)
        self.assertAlmostEqual(self.ind["walk_fallback_mean"], 42.121987, places=6)

    def test_identity_and_transport_reproduce_exactly_with_action_hashes(self):
        for check in ("identity", "transport"):
            c = self.table["rebuilt"][check]
            self.assertTrue(c["reproduces"])
            self.assertEqual((c["compared"], c["max_abs_return_diff"], c["action_hashes_compared"]), (10, 0.0, 10))

    def test_rebuilt_decision_record_equals_the_released_record(self):
        self.assertEqual(self.table["rebuilt"]["record"], self.files["record"]["record"])
        self.assertEqual(R.disagreements(self.table, self.files["record"]), [])

    def test_first_attempt_is_incomplete_by_the_same_rule(self):
        self.assertEqual(self.table["first_attempt"]["outcome"], "INCOMPLETE")

    def test_cost_is_the_final_ledger_across_both_attempts(self):
        c = self.table["cost"]
        self.assertEqual(c["step0a_s"], 878.0)
        self.assertAlmostEqual(c["charged_h"], 7.375629, places=6)
        self.assertAlmostEqual(c["remaining_h"], 4.624371, places=6)
        self.assertAlmostEqual(c["charged_s"], 26552.265, places=3)

    def test_main_exits_zero_on_the_released_files(self):
        printed = io.StringIO()
        with contextlib.redirect_stdout(printed):
            self.assertEqual(R.main(["recompute_entry02.py", ENTRY_DIR]), 0)
        self.assertIn("AGREES with stage0.json", printed.getvalue())


class RefusesOrReportsInconsistentInputs(unittest.TestCase):
    def setUp(self):
        self.files = copy.deepcopy(R.load_entry(ENTRY_DIR))

    def test_a_tampered_z_episode_is_reported_as_a_disagreement(self):
        self.files["gpu"]["Z"]["per_episode"][0]["ud"] -= 1.0
        self.files["gpu"]["Z"]["mean_ud"] -= 0.1
        found = R.disagreements(R.recompute(self.files), self.files["record"])
        self.assertTrue(any(f.startswith("rebuilt decision record") for f in found))
        self.assertTrue(any(f.startswith("mean_Z (independent)") for f in found))

    def test_a_changed_identity_action_hash_makes_the_class_invalid(self):
        self.files["gpu"]["identity"]["per_episode"][3]["actions_sha256"] = "0" * 64
        table = R.recompute(self.files)
        self.assertEqual(table["registered"]["outcome"], "INVALID")
        self.assertEqual(table["independent"]["outcome"], "INVALID")
        self.assertTrue(any(f.startswith("outcome") for f in R.disagreements(table, self.files["record"])))

    def test_a_consequential_z_flips_both_readings_to_pass(self):
        z = self.files["gpu"]["Z"]
        for e, h in zip(z["per_episode"], self.files["gpu"]["H"]["per_episode"]):
            e["ud"] = 0.3 * h["ud"]
        z["mean_ud"] = sum(e["ud"] for e in z["per_episode"]) / 10
        table = R.recompute(self.files)
        self.assertEqual((table["registered"]["outcome"], table["independent"]["outcome"]), ("PASS", "PASS"))

    def test_a_check_log_that_differs_from_fallback_json_is_refused(self):
        self.files["fallback_checks"][1]["check"]["observed"]["updates"] += 1
        with self.assertRaises(R.Inconsistent):
            R.recompute(self.files)

    def test_a_block_on_the_wrong_bank_is_refused(self):
        self.files["gpu"]["M0_4000"]["bank"] = 3000
        with self.assertRaises(R.Inconsistent):
            R.recompute(self.files)

    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()