Files
aji/tests/hosted/source_map.py
T
dev 57603860df cli: add safe owning numeric-key maps
Register std.map with bounded U64Map<V>, exact key equality and explicit
allocation/growth. Preserve returned ownership, old slots on reserve failure
and original-authority refunds; support Copy values, owners and resources.

Cover collision/tombstone/wrap behavior, independent dictionary traces,
quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI.
Publish the API, roadmap and measured documentation cost. General key
hashing/equality remains Q-037; numeric maps do not satisfy that gate.
2026-10-07 08:36:23 +03:00

74 lines
3.3 KiB
Python

#!/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")