omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / controls.py
| 1 | """Deliberately damage emitted programs: the execution comparisons must notice.""" |
| 2 | import pathlib, subprocess, os, sys, hashlib, json |
| 3 | root=pathlib.Path(__file__).resolve().parents[1];os.chdir(root) |
| 4 | b=root/'build/controls';b.mkdir(exist_ok=True) |
| 5 | seed=os.environ.get('OMEGA_SEED','build/bootstrap/omc') |
| 6 | def run(cmd,data=b''): |
| 7 | env=dict(os.environ,ASAN_OPTIONS='detect_leaks=0:exitcode=86:abort_on_error=0') |
| 8 | return subprocess.run([str(x) for x in cmd],input=data,capture_output=True,timeout=20,env=env) |
| 9 | def native(name): |
| 10 | src=pathlib.Path(f'tests/fixtures/{name}.om').read_bytes() |
| 11 | p=run([seed],src);assert p.returncode==0 and p.stdout[:4]==b'\x7fELF' |
| 12 | path=b/'native';path.write_bytes(p.stdout);path.chmod(0o755) |
| 13 | return run([path]) |
| 14 | def result(p):return (p.returncode,p.stdout,p.stderr) |
| 15 | count=0; receipts=[] |
| 16 | for target,name,old,new in [ |
| 17 | ('c','num_ulong_add','(a+b)&mask','(a-b)&mask'), |
| 18 | ('python','num_ulong_add','elif op == 1: z=x+y','elif op == 1: z=x-y'), |
| 19 | ('c','t_bounds','if(i>=n)o_trap(3);unsigned','unsigned'), |
| 20 | ('python','t_bounds','if i>=n: o_trap(3)','if False: o_trap(3)'), |
| 21 | ('c','order_calls','o_op(2, 5, _a,_b)','o_op(2, 5, _b,_a)'), |
| 22 | ('python','order_calls','o_op(2, 5, v1, v0)','o_op(2, 5, v0, v1)'), |
| 23 | ]: |
| 24 | expected=native(name) |
| 25 | p=run(['tools/bridge',target,f'tests/fixtures/{name}.om']);assert p.returncode==0 |
| 26 | text=p.stdout.decode();assert old in text,(target,name,'mutation did nothing') |
| 27 | text=text.replace(old,new) |
| 28 | path=b/('mut.c' if target=='c' else 'mut.py');path.write_text(text) |
| 29 | if target=='c': |
| 30 | exe=b/'mut';cc=run(['gcc','-std=gnu11','-O1','-fsanitize=address,undefined','-fno-sanitize-recover=all',path,'-o',exe]);assert cc.returncode==0,cc.stderr |
| 31 | got=run([exe]) |
| 32 | else: got=run([sys.executable,path]) |
| 33 | assert result(expected)!=result(got),('undetected mutant',target,name) |
| 34 | count+=1;print('PASS detected',target,name,'native_exit',expected.returncode,'mutant_exit',got.returncode) |
| 35 | receipts.append(dict(target=target,case=name,native_exit=expected.returncode,mutant_exit=got.returncode,mutant_sha256=hashlib.sha256(text.encode()).hexdigest(),stderr=got.stderr.decode(errors='replace').replace(str(root),'<repo>')[:1500])) |
| 36 | # Positive comparison cannot silently accept an empty target artifact or wrong output. |
| 37 | a=run(['tools/bridge','c','tests/fixtures/r5b_fwd.om']);z=run(['tools/bridge','c','tests/fixtures/r5b_fwd.om']) |
| 38 | assert a.returncode==0 and a.stdout==z.stdout and a.stdout |
| 39 | assert a.stdout != b'' |
| 40 | count+=1;print('PASS deterministic nonempty output') |
| 41 | pathlib.Path('build/control-receipts.json').write_text(json.dumps(receipts,indent=2)+'\n') |
| 42 | print(f'control cases={count} failures=0 RC=0') |