Files
ww/test/wcc/781_fmt_vstream_compositions_run.c
Hojun-Cho 7d39f6d623 lib: collapse the parallel vstream scaffold onto the single Hare io surface (#94 fold-eFinal)
The Option-C parallel _v vstream API was scaffolding to bring the io stack up alongside the old surface; carrying both permanently is a rule-9 divergence from ref/hare, which has exactly one io surface. Collapse onto that surface (stream = *vtable, ref/hare/io/stream.ha) and rename the _v symbols to their Hare names (io vstream->stream, fmt vfprint->fprint, bufio/memio/log surfaces, log.new). Deletes the 4 lib/*/vstream.ww scaffold files; regenerates w6c/wwdump combined.ww. cstage and wwstage stay byte-identical and combined_ww_fresh holds; all 220 tests pass.
2026-05-30 03:24:23 +09:00

380 lines
13 KiB
C

/*
* 781_fmt_vstream_compositions_run — project #94 fold-eFinal sentinel.
* Pins the composition wrappers in lib/fmt/fmt.ww —
* fprintln / fprintfln / bsprintf / asprintf — plus the memio
* accessor (memio.string in lib/memio/memio.ww) they read back
* through. The fold-eFinal FLIP collapsed the dual fmt + memio
* surfaces into one (the former `v`-suffixed wrappers and the
* fixed_string / dynamic_string accessors merged into the bare names).
*
* memio.string is the direct enabler: bsprintf reads its written
* prefix via memio.string; asprintf reads its grown buffer via the
* same accessor.
*
* Each row imports fmt + os + io, drives one of the four V wrappers,
* and asserts the exact bytes / size emitted. Per-row unique exit
* constant lets a failure pinpoint the offending case.
*
* row | what it pins
* --------------------------+--------------------------------------
* fdprintln_v_run_basic | fdprintln wired vstream sink
* | (inline fprint + vputbytes "\n").
* | One i64 arg + str arg + bool arg;
* | asserts space-separator plus trailing
* | newline byte content.
* fdprintfln_v_run_fmt | fdprintfln wired vstream sink with
* | a {n}-placeholder format string.
* | Asserts placeholder substitution +
* | trailing newline shape (mirror of
* | fmt.fprintfln, fmt.ww:740).
* fprintln_basic | fmt.fprintln dispatched DIRECTLY
* | through memio.fixed + read
* | back via memio.string. Pins
* | the composition symbol itself
* | (fdprintln open-codes the same
* | fprint+putbytes chain rather than
* | calling fprintln, so without this
* | row the symbol ships uncovered).
* fprintfln_basic | fmt.fprintfln direct dispatch via
* | memio.fixed + memio.string,
* | same direct-symbol rationale as
* | fprintln_basic.
* bsprintf_v_basic | bsprintf into a caller-supplied
* | [16]u8 buffer through
* | memio.fixed;
* | memio.string returns the prefix
* | view. Asserts both the bytes in the
* | caller's buffer AND the returned str
* | view (ptr == buf + 0, len == 5).
* asprintf_v_basic | asprintf into a heap-grown str
* | through memio.dynamic;
* | memio.string + shrink-copy +
* | io.close. Asserts the owned str
* | bytes, then frees via os.free.
*
* Cstage-only per row (no STAGE_WW, no byte_id) — pre-existing
* wwstage bug #209 (sibling of #190): the wwstage checker bails
* `case: not a variant of scrutinee (X)` + `match: variant not
* handled (formattable)` on fmt.formattable match-arms whenever a
* downstream probe `import fmt;`s the package. Identical carve-out
* to 777_fmt_vstream_run + 780_fmt_vstream_mods_run (cited there at
* line 18-33 / 18-23). Byte-id graduates with #209 close.
*
* IMPORT-ORDER WORKAROUND (sibling task #208, pre-existing): every
* row places `import os;` FIRST for the same reason 776/777/780
* document (wwstage checker ordering-sensitivity on os.tryread /
* trywrite / tryopen's bare `return r;` over a tagged return type).
*
* DEFERRALS / RELATED (NOT addressed by these rows):
*
* - #209 (wwstage formattable match-arm bail): blocks STAGE_WW
* here, same as 777/780. Cstage-only until close.
* - #173 (TRY-on-tagged-return both-stages broken): bsprintf
* widens nomem into io.error inline rather than `memio.
* fixed(buf)?` for the same reason memio.ww does.
*
* BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost
* combined.ww (grep verified). memio IS embedded in w6c + wwdump
* combined.ww — the fold-eFinal memio collapse rides along via the
* regen committed in this same commit (per #110 SSoT). 990/994/995
* byte-id gates + combined_ww_fresh stay green by virtue of the
* regen.
*
* GATE POLARITY: must stay GREEN. A red here means fprintln /
* fprintfln dropped the newline byte, bsprintf returned a stale
* memio.string view (intrusive cast broke), asprintf failed
* to shrink-copy / close cleanly, or the underlying fprint /
* fprintf primitives regressed.
*/
#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 STAGE_CS 1
#define STAGE_WW 2
struct row {
const char *label;
const char *src;
int want_exit;
int stage_mask;
int byte_id;
};
static const struct row rows[] = {
{ "fdprintln_v_run_basic",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_781_a\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let wr = fmt.fdprintln(fd, 1i64, \"hi\", true);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [16]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
" os.close(rd);\n"
" if (n != 10i64) { return 93; };\n"
" if (nw != 10: size) { return 94; };\n"
" if (buf[0] != 49u8) { return 95; };\n"
" if (buf[1] != 32u8) { return 96; };\n"
" if (buf[2] != 104u8 || buf[3] != 105u8) { return 97; };\n"
" if (buf[4] != 32u8) { return 98; };\n"
" if (buf[9] != 10u8) { return 99; };\n"
" return 60;\n"
"};\n",
60,
STAGE_CS, 0 },
{ "fdprintfln_v_run_fmt",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_781_b\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let wr = fmt.fdprintfln(fd, \"x={}\", 7i64);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [16]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
" os.close(rd);\n"
" if (n != 4i64) { return 93; };\n"
" if (nw != 4: size) { return 94; };\n"
" if (buf[0] != 120u8 || buf[1] != 61u8 || buf[2] != 55u8 || buf[3] != 10u8) { return 95; };\n"
" return 61;\n"
"};\n",
61,
STAGE_CS, 0 },
{ "fprintln_basic",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"import memio;\n"
"export fn main() i32 = {\n"
" let buf: [16]u8;\n"
" let st: memio.stream = memio.fixed(buf[0:16]);\n"
" let vs: io.stream = &st.vt;\n"
" let wr = fmt.fprintln(vs, 1i64, \"hi\", true);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" if (nw != 10: size) { return 92; };\n"
" let view: str = memio.string(&st);\n"
" if (view.len != 10) { return 93; };\n"
" if (view[0] != 49u8) { return 94; };\n"
" if (view[1] != 32u8) { return 95; };\n"
" if (view[2] != 104u8 || view[3] != 105u8) { return 96; };\n"
" if (view[9] != 10u8) { return 97; };\n"
" return 64;\n"
"};\n",
64,
STAGE_CS, 0 },
{ "fprintfln_basic",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"import memio;\n"
"export fn main() i32 = {\n"
" let buf: [16]u8;\n"
" let st: memio.stream = memio.fixed(buf[0:16]);\n"
" let vs: io.stream = &st.vt;\n"
" let wr = fmt.fprintfln(vs, \"x={}\", 7i64);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" if (nw != 4: size) { return 92; };\n"
" let view: str = memio.string(&st);\n"
" if (view.len != 4) { return 93; };\n"
" if (view[0] != 120u8 || view[1] != 61u8) { return 94; };\n"
" if (view[2] != 55u8 || view[3] != 10u8) { return 95; };\n"
" return 65;\n"
"};\n",
65,
STAGE_CS, 0 },
{ "bsprintf_v_basic",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let buf: [16]u8;\n"
" let r = fmt.bsprintf(buf[0:16], \"v={}\", 42i64);\n"
" let view: str = \"\";\n"
" match (r) {\n"
" case let s: str => { view = s; };\n"
" case io.error => { return 90; };\n"
" };\n"
" if (view.len != 4) { return 91; };\n"
" if (view[0] != 118u8 || view[1] != 61u8) { return 92; };\n"
" if (view[2] != 52u8 || view[3] != 50u8) { return 93; };\n"
" if (buf[0] != 118u8 || buf[3] != 50u8) { return 94; };\n"
" return 62;\n"
"};\n",
62,
STAGE_CS, 0 },
{ "asprintf_v_basic",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let view: str = fmt.asprintf(\"hello={}\", 9i64);\n"
" if (view.len != 7) { return 90; };\n"
" if (view[0] != 104u8 || view[1] != 101u8 || view[2] != 108u8) { return 91; };\n"
" if (view[3] != 108u8 || view[4] != 111u8) { return 92; };\n"
" if (view[5] != 61u8 || view[6] != 57u8) { return 93; };\n"
" os.free(view.ptr: *void, view.len: u64);\n"
" return 63;\n"
"};\n",
63,
STAGE_CS, 0 },
};
static int
write_source(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
return 0;
}
/* Per-row tmpdir cleanup. ww_ww writes intermediates next to the
* source (task #15), so each row's build leaves <src>.{combined.ww,
* s,o} + bare exe alongside. Mirror of 777/780's. */
static void
cleanup_tmp(const char *tmpdir, const char *base)
{
char p[640];
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
rmdir(tmpdir);
}
static int
build_via_driver(const char *driver, const char *tmpdir, const char *cwd,
const char *src)
{
char cmd[2048];
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s build -I %s/lib %s 2>/dev/null",
tmpdir, driver, cwd, src);
return runwait(cmd);
}
static int
run_row(const char *driver, const char *cwd, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64], outbin[768];
snprintf(tmpdir, sizeof tmpdir, "/tmp/fvc_%d_d_%d", getpid(), seq);
snprintf(base, sizeof base, "main781");
snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base);
mkdir(tmpdir, 0755);
if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return -1; }
int rc;
int br = build_via_driver(driver, tmpdir, cwd, src);
if (br == 0) {
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
rc = runwait(outbin);
} else {
rc = -1;
}
cleanup_tmp(tmpdir, base);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[512];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
int wwpresent = (access(wdrv, X_OK) == 0);
int seq = 0;
for (int i = 0; i < n; i++) {
if (rows[i].stage_mask & STAGE_CS) {
total++;
int got = run_row(cdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"fmt_vstream_compositions_run[cs][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
}
if (wwpresent && (rows[i].stage_mask & STAGE_WW)) {
total++;
int got = run_row(wdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"fmt_vstream_compositions_run[ww][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
}
}
if (!wwpresent)
fprintf(stderr, "fmt_vstream_compositions_run: skip wwstage (no %s)\n", wdrv);
if (fail) {
fprintf(stderr, "fmt_vstream_compositions_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("fmt_vstream_compositions_run: %d/%d ok\n", total, total);
return 0;
}