"""Capacity replay regressions; run also with python -O.""" import importlib.metadata from pathlib import Path import runpy import unittest from unittest.mock import patch m = runpy.run_path(str(Path(__file__).with_name("cache-capacity.py"))) class CapacityTests(unittest.TestCase): def test_complete_validation(self): self.assertEqual(m["verify"](), 7290) def test_empty_and_repeat(self): self.assertEqual(m["oracle"]("", 1, "FIFO")["outcomes"], "") self.assertEqual(m["oracle"]("AAAA", 1, "LRU")["outcomes"], "MHHH") def test_invalid_inputs(self): for capacity in (0, -1, True, 1.5, "3"): with self.assertRaises(ValueError): m["oracle"]("AB", capacity, "FIFO") for refs in (None, [], 3, "a", "A B"): with self.assertRaises(ValueError): m["oracle"](refs, 3, "FIFO") with self.assertRaises(ValueError): m["oracle"]("AB", 2, "unknown") def test_wrong_version(self): with patch.object(importlib.metadata, "version", return_value="0.0"): with self.assertRaises(AssertionError): m["verify"]() def test_broken_oracle(self): globals_ = m["verify"].__globals__ original = globals_["oracle"] def broken(*args): result = original(*args) result["outcomes"] = "X" * len(args[0]) return result with patch.dict(globals_, {"oracle": broken}): with self.assertRaises(AssertionError): m["verify"]() def test_transcription(self): self.assertEqual(len(m["PREFIX"]), 34) refs = m["PREFIX"] + m["CYCLE"] * 7 self.assertEqual(len(refs), 181) self.assertEqual([m["replay"](refs, c, "FIFO")["misses"] for c in (5, 6)], [78, 161]) if __name__ == "__main__": unittest.main()