Ports ref/hare/sort/{search,bisect}.ha and the cmpfunc type
(types.ha), replacing the experimental vtable placeholder. The
powersort sort()/shuffle() surface stays out of scope.
Divergences forced by ww's surface (rule-10 align-down, not
behavioural):
- cmp is a fn-VALUE param (cmpfunc), not Hare's *cmpfunc: ww
renders functions-in-an-interface by value, as lib/io.ww's
stream vtable does; *cmpfunc is not callable (no fn-ptr
auto-deref) and &fn is *fn(...), unassignable to the alias.
- no const (ww has none); *u8 base + uintptr stride (no [*]
unbounded array, per 962); len() is i32 so cast : size;
single-condition for, so Hare's afterthought is a body tail.
- merged into one sort.ww (ww per-module convention; 900_stdlib
smoke-compiles the file standalone, which a split breaks).
963_sort_run exercises all three on a []i32 with a real cmpfunc,
mirroring +test.ha's search/lbisect/rbisect @test fns. The
comparator binds its derefs to locals to dodge the pre-existing
inline-deref-in-comparison cgen bug (#116); that bug is in the
user comparator, not search/bisect, so the port is faithful.
lib/sort is not compiler-imported: byte-id-neutral, no combined.ww
change, 990-997 unaffected.
190 lines
6.3 KiB
C
190 lines
6.3 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 <string.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 src[64];
|
|
snprintf(src, sizeof src, "/tmp/wwsort_%d_%d.ww", getpid(), i);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsort_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build -I %s/lib %s",
|
|
tmpdir, bin, cwd, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: build failed\n", rows[i].label);
|
|
fail++;
|
|
unlink(src); rmdir(tmpdir);
|
|
continue;
|
|
}
|
|
|
|
char outbin[128];
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(outbin, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
|
|
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++;
|
|
}
|
|
unlink(src); unlink(outbin); rmdir(tmpdir);
|
|
}
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d sort tests failed\n", fail, n);
|
|
return 1;
|
|
}
|
|
printf("sort: %d/%d ok\n", n, n);
|
|
return 0;
|
|
}
|