Files
ww/test/wcc/781_fmt_vstream_compositions_run.c
Hojun-Cho bc9d7df002 test/wcc/781: pin vfprintln/vfprintfln direct dispatch (#94 fold-e7 follow-up)
reviewer-e7 R1 finding: fold-e7 (df28784) shipped four new V-side
composition symbols (vfprintln / vfprintfln / vbsprintf / vasprintf)
but 781's rows 1-2 drove fdprintln_v / fdprintfln_v instead of the
newly-added vfprintln / vfprintfln. fdprintln_v's body open-codes
the same `match (vfprint(...)) ... match (vputbytes("\n", 1))` chain
rather than calling vfprintln, so the new V-side composition symbols
themselves had zero direct probe coverage — only transitive coverage
of the (mechanically identical) inline blocks.

Two new cstage-only rows:
  - vfprintln_basic   memio.fixed_vstream + fmt.vfprintln + read-back
                      via memio.fixed_string; asserts the 10-byte
                      "1 hi true\n" payload + size return.
  - vfprintfln_basic  same shape, fmt.vfprintfln + "x={}" + 7 → 4-byte
                      "x=7\n" payload.

Both rows mirror row 3 (bsprintf_v_basic) — memio.fixed_vstream wiring
+ direct vfprintln/vfprintfln dispatch + memio.fixed_string read-back —
so the new V-side symbols ride exactly the same probe shape. Cstage-
only per #209 (wwstage formattable match-arm bail); byte-id graduates
on close. Exit codes 64/65 (60-63 used by rows 1-4).

Header table updated to reflect rows 1-2's actual surface (fdprintln_v
wired sink, not vfprintln) and to cite the new rows 3-4. fmt is not
embedded in selfhost so no combined.ww regen; Makefile dep set
unchanged. 214 total tests still green (sub-rows are internal to
781_fmt_vstream_compositions_run).
2026-05-29 13:08:19 +09:00

397 lines
14 KiB
C

/*
* 781_fmt_vstream_compositions_run — project #94 fold-e7 sentinel.
* Pins the V-side composition wrappers in lib/fmt/vstream.ww —
* vfprintln / vfprintfln / vbsprintf / vasprintf — plus the two
* memio enablers fold-e7 bundles alongside (memio.fixed_string /
* memio.dynamic_string in lib/memio/vstream.ww). Coexists with the
* OLD fmt.fprintln / fprintfln / bsprintf / asprintf surface in
* fmt.ww:240,740,839,873 (fmt.ww UNCHANGED this fold; eFinal
* task #50 collapses both surfaces and renames the `v` suffix off).
*
* The two memio helpers are direct enablers (vbsprintf reads its
* written prefix via fixed_string; vasprintf reads its grown buffer
* via dynamic_string), drew-approved per
* feedback_refactor_routing_same_class_drops (prereqs, not churn).
*
* 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_v wired vstream sink
* | (inline vfprint + vputbytes "\n").
* | One i64 arg + str arg + bool arg;
* | asserts space-separator plus trailing
* | newline byte content.
* fdprintfln_v_run_fmt | fdprintfln_v wired vstream sink with
* | a {n}-placeholder format string.
* | Asserts placeholder substitution +
* | trailing newline shape (mirror of
* | fmt.fprintfln, fmt.ww:740).
* vfprintln_basic | fmt.vfprintln dispatched DIRECTLY
* | through memio.fixed_vstream + read
* | back via memio.fixed_string. Pins
* | the new V-side composition symbol
* | itself (fdprintln_v open-codes the
* | same vfprint+vputbytes chain rather
* | than calling vfprintln, so without
* | this row the new symbol ships
* | uncovered).
* vfprintfln_basic | fmt.vfprintfln direct dispatch via
* | memio.fixed_vstream + fixed_string,
* | same direct-symbol rationale as
* | vfprintln_basic.
* bsprintf_v_basic | vbsprintf into a caller-supplied
* | [16]u8 buffer through
* | memio.fixed_vstream;
* | memio.fixed_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 | vasprintf into a heap-grown str
* | through memio.dynamic_vstream;
* | memio.dynamic_string + shrink-copy +
* | io.st_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).
*
* SIBLINGS (filed inline, NOT fixed here — fold-e7 is purely
* additive over fold-e1/e2/e3/e4/e5/e6's frozen io.* + memio.* +
* fmt.* + bufio.* + log.* surface):
*
* - #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): vbsprintf
* widens nomem into io.error inline rather than `memio.
* fixed_vstream(buf)?` for the same reason memio.vstream.ww
* does at line 87-99.
* - eFinal (#50): collapses V + OLD surfaces; the `v` names
* rename off and the OLD fprintln / fprintfln / bsprintf /
* asprintf delete.
*
* BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost
* combined.ww (grep verified pre-impl). memio.vstream.ww IS embedded
* in w6c + wwdump combined.ww — the fold-e7 memio helpers ride 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 vfprintln /
* vfprintfln dropped the newline byte, vbsprintf returned a stale
* memio.fixed_string view (intrusive cast broke), vasprintf failed
* to shrink-copy / close cleanly, or the underlying vfprint /
* vfprintf 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_v(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_v(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 },
{ "vfprintln_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 r: (io.vstream | nomem) = memio.fixed_vstream(buf[0:16]);\n"
" let vs: io.vstream = nil: *io.vtable;\n"
" match (r) {\n"
" case let v: io.vstream => { vs = v; };\n"
" case nomem => { return 90; };\n"
" };\n"
" let wr = fmt.vfprintln(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.fixed_string(vs);\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 },
{ "vfprintfln_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 r: (io.vstream | nomem) = memio.fixed_vstream(buf[0:16]);\n"
" let vs: io.vstream = nil: *io.vtable;\n"
" match (r) {\n"
" case let v: io.vstream => { vs = v; };\n"
" case nomem => { return 90; };\n"
" };\n"
" let wr = fmt.vfprintfln(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.fixed_string(vs);\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.vbsprintf(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.vasprintf(\"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;
}