"""Regression tests for the fault-book report, with no game files.""" from contextlib import redirect_stdout from io import StringIO from pathlib import Path from tempfile import TemporaryDirectory import unittest from faultbook import RECORD, book class FaultBookTest(unittest.TestCase): def report(self, blocks_per_frame, extra_faults=()): with TemporaryDirectory() as temp: path = Path(temp) / "capture.bin" with path.open("wb") as capture: for frame, blocks in enumerate(blocks_per_frame): began = frame * 1000 for block in blocks: capture.write(RECORD.pack(began + 100, block, 0, 0, 1, 1)) capture.write(RECORD.pack(began + 1000, frame, 0, 0, 1, 0)) for offset, block in extra_faults: at = len(blocks_per_frame) * 1000 + offset capture.write(RECORD.pack(at, block, 0, 0, 1, 1)) out = StringIO() with redirect_stdout(out): book(path, 1_000_000) return out.getvalue() def test_rates_use_frame_count_but_timing_uses_marker_intervals(self): text = self.report([range(10)] * 40) self.assertIn("frames: 40 over 39.0 ms (1.0 ms a frame)", text) self.assertIn("faults under the guards' lock, per frame 10.0", text) def test_hot_blocks_must_fault_in_at_least_three_quarters_of_frames(self): text = self.report([[1, 2], [1, 2], [1, 2], [2], []]) self.assertIn("blocks faulting in three frames of four or more: 1,", text) def test_faults_at_or_after_the_last_marker_do_not_count(self): for offset in (0, 100): with self.subTest(offset=offset): text = self.report([[1], [1], [], []], extra_faults=[(offset, 1)]) self.assertIn(": 7 records\n", text) self.assertIn("faults under the guards' lock, per frame 0.5", text) self.assertIn("records per frame: frame 1.0, fault 0.5", text) self.assertIn("blocks faulting in three frames of four or more: 0,", text) def test_faults_without_frame_markers_still_count(self): text = self.report([], extra_faults=[(100, 1)]) self.assertIn("records per frame: fault 1.0", text) if __name__ == "__main__": unittest.main()