Files
ww/test/wcc/963_sort_run.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

188 lines
6.1 KiB
C

/*
* 963_sort_run — runtime proof of lib/sort's search + lbisect + rbisect
* (faithful port of ref/hare/sort/{search,bisect}.ha). The three rows
* mirror the three relevant @test fns in ref/hare/sort/+test.ha
* (search:42-51, lbisect:8-23, rbisect:25-40), instantiated on a
* concrete []i32 with a real cmpfunc.
*
* lib/sort rides the opaque type-erasure surface proven by 962
* (slice->[]opaque, *opaque<->*u8 reinterpret, uintptr byte stride). The
* functions are exercised only here — they are dead in the bootstrap, so
* no 990-997 gate touches them; this executed-and-checked probe is their
* coverage (per the bootstrap-coverage discipline).
*
* The comparator binds its derefs to locals (`let va = *pa; if (va <
* vb)`) rather than Hare's inline `if (*pa < *pb)`: the inline-deref-in-
* comparison shape is mis-compiled by a PRE-EXISTING cgen bug (#116 /
* task #18, the same family 962's header documents). The bug is in the
* user-supplied comparator, NOT in search/bisect — those bind `r =
* cmp(...)` then compare, so the lib port is faithful and unaffected.
*
* Table-driven like 958_types_sizelim_run / 952_floats_run: each row is a
* self-contained ww program `import sort;`. cstage `ww build -I lib`
* compiles it, we run the binary and assert the exit code (0 == every
* assertion held; a small locator otherwise). cstage-only by design
* (mirrors 958/962): lib/sort is not compiler-imported, so per-program
* wwstage byte-id is out of scope and the 990-997 gates are unaffected.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
#define INTS \
"fn ints(a: *opaque, b: *opaque) int = {\n" \
" let pa: *i32 = a: *i32;\n" \
" let pb: *i32 = b: *i32;\n" \
" let va: i32 = *pa;\n" \
" let vb: i32 = *pb;\n" \
" if (va < vb) { return -1; };\n" \
" if (va > vb) { return 1; };\n" \
" return 0;\n" \
"};\n"
struct row { const char *label; const char *src; int want_exit; };
static const struct row rows[] = {
/* search: every present key resolves to its index; an absent key
* is void. ref/hare/sort/+test.ha:42-51. */
{ "search",
"package main;\n"
"import sort;\n"
INTS
"export fn main() i32 = {\n"
" let nums: [10]i32 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n"
" let s: []i32 = nums[0:10];\n"
" let n: size = len(s): size;\n"
" let i: size = 0;\n"
" for (i < n) {\n"
" let key: i32 = nums[i];\n"
" let p: size = sort.search(s, size(i32), &key, ints) as size;\n"
" if (p != i) { return 1; };\n"
" i = i + 1;\n"
" };\n"
" let miss: i32 = 1337;\n"
" let r = sort.search(s, size(i32), &miss, ints);\n"
" if (!(r is void)) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
/* lbisect: first-occurrence index for present values; insertion
* point for absent ones. ref/hare/sort/+test.ha:8-23. */
{ "lbisect",
"package main;\n"
"import sort;\n"
INTS
"export fn main() i32 = {\n"
" let nums: [10]i32 = [1, 3, 4, 4, 5, 7, 9, 11, 11, 11];\n"
" let s: []i32 = nums[0:10];\n"
" let n: size = len(s): size;\n"
" let i: size = 0;\n"
" for (i < n) {\n"
" if (i == 0 || nums[i - 1] != nums[i]) {\n"
" let key: i32 = nums[i];\n"
" if (sort.lbisect(s, size(i32), &key, ints) != i) { return 1; };\n"
" };\n"
" i = i + 1;\n"
" };\n"
" let n0: i32 = 0; if (sort.lbisect(s, size(i32), &n0, ints) != 0) { return 2; };\n"
" let n6: i32 = 6; if (sort.lbisect(s, size(i32), &n6, ints) != 5) { return 3; };\n"
" let n8: i32 = 8; if (sort.lbisect(s, size(i32), &n8, ints) != 6) { return 4; };\n"
" let n12: i32 = 12; if (sort.lbisect(s, size(i32), &n12, ints) != n) { return 5; };\n"
" return 0;\n"
"};\n", 0 },
/* rbisect: last-occurrence+1 index for present values; insertion
* point for absent ones. ref/hare/sort/+test.ha:25-40. */
{ "rbisect",
"package main;\n"
"import sort;\n"
INTS
"export fn main() i32 = {\n"
" let nums: [10]i32 = [1, 3, 4, 4, 5, 7, 9, 11, 11, 11];\n"
" let s: []i32 = nums[0:10];\n"
" let n: size = len(s): size;\n"
" let i: size = 0;\n"
" for (i < n) {\n"
" if (i == n - 1 || nums[i + 1] != nums[i]) {\n"
" let key: i32 = nums[i];\n"
" if (sort.rbisect(s, size(i32), &key, ints) != i + 1) { return 1; };\n"
" };\n"
" i = i + 1;\n"
" };\n"
" let n0: i32 = 0; if (sort.rbisect(s, size(i32), &n0, ints) != 0) { return 2; };\n"
" let n6: i32 = 6; if (sort.rbisect(s, size(i32), &n6, ints) != 5) { return 3; };\n"
" let n8: i32 = 8; if (sort.rbisect(s, size(i32), &n8, ints) != 6) { return 4; };\n"
" let n12: i32 = 12; if (sort.rbisect(s, size(i32), &n12, ints) != n) { return 5; };\n"
" return 0;\n"
"};\n", 0 },
{ NULL, NULL, 0 }
};
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[1024];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsort_%d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[128];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128];
snprintf(src, sizeof src, "%s/wwsort_%d_%d.ww", tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); continue; }
fputs(rows[i].src, f);
fclose(f);
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/out", tmpdir);
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s/ww build -I %s/lib -o %s %s",
bin, cwd, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build failed\n", rows[i].label);
fail++;
runwait(rmcmd);
continue;
}
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "%d/%d sort tests failed\n", fail, n);
return 1;
}
printf("sort: %d/%d ok\n", n, n);
return 0;
}