okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
3.3 KiB78 linesraw
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
8    python -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
29    def setUpClass(cls):
30        cls.data = (PRODUCT / "strata_sin_exc.bin").read_bytes()
31        cls.entries = M.unpack(cls.data)
32
33    def test_sha256_and_size(self):
34        self.assertEqual(hashlib.sha256(self.data).hexdigest(), SHA256)
35        self.assertEqual(len(self.entries), COUNT)
36        self.assertLess(len(self.data), 200_000)
37
38    def 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.
41        keys = [k for k, _ in self.entries]
42        self.assertEqual(keys, sorted(keys))
43        self.assertEqual(len(set(keys)), 92529)
44        flags = {}
45        for k, down in self.entries:
46            flags.setdefault(k, set()).add(down)
47        self.assertTrue(all(len(s) == 1 for s in flags.values()))
48        for k in (keys[0], keys[-1]):
49            v = M.fval(k)
50            self.assertTrue(0 < v < 6.3, v)
51
52    def test_sample_rederived(self):
53        glibc_x86 = sys.platform.startswith("linux") and platform.machine() == "x86_64" and \
54            platform.libc_ver()[0] == "glibc"
55        for k, down in random.Random(11).sample(self.entries, 300):
56            v = M.fval(k)
57            cr = M.dbits(M.cr_sin(v))
58            if glibc_x86:
59                self.assertEqual(M.dbits(math.sin(v)), cr + (-1 if down else 1), hex(k))
60            self.assertIn(down, (0, 1))
61
62    def test_pack_round_trip(self):
63        self.assertEqual(M.pack(self.entries), self.data)
64
65
66class Constants(unittest.TestCase):
67    def test_tables_are_what_make_tables_writes(self):
68        import subprocess
69        import tempfile
70        with tempfile.TemporaryDirectory() as d:
71            subprocess.run([sys.executable, "-I", "-B", str(HERE / "make_tables.py"),
72                            str(Path(d) / "strata_noise_tables.inc")], check=True)
73            self.assertEqual((Path(d) / "strata_noise_tables.inc").read_text().splitlines(),
74                             (PRODUCT / "strata_noise_tables.inc").read_text().splitlines())
75
76
77if __name__ == "__main__":
78    unittest.main()