"""Independent finite oracles and boundary checks; no device simulation.""" from fractions import Fraction as Q from itertools import product import unittest from model import bounds, curve, quantity, rendered from pathlib import Path class BoundsTests(unittest.TestCase): def test_worked_example(self): result = bounds(1, 80, [(200, "0.002"), (100, "0.003")]) self.assertEqual(result["delay"], Q(3, 200)) self.assertEqual(result["backlog"], Q(7, 5)) self.assertEqual(result["separate_delay_sum"], Q(27, 1250)) self.assertEqual(result["nodes"][1]["input_burst"], Q(29, 25)) def test_horizontal_vertical_oracles(self): cases = 0 for b, r, rate, lag in product(range(5), range(5), range(1, 6), [Q(0), Q(1, 2), Q(2)]): if r > rate: continue result = bounds(b, r, [(rate, lag)]) times = [Q(i, 2) for i in range(41)] # Invert beta only for positive work. Zero work has zero delay. candidates = [Q(0)] for t in times: work = b + r*t if work > 0: candidates.append(max(Q(0), lag + work/rate - t)) if b == 0 and r > 0: candidates.append(lag) # Limit as positive time approaches zero. horizontal = max(candidates) vertical = max(b + r*t - curve(t, rate, lag) for t in times) self.assertGreaterEqual(result["delay"], horizontal) if b or r: self.assertEqual(result["delay"], horizontal) self.assertEqual(result["backlog"], vertical) cases += 1 self.assertEqual(cases, 285) def test_min_plus_concatenation(self): cases = 0 for r1, r2, t1, t2, tick in product(range(1, 5), range(1, 5), [Q(0), Q(1, 2), Q(1)], [Q(0), Q(1, 2), Q(1)], range(17)): time = Q(tick, 2) direct = min(curve(Q(i, 2), r1, t1) + curve(time-Q(i, 2), r2, t2) for i in range(tick+1)) composed = curve(time, min(r1, r2), t1+t2) self.assertEqual(direct, composed) cases += 1 self.assertEqual(cases, 2448) def test_overload_and_equality(self): with self.assertRaises(ValueError): bounds(1, 101, [(200, 0), (100, 0)]) self.assertEqual(bounds(1, 100, [(100, 0)])["delay"], Q(1, 100)) self.assertEqual(bounds(0, 0, [(100, 0)])["backlog"], 0) def test_invalid_inputs(self): for value in [True, False, -1, "nan", "inf", None, "1/0", "bad"]: with self.subTest(value=value), self.assertRaises(ValueError): quantity(value) for stages in [[], [(0, 0)], [(-1, 0)], [(1, -1)]]: with self.subTest(stages=stages), self.assertRaises(ValueError): bounds(1, 0, stages) def test_same_unit_scaling_and_stage_order(self): a = bounds(1, 80, [(200, "0.002"), (100, "0.003")]) b = bounds(1024, 81920, [(204800, "0.002"), (102400, "0.003")]) self.assertEqual(a["delay"], b["delay"]) self.assertEqual(a["backlog"] * 1024, b["backlog"]) c = bounds(1, 80, [(100, "0.003"), (200, "0.002")]) self.assertEqual(a["delay"], c["delay"]) self.assertNotEqual(a["separate_delay_sum"], c["separate_delay_sum"]) def test_total_average_is_not_an_envelope(self): # 100 MiB after 10 idle seconds passes a start-to-end rate check, # but violates b=1 MiB, r=80 MiB/s in a 10 ms window around the burst. self.assertLessEqual(100, 1 + 80 * 10) self.assertGreater(100, 1 + 80 * Q(1, 100)) def test_generated_result(self): self.assertEqual(Path(__file__).with_name("results.json").read_text(), rendered()) if __name__ == "__main__": unittest.main()