Files
ww/test/wcc/963_sort_run.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +09:00

200 lines
6.5 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 <errno.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] = "/tmp/wwsort_XXXXXX";
if (mkdtemp(tmpdir) == NULL) {
perror("sort: mkdtemp");
fail++;
continue;
}
char src[128], outbin[128], sepwork[160], rmcmd[192];
snprintf(src, sizeof src, "%s/wwsort_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/out", tmpdir);
snprintf(sepwork, sizeof sepwork, "%s.sepwork", outbin);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", sepwork);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; goto cleanup; }
fputs(rows[i].src, f);
fclose(f);
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++;
goto cleanup;
}
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++;
}
cleanup: {
int cleanbad = 0;
if (unlink(src) != 0 && errno != ENOENT) cleanbad = 1;
if (unlink(outbin) != 0 && errno != ENOENT) cleanbad = 1;
if (runwait(rmcmd) != 0) cleanbad = 1;
if (rmdir(tmpdir) != 0) cleanbad = 1;
if (cleanbad) {
fprintf(stderr, "row[%s]: temporary cleanup failed\n",
rows[i].label);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "%d/%d sort tests failed\n", fail, n);
return 1;
}
printf("sort: %d/%d ok\n", n, n);
return 0;
}