- air: cap<K> and built-in enums (cap_error) as types; enum.switch over built-in enums; cap.narrow terminator with ok/err targets; intrinsic syntax (registry still empty: E0004); registries for effects, capability kinds and built-in enums, synced with reference docs - verifier: effect stage (E3101, flat, direct callees); cap.narrow form check (E3202) and ok/err parameter rules (E5002, E1001); capabilities are copyable, not zeroable, not comparable; sizes cap 8, built-in tag - interp: host authority table; @main instantiation from grants by parameter name with not_instantiated reasons; capabilities render as opaque - CLI: --grant P=KIND[:SCOPE], exit code 6; runner: grant: and not_instantiated: headers, pending only for unsupported features - conformance: 18 Phase 2 tests; 4000 mutants without a crash
337 lines
14 KiB
Zig
337 lines
14 KiB
Zig
//! Conformance suite runner. Format: tests/conformance/README.md.
|
|
//!
|
|
//! usage: aji-conformance <dir> [--json] [--filter <substring>]
|
|
//!
|
|
//! Exit status is 1 if any test FAILs or has a malformed header. PENDING
|
|
//! (pipeline stage not implemented yet) does not fail the run.
|
|
|
|
const std = @import("std");
|
|
const aji = @import("aji");
|
|
|
|
const Expect = union(enum) {
|
|
accept,
|
|
/// Exact multiset of error codes, sorted.
|
|
reject: []const []const u8,
|
|
/// Verify, run `@main` and compare the run record.
|
|
run: Run,
|
|
|
|
const Run = union(enum) {
|
|
/// Expected `value` of a `returned` record, as canonical JSON.
|
|
result: []const u8,
|
|
/// Expected trap code.
|
|
trap: []const u8,
|
|
/// Expected `reason` of a `not_instantiated` record.
|
|
not_instantiated: []const u8,
|
|
};
|
|
};
|
|
|
|
const Outcome = enum { pass, fail, pending };
|
|
|
|
/// Fuel for run tests without a `fuel:` header (spec 11 §7.1: conformance
|
|
/// runs always use a limit).
|
|
const default_fuel: u64 = 1_000_000;
|
|
|
|
const Header = struct {
|
|
expect: Expect,
|
|
fuel: u64,
|
|
grants: []const aji.interp.Grant,
|
|
phase: u8,
|
|
spec: []const u8,
|
|
};
|
|
|
|
pub fn main(init: std.process.Init) !void {
|
|
const arena = init.arena.allocator();
|
|
const io = init.io;
|
|
const args = try init.minimal.args.toSlice(arena);
|
|
|
|
if (args.len < 2) {
|
|
std.debug.print("usage: aji-conformance <dir> [--json] [--filter <substring>]\n", .{});
|
|
std.process.exit(2);
|
|
}
|
|
const root = args[1];
|
|
var json = false;
|
|
var filter: []const u8 = "";
|
|
var i: usize = 2;
|
|
while (i < args.len) : (i += 1) {
|
|
if (std.mem.eql(u8, args[i], "--json")) {
|
|
json = true;
|
|
} else if (std.mem.eql(u8, args[i], "--filter") and i + 1 < args.len) {
|
|
i += 1;
|
|
filter = args[i];
|
|
} else {
|
|
std.debug.print("aji-conformance: unknown argument {s}\n", .{args[i]});
|
|
std.process.exit(2);
|
|
}
|
|
}
|
|
|
|
// Collect and sort paths so that output order is deterministic.
|
|
var paths: std.ArrayList([]const u8) = .empty;
|
|
var dir = try std.Io.Dir.cwd().openDir(io, root, .{ .iterate = true });
|
|
defer dir.close(io);
|
|
var walker = try dir.walk(arena);
|
|
defer walker.deinit();
|
|
while (try walker.next(io)) |entry| {
|
|
if (entry.kind != .file or !std.mem.endsWith(u8, entry.path, ".air")) continue;
|
|
if (std.mem.indexOf(u8, entry.path, filter) == null) continue;
|
|
try paths.append(arena, try arena.dupe(u8, entry.path));
|
|
}
|
|
std.mem.sort([]const u8, paths.items, {}, lessThan);
|
|
|
|
var stdout_buf: [4096]u8 = undefined;
|
|
var stdout_fw: std.Io.File.Writer = .init(.stdout(), io, &stdout_buf);
|
|
const out = &stdout_fw.interface;
|
|
|
|
var counts = std.EnumArray(Outcome, usize).initFill(0);
|
|
for (paths.items) |rel| {
|
|
const source = try dir.readFileAlloc(io, rel, arena, .limited(16 << 20));
|
|
const display = try std.fs.path.join(arena, &.{ root, rel });
|
|
const result = try runOne(arena, display, source);
|
|
counts.getPtr(result.outcome).* += 1;
|
|
if (json) {
|
|
var s: std.json.Stringify = .{ .writer = out };
|
|
try s.write(.{ .@"test" = display, .outcome = @tagName(result.outcome), .detail = result.detail });
|
|
try out.writeByte('\n');
|
|
} else {
|
|
try out.print("{s:<8} {s}", .{ label(result.outcome), display });
|
|
if (result.detail.len != 0) try out.print(" ({s})", .{result.detail});
|
|
try out.writeByte('\n');
|
|
}
|
|
}
|
|
if (!json) {
|
|
try out.print("\n{d} passed, {d} failed, {d} pending\n", .{
|
|
counts.get(.pass), counts.get(.fail), counts.get(.pending),
|
|
});
|
|
}
|
|
try out.flush();
|
|
if (counts.get(.fail) != 0) std.process.exit(1);
|
|
}
|
|
|
|
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
|
return std.mem.lessThan(u8, a, b);
|
|
}
|
|
|
|
fn label(o: Outcome) []const u8 {
|
|
return switch (o) {
|
|
.pass => "PASS",
|
|
.fail => "FAIL",
|
|
.pending => "PENDING",
|
|
};
|
|
}
|
|
|
|
const Result = struct { outcome: Outcome, detail: []const u8 = "" };
|
|
|
|
fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Result {
|
|
const header = parseHeader(arena, source) catch |err|
|
|
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "bad header: {t}", .{err}) };
|
|
|
|
// A test is pending only while its phase is unfinished and it needs a
|
|
// feature this toolchain does not support yet.
|
|
if (header.phase > aji.implemented_phase) if (unsupportedFeature(source)) |name|
|
|
return .{ .outcome = .pending, .detail = try std.fmt.allocPrint(arena, "phase {d}: feature {s} not implemented", .{ header.phase, name }) };
|
|
|
|
// Blank header lines (keeping newlines) so spans in diagnostics stay accurate
|
|
// and the AIR grammar does not have to know about test metadata.
|
|
const body = try arena.dupe(u8, source);
|
|
var line_start: usize = 0;
|
|
while (line_start < body.len and body[line_start] == ';') {
|
|
const end = std.mem.indexOfScalarPos(u8, body, line_start, '\n') orelse body.len;
|
|
@memset(body[line_start..end], ' ');
|
|
line_start = end + 1;
|
|
}
|
|
|
|
const report = try aji.check.checkAir(arena, path, body);
|
|
if (report.status == .unimplemented)
|
|
return .{ .outcome = .pending, .detail = report.missing_stage };
|
|
|
|
var got: std.ArrayList([]const u8) = .empty;
|
|
for (report.diagnostics) |d| {
|
|
if (d.severity == .@"error") try got.append(arena, d.code.id);
|
|
}
|
|
std.mem.sort([]const u8, got.items, {}, lessThan);
|
|
|
|
switch (header.expect) {
|
|
.accept => if (report.status == .accepted) return .{ .outcome = .pass },
|
|
.reject => |want| if (report.status == .rejected and eqlCodes(want, got.items)) return .{ .outcome = .pass },
|
|
.run => |want| if (report.status == .accepted) return runModule(arena, report.module.?, want, header),
|
|
}
|
|
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "expected {f}, got {t} {s}", .{
|
|
fmtExpect(header.expect), report.status, try std.mem.join(arena, " ", got.items),
|
|
}) };
|
|
}
|
|
|
|
/// Runs the module twice and requires byte-identical run records (spec 11)
|
|
/// before comparing against the expectation.
|
|
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run, header: Header) !Result {
|
|
var records: [2][]const u8 = undefined;
|
|
var outcome: aji.interp.Outcome = undefined;
|
|
for (&records) |*rec| {
|
|
outcome = aji.interp.run(arena, module, "main", .{ .fuel = header.fuel, .grants = header.grants }) catch |err| switch (err) {
|
|
error.EntryNotFound => return .{ .outcome = .fail, .detail = "module has no @main whose parameters are all capabilities" },
|
|
error.DuplicateGrant => return .{ .outcome = .fail, .detail = "a parameter is granted more than once" },
|
|
else => |e| return e,
|
|
};
|
|
var w: std.Io.Writer.Allocating = .init(arena);
|
|
try aji.interp.writeOutcome(module, outcome, &w.writer);
|
|
rec.* = w.written();
|
|
}
|
|
if (!std.mem.eql(u8, records[0], records[1]))
|
|
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "nondeterministic: {s} vs {s}", .{ records[0], records[1] }) };
|
|
|
|
const ok = switch (want) {
|
|
.trap => |code| outcome == .trapped and std.mem.eql(u8, outcome.trapped.code.id, code),
|
|
.result => |json| outcome == .returned and std.mem.eql(u8, json, try valueJson(arena, module, outcome.returned)),
|
|
.not_instantiated => |reason| outcome == .not_instantiated and std.mem.eql(u8, outcome.not_instantiated.reason, reason),
|
|
};
|
|
if (ok) return .{ .outcome = .pass };
|
|
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "got {s}", .{records[0]}) };
|
|
}
|
|
|
|
fn valueJson(arena: std.mem.Allocator, module: aji.air.Module, v: aji.interp.Value) ![]const u8 {
|
|
var w: std.Io.Writer.Allocating = .init(arena);
|
|
var s: std.json.Stringify = .{ .writer = &w.writer };
|
|
try aji.interp.writeValue(module, v, &s);
|
|
return w.written();
|
|
}
|
|
|
|
/// First feature of the module header that this toolchain does not support.
|
|
fn unsupportedFeature(source: []const u8) ?[]const u8 {
|
|
var lines = std.mem.splitScalar(u8, source, '\n');
|
|
while (lines.next()) |line| {
|
|
if (!std.mem.startsWith(u8, line, "features [")) continue;
|
|
const close = std.mem.indexOfScalar(u8, line, ']') orelse return null;
|
|
var names = std.mem.tokenizeAny(u8, line["features [".len..close], ", ");
|
|
while (names.next()) |name| {
|
|
const known = for (aji.air.features.supported) |s| {
|
|
if (std.mem.eql(u8, s, name)) break true;
|
|
} else false;
|
|
if (!known) return name;
|
|
}
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
fn isKey(s: []const u8) bool {
|
|
if (s.len == 0) return false;
|
|
for (s) |ch| if (!std.ascii.isLower(ch) and ch != '_') return false;
|
|
return true;
|
|
}
|
|
|
|
fn eqlCodes(a: []const []const u8, b: []const []const u8) bool {
|
|
if (a.len != b.len) return false;
|
|
for (a, b) |x, y| if (!std.mem.eql(u8, x, y)) return false;
|
|
return true;
|
|
}
|
|
|
|
fn fmtExpect(e: Expect) std.fmt.Alt(Expect, formatExpect) {
|
|
return .{ .data = e };
|
|
}
|
|
|
|
fn formatExpect(e: Expect, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
|
switch (e) {
|
|
.accept => try w.writeAll("accept"),
|
|
.reject => |codes| {
|
|
try w.writeAll("reject");
|
|
for (codes) |c| try w.print(" {s}", .{c});
|
|
},
|
|
.run => |r| switch (r) {
|
|
.result => |j| try w.print("run returning {s}", .{j}),
|
|
.trap => |c| try w.print("run trapping {s}", .{c}),
|
|
.not_instantiated => |reason| try w.print("not_instantiated ({s})", .{reason}),
|
|
},
|
|
}
|
|
}
|
|
|
|
const HeaderError = error{ BadGrant, MissingExpect, MissingPhase, MissingSpec, MissingRunResult, UnknownKey, UnknownCode, BadExpect, OutOfMemory, InvalidCharacter, Overflow };
|
|
|
|
fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header {
|
|
var expect: ?Expect = null;
|
|
var run_result: ?Expect.Run = null;
|
|
var phase: ?u8 = null;
|
|
var fuel: u64 = default_fuel;
|
|
var grants: std.ArrayList(aji.interp.Grant) = .empty;
|
|
var spec: ?[]const u8 = null;
|
|
|
|
var lines = std.mem.splitScalar(u8, source, '\n');
|
|
while (lines.next()) |line| {
|
|
if (!std.mem.startsWith(u8, line, ";")) break;
|
|
const content = std.mem.trim(u8, line[1..], " \t\r");
|
|
const colon = std.mem.indexOfScalar(u8, content, ':') orelse continue; // free-form comment
|
|
const key = content[0..colon];
|
|
if (!isKey(key)) continue; // free-form comment containing a colon
|
|
const value = std.mem.trim(u8, content[colon + 1 ..], " \t");
|
|
if (std.mem.eql(u8, key, "expect")) {
|
|
var it = std.mem.tokenizeScalar(u8, value, ' ');
|
|
const verb = it.next() orelse return error.BadExpect;
|
|
if (std.mem.eql(u8, verb, "run")) {
|
|
if (it.next() != null) return error.BadExpect;
|
|
expect = .{ .run = undefined };
|
|
} else if (std.mem.eql(u8, verb, "accept")) {
|
|
if (it.next() != null) return error.BadExpect;
|
|
expect = .accept;
|
|
} else if (std.mem.eql(u8, verb, "reject")) {
|
|
var codes: std.ArrayList([]const u8) = .empty;
|
|
while (it.next()) |c| {
|
|
if (aji.diag.lookup(c) == null) return error.UnknownCode;
|
|
try codes.append(arena, c);
|
|
}
|
|
if (codes.items.len == 0) return error.BadExpect;
|
|
std.mem.sort([]const u8, codes.items, {}, lessThan);
|
|
expect = .{ .reject = codes.items };
|
|
} else return error.BadExpect;
|
|
} else if (std.mem.eql(u8, key, "result")) {
|
|
if (run_result != null) return error.BadExpect;
|
|
run_result = .{ .result = value };
|
|
} else if (std.mem.eql(u8, key, "trap")) {
|
|
if (run_result != null) return error.BadExpect;
|
|
const code = aji.diag.lookup(value) orelse return error.UnknownCode;
|
|
if (code.id[0] != 'T') return error.UnknownCode;
|
|
run_result = .{ .trap = value };
|
|
} else if (std.mem.eql(u8, key, "not_instantiated")) {
|
|
if (run_result != null) return error.BadExpect;
|
|
run_result = .{ .not_instantiated = value };
|
|
} else if (std.mem.eql(u8, key, "grant")) {
|
|
const eq = std.mem.indexOfScalar(u8, value, '=') orelse return error.BadGrant;
|
|
const rest = value[eq + 1 ..];
|
|
const sep = std.mem.indexOfScalar(u8, rest, ':');
|
|
try grants.append(arena, .{
|
|
.parameter = value[0..eq],
|
|
.kind = if (sep) |c| rest[0..c] else rest,
|
|
.scope = if (sep) |c| rest[c + 1 ..] else null,
|
|
});
|
|
} else if (std.mem.eql(u8, key, "fuel")) {
|
|
fuel = try std.fmt.parseInt(u64, value, 10);
|
|
} else if (std.mem.eql(u8, key, "phase")) {
|
|
phase = try std.fmt.parseInt(u8, value, 10);
|
|
} else if (std.mem.eql(u8, key, "spec")) {
|
|
spec = value;
|
|
} else return error.UnknownKey;
|
|
}
|
|
if (expect) |*e| switch (e.*) {
|
|
.run => e.* = .{ .run = run_result orelse return error.MissingRunResult },
|
|
else => if (run_result != null) return error.BadExpect,
|
|
};
|
|
return .{
|
|
.expect = expect orelse return error.MissingExpect,
|
|
.fuel = fuel,
|
|
.grants = grants.items,
|
|
.phase = phase orelse return error.MissingPhase,
|
|
.spec = spec orelse return error.MissingSpec,
|
|
};
|
|
}
|
|
|
|
test parseHeader {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const h = try parseHeader(arena_state.allocator(),
|
|
\\; Use of %x without a definition.
|
|
\\; expect: reject E5001
|
|
\\; phase: 0
|
|
\\; spec: docs/spec/07-air.md
|
|
\\fn @f() -> u64
|
|
);
|
|
try std.testing.expectEqual(@as(u8, 0), h.phase);
|
|
try std.testing.expectEqualStrings("E5001", h.expect.reject[0]);
|
|
try std.testing.expectError(error.UnknownCode, parseHeader(arena_state.allocator(), "; expect: reject E9999\n"));
|
|
}
|