omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / omc / tests / size-record.py
| 1 | #!/usr/bin/env python3 |
| 2 | """External oracle for the seed-born source/binary size record.""" |
| 3 | import hashlib |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import tempfile |
| 8 | |
| 9 | root = Path(__file__).resolve().parents[1] |
| 10 | tool = Path(sys.argv[1]).resolve() |
| 11 | binary = (root.parent / "out" / "omc").read_bytes() |
| 12 | source = (root / "omc.om").read_bytes() |
| 13 | passed = 0 |
| 14 | |
| 15 | with tempfile.TemporaryDirectory(prefix="omega-size-record-") as tmp: |
| 16 | tmp = Path(tmp) |
| 17 | src, exe = tmp / "source.om", tmp / "compiler" |
| 18 | def run(s, b): |
| 19 | src.write_bytes(s) |
| 20 | exe.write_bytes(b) |
| 21 | return subprocess.run([str(tool), str(src), str(exe)], |
| 22 | capture_output=True, timeout=20) |
| 23 | |
| 24 | for name, s, b in [ |
| 25 | ("current", source, binary), |
| 26 | ("source-lines", b"fn a\n note fn ignored\nfn b\nfn c", binary), |
| 27 | ("binary-content", source, binary[:-1] + bytes([binary[-1] ^ 1])), |
| 28 | ]: |
| 29 | p = run(s, b) |
| 30 | digest = hashlib.sha256(b).hexdigest() |
| 31 | expected = "\t".join([ |
| 32 | "seeded-" + digest[:12], "Generated from seeded source and executable", |
| 33 | str(s.count(b"\n")), |
| 34 | str(sum(line.startswith(b"fn ") for line in s.split(b"\n"))), |
| 35 | str((len(b) - 176) // 4), digest, |
| 36 | ]).encode() + b"\n" |
| 37 | assert p.returncode == 0 and p.stdout == expected, (name, p.returncode, p.stdout, expected) |
| 38 | passed += 1 |
| 39 | |
| 40 | for name, b in [ |
| 41 | ("empty", b""), ("short", binary[:175]), |
| 42 | ("unaligned", binary[:-1]), ("magic", b"BAD!" + binary[4:]), |
| 43 | ]: |
| 44 | p = run(source, b) |
| 45 | assert p.returncode == 67 and not p.stdout, (name, p.returncode, p.stdout) |
| 46 | passed += 1 |
| 47 | p = subprocess.run([str(tool)], capture_output=True, timeout=20) |
| 48 | assert p.returncode == 60 and not p.stdout |
| 49 | passed += 1 |
| 50 | |
| 51 | print(f"SIZE_RECORD PASS cases={passed}") |