Files
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

61 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""Bounded terrain diagnostic and optional actual-tile original-code oracle."""
import array,json,math,os,pickle,struct,subprocess,sys,tempfile,unittest
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
from map_asset_preflight import adt_height_profile,adt_height_sample
def synthetic():
raw=b'REVM'+struct.pack('<II',4,18)
for iy in range(16):
for ix in range(16):
header=bytearray(128);struct.pack_into('<III',header,0,0,ix,iy);struct.pack_into('<I',header,20,136)
body=bytes(header)+b'TVCM'+struct.pack('<I',580)+bytes(580)
raw+=b'KNCM'+struct.pack('<I',len(body))+body
return raw
class HeightDiagnostic(unittest.TestCase):
def test_new_triangle_hole_and_framing_boundary(self):
raw=synthetic();p=adt_height_profile(raw,(40,30));self.assertEqual(len(p['cells']),256)
p['V9']=[100.0+2*r+3*c for r in range(129)for c in range(129)]
p['V8']=[100.0+2*(r+.5)+3*(c+.5)for r in range(128)for c in range(128)]
grid=struct.unpack('<f',struct.pack('<f',533.3333))[0];seen=set()
for x,y in[(.75,.1),(.1,.75),(.9,.4),(.4,.9)]:
wx=(2-(20+x)/128)*grid;wy=(-8-(30+y)/128)*grid
r=adt_height_sample(p,wx,wy);seen.add(r['triangle']);row,col=r['sample'];fx,fy=r['fractional_xy']
self.assertAlmostEqual(r['height'],100+2*(row+fx)+3*(col+fy),places=3)
self.assertEqual(seen,{1,2,3,4})
cell=p['cells'][(col//8,row//8)];cell['holes']=1<<((row%8)*8+col%8)
self.assertIsNone(adt_height_sample(p,wx,wy)['height'])
with self.assertRaises(ValueError):adt_height_sample(p,50000,50000)
for bad in[raw[:-1],raw+b'bad',raw[:12]+b'KNCM'+struct.pack('<I',127)+bytes(127)]:
with self.assertRaises(ValueError):adt_height_profile(bad,(40,30))
bad=bytearray(raw);struct.pack_into('<f',bad,12+8+112,math.nan)
with self.assertRaises(ValueError):adt_height_profile(bytes(bad),(40,30))
bad=bytearray(raw);struct.pack_into('<I',bad,12+8+20,140)
with self.assertRaises(ValueError):adt_height_profile(bytes(bad),(40,30))
@unittest.skipUnless(os.environ.get('GW_ADT_ACTUAL_INPUTS'),'owner supplied bounded actual tile batch')
def test_actual_tile_against_unmodified_original_algorithm(self):
cfg=json.loads(Path(os.environ['GW_ADT_ACTUAL_INPUTS']).read_text())
for case in cfg.get('cases',[cfg]):
with self.subTest(tile=case['tile']):self.check_actual_case(case)
def check_actual_case(self,cfg):
out=Path(cfg['output']);out.mkdir(exist_ok=True)
points=cfg['points'];pointfile=out/'points.tsv';pointfile.write_text(''.join(f"{p['position'][0]} {p['position'][1]}\n"for p in points));rows=[]
for n,item in enumerate(cfg['inputs']):
with self.subTest(role=item['role']):
raw=Path(item['file']).read_bytes();profile=adt_height_profile(raw,tuple(cfg['tile']));prefix=out/f'oracle-{n}'
result=subprocess.run([cfg['oracle'],item['file'],str(pointfile),str(prefix)],capture_output=True,text=True,timeout=20)
(out/f'oracle-{n}.log').write_text(result.stderr);self.assertEqual(result.returncode,0,result.stderr)
expected=(prefix.with_suffix('.arrays')).read_bytes();actual=array.array('f',profile['V9']+profile['V8']).tobytes()
self.assertEqual(actual,expected,'all original MCNK base+MCVT V9/V8 float bytes')
reference=[float(line.split()[2])for line in prefix.with_suffix('.samples.tsv').read_text().splitlines()]
self.assertEqual(len(reference),len(points));samples=[]
for point,height in zip(points,reference):
sample=adt_height_sample(profile,*point['position'][:2])
if math.isnan(height):self.assertIsNone(sample['height'])
else:self.assertEqual(struct.pack('<f',sample['height']),struct.pack('<f',height),'original float32 triangle sample')
samples.append({'source_sequence':point['sequence'],'source_GUID':point['guid'],'position':point['position'],'terrain':sample,'source_Z_minus_terrain':None if sample['height']is None else point['position'][2]-sample['height']})
cache=out/f'profile-{n}.pickle';cache.write_bytes(pickle.dumps(profile,protocol=4));rows.append({'role':item['role'],'file':item['file'],'source_archive':item.get('source_archive'),'all_V9_V8_bytes_match_original':True,'all_samples_match_original':True,'profile_cache':str(cache),'samples':samples})
(out/'actual-tile-check-and-samples.json').write_text(json.dumps({'status':'PASS_ORIGINAL_EXTRACTOR_AND_SAMPLER_ALL_ACTUAL_INPUTS','tile':cfg['tile'],'cohort_points':len(points),'inputs':rows,'native_rendered_actor_position_observed':False},indent=2)+'\n')
if __name__=='__main__':unittest.main()