omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / quantity / test / run.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Native contract test for om/quantity. Python compiles and compares; the checks are Omega.""" |
| 3 | import subprocess |
| 4 | import sys |
| 5 | from pathlib import Path |
| 6 | |
| 7 | ROOT = Path(__file__).resolve().parents[3] |
| 8 | UNIT = ROOT / "om/quantity" |
| 9 | OUT = ROOT / "build/quantity" |
| 10 | COMPILER = subprocess.check_output([str(ROOT / "tools/omc.sh")], text=True).strip() |
| 11 | |
| 12 | source = (UNIT / "quantity.om").read_text() + "\n" + (UNIT / "test/driver.om").read_text() |
| 13 | result = subprocess.run([COMPILER], input=source.encode(), capture_output=True) |
| 14 | if result.returncode: |
| 15 | OUT.mkdir(parents=True, exist_ok=True) |
| 16 | (OUT / "driver.om").write_text(source) |
| 17 | sys.exit(f"quantity: compile failed: {result.stderr.decode()}") |
| 18 | OUT.mkdir(parents=True, exist_ok=True) |
| 19 | binary = OUT / "driver" |
| 20 | binary.write_bytes(result.stdout) |
| 21 | binary.chmod(0o700) |
| 22 | run = subprocess.run([str(binary)], capture_output=True, timeout=120) |
| 23 | if run.returncode != 0: |
| 24 | sys.exit(f"quantity: driver exit {run.returncode}: {run.stderr.decode()}") |
| 25 | if run.stdout != bytes([0]): |
| 26 | sys.exit(f"quantity: contract case {run.stdout[0]} failed (0 means every case passed)") |
| 27 | print("PASS quantity: checked-quantity contract cases, refusal leaves output unchanged") |