import itertools import math from pathlib import Path import random import tempfile import unittest from round3_strict_bounds import BoundsTests, exp_bounds import verify_k9_proof as verifier class Round4Tests(unittest.TestCase): def test_round3_asymmetric_hole_regression(self): holes = verifier.all_holes_of([(3.0, 3.0)], [(5.0, 1.0, 4.0)], []) self.assertTrue(any(lo <= 4.5 <= hi for lo, hi in verifier.get_support(6.0, holes))) self.assertTrue(any(lo <= 3.0 <= hi for lo, hi in verifier.get_support(6.0, holes))) def test_open_holes_and_singleton_shell_intersection(self): segments = verifier.get_support(6.0, [(0.0, 2.0), (2.0, 4.0)]) self.assertEqual(segments, [(2.0, 2.0), (4.0, 6.0)]) self.assertEqual(verifier.shell_intersections(segments, (2.0, 4.0), (1.0, 5.0)), [(2.0, 2.0), (4.0, 4.0)]) self.assertEqual(verifier.shell_intersections(segments, (2.0, 4.0), (2.0, 4.0)), []) def test_nearest_tie_and_repeated_root(self): for values in ((3.0, 3.0, 7.0), (7.0, 7.0, 3.0)): selected, *remaining = values holes = verifier.all_holes_of([(selected, selected)], [(5.0, 1.0, 9.0)], []) self.assertTrue(all(not any(a < value < b for a, b in holes) for value in remaining)) def test_random_nearest_branch_coverage(self): rng = random.Random(877) domain = tuple(i / 8 for i in range(1, 97)) for _ in range(5000): roots = rng.choices(domain, k=rng.randrange(1, 13)) xi, lo, hi = rng.choice(domain), *sorted(rng.sample(domain, 2)) old = [tuple(sorted(rng.sample(domain, 2))) for _ in range(rng.randrange(4))] roots = [root for root in roots if all(not a < root < b for a, b in old)] support = verifier.get_support(12.0, old) in_hole = [r for r in roots if lo < r < hi] if not in_hole: children_support = verifier.get_support(12.0, old + [(lo, hi)]) self.assertTrue(all(any(a <= r <= b for a, b in children_support) for r in roots)) continue selected = min(in_hole, key=lambda r: abs(r - xi)) remaining = list(roots) remaining.remove(selected) cut = (lo + hi) / 2 boxes = list(itertools.chain.from_iterable( verifier.shell_intersections(support, shell, (lo, hi)) for shell in ((lo, cut), (cut, hi)))) self.assertTrue(any(a <= selected <= b and all( any(c <= root <= d for c, d in verifier.get_support( 12.0, verifier.all_holes_of([(a, b)], [(xi, lo, hi)], old))) for root in remaining) for a, b in boxes)) def test_ulp_asymmetry(self): xi = 5.1 lo, hi = math.nextafter(4.0, math.inf), math.nextafter(6.0, -math.inf) selected = math.nextafter(lo, math.inf) holes = verifier.all_holes_of([(selected, selected)], [(xi, lo, hi)], []) self.assertTrue(all(lo <= a < b <= hi for a, b in holes)) self.assertTrue(any(a <= hi <= b for a, b in verifier.get_support(7.0, holes))) def test_nonrepresentable_nonfinite_hex(self): bad = ('inf', 'nan', '0x1p+1024', '0x1p-1075', '0x1.00000000000001p0', '1.0') for token in bad: with self.subTest(token=token), self.assertRaises(verifier.CertificateError): verifier.parse_hex_float(token) self.assertEqual(verifier.parse_hex_float('0x0.0000000000001p-1022'), math.ulp(0.0)) def test_exp_underflow_and_overflow(self): lower, upper = exp_bounds(-1000.0) self.assertLessEqual(lower, 0.0) self.assertGreater(upper, 0.0) with self.assertRaises(OverflowError): exp_bounds(1000.0) def test_strict_tree_file_coverage_and_shape(self): with tempfile.TemporaryDirectory() as folder: path = Path(folder) / 'test.txt' for certificate, message in ( ('C4 8\nLEAF_POLY 0\n', 'Missing'), ('C4 8\nLEAF_POLY 0\nC4 8\nLEAF_POLY 0\n', 'Duplicate'), ('C4 118\nLEAF_POLY 0\n', 'Unexpected'), ('C4 8\nLEAF_POLY 0 garbage\n', 'tokens'), ('LEAF_POLY 0\nC4 8\n', 'precedes'), ('C4 8\nHOLE_OR_HIT 0x1p0 0 0x1p0 0x0p0 0x1p0 2 0x0p0 0x1p0\n', 'tokens'), ): with self.subTest(certificate=certificate), self.assertRaisesRegex( verifier.CertificateError, message ): path.write_text(certificate, encoding='ascii') verifier.read_blocks(str(path), verifier.FULL_C4) def test_negative_index_and_extra_child_record(self): with self.assertRaises(verifier.CertificateError): verifier.verify_single_c4_block(['C4 8', 'LEAF_POLY -1']) with self.assertRaisesRegex(verifier.CertificateError, 'unconsumed'): verifier.verify_subtree_task((8, [(90.0, 90.0)], [None], [], ['LEAF_POLY 0', 'LEAF_POLY 0'])) if __name__ == '__main__': unittest.main(verbosity=2)