float_boundary_test.py (2327B)
1 #!/usr/bin/env python3 2 import json 3 import re 4 import unittest 5 6 from float_boundary import ROOT, boundary_cases, decode_word, small_trees, nat, LEAF, TRUE, pair 7 from float_library_extract import parsed 8 9 10 class BoundaryTests(unittest.TestCase): 11 def test_small_tree_exhaustion(self): 12 rows=[t for size in range(1,11) for t in small_trees(size)] 13 accepted={t for t in rows if decode_word(t) is not None} 14 self.assertEqual(accepted,{nat(n) for n in range(1024) if len(nat(n))<=10}) 15 16 def test_width_and_canonicality(self): 17 for width in range(1,65): 18 self.assertEqual(decode_word(nat(1<<(width-1))),1<<(width-1)) 19 self.assertEqual(decode_word(nat((1<<width)-1)),(1<<width)-1) 20 for n in [1<<64,(1<<65)-1,1<<256]:self.assertIsNone(decode_word(nat(n))) 21 self.assertIsNone(decode_word(pair(LEAF,LEAF))) 22 self.assertIsNone(decode_word(TRUE)) 23 self.assertIsNone(decode_word(pair(pair(LEAF,LEAF),nat(1)))) 24 25 def test_corpus_contains_both_domains(self): 26 rows=boundary_cases() 27 self.assertEqual(len(rows),741) 28 self.assertEqual(sum(decode_word(t) is not None for t in rows),274) 29 30 def test_library_has_no_benchmark_dependency(self): 31 manifest=json.loads((ROOT/'test/float-library-extraction.json').read_text()) 32 names={'binary64_'+m for m in manifest if m!='boundary'}|{'native_math'} 33 for row in manifest.values(): 34 source=(ROOT/row['path']).read_text() 35 self.assertNotIn('bench/',source) 36 for name in re.findall(r'^!import (\w+) as',source,re.M):self.assertIn(name,names) 37 exports=manifest['boundary']['exports'] 38 self.assertNotIn('make',exports) 39 self.assertNotIn('step',exports) 40 self.assertIn('fromBits',exports) 41 42 def test_initial_extraction_keeps_bodies(self): 43 # Audit executable-body identity against the initial extraction receipt. 44 manifest=json.loads((ROOT/'test/float-library-extraction.json').read_text()) 45 for row in manifest.values(): 46 _,_,original=parsed(row['source']);_,_,library=parsed(row['path']) 47 self.assertEqual(set(library),set(row['declarations'])) 48 for name,body in library.items():self.assertEqual(body,original[name]) 49 50 51 if __name__=='__main__':unittest.main()