60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
import math
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
PROJECT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(PROJECT / 'tools/binary'))
|
|
import shrink_heading as heading
|
|
|
|
|
|
class ShrinkHeadingTests(unittest.TestCase):
|
|
def test_quadrants(self):
|
|
for x, z, units in [(0, 0, 0), (0, 1, 0), (-1, 0, 4096),
|
|
(0, -1, 8192), (1, 0, 12288),
|
|
(-1, 1, 2048), (-1, -1, 6144),
|
|
(1, -1, 10240), (1, 1, 14336)]:
|
|
self.assertEqual(heading.heading_units(x, z), units)
|
|
|
|
def test_strict_window_and_no_seam_wrap(self):
|
|
self.assertTrue(heading.within_heading_window(0, 1, 0))
|
|
self.assertFalse(heading.within_heading_window(0, 1, 5))
|
|
self.assertFalse(heading.within_heading_window(0, 1, -5))
|
|
self.assertFalse(heading.within_heading_window(1, 1000, 0))
|
|
self.assertTrue(heading.within_heading_window(-1, 1000, 0))
|
|
|
|
def test_nonfinite(self):
|
|
for value in [math.nan, math.inf, -math.inf]:
|
|
with self.assertRaises(ValueError):
|
|
heading.heading_units(value, 1)
|
|
|
|
def test_copied_dll_samples(self):
|
|
for x, z, expected in [(1, 1000, 16364), (-1, 1000, 20),
|
|
(1000, 1, 12288), (-1000, 1, 4096),
|
|
(.1234567, .7654321, 15967), (-99.1, 15.7, 3686)]:
|
|
self.assertEqual(heading.heading_units(x, z), expected)
|
|
|
|
def test_copied_dll_full_bin_quadrant_sweep(self):
|
|
fingerprint = 2166136261
|
|
for i in range(16385):
|
|
a = heading.f32(math.sqrt((i + .25) / 16384.5))
|
|
b = heading.f32(math.sqrt(1 - (i + .25) / 16384.5))
|
|
for quadrant in range(4):
|
|
value = heading.heading_units(-b if quadrant & 1 else b,
|
|
-a if quadrant & 2 else a)
|
|
fingerprint = ((fingerprint ^ value) * 16777619) & 0xffffffff
|
|
self.assertEqual(fingerprint, 0x24e64f91)
|
|
|
|
def test_full_source_identity(self):
|
|
exe = PROJECT / 'artifacts/uncontrolled-phases/rea/Neutron.exe'
|
|
dll = PROJECT / 'artifacts/asset-probe/game/OMT2.dll'
|
|
if not exe.exists() or not dll.exists():
|
|
self.skipTest('generated original copies unavailable')
|
|
report = heading.evidence(exe, dll)
|
|
self.assertEqual(report['table_fnv32'], '4f800433')
|
|
self.assertEqual(report['exported']['ordinal'], 278)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|