From aab9ec5e5fc4dd82f99eb6bbcf65dc65caef0f1c Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 8 Aug 2026 15:02:43 +0900 Subject: [PATCH] test: birth test/misc + port the arrglob DATAW observer; retire 689_arrglob_nodup_run test/misc hosts the residual mixed-shape observers (divergence pins, stamp/stderr probes, .wwi trees) on testenv, wired into test-compiler beside the sep/xmod/asm families; its rule declares both full per-stage toolchains plus the wwdump twins for the coming files. The 689 port keeps the both-driver run-exit-6 backstop and the per-stage absolute DATAW row count (linker dedup and byte-id sweeps are both blind to a symmetric double-emit). --- Makefile | 22 +++- test/misc/arrglob_test.ww | 93 ++++++++++++++++ test/wcc/689_arrglob_nodup_run.c | 177 ------------------------------- 3 files changed, 114 insertions(+), 178 deletions(-) create mode 100644 test/misc/arrglob_test.ww delete mode 100644 test/wcc/689_arrglob_nodup_run.c diff --git a/Makefile b/Makefile index 5dc836fc..fe2bc067 100644 --- a/Makefile +++ b/Makefile @@ -400,6 +400,15 @@ ASM_WW_TARGETS = $(ASM_WW_TESTS:%=wwtest/%) OBJECT_WW_TESTS = test/object/elfobj_test.ww test/object/link_test.ww \ test/object/archive_test.ww test/object/asmgate_test.ww OBJECT_WW_TARGETS = $(OBJECT_WW_TESTS:%=wwtest/%) + +# Ww-native misc observers: single-file ww tests under test/misc/ on +# the test/testenv helper package, porting the residual mixed-shape C +# carriers (per-stage divergence pins, wwstage stamp-diagnostic and +# run-stderr probes, .wwi round-trip trees, tool-dump AST proofs, +# cstage-only rejects). Compiler/driver gates like test/sep: they run +# under test-compiler. +MISC_WW_TESTS = test/misc/arrglob_test.ww +MISC_WW_TARGETS = $(MISC_WW_TESTS:%=wwtest/%) BOOTSTRAP_WRAPPER_SOURCES = test/wcc/950_selfcheck.c \ test/wcc/991_w6a_ww.c \ test/wcc/992_w6l_ww.c test/wcc/993_ww_ww.c \ @@ -486,7 +495,7 @@ test-compiler-smoke: $(WWFIXTURE_BIN) $(BIN)/w6c_ww $(BIN)/w6a_ww \ # ww-native sep observers that replaced the 989_sep* carriers. test-compiler: $(WWFIXTURE_BIN) $(WRAPPER_TOOLS) $(COMPILER_WRAPPER_BINS) \ $(SEP_WW_TARGETS) $(XMOD_WW_TARGETS) $(ASM_WW_TARGETS) \ - $(OBJECT_WW_TARGETS) + $(OBJECT_WW_TARGETS) $(MISC_WW_TARGETS) @$(CURDIR)/$(WWFIXTURE_BIN) -j $(JOBS) @set -e; for t in $(COMPILER_WRAPPER_BINS); do \ echo "compiler artifact $$t"; BIN=$(CURDIR)/$(BIN) $(CURDIR)/$$t; \ @@ -568,6 +577,17 @@ $(OBJECT_WW_TARGETS): wwtest/%: $(BIN)/ww $(BIN)/w6c $(BIN)/w6a \ -w $(WWBUILD)/wwtest/$(subst /,_,$*) \ -I $(CURDIR)/test $* +# The misc observers drive both driver stages end-to-end plus the +# wwdump twins, so both full per-stage toolchains are prerequisites. +$(MISC_WW_TARGETS): wwtest/%: $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww \ + $(BIN)/w6l_ww $(BIN)/wwdump $(BIN)/wwdump_ww + @echo "ww test $*" + @mkdir -p $(WWBUILD)/wwtest/$(subst /,_,$*) + @WW_TEST_REPO=$(CURDIR) $(BIN)/ww test \ + -w $(WWBUILD)/wwtest/$(subst /,_,$*) \ + -I $(CURDIR)/test $* + test-lang: $(LANG_TEST_TARGETS) test-library: $(LIBRARY_TEST_TARGETS) $(BIN)/w6c $(BIN)/w6c_ww \ diff --git a/test/misc/arrglob_test.ww b/test/misc/arrglob_test.ww new file mode 100644 index 00000000..becf8227 --- /dev/null +++ b/test/misc/arrglob_test.ww @@ -0,0 +1,93 @@ +package arrglob_test; + +// #12: a no-init module-level array global emits its zero-fill DATAW +// row EXACTLY ONCE. Port of the retired native carrier +// test/wcc/689_arrglob_nodup_run.c; every assertion preserved. The +// wwstage bug was a SIZE-keyed (sz == 24) str-fallback arm matching a +// [3]u64 global on top of the TY_ARRAY arm — two identical rows the +// linker deduped, so runtime and byte-id-only sweeps stayed blind to +// the absolute count: the per-stage count == 1 is the unowned claim. +// +// Legs: build+run exit 6 through both driver stages (correctness +// backstop), then per-stage `DATAW main.g(SB)` occurrence count == 1 +// over the emitted .s (both stages emitting 2 identical rows would +// pass a byte-id sweep). + +import os; +import os.exec; +import strings; +import testenv; +import time; + +fn fail(label: str, why: str) void = { + let m: str = strings.concat("arrglob 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; +}; + +fn src() str = { + return strings.concat( + "package main;\n", + "let g: [3]u64;\n", + "export fn main() i32 = {\n", + " g[0] = 1; g[1] = 2; g[2] = 3;\n", + " return (g[0] + g[1] + g[2]): i32;\n", + "};\n"); +}; + +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 rundrivers() void = { + let drvs: []str = ["ww", "ww_ww"]; + let s: i32 = 0; + for (s < 2) { + let td: str = testenv.fresh(); + testenv.writefile(strings.concat(td, "/t.ww"), src()); + let av: []str = [testenv.driver(drvs[s]), "build", "-o", + "t", "t.ww"]; + if (runcode(td, strings.concat("build_", drvs[s]), av) != 0) { + fail(drvs[s], "driver build failed"); + }; + let rav: []str = [strings.concat(td, "/t")]; + if (runcode(td, strings.concat("run_", drvs[s]), rav) != 6) { + fail(drvs[s], "run-exit != 6"); + }; + testenv.clean(td); + s += 1; + }; +}; + +@test fn datawcount() void = { + let tools: []str = ["w6c", "w6c_ww"]; + let s: i32 = 0; + for (s < 2) { + let td: str = testenv.fresh(); + testenv.writefile(strings.concat(td, "/t.ww"), src()); + let av: []str = [testenv.driver(tools[s]), "-o", + strings.concat(td, "/t.s"), + strings.concat(td, "/t.ww")]; + let co: testenv.commandout; + testenv.runcommand(td, td, tools[s], av, tmo(), &co); + if (co.termination != exec.termination.EXIT || co.code != 0) { + fail(tools[s], "compile failed"); + }; + let asm: str = testenv.readfile(strings.concat(td, "/t.s")); + let n: i32 = testenv.occurrences(asm, "DATAW main.g(SB)"); + if (n != 1) { + fail(tools[s], + "DATAW main.g rows != 1 (double-emit #12)"); + }; + testenv.clean(td); + s += 1; + }; +}; diff --git a/test/wcc/689_arrglob_nodup_run.c b/test/wcc/689_arrglob_nodup_run.c deleted file mode 100644 index 94a16d02..00000000 --- a/test/wcc/689_arrglob_nodup_run.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * 689_arrglob_nodup_run (#12) — a no-init module-level array global emits - * its zero-fill DATAW row EXACTLY ONCE, and cstage / wwstage agree - * byte-for-byte. - * - * THE BUG (wwstage only): emitletdataw's str-payload zero-fallback arm is - * SIZE-keyed (`sz == size(str)` == 24), not type-keyed. A no-init array - * global whose bytes sum to 24 (e.g. `let g: [3]u64;`) matched that arm AND - * the dedicated TY_ARRAY arm below → two identical `DATAW main.g` rows. - * cstage discriminates on the declared type kind (let_isstr) and emits one. - * Runtime reads the null bytes correctly either way (the linker keeps one - * symbol), so the divergence is byte-id-visible only — the asm-byte-id check - * is the real discriminator; the row-count + run rows pin absolute correctness. - * - * THE FIX: exclude arrays from the str-size arm (`&& !isarr8`), mirroring the - * isarr8 guard already on the sz==8 scalar arm. The emitarraydata path owns - * array globals. - * - * check | shape | want - * -------------------+-----------------------------+-------------- - * run | g[0]+g[1]+g[2], = 1+2+3 | 6 - * dataw_rows (ww) | count of `DATAW main.g` rows | 1 - * asm_byte_identical | w6c vs w6c_ww .s | identical - * Pre-fix: ww emitted 2 `DATAW main.g` rows; cs vs ww .s differed. - */ -#include -#include -#include -#include -#include -#include -#include - -static int -runwait(const char *cmd) -{ - int rc = system(cmd); - if (rc == -1) return -1; - if (WIFEXITED(rc)) return WEXITSTATUS(rc); - return -1; -} - -static const char *SRC = - "package main;\n" - "let g: [3]u64;\n" - "export fn main() i32 = {\n" - " g[0] = 1; g[1] = 2; g[2] = 3;\n" - " return (g[0] + g[1] + g[2]): i32;\n" - "};\n"; - -static int -run_driver(const char *driver) -{ - char tmpdir[64], src[128], outbin[128], scratch[160], rmcmd[192], cmd[1024]; - int result = -1, cleanfail = 0; - snprintf(tmpdir, sizeof tmpdir, "/tmp/agnd_%d_d", getpid()); - if (mkdir(tmpdir, 0755) != 0) return -1; - snprintf(src, sizeof src, "%s/agnd_%d.ww", tmpdir, getpid()); - snprintf(outbin, sizeof outbin, "%s/agnd_%d", tmpdir, getpid()); - snprintf(scratch, sizeof scratch, "%s.sepwork", outbin); - - FILE *f = fopen(src, "wb"); - if (!f) goto cleanup; - int writefail = fputs(SRC, f) == EOF; - if (fclose(f) != 0) writefail = 1; - if (writefail) goto cleanup; - - snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null", - driver, outbin, src); - if (runwait(cmd) != 0) goto cleanup; - result = runwait(outbin); -cleanup: - snprintf(rmcmd, sizeof rmcmd, "rm -rf -- %s", scratch); - if (runwait(rmcmd) != 0) cleanfail = 1; - if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1; - if (unlink(outbin) != 0 && errno != ENOENT) cleanfail = 1; - if (rmdir(tmpdir) != 0) cleanfail = 1; - if (cleanfail) { - fprintf(stderr, "arrglob_nodup: temporary cleanup failed\n"); - if (result == 6) return -1; - } - return result; -} - -/* emit .s via `tool`, return the count of `DATAW main.g(SB)` rows, or -1. */ -static int -dataw_rows(const char *bin, const char *tool) -{ - char tmpdir[80], src[96], asmf[96], cmd[1024]; - int n = -1, cleanfail = 0; - snprintf(tmpdir, sizeof tmpdir, "/tmp/agnd_asm_%d_%s", getpid(), tool); - if (mkdir(tmpdir, 0755) != 0) return -1; - snprintf(src, sizeof src, "%s/t.ww", tmpdir); - snprintf(asmf, sizeof asmf, "%s/t.s", tmpdir); - - FILE *f = fopen(src, "wb"); - if (!f) goto cleanup; - int writefail = fputs(SRC, f) == EOF; - if (fclose(f) != 0) writefail = 1; - if (writefail) goto cleanup; - - snprintf(cmd, sizeof cmd, "%s/%s -o %s %s 2>/dev/null", bin, tool, asmf, src); - if (runwait(cmd) != 0) goto cleanup; - - FILE *a = fopen(asmf, "rb"); - n = 0; - if (a) { - char line[512]; - while (fgets(line, sizeof line, a)) - if (strncmp(line, "DATAW main.g(SB)", 16) == 0) n++; - fclose(a); - } else n = -1; -cleanup: - if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1; - if (unlink(asmf) != 0 && errno != ENOENT) cleanfail = 1; - if (rmdir(tmpdir) != 0) cleanfail = 1; - if (cleanfail) { - fprintf(stderr, "arrglob_nodup[%s]: temporary cleanup failed\n", tool); - if (n == 1) return -1; - } - return n; -} - -int -main(void) -{ - const char *bin = getenv("BIN"); - if (!bin) bin = "out/bin"; - char absbin[1024]; - if (bin[0] != '/') { - char cwd[1024]; - if (getcwd(cwd, sizeof cwd) == NULL) return 1; - snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); - bin = absbin; - } - - char cdrv[1024], wdrv[1024]; - snprintf(cdrv, sizeof cdrv, "%s/ww", bin); - snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); - int have_ww = (access(wdrv, X_OK) == 0); - - int fail = 0, total = 0; - - /* run correctness, both drivers */ - total++; - if (run_driver(cdrv) != 6) { - fprintf(stderr, "arrglob_nodup[cstage]: run != 6\n"); fail++; - } - if (have_ww) { - total++; - if (run_driver(wdrv) != 6) { - fprintf(stderr, "arrglob_nodup[wwstage]: run != 6\n"); fail++; - } - } - - /* exactly one DATAW row per stage */ - total++; - if (dataw_rows(bin, "w6c") != 1) { - fprintf(stderr, "arrglob_nodup[w6c]: DATAW main.g rows != 1\n"); fail++; - } - if (access(wdrv, X_OK) == 0) { - total++; - int r = dataw_rows(bin, "w6c_ww"); - if (r != 1) { - fprintf(stderr, "arrglob_nodup[w6c_ww]: DATAW main.g rows = %d " - "(want 1 — double-emit #12)\n", r); - fail++; - } - } - - if (fail) { - fprintf(stderr, "arrglob_nodup_run: %d/%d checks failed\n", fail, total); - return 1; - } - printf("arrglob_nodup_run: %d/%d ok\n", total, total); - return 0; -}