#!/usr/bin/env python3 """Adversarial and arithmetic tests. Explicit unittest checks survive -O.""" import math import unittest from dataclasses import replace from fractions import Fraction as F import tga_verifier as t class TGA(unittest.TestCase): def spec(self,name): return t.make_spec(name) def test_01_golden_normal_forms(self): self.assertEqual(t.normalize('(1+sqrt5)/2'),t.normalize('phi')) self.assertEqual(t.normalize('phi*phi-phi-1'),t.Golden()) self.assertEqual(t.normalize('1/phi'),t.normalize('phi-1')) self.assertNotEqual(t.normalize('phi'),t.normalize('1+sqrt5')) def test_02_normalizer_rejects_code_and_float(self): for s in ["__import__('os')",'sqrt5.real','0.5+sqrt5/2','2**3','1/0']: with self.assertRaises((t.Reject,SyntaxError)): t.normalize(s) def test_03_candidates_do_not_admit(self): claims=(t.Claim('a','constant','phi',('sqrt5',)),t.Claim('b','constant','1+sqrt5',('sqrt5',))) p=t.propose(claims);self.assertEqual(len(p),1);self.assertFalse(p[0].admitted) self.assertEqual(t.Graph().report()['edges'],[]) def test_04_all_supported_edges_admit(self): g=t.Graph() for name in t.MAP_IDS:g.admit(self.spec(name)) self.assertEqual(len(g.report()['edges']),19) def test_05_no_witness(self): m=self.spec('pair') with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,witness=None))) def test_06_numerical_evidence_is_not_proof(self): m=self.spec('pair');w=replace(m.alignment.witness,support=t.Support.NUMERICAL_TEST_ONLY) with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,witness=w))) def test_07_external_reference_is_not_proof(self): m=self.spec('pair');w=replace(m.alignment.witness,support=t.Support.EXTERNAL_REFERENCE_ONLY) with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,witness=w))) def test_08_cannot_relabel_parameter_choice_as_equivalence(self): m=self.spec('lambda_beta') with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,relation=t.Relation.EQUIVALENCE))) def test_09_cannot_broaden_domain(self): m=self.spec('floor_agreement');c=replace(m.alignment.contract,domain='all nonnegative integers') with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,contract=c))) def test_10_cannot_mark_floor_reversible(self): m=self.spec('floor');c=replace(m.alignment.contract,reversible=True) with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,contract=c))) def test_11_inverse_required_and_bound(self): m=self.spec('pair') with self.assertRaises(t.Reject):t.verify(replace(m,inverse=None)) with self.assertRaises(t.Reject):t.verify(replace(m,inverse=replace(m.inverse,map_id='floor'))) def test_12_implementation_and_payload_bound(self): m=self.spec('pair') with self.assertRaises(t.Reject):t.verify(replace(m,implementation='floor')) w=replace(m.alignment.witness,payload='{}') with self.assertRaises(t.Reject):t.verify(replace(m,alignment=replace(m.alignment,witness=w))) def test_13_unknown_schema_and_duplicate_rejected(self): with self.assertRaises(t.Reject):self.spec('shared_phi_proves_physics') g=t.Graph();g.admit(self.spec('pair')) with self.assertRaises(t.Reject):g.admit(self.spec('pair')) def test_14_floor_sum_matches_brute_oracle(self): for n in range(21): for m in range(1,12): for a in range(13): for b in (0,1,7,20): self.assertEqual(t.floor_sum(n,m,a,b),sum((a*k+b)//m for k in range(n))) def test_15_boundary_exact_certificate(self): c=t.boundary_certificate();self.assertEqual(c['common_prefix_sum'],302541195258184) self.assertEqual(c['at_boundary'],[14342162,14342163,14342163]) self.assertEqual(c['domain']['upper_exclusive'],42189065) lo,hi=F(c['lambda_lower']),F(c['lambda_upper']) self.assertLess(F(t.Q,t.S),lo);self.assertLess(lo,hi) self.assertLess(hi-F(t.Q,t.S),F(1,10**15)) def test_16_cached_certificate_cannot_be_mutated(self): c=t.boundary_certificate();c['at_boundary'][0]=-9;c['domain']['upper_exclusive']=999 self.assertEqual(t.boundary_certificate()['at_boundary'][0],14342162) self.assertEqual(t.boundary_certificate()['domain']['upper_exclusive'],42189065) def test_17_rewrite_guard_is_exclusive(self): m=self.spec('floor_agreement') self.assertEqual(t.execute(m,t.DSTAR-1),t.Q*(t.DSTAR-1)//t.S) with self.assertRaises(t.Reject):t.execute(m,t.DSTAR) with self.assertRaises(t.Reject):t.execute(m,-1) def test_18_exact_399_ledger(self): rows=t.execute(self.spec('ledger'),399) self.assertEqual(rows,(639958766566422,893219131545030,297739710515010,99246570171670,699748856723890,339949771344778)) self.assertEqual(t.execute(self.spec('unledger'),rows),399) def test_19_all_32768_seeds_roundtrip(self): # Verify supported maps once; exact arithmetic oracle separately exercises every seed. enc,dec=self.spec('ledger'),self.spec('unledger');t.verify(enc);t.verify(dec) for seed in range(32768): d=seed;rs=[] while d: d,r=divmod(t.Q*d,t.S);rs.append(r) back=0 for r in reversed(rs): self.assertEqual((t.S*back+r)%t.Q,0);back=(t.S*back+r)//t.Q self.assertEqual(back,seed) # Public execution path on every seed as well. for seed in range(32768): self.assertEqual(t.execute(dec,t.execute(enc,seed)),seed) def test_20_ledger_mutations_and_suffix(self): rows=t.execute(self.spec('ledger'),399);m=self.spec('unledger') with self.assertRaises(t.Reject):t.execute(m,(rows[0]+1,)+rows[1:]) with self.assertRaises(t.Reject):t.execute(m,rows+(0,)) with self.assertRaises(t.Reject):t.execute(m,(-1,)) self.assertEqual(t.execute(m,rows[1:]),135) self.assertEqual(t.execute(m,()),0) def test_21_pair_image_guard(self): m=self.spec('unpair') with self.assertRaises(t.Reject):t.execute(m,(135,1)) with self.assertRaises(t.Reject):t.execute(m,(0,t.S)) with self.assertRaises(t.Reject):t.execute(self.spec('floor'),True) self.assertEqual(t.execute(m,t.execute(self.spec('pair'),399)),399) def test_22_wrong_type_composition_rejected(self): with self.assertRaises(TypeError):t.compose(self.spec('marks_probability'),self.spec('unledger')) with self.assertRaises(t.Reject):t.compose(self.spec('lambda_beta')) with self.assertRaises(t.Reject):t.compose() def test_23_type_conventions_are_bound(self): m=self.spec('marks_probability');changed=replace(m.source,labels=tuple(reversed(t.LABELS))) with self.assertRaises(t.Reject):t.compose(replace(m,source=changed),self.spec('probability_marks')) def test_24_centered_probability_roundtrip(self): chain=t.compose(self.spec('marks_probability'),self.spec('probability_marks'),self.spec('marks_complex')) x=(-3.,-2.,-1.,1.,2.,3.);back=chain.reverse(chain.run(x)) self.assertLess(max(abs(a-b) for a,b in zip(x,back)),1e-12) meta=chain.metadata();self.assertTrue(meta['formulas_exact']);self.assertTrue(meta['certified_reversible']) self.assertEqual(meta['implementation_arithmetic'],['binary64 preview']) def test_25_softmax_gauge_and_positivity_guards(self): with self.assertRaises(t.Reject):t.execute(self.spec('marks_probability'),(1.,2.,3.,4.,5.,6.)) with self.assertRaises(t.Reject):t.execute(self.spec('probability_marks'),(0.,.2,.2,.2,.2,.2)) with self.assertRaises(t.Reject):t.execute(self.spec('probability_marks'),(.2,)*6) def test_26_numerical_underflow_is_not_silent(self): with self.assertRaises(t.Reject):t.execute(self.spec('marks_probability'),(-1e6,-2.,-1.,1.,2.,1e6)) def test_27_full_geometry_chain_retains_loss(self): chain=t.compose(*(self.spec(n) for n in ('geometry_marks','marks_probability','probability_marks','marks_complex'))) meta=chain.metadata();self.assertFalse(meta['certified_reversible']);self.assertIn('baseline_origin',meta['losses_or_discards_encountered']) g={'a':1.,'epsilon':math.pi/648000,'origin':0.};g2={**g,'origin':7.} self.assertEqual(chain.run(g),chain.run(g2)) with self.assertRaises(t.Reject):chain.reverse(chain.run(g)) def test_28_complex_image_guard(self): with self.assertRaises(t.Reject):t.execute(self.spec('complex_marks'),(2+0j,)*6) def test_29_fourier_full_inverse_and_magnitude_loss(self): x=(1+1j,2-3j,4+0j,-2j,7,8) chain=t.compose(self.spec('dft'),self.spec('idft')) self.assertLess(max(abs(a-b) for a,b in zip(x,chain.run(x))),1e-12) loss=t.compose(self.spec('dft'),self.spec('fourier_magnitude')) self.assertFalse(loss.metadata()['certified_reversible']) self.assertLess(max(abs(a-b) for a,b in zip(loss.run(x),loss.run(tuple(-v for v in x)))),1e-12) def test_30_marked_fourier_chain(self): chain=t.compose(self.spec('marks_complex'),self.spec('marks_dft'),self.spec('dft_marks'),self.spec('complex_marks')) x=(-3.,-2.,-1.,1.,2.,3.) self.assertLess(max(abs(a-b) for a,b in zip(x,chain.run(x))),1e-12) with self.assertRaises(TypeError):t.compose(self.spec('marks_complex'),self.spec('dft')) def test_31_preservation_is_intersection_not_union(self): c=t.compose(self.spec('pair'),self.spec('unpair'),self.spec('floor')).metadata() self.assertNotIn('integer_seed_information',c['preserves']) self.assertEqual(len(c['stage_requirements']),3) def test_32_report_support_labels(self): r=t.build_report();self.assertEqual(r['ledger_399']['recovered'],399) certs={c['edge_id']:c for c in r['graph']['certificates']} self.assertEqual(certs['marks_probability']['support'],'TRUSTED_ANALYTIC_SCHEMA') self.assertEqual(certs['floor_agreement']['support'],'EXACT_CERTIFICATE') self.assertEqual(certs['lambda_beta']['support'],'EXPLICIT_DECLARATION') if __name__=='__main__':unittest.main(verbosity=2)