"""Static dispatcher source guards and independent ordering/state fixtures.""" from pathlib import Path import sys import unittest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tools/binary')) from collision_grid_traversal import ( EPSILON, NearestProjection, append_registration, cell, cells, collect, scan_projection, traversal_bounds, visits, ) class CollisionGridTraversalTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.report = collect(ROOT.parent / 'Original') def test_source_retry_and_asymmetric_upper_bound_are_guarded(self): sites = {s['name']: s for s in self.report['sites']} self.assertEqual(sites['z_upper_uses_min']['bytes'], '663bf8897c2418ddd87c0489442418') self.assertEqual(sites['retry_flag_branch']['bytes'], '8a44241384c00f8597000000') self.assertEqual(sites['retry_call_same_dispatcher']['file_offset'], '0x7b877') self.assertEqual(sites['wrapper_stores_last_polygon_and_retry']['bytes'], '89354c5d6900c60101') bodies = {b['name']: b for b in self.report['bodies']} self.assertEqual(bodies['static_dispatcher']['length'], 0x3D9) self.assertEqual(self.report['constants']['0x4d5e40'], EPSILON) self.assertAlmostEqual(self.report['constants']['0x4a3148'], -.7, places=7) def test_extent_and_z_upper_min_are_distinct_from_x_upper_max(self): self.assertEqual(cell(0), 130) self.assertEqual(cell(-65001), 0, 'Truncate toward zero before signed-short storage') self.assertEqual(traversal_bounds((0,0,0), (0,0,500), 50, 500), (128,131,128,131)) self.assertEqual(traversal_bounds((0,0,0), (500,0,0), 50, 500), (128,132,128,131)) self.assertEqual(traversal_bounds((0,0,0), (0,0,-500), 50, 500), (128,131,127,130)) def test_inclusive_x_outer_z_inner_and_skipped_outside_cells(self): self.assertEqual(cells((1,2,4,5)), [(1,4),(1,5),(2,4),(2,5)]) self.assertEqual(cells((-1,1,258,260)), [(0,258),(0,259),(1,258),(1,259)]) self.assertEqual(cells((2,1,0,1)), []) def test_polygon_only_per_cell_dedup_and_fifo_across_cells(self): grid = {} self.assertTrue(append_registration(grid, (1,2), 100, 20)) self.assertFalse(append_registration(grid, (1,2), 100, 99)) self.assertTrue(append_registration(grid, (1,2), 200, 20)) self.assertTrue(append_registration(grid, (1,3), 100, 20)) self.assertEqual(visits(grid, (1,1,2,3)), [ ((1,2),(100,20)), ((1,2),(200,20)), ((1,3),(100,20)), ]) def test_mutable_candidate_and_nearest_survive_duplicate_visits_and_retry(self): grid = {(1,2): [(100,20),(200,20)], (1,3): [(100,20)]} nearest = NearestProjection(130000, 10) observed = [] def first_pass(xy, polygon, candidate, state): observed.append((xy, polygon[0], candidate, state.face, state.last_polygon)) self.assertIs(state, nearest) if len(observed) == 1: state.face = 3 return (8,9,10), True return candidate, False candidate, retry = scan_projection(grid, (1,1,2,3), (0,0,0), nearest, first_pass) self.assertTrue(retry) self.assertEqual(candidate, (8,9,10)) self.assertEqual(observed[1][2:], ((8,9,10),3,100)) self.assertEqual(observed[2][1], 100, 'A repeated cell visit is retained') candidate, retry = scan_projection( grid, (1,1,2,3), candidate, nearest, lambda xy, polygon, current, state: (current, False)) self.assertFalse(retry) self.assertEqual((nearest.face, nearest.edge, nearest.last_polygon), (3,10,100)) self.assertEqual(candidate, (8,9,10)) if __name__ == '__main__': unittest.main()