"""Independent exact checks of the monotone bound. SPDX-License-Identifier: MIT""" from fractions import Fraction import itertools import json from pathlib import Path import runpy import tempfile import unittest ROOT = Path(__file__).parent module = runpy.run_path(str(ROOT / "monotone-mrc.py")) fit_counts, report = module["fit_counts"], module["report"] class MonotoneBoundTests(unittest.TestCase): def test_existing_fifo_witness_and_attaining_fit(self): result = fit_counts([1, 2, 3, 4, 5], [12, 12, 9, 10, 5], 12) self.assertEqual(result["maxError"], Fraction(1, 24)) self.assertEqual(result["witnessIndices"], [2, 3]) self.assertEqual(result["fitted"][2:4], [Fraction(19, 24)] * 2) def test_exhaustive_half_count_grid_is_optimal(self): grid = [Fraction(i, 8) for i in range(9)] candidates = list(itertools.combinations_with_replacement(grid, 3)) for counts in itertools.product(range(5), repeat=3): original = [Fraction(x, 4) for x in counts] brute = min(max(abs(x - y) for x, y in zip(original, reversed(candidate))) for candidate in candidates) result = fit_counts([1, 2, 3], list(counts), 4) self.assertEqual(result["maxError"], brute, counts) fitted = result["fitted"] self.assertTrue(all(a >= b for a, b in zip(fitted, fitted[1:]))) self.assertTrue(all(0 <= x <= 1 for x in fitted)) self.assertEqual(max(abs(a - b) for a, b in zip(original, fitted)), brute) def test_nonadjacent_upturn_is_not_lost(self): result = fit_counts([1, 2, 3, 4], [1, 3, 2, 5], 8) self.assertEqual(result["maxError"], Fraction(1, 4)) self.assertEqual(result["witnessIndices"], [0, 3]) def test_exact_above_float_integer_limit(self): n = 2 ** 60 result = fit_counts([1, 2], [n - 2, n - 1], n) self.assertEqual(result["maxError"], Fraction(1, 2 * n)) def test_monotone_and_singleton_curves_are_unchanged(self): for counts in ([0], [4], [4, 3, 1, 0], [2, 2, 2]): result = fit_counts(list(range(len(counts))), list(counts), 4) self.assertEqual(result["maxError"], 0) self.assertEqual(result["fitted"], [Fraction(x, 4) for x in counts]) self.assertIsNone(result["witnessIndices"]) def test_rejects_invalid_inputs(self): cases = [([], [], 1), ([1], [], 1), ([1, 1], [0, 1], 2), ([2, 1], [0, 1], 2), ([True], [0], 1), ([1], [True], 1), ([1], [1], True), ([-1], [0], 1), ([1], [-1], 1), ([1], [2], 1), ([1], [0], 0), ([1.0], [0], 1), ([1], [0.0], 1), ((1,), [0], 1)] for args in cases: with self.subTest(args=args), self.assertRaises(ValueError): fit_counts(*args) def test_committed_report_matches_complete_recomputation(self): saved = json.loads((ROOT / "monotone-mrc.json").read_text()) self.assertEqual(saved, report(ROOT / "cache-capacity.json")) def test_source_identity_and_counts_are_required(self): original = json.loads((ROOT / "cache-capacity.json").read_text()) with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "source.json" for field, value in (("requests", 13), ("references", "other")): changed = json.loads(json.dumps(original)) changed["classic"][field] = value path.write_text(json.dumps(changed)) with self.assertRaises(ValueError): report(path) original["classic"]["rows"][2]["policies"]["FIFO"]["misses"] = 8 path.write_text(json.dumps(original)) with self.assertRaises(ValueError): report(path) if __name__ == "__main__": unittest.main()