okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
1"""tools/image/gate/noise/test_noise_table.py - TEST-ONLY: the packed glibc sin exception table (strata_sin_exc.bin). 2 3Checks the file's sha256, that it unpacks to a sorted list of reachable v (positive floats below 6.3; repeats carry the same flag), and 4re-derives a sample of entries: the correctly rounded sin from Python's decimal module and, where this Python's 5math.sin is glibc on x86-64 (the reference platform), that glibc's double is that value one unit up or down as the 6entry says. Also checks the tables in strata_noise_tables.inc against make_tables.py. 7 8python -m pytest tools/image/gate/noise/test_noise_table.py""" 9import hashlib 10import math 11import platform 12import random 13import struct 14import sys 15import unittest 16from pathlib import Path 17 18HERE = Path (__file__ ).resolve ().parent 19PRODUCT = HERE .parents [1 ]/ "sdcpp_noise" # tools/image/sdcpp_noise: the generator, its tables and the packed sin table 20sys .path .insert (0 ,str (HERE )) 21import make_sin_exceptions as M # noqa: E402 22 23SHA256 = "3d61d50cca41c09e25b4acb0202aedd3c86bffdfbfed30df6066a5b5972429d6" 24COUNT = 109290 25 26 27class Table (unittest .TestCase ): 28@classmethod 29def setUpClass (cls ): 30cls .data = (PRODUCT / "strata_sin_exc.bin" ).read_bytes () 31cls .entries = M .unpack (cls .data ) 32 33def test_sha256_and_size (self ): 34self .assertEqual (hashlib .sha256 (self .data ).hexdigest (),SHA256 ) 35self .assertEqual (len (self .entries ),COUNT ) 36self .assertLess (len (self .data ),200_000 ) 37 38def test_sorted_reachable_repeats_agree (self ): 39# 109,290 entries over 92,529 distinct floats: 16,761 floats are listed twice, always with the same 40# flag (found by the second worker; the list comes from check_domain as it was). The loader's lower-bound lookup is fine with that. 41keys = [k for k ,_ in self .entries ] 42self .assertEqual (keys ,sorted (keys )) 43self .assertEqual (len (set (keys )),92529 ) 44flags = {} 45for k ,down in self .entries : 46flags .setdefault (k ,set ()).add (down ) 47self .assertTrue (all (len (s )== 1 for s in flags .values ())) 48for k in (keys [0 ],keys [- 1 ]): 49v = M .fval (k ) 50self .assertTrue (0 < v < 6.3 ,v ) 51 52def test_sample_rederived (self ): 53glibc_x86 = sys .platform .startswith ("linux" )and platform .machine ()== "x86_64" and \ 54platform .libc_ver ()[0 ]== "glibc" 55for k ,down in random .Random (11 ).sample (self .entries ,300 ): 56v = M .fval (k ) 57cr = M .dbits (M .cr_sin (v )) 58if glibc_x86 : 59self .assertEqual (M .dbits (math .sin (v )),cr + (- 1 if down else 1 ),hex (k )) 60self .assertIn (down , (0 ,1 )) 61 62def test_pack_round_trip (self ): 63self .assertEqual (M .pack (self .entries ),self .data ) 64 65 66class Constants (unittest .TestCase ): 67def test_tables_are_what_make_tables_writes (self ): 68import subprocess 69import tempfile 70with tempfile .TemporaryDirectory ()as d : 71subprocess .run ([sys .executable ,"-I" ,"-B" ,str (HERE / "make_tables.py" ), 72str (Path (d )/ "strata_noise_tables.inc" )],check = True ) 73self .assertEqual ((Path (d )/ "strata_noise_tables.inc" ).read_text ().splitlines (), 74 (PRODUCT / "strata_noise_tables.inc" ).read_text ().splitlines ()) 75 76 77if __name__ == "__main__" : 78unittest .main ()