omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / differential.py
| 1 | """Test infrastructure only. No compiler or source translation lives here.""" |
| 2 | import os, pathlib, subprocess, sys, hashlib, json, time |
| 3 | ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 4 | os.chdir(ROOT) |
| 5 | SEED = os.environ.get('OMEGA_SEED', 'build/bootstrap/omc') |
| 6 | B = ROOT / 'build/tests' |
| 7 | B.mkdir(parents=True, exist_ok=True) |
| 8 | def run(cmd, data=b''): |
| 9 | return subprocess.run([str(c) for c in cmd], input=data, capture_output=True, timeout=20) |
| 10 | def elf(path): |
| 11 | b = path.read_bytes() |
| 12 | assert len(b) >= 120 and b[:5] == b'\x7fELF\x02' and b[18:20] == b'\xb7\x00', 'not a nonempty AArch64 ELF' |
| 13 | path.chmod(0o755) |
| 14 | def same(a,b): |
| 15 | return (a.returncode,a.stdout,a.stderr)==(b.returncode,b.stdout,b.stderr) |
| 16 | fail = count = 0 |
| 17 | receipts=[] |
| 18 | started=time.perf_counter() |
| 19 | for f in sorted((ROOT/'tests/fixtures').glob('*.om')): |
| 20 | if f.stem in ('r5b_cycle','r5b_assign','r5b_fwd_fm'): continue |
| 21 | count += 1 |
| 22 | try: |
| 23 | data=f.read_bytes() |
| 24 | native=run([SEED],data) |
| 25 | assert native.returncode==0, native.stderr |
| 26 | exe=B/f'{f.stem}.native'; exe.write_bytes(native.stdout); elf(exe) |
| 27 | inp=bytes(range(256))*195 if f.stem in ('echo','bytecopy') else b'' |
| 28 | expected=run([exe],inp) |
| 29 | rec=dict(case=f.stem,source_sha256=hashlib.sha256(data).hexdigest(),input_sha256=hashlib.sha256(inp).hexdigest(),native=dict(exit=expected.returncode,stdout_hex=expected.stdout.hex(),stderr_hex=expected.stderr.hex())) |
| 30 | if f.stem=='echo': assert expected.stdout==inp |
| 31 | if f.stem=='bytecopy': assert expected.stdout==inp[::-1] |
| 32 | if f.stem=='order_calls': assert expected.stdout==b'ABCD' and expected.returncode==1 |
| 33 | if f.stem=='order_trap': assert expected.stdout==b'' and expected.returncode==102 |
| 34 | if f.stem=='fib': |
| 35 | nums=[0,1] |
| 36 | for _ in range(24): nums.append(sum(nums[-2:])) |
| 37 | assert expected.stdout==(''.join(str(x)+'\n' for x in nums)).encode() |
| 38 | want=f.with_suffix('.want') |
| 39 | if want.exists(): assert expected.returncode==int(want.read_text()), 'native differs from fixture' |
| 40 | c=run(['build/toc'],data) if '--baseline' in sys.argv else run(['tools/bridge','c',f]) |
| 41 | assert c.returncode==0 and c.stdout, ('conversion',c.returncode,c.stderr) |
| 42 | cfile=B/f'{f.stem}.c'; cfile.write_bytes(c.stdout) |
| 43 | rec['c_sha256']=hashlib.sha256(c.stdout).hexdigest() |
| 44 | ce=B/f'{f.stem}.c.exe' |
| 45 | cc=run(['gcc','-std=gnu11','-O1','-fsanitize=undefined','-fno-sanitize-recover=all',cfile,'-o',ce]) |
| 46 | assert cc.returncode==0, ('gcc',cc.stderr[:300]) |
| 47 | got=run([ce],inp) |
| 48 | rec['c']=dict(exit=got.returncode,stdout_hex=got.stdout.hex(),stderr_hex=got.stderr.hex()) |
| 49 | assert same(expected,got), ('C mismatch',expected.returncode,got.returncode,expected.stdout[:80],got.stdout[:80],got.stderr[:100]) |
| 50 | if '--baseline' not in sys.argv: |
| 51 | p=run(['tools/bridge','python',f]); assert p.returncode==0 and p.stdout, p.stderr |
| 52 | py=B/f'{f.stem}.py'; py.write_bytes(p.stdout) |
| 53 | rec['python_sha256']=hashlib.sha256(p.stdout).hexdigest() |
| 54 | got=run([sys.executable,py],inp) |
| 55 | rec['python']=dict(exit=got.returncode,stdout_hex=got.stdout.hex(),stderr_hex=got.stderr.hex()) |
| 56 | assert same(expected,got), ('Python mismatch',expected.returncode,got.returncode,got.stderr[:200]) |
| 57 | receipts.append(rec) |
| 58 | print('PASS', f.stem) |
| 59 | except Exception as e: |
| 60 | fail+=1; print('FAIL',f.stem,str(e)) |
| 61 | print(f'cases={count} failures={fail} RC={int(bool(fail))}') |
| 62 | pathlib.Path('build/differential-receipts.json').write_text(json.dumps(dict(cases=count,failures=fail,seconds=time.perf_counter()-started,results=receipts),indent=2)+'\n') |
| 63 | sys.exit(bool(fail)) |