#!/usr/bin/env python3 """Unit tests for window_kernel.py (job #4472). Exact-arithmetic re-check of the three load-bearing elementary claims of the served record on the Var/E identification step, plus negative controls that show each check can fail. Run from the workspace root: ./.venv/bin/python3 -m unittest discover -s outputs/job4472 -p 'test_*.py' """ import os import sys import unittest from fractions import Fraction _HERE = os.path.dirname(os.path.abspath(__file__)) for _p in (_HERE, os.path.dirname(_HERE)): if _p not in sys.path: sys.path.insert(0, _p) import window_kernel as wk # noqa: E402 class TestWindowKernel(unittest.TestCase): def test_closed_form_matches_bruteforce_exhaustively(self): # exhaustive over all (L, n, c) with L <= 40, n <= 60: 0 mismatches bad = [] for L in range(1, 41): for n in range(1, 61): for c in range(n): if wk.rn(L, n, c) != wk.shift_sum(L, n, c) - Fraction(L, n): bad.append((L, n, c)) self.assertEqual(bad, [], "closed form mismatches: %r" % bad[:5]) def test_class_sum_zero(self): for L in range(1, 30): for n in range(1, 40): self.assertEqual(wk.class_sum(L, n), 0, "L=%d n=%d" % (L, n)) def test_kernel_bound(self): for L in range(1, 30): for n in range(1, 50): for c in range(n): self.assertTrue(wk.kernel_bound_holds(L, n, c), (L, n, c)) def test_large_modulus_form(self): for L in range(1, 25): for n in range(2 * L + 1, 2 * L + 40): for c in range(n): self.assertEqual(wk.rn(L, n, c), wk.rn_large_modulus(L, n, c), "L=%d n=%d c=%d" % (L, n, c)) def test_c0_is_the_fact_A_case(self): # R_n(0) = r(n-r)/(nL) for L in range(1, 60): for n in range(1, 60): r = L % n self.assertEqual(wk.rn(L, n, 0), Fraction(r * (n - r), n * L)) def test_kloosterman_class_forms(self): # the two forms of the mixed lag's class, on coprime triples of the # y-smooth primes >= 7 used by the record primes = [7, 11, 13, 17, 19, 23] tested = 0 for n0 in primes: for np_ in primes: for nm in primes: if len({n0, np_, nm}) != 3: continue c, n, frac, root, v, m = wk.kloosterman_class(n0, np_, nm) self.assertEqual(c % n0, 0) self.assertEqual(c % np_, 2 % np_) self.assertEqual((c + 2) % nm, 0) self.assertEqual(c % n, frac % n, "fraction form") self.assertEqual(c % n, root % n, "root form") self.assertEqual((v * v - pow(n0, -1, m) ** 2) % m, 0, "v^2 = inv(n0)^2 mod n+ n-") tested += 1 self.assertGreaterEqual(tested, 100) def test_chi7_values(self): self.assertEqual([wk.chi7(n) for n in range(1, 8)], [1, 1, -1, 1, -1, -1, 0]) def test_first_block_is_21_over_20(self): self.assertEqual(wk.block_first_value(), Fraction(21, 20)) def test_every_character_block_positive(self): for v in wk.block_values(400): self.assertGreater(v, 0) def test_l_one_chi7_below_first_blocks(self): # L(1,chi) >= 21/20 from the first block alone (every later block > 0) self.assertGreaterEqual(wk.l_one_chi7(50), Fraction(21, 20)) def test_c_chi_lower_bound_arithmetic(self): self.assertEqual(Fraction(21, 20) * Fraction(4, 5) * Fraction(809, 2016), wk.C_CHI_LOWER_BOUND) self.assertGreater(wk.C_CHI_LOWER_BOUND, Fraction(1, 3)) def test_c_chi_deficit_bound(self): # sum_{n>=11} 1/(n(n-4)) = (1/4)(1/7+1/8+1/9+1/10) s = sum((Fraction(1, n * (n - 4)) for n in range(11, 4000)), Fraction(0)) self.assertLess(abs(s - Fraction(1, 4) * (Fraction(1, 7) + Fraction(1, 8) + Fraction(1, 9) + Fraction(1, 10))), Fraction(1, 1000)) self.assertEqual(Fraction(5) * Fraction(1, 4) * (Fraction(1, 7) + Fraction(1, 8) + Fraction(1, 9) + Fraction(1, 10)), Fraction(1207, 2016)) class TestNegativeControls(unittest.TestCase): """Each check must be able to fail: perturb the object and watch it fire.""" def test_counterexample_wrong_crt_class(self): c, n, frac, root, v, m = wk.kloosterman_class(7, 11, 13) self.assertNotEqual((c + 1) % n, frac % n) def test_counterexample_shifted_modulus_in_large_form(self): self.assertNotEqual(wk.rn(10, 25, 3), wk.rn_large_modulus(10, 12, 3)) def test_counterexample_class_sum_with_a_wrong_term(self): tot = sum((wk.rn(9, 5, c) for c in range(5)), Fraction(0)) self.assertEqual(tot, 0) self.assertNotEqual(tot + Fraction(1, 100), 0) def test_counterexample_block_constant(self): # 1/3 is below the bound; 809/2400 - 1/100 must NOT be a lower bound claim self.assertLess(Fraction(1, 3), wk.C_CHI_LOWER_BOUND) self.assertLess(wk.C_CHI_LOWER_BOUND - Fraction(1, 100), Fraction(1, 3)) class TestHarperRange(unittest.TestCase): def test_range(self): self.assertTrue(wk.harper_range_holds(1000, 500)) self.assertFalse(wk.harper_range_holds(1000, 10)) # Q below sqrt(2x) self.assertFalse(wk.harper_range_holds(1000, 5000)) # Q above x if __name__ == "__main__": unittest.main(verbosity=2)