Files
ww/test/xmod/collide_test.ww
Hojun-Cho 57675431e2 test: port the xmod collide observers to ww; birth test/xmod
test/xmod/collide_test.ww replaces 989_fnptrcollide_run.c and
989_barefn_collide_run.c with every assertion preserved (build-reject
both stages; run-exit 9 + exactly-one TEXT main.run/aa.run + cs==ww).
Makefile grows the XMOD_WW_TESTS block mirroring SEP_WW_TESTS, wired
into test-compiler.
2026-08-08 14:34:53 +09:00

135 lines
4.7 KiB
Plaintext

package collide_test;
// Cross-module leaf-name collision observers on both driver stages.
// Ports of the retired native carriers test/wcc/989_fnptrcollide_run.c
// and 989_barefn_collide_run.c; every assertion preserved.
//
// fnptrcollide (#14 F7-c7) — a data global `slot: i64` whose LEAF
// collides with the imported fn bar.slot must FAIL to build on BOTH
// stages: type-keyed nodefnptr refuses to fold `&slot` into the fn's
// TEXT reloc. Pre-fix wwstage was name-keyed and silently BUILT it
// (a DATAR to the fn symbol — cat-A: cs loud-fails, ww builds). The
// collision REQUIRES the import path (imported fn leaf vs local data
// global), so a single-file fixture cannot express it.
//
// barefn (#84/#24a) — a `package main;` root `fn run` coexists with
// imported aa.run: build+run exit 9 on BOTH stages (the user's
// main.run wins, never aa.run=5); the concatenated
// __root.s+aa.s (fixed #93 sep order) carries EXACTLY ONE
// `TEXT main.run,` and EXACTLY ONE `TEXT aa.run,` (distinct symbols,
// no #31-class w6l-tolerated duplicate, no silently-dead bare fn);
// cs==ww byte-for-byte (rule 10).
//
// Dropped C machinery, not assertions: the ww_ww-absent skip gate
// (the Make target declares both drivers), the shell `timeout 240`
// (runcommand's deadline carries it), and the unlink/rmdir accounting
// (testenv.clean asserts the removal).
import os;
import os.exec;
import strings;
import testenv;
import time;
fn fail(label: str, why: str) void = {
let m: str = strings.concat("collide FAIL: ", label, " -- ", why,
"\n");
os.write(2, m.ptr, m.len: u64);
assert(false);
};
fn tmo() time.duration = {
return (240i64 * (time.second: i64)): time.duration;
};
// -1 encodes an abnormal (non-EXIT) termination, never a valid code.
fn runcode(dir: str, name: str, argv: []str) i32 = {
let co: testenv.commandout;
testenv.runcommand(dir, dir, name, argv, tmo(), &co);
if (co.termination != exec.termination.EXIT) { return -1; };
return co.code;
};
@test fn fnptrcollide() void = {
let drvs: []str = ["ww", "ww_ww"];
let i: i32 = 0;
for (i < 2) {
let td: str = testenv.fresh();
assert(os.mkdir(strings.concat(td, "/bar"), 493) == 0);
testenv.writefile(strings.concat(td, "/bar/bar.ww"),
strings.concat(
"package bar;\n",
"export fn slot() i64 = { return 99; };\n"));
testenv.writefile(strings.concat(td, "/main.ww"), strings.concat(
"package main;\n",
"import bar;\n",
"let slot: i64 = 7;\n",
"let fp: *i64 = &slot;\n",
"export fn main() int = { return (*fp): int; };\n"));
let av: []str = [testenv.driver(drvs[i]), "build", "-I", "bar",
"main.ww"];
if (runcode(td, strings.concat("build_", drvs[i]), av) == 0) {
fail("fnptrcollide", strings.concat(drvs[i], " built ok, ",
"expected the leaf-name collision to be rejected (#14 -- ",
"&slot mis-folded to the fn TEXT reloc)"));
};
testenv.clean(td);
i += 1;
};
};
// __root.s then aa.s: the carrier's fixed #93 sep concat order.
fn catfixed(stem: str) str = {
return strings.concat(
testenv.readfile(strings.concat(stem, ".sepwork/__root.s")),
testenv.readfile(strings.concat(stem, ".sepwork/aa.s")));
};
// column-0 anchored label lines; the '\n' prepend counts a file-leading
// label too.
fn textcount(s: str, sym: str) i32 = {
return testenv.occurrences(strings.concat("\n", s),
strings.concat("\nTEXT ", sym, ","));
};
@test fn barefn() void = {
let td: str = testenv.fresh();
assert(os.mkdir(strings.concat(td, "/aa"), 493) == 0);
testenv.writefile(strings.concat(td, "/aa/aa.ww"), strings.concat(
"package aa;\n",
"export fn run() i32 = { return 5; };\n"));
testenv.writefile(strings.concat(td, "/root.ww"), strings.concat(
"package main;\n",
"import aa;\n",
"fn run() i32 = { return 9; };\n",
"export fn main() i32 = { return run(); };\n"));
let drvs: []str = ["ww", "ww_ww"];
let tags: []str = ["cs", "ww"];
let asms: []str = ["", ""];
let s: i32 = 0;
for (s < 2) {
let stem: str = strings.concat(td, "/prog.", tags[s]);
let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem,
"-I", td, strings.concat(td, "/root.ww")];
if (runcode(td, strings.concat("build_", tags[s]), av) != 0) {
fail("barefn", strings.concat(drvs[s], " build failed"));
};
let rav: []str = [stem];
if (runcode(td, strings.concat("run_", tags[s]), rav) != 9) {
fail("barefn", strings.concat(drvs[s], " exit != 9 ",
"(user main.run must win, not aa.run=5)"));
};
asms[s] = catfixed(stem);
if (textcount(asms[s], "main.run") != 1
|| textcount(asms[s], "aa.run") != 1) {
fail("barefn", strings.concat(drvs[s], " TEXT label counts ",
"!= 1/1 (user main.run distinct from import, no dup)"));
};
s += 1;
};
if (!testenv.same(asms[0], asms[1])) {
fail("barefn", "cs .s != ww .s (rule 10)");
};
testenv.clean(td);
};