#!/usr/bin/env python3 """Numeric owning map: source/AIR parity, profiles, packages and diagnostics.""" import json from pathlib import Path import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / "zig-out/bin/aji" FIXTURES = REPO / "tests/conformance/source/map" checks = 0 def cli(*args, code=0): global checks result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True) assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr) checks += 1 return result.stdout def returned(path, *grants): arguments = [argument for grant in grants for argument in ("--grant", grant)] record = json.loads(cli("run", "--fuel", "2000000", *arguments, path)) assert record["status"] == "returned" and record["value"] == 42, record return record example = REPO / "examples/std/map.aji" returned(example, "heap=mem.alloc:256") with tempfile.TemporaryDirectory(prefix="aji-map-") as directory: root = Path(directory) air = root / "result.air" for name, grants in ( ("copy", ("heap=mem.alloc:1024",)), ("owner", ("heap=mem.alloc:1024",)), ("zero", ("heap=mem.alloc:8",)), ("allocation", ("heap=mem.alloc:35",)), ("collision", ("heap=mem.alloc:1024",)), ("tombstones", ("heap=mem.alloc:1024",)), ("reserve_failure", ("heap=mem.alloc:195",)), ("reserve_peak", ("heap=mem.alloc:196",)), ("reserve_authority", ("first=mem.alloc:72", "second=mem.alloc:108", "items=mem.alloc:8")), ("model", ("heap=mem.alloc:1024",)), ("model_owner", ("heap=mem.alloc:1024",)), ("loan_join", ("heap=mem.alloc:44",)), ("sender", ("heap=mem.alloc:256",)), ): source = FIXTURES / f"{name}.aji" canonical = cli("lower", source) assert cli("lower", source) == canonical assert "unsafe" not in canonical air.write_text(canonical) cli("check", air) assert returned(source, *grants) == returned(air, *grants) for name, code in (("get_owner", "E7005"), ("missing_move", "E7003"), ("loan", "E4101"), ("alias", "E2101"), ("not_share", "E4002")): diagnostics = cli("check", FIXTURES / f"{name}.aji", code=1) assert [json.loads(line)["code"] for line in diagnostics.splitlines()] == [code] assert cli("check", "--report", "unsafe", example) == "" reordered = root / "reordered.aji" reordered.write_text(example.read_text().replace("import std.map;\nimport std.option;", "import std.option;\nimport std.map;")) assert cli("lower", reordered) == cli("lower", example) cli("run", "--profile", "hosted", "--grant", "heap=mem.alloc:256", "--result-file", root / "hosted.json", example) record = json.loads((root / "hosted.json").read_text()) assert record["status"] == "returned" and record["value"] == 42, record (root / "main.aji").write_text(example.read_text()) (root / "aji.toml").write_text('[package]\nname = "map"\nversion = "1"\nentry = "main.aji"\n[capabilities]\nheap = "mem.alloc:256"\n') record = json.loads(cli("run", "--manifest", root / "aji.toml")) assert record["status"] == "returned" and record["value"] == 42, record print(f"source map: {checks} checks passed")