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.
This commit is contained in:
2026-05-30 03:24:23 +09:00
parent 80184a3acf
commit 7d39f6d623
31 changed files with 2371 additions and 4834 deletions

View File

@@ -1,11 +1,10 @@
// bufiotest — exercises lib/bufio. Run with `out/bin/ww run lib/bufio/bufiotest.ww`.
//
// The scanner @tests enumerate parallel `[N]T` arrays of inputs and
// expectations, then iterate one body across them. Parallel arrays
// (rather than `[N]struct{...}`) match the same cgen workaround
// memiotest leans on. The stream @tests are one scenario per fn
// (the ww-stdlib idiom): the "table" is the fn list in main, not
// a row array.
// expectations, then iterate one body across them. The stream @tests
// are one scenario per fn. #94 fold-eFinal: the unified value-return
// surface — `let st = memio.fixed(buf); &st.vt` into the scanner/stream
// init, io.read/write/close return size/io.error.
package bufio;
@@ -39,38 +38,34 @@ fn putstr(s: str, into: []u8, off: i32) i32 = {
return off + s.len;
};
// ---- A closed-on-read stream for the io.closed surfacing test ---------
// ---- an error-returning stream for the io.error surfacing test --------
//
// Replaces the OLD io.closed source. errvt is module-static; its
// reader/writer return a nomem-widened io.error.
let errvt: io.vtable;
fn closedread(s: *io.stream, buf: []u8) (i32 | io.eof | io.closed) = {
let e: io.closed; return e;
fn errread(s: io.stream, buf: []u8) (size | io.eof | io.error) = {
let nm: nomem; let e: io.error = nm; return e;
};
fn closedwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
let e: io.closed; return e;
fn errwrite(s: io.stream, buf: []u8) (size | io.error) = {
let nm: nomem; let e: io.error = nm; return e;
};
fn closedclose(s: *io.stream) (void | io.closed) = { return; };
fn closedstream(s: *io.stream) void = {
s.ctx = nil;
s.read = closedread;
s.write = closedwrite;
s.close = closedclose;
fn errsource() io.stream = {
errvt.reader = (&errread): *io.reader;
errvt.writer = (&errwrite): *io.writer;
return &errvt;
};
// ---- scanbyte: drain four bytes through a 2-byte window ---------------
// Reading buf is smaller than the stream contents (2 vs 4) — the
// scanner has to refill twice. Row 4 is one past EOF, row 5 stays at
// EOF (idempotent).
@test fn scanbytecases() void = {
let raw: [4]u8;
raw[0] = 11u8; raw[1] = 22u8; raw[2] = 33u8; raw[3] = 44u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:4]);
let mem: memio.stream = memio.fixed(raw[0:4]);
let m: io.stream = &mem.vt;
let buf: [2]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:2]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:2]);
// (wantbyte, weof)
let want: [6]u8;
@@ -84,14 +79,14 @@ fn closedstream(s: *io.stream) void = {
let i: i32 = 0;
for (i < 6) {
let r: (u8 | io.eof | io.closed) = bufio.scanbyte(&sc);
let r: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
match (r) {
case let b: u8 => {
if (weof[i] != 0) { fail(); };
if (b != want[i]) { fail(); };
};
case io.eof => { if (weof[i] == 0) { fail(); }; };
case io.closed => fail();
case let e: io.error => fail();
};
i += 1;
};
@@ -105,17 +100,12 @@ fn closedstream(s: *io.stream) void = {
let src: [32]u8;
let n: i32 = putstr("foo\nbar\nbaz\ntrailing", src[0:32], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, src[0:n]);
let mem: memio.stream = memio.fixed(src[0:n]);
let m: io.stream = &mem.vt;
let buf: [32]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:32]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:32]);
// (wantlen, wantfirst, weof, wovr)
// Row 0..2: terminated lines.
// Row 3: tail "trailing" — EOF_DISCARD drops it, returns eof.
// Row 4: subsequent call stays at eof.
let wl: [5]i32;
let wf: [5]u8;
let weof: [5]i32;
@@ -128,7 +118,7 @@ fn closedstream(s: *io.stream) void = {
let i: i32 = 0;
for (i < 5) {
let r: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (r) {
case let v: str => {
if (weof[i] != 0) { fail(); };
@@ -137,7 +127,7 @@ fn closedstream(s: *io.stream) void = {
if (v.len > 0 && v[0] != wf[i]) { fail(); };
};
case io.eof => { if (weof[i] == 0) { fail(); }; };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => { if (wovr[i] == 0) { fail(); }; };
};
i += 1;
@@ -152,47 +142,37 @@ fn closedstream(s: *io.stream) void = {
let src: [32]u8;
let n: i32 = putstr("ABCDEFGHI\n", src[0:32], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, src[0:n]);
let mem: memio.stream = memio.fixed(src[0:n]);
let m: io.stream = &mem.vt;
// Buffer too small for the 9-byte payload + delim. Caller
// owns the budget; overflow signals "raise it".
// Buffer too small for the 9-byte payload + delim.
let buf: [4]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:4]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:4]);
let r: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (r) {
case let v: str => fail();
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => { };
};
bufio.finish(&sc);
};
// ---- scantok: multi-delim hit, delim-at-start, EOF before delim ------
// ---- scanbytes: multi-delim hit, delim-at-start, EOF before delim ----
@test fn scantokcases() void = {
@test fn scanbytescases() void = {
let src: [16]u8;
// ",a,,b,c" — leading delim, empty token, multi-hit, trailing fragment.
let n: i32 = putstr(",a,,b,c", src[0:16], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, src[0:n]);
let mem: memio.stream = memio.fixed(src[0:n]);
let m: io.stream = &mem.vt;
let buf: [8]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:8]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
// (wantlen, wantfirst, weof)
// Row 0: leading "," → empty token.
// Row 1: "a" before next ",".
// Row 2: empty token between consecutive ",,".
// Row 3: "b".
// Row 4: "c" — no trailing delim → EOF_DISCARD drops + eof.
let wl: [5]i32;
let wf: [5]u8;
let weof: [5]i32;
@@ -204,7 +184,7 @@ fn closedstream(s: *io.stream) void = {
let i: i32 = 0;
for (i < 5) {
let r: ([]u8 | io.eof | io.closed | bufio.overflow) = bufio.scantok(&sc, 44u8); // ','
let r: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 44u8); // ','
match (r) {
case let v: []u8 => {
if (weof[i] != 0) { fail(); };
@@ -212,7 +192,7 @@ fn closedstream(s: *io.stream) void = {
if (v.len > 0 && v[0] != wf[i]) { fail(); };
};
case io.eof => { if (weof[i] == 0) { fail(); }; };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
i += 1;
@@ -225,136 +205,120 @@ fn closedstream(s: *io.stream) void = {
@test fn emptystream() void = {
let raw: [1]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:0]);
let mem: memio.stream = memio.fixed(raw[0:0]);
let m: io.stream = &mem.vt;
let buf: [8]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:8]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
let r1: (u8 | io.eof | io.closed) = bufio.scanbyte(&sc);
let r1: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
match (r1) {
case let b: u8 => fail();
case io.eof => { };
case io.closed => fail();
case let e: io.error => fail();
};
let r2: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (r2) {
case let v: str => fail();
case io.eof => { };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
let r3: ([]u8 | io.eof | io.closed | bufio.overflow) = bufio.scantok(&sc, 10u8);
let r3: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 10u8);
match (r3) {
case let v: []u8 => fail();
case io.eof => { };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
bufio.finish(&sc);
};
// ---- io.closed surfacing on a stream that returns closed --------------
// ---- io.error surfacing on a stream that returns error ----------------
@test fn closedsource() void = {
let m: io.stream;
closedstream(&m);
@test fn errorsource() void = {
let m: io.stream = errsource();
let buf: [8]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:8]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
let r1: (u8 | io.eof | io.closed) = bufio.scanbyte(&sc);
let r1: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
match (r1) {
case let b: u8 => fail();
case io.eof => fail();
case io.closed => { };
case let e: io.error => { };
};
let r2: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (r2) {
case let v: str => fail();
case io.eof => fail();
case io.closed => { };
case let e: io.error => { };
case bufio.overflow => fail();
};
let r3: ([]u8 | io.eof | io.closed | bufio.overflow) = bufio.scantok(&sc, 32u8);
let r3: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 32u8);
match (r3) {
case let v: []u8 => fail();
case io.eof => fail();
case io.closed => { };
case let e: io.error => { };
case bufio.overflow => fail();
};
bufio.finish(&sc);
};
// ---- boundary: empty line + idempotent EOF on scantok ----------------
// ---- boundary: empty line + idempotent EOF on scanbytes --------------
// scanline on a leading '\n' must return an empty (len=0) view, not
// skip the empty line and pretend it's "the first non-empty line".
// scantok after EOF_DISCARD must keep returning io.eof on every
// subsequent call — same idempotency guaranteed by scanbyte rows 4/5
// and scanline rows 3/4, but the scantok path filled at the bottom
// of `for (true)` is its own state machine. Worth pinning.
@test fn boundarycases() void = {
// ---- empty line via scanline -----------------------------------
let s1: [16]u8;
let n1: i32 = putstr("\nfoo\n", s1[0:16], 0);
let m1mem: memio.state;
let m1: io.stream;
memio.fixed(&m1mem, &m1, s1[0:n1]);
let m1mem: memio.stream = memio.fixed(s1[0:n1]);
let m1: io.stream = &m1mem.vt;
let b1: [8]u8;
let sc1: bufio.scanner;
bufio.newscanner(&sc1, &m1, b1[0:8]);
let sc1: bufio.scanner = bufio.newscannerbuf(m1, b1[0:8]);
let r1: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc1);
let r1: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc1);
match (r1) {
case let v: str => { if (v.len != 0) { fail(); }; };
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
let r2: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc1);
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc1);
match (r2) {
case let v: str => {
if (v.len != 3) { fail(); };
if (v[0] != 102u8) { fail(); }; // 'f'
};
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
bufio.finish(&sc1);
// ---- scantok idempotent EOF -----------------------------------
// "ab" with delim ',' — no delim, EOF_DISCARD drops "ab", first
// call returns eof. The next call must too (state stays at eof).
// ---- scanbytes idempotent EOF ---------------------------------
let s2: [8]u8;
let n2: i32 = putstr("ab", s2[0:8], 0);
let m2mem: memio.state;
let m2: io.stream;
memio.fixed(&m2mem, &m2, s2[0:n2]);
let m2mem: memio.stream = memio.fixed(s2[0:n2]);
let m2: io.stream = &m2mem.vt;
let b2: [8]u8;
let sc2: bufio.scanner;
bufio.newscanner(&sc2, &m2, b2[0:8]);
let sc2: bufio.scanner = bufio.newscannerbuf(m2, b2[0:8]);
let t1: ([]u8 | io.eof | io.closed | bufio.overflow) = bufio.scantok(&sc2, 44u8);
let t1: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc2, 44u8);
match (t1) {
case let v: []u8 => fail();
case io.eof => { };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
let t2: ([]u8 | io.eof | io.closed | bufio.overflow) = bufio.scantok(&sc2, 44u8);
let t2: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc2, 44u8);
match (t2) {
case let v: []u8 => fail();
case io.eof => { };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
bufio.finish(&sc2);
@@ -362,20 +326,14 @@ fn closedstream(s: *io.stream) void = {
// ---- multi-fill: window smaller than longest token, shift path --------
// Buffer 4B, line "abcd\nxy\n". After reading "abcd", a delim search
// finds nothing, the buffer is full at start=0 → overflow. So bump
// to 5B (room for "abcd" + '\n'). Reads come in 5-byte chunks; the
// second line "xy" fits trivially and exercises the shift path.
@test fn multifill() void = {
let src: [32]u8;
let n: i32 = putstr("abcd\nxy\n", src[0:32], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, src[0:n]);
let mem: memio.stream = memio.fixed(src[0:n]);
let m: io.stream = &mem.vt;
let buf: [5]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, &m, buf[0:5]);
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:5]);
// (wantlen, wantfirst, weof)
let wl: [3]i32;
@@ -387,7 +345,7 @@ fn closedstream(s: *io.stream) void = {
let i: i32 = 0;
for (i < 3) {
let r: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (r) {
case let v: str => {
if (weof[i] != 0) { fail(); };
@@ -395,7 +353,7 @@ fn closedstream(s: *io.stream) void = {
if (v.len > 0 && v[0] != wf[i]) { fail(); };
};
case io.eof => { if (weof[i] == 0) { fail(); }; };
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
i += 1;
@@ -408,30 +366,28 @@ fn closedstream(s: *io.stream) void = {
@test fn streamsmallwrite() void = {
let raw: [16]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:16]);
let mem: memio.stream = memio.fixed(raw[0:16]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [8]u8;
let wb: [8]u8;
bufio.init(&b, &m, rb[0:8], wb[0:8]);
let b: bufio.stream = bufio.init(m, rb[0:8], wb[0:8]);
// Write under the buffer limit — nothing reaches src.
let buf: [5]u8;
let z: i32 = putstr("hello", buf[0:5], 0);
let p: *io.stream = &b.vtable;
let r: (i32 | io.closed) = io.write(p, buf[0:5]);
let p: io.stream = &b.vt;
let r: (size | io.error) = io.write(p, buf[0:5]);
match (r) {
case let n: i32 => { if (n != 5) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 5) { fail(); }; };
case let e: io.error => fail();
};
if (mem.pos != 0) { fail(); };
let fr: (void | io.closed) = bufio.flush(&b);
let fr: (void | io.error) = bufio.flush(&b);
match (fr) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 5) { fail(); };
if (raw[0] != 104u8) { fail(); };
@@ -442,32 +398,29 @@ fn closedstream(s: *io.stream) void = {
@test fn streamautoflush() void = {
let raw: [32]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:32]);
let mem: memio.stream = memio.fixed(raw[0:32]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:4], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
// 10B payload through a 4B wbuf: bwrite must flush twice mid-write,
// leaving the final 2B pending until an explicit flush.
// 10B payload through a 4B wbuf: bwrite must flush twice mid-write.
let buf: [10]u8;
let z: i32 = putstr("abcdefghij", buf[0:10], 0);
let p: *io.stream = &b.vtable;
let r: (i32 | io.closed) = io.write(p, buf[0:10]);
let p: io.stream = &b.vt;
let r: (size | io.error) = io.write(p, buf[0:10]);
match (r) {
case let n: i32 => { if (n != 10) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 10) { fail(); }; };
case let e: io.error => fail();
};
// Two full flushes happened (8B); 2B still pending.
if (mem.pos != 8) { fail(); };
let fr: (void | io.closed) = bufio.flush(&b);
let fr: (void | io.error) = bufio.flush(&b);
match (fr) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 10) { fail(); };
if (raw[0] != 97u8) { fail(); }; // 'a'
@@ -476,40 +429,34 @@ fn closedstream(s: *io.stream) void = {
// ---- default flush byte-set: '\n' in payload triggers flush ---------
// init seeds b.flush to ['\n']; bwrite must flush after copying when
// the payload contains any byte from b.flush. No setflush call —
// this exercises the default. Mirrors Hare's flag::NONE default
// (ref/hare/bufio/stream.ha:75 + 100).
@test fn streamlineflush() void = {
let raw: [32]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:32]);
let mem: memio.stream = memio.fixed(raw[0:32]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [16]u8;
bufio.init(&b, &m, rb[0:4], wb[0:16]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:16]);
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
// First write: no '\n', stays buffered.
let h: [5]u8;
let zh: i32 = putstr("hello", h[0:5], 0);
let r1: (i32 | io.closed) = io.write(p, h[0:5]);
let r1: (size | io.error) = io.write(p, h[0:5]);
match (r1) {
case let n: i32 => { if (n != 5) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 5) { fail(); }; };
case let e: io.error => fail();
};
if (mem.pos != 0) { fail(); };
// Second write: '\n' present, triggers flush of everything (6B).
let nl: [1]u8;
nl[0] = 10u8;
let r2: (i32 | io.closed) = io.write(p, nl[0:1]);
let r2: (size | io.error) = io.write(p, nl[0:1]);
match (r2) {
case let n: i32 => { if (n != 1) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 1) { fail(); }; };
case let e: io.error => fail();
};
if (mem.pos != 6) { fail(); };
if (raw[0] != 104u8) { fail(); };
@@ -518,48 +465,42 @@ fn closedstream(s: *io.stream) void = {
// ---- explicit-flush-per-write: caller drives the drain ---------------
// The previous ww-only FLUSH_ON_WRITE flag is gone; the Hare way
// to drain after every write is a manual flush call. Setflush to
// an empty byte-set first so the default '\n' detector doesn't
// shadow what we're testing.
@test fn streamflushonwrite() void = {
let raw: [32]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:32]);
let mem: memio.stream = memio.fixed(raw[0:32]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [16]u8;
bufio.init(&b, &m, rb[0:4], wb[0:16]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:16]);
let nilbs: [1]u8;
bufio.setflush(&b, nilbs[0:0]);
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
let h: [2]u8;
h[0] = 65u8; h[1] = 66u8; // "AB"
let r1: (i32 | io.closed) = io.write(p, h[0:2]);
let r1: (size | io.error) = io.write(p, h[0:2]);
match (r1) {
case let n: i32 => { if (n != 2) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 2) { fail(); }; };
case let e: io.error => fail();
};
let f1: (void | io.closed) = bufio.flush(&b);
let f1: (void | io.error) = bufio.flush(&b);
match (f1) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 2) { fail(); };
if (raw[0] != 65u8) { fail(); };
let r2: (i32 | io.closed) = io.write(p, h[0:1]);
let r2: (size | io.error) = io.write(p, h[0:1]);
match (r2) {
case let n: i32 => { if (n != 1) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 1) { fail(); }; };
case let e: io.error => fail();
};
let f2: (void | io.closed) = bufio.flush(&b);
let f2: (void | io.error) = bufio.flush(&b);
match (f2) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 3) { fail(); };
if (raw[2] != 65u8) { fail(); };
@@ -569,17 +510,15 @@ fn closedstream(s: *io.stream) void = {
@test fn streamisbuffered() void = {
let raw: [8]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:8]);
let mem: memio.stream = memio.fixed(raw[0:8]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:4], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
if (!bufio.isbuffered(&b.vtable)) { fail(); };
if (bufio.isbuffered(&m)) { fail(); };
if (!bufio.isbuffered(&b.vt)) { fail(); };
if (bufio.isbuffered(m)) { fail(); };
};
// ---- bread + unread: pushed-back bytes come out first ----------------
@@ -588,25 +527,22 @@ fn closedstream(s: *io.stream) void = {
let raw: [8]u8;
let n: i32 = putstr("ABCDEFGH", raw[0:8], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:n]);
let mem: memio.stream = memio.fixed(raw[0:n]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [8]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:8], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:8], wb[0:4]);
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
// Pull 3 bytes through bread — fills rbuf from src (8B), serves
// 3 of them; rstart=3 after.
// Pull 3 bytes through bread — fills rbuf from src (8B), serves 3.
let out: [4]u8;
let r1: (i32 | io.eof | io.closed) = io.read(p, out[0:3]);
let r1: (size | io.eof | io.error) = io.read(p, out[0:3]);
match (r1) {
case let z: i32 => { if (z != 3) { fail(); }; };
case let z: size => { if (z: i32 != 3) { fail(); }; };
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
};
if (out[0] != 65u8) { fail(); }; // 'A'
if (out[2] != 67u8) { fail(); }; // 'C'
@@ -616,21 +552,21 @@ fn closedstream(s: *io.stream) void = {
push[0] = 88u8; push[1] = 89u8; // "XY"
bufio.unread(&b, push[0:2]);
let r2: (i32 | io.eof | io.closed) = io.read(p, out[0:2]);
let r2: (size | io.eof | io.error) = io.read(p, out[0:2]);
match (r2) {
case let z: i32 => { if (z != 2) { fail(); }; };
case let z: size => { if (z: i32 != 2) { fail(); }; };
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
};
if (out[0] != 88u8) { fail(); }; // 'X'
if (out[1] != 89u8) { fail(); }; // 'Y'
// Subsequent read returns the original tail.
let r3: (i32 | io.eof | io.closed) = io.read(p, out[0:4]);
let r3: (size | io.eof | io.error) = io.read(p, out[0:4]);
match (r3) {
case let z: i32 => { if (z != 4) { fail(); }; };
case let z: size => { if (z: i32 != 4) { fail(); }; };
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
};
if (out[0] != 68u8) { fail(); }; // 'D'
if (out[3] != 71u8) { fail(); }; // 'G'
@@ -638,34 +574,26 @@ fn closedstream(s: *io.stream) void = {
// ---- scanner over a stream-wrapped src: pre-read unread + scanline --
// Push three bytes into a freshly-init'd stream (rstart=rend=rbuf.len
// post-init, per Hare bufio/stream.ha:101 — the unread budget before
// the first read is the full rbuf), then scan a line through the
// stream. The scanner pulls "XYZ" from rbuf first, refills "hello\n"
// from src, and scanline returns "XYZhello".
@test fn streamscannerunread() void = {
let raw: [16]u8;
let n: i32 = putstr("hello\n", raw[0:16], 0);
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:n]);
let mem: memio.stream = memio.fixed(raw[0:n]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [16]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:16], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:16], wb[0:4]);
let push: [3]u8;
push[0] = 88u8; push[1] = 89u8; push[2] = 90u8; // "XYZ"
bufio.unread(&b, push[0:3]);
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
let sbuf: [32]u8;
let sc: bufio.scanner;
bufio.newscanner(&sc, p, sbuf[0:32]);
let sc: bufio.scanner = bufio.newscannerbuf(p, sbuf[0:32]);
let lr: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
let lr: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
match (lr) {
case let v: str => {
if (v.len != 8) { fail(); }; // "XYZhello"
@@ -673,7 +601,7 @@ fn closedstream(s: *io.stream) void = {
if (v[7] != 111u8) { fail(); }; // 'o'
};
case io.eof => fail();
case io.closed => fail();
case let e: io.error => fail();
case bufio.overflow => fail();
};
bufio.finish(&sc);
@@ -683,55 +611,48 @@ fn closedstream(s: *io.stream) void = {
@test fn streamflushempty() void = {
let raw: [8]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:8]);
let mem: memio.stream = memio.fixed(raw[0:8]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:4], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
let r: (void | io.closed) = bufio.flush(&b);
let r: (void | io.error) = bufio.flush(&b);
match (r) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 0) { fail(); };
};
// ---- bclose drains pending wbuf then forwards close to src ----------
// io.close on the stream vtable must run the flush path (Hare
// stream.ha:188-202). Buffer two bytes, route io.close through the
// stream, and confirm both reach src.
@test fn streamcloseflushes() void = {
let raw: [8]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:8]);
let mem: memio.stream = memio.fixed(raw[0:8]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [4]u8;
bufio.init(&b, &m, rb[0:4], wb[0:4]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
let nilbs: [1]u8;
bufio.setflush(&b, nilbs[0:0]); // suppress the default '\n' detector
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
let h: [2]u8;
h[0] = 80u8; h[1] = 81u8; // "PQ"
let r: (i32 | io.closed) = io.write(p, h[0:2]);
let r: (size | io.error) = io.write(p, h[0:2]);
match (r) {
case let n: i32 => { if (n != 2) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 2) { fail(); }; };
case let e: io.error => fail();
};
if (mem.pos != 0) { fail(); }; // still buffered
let cr: (void | io.closed) = io.close(p);
let cr: (void | io.error) = io.close(p);
match (cr) {
case void => { };
case io.closed => fail();
case let e: io.error => fail();
};
if (mem.pos != 2) { fail(); };
if (raw[0] != 80u8) { fail(); };
@@ -740,34 +661,27 @@ fn closedstream(s: *io.stream) void = {
// ---- setflush with a custom non-empty byte-set -----------------------
// setflush installs a fresh byte-set (not just clears it). Use ' '
// (space) as the trigger; "ab cd" must flush at the space, leaving
// "cd" pending.
@test fn streamsetflushcustom() void = {
let raw: [16]u8;
let mem: memio.state;
let m: io.stream;
memio.fixed(&mem, &m, raw[0:16]);
let mem: memio.stream = memio.fixed(raw[0:16]);
let m: io.stream = &mem.vt;
let b: bufio.stream;
let rb: [4]u8;
let wb: [16]u8;
bufio.init(&b, &m, rb[0:4], wb[0:16]);
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:16]);
let bs: [1]u8;
bs[0] = 32u8; // ' '
bufio.setflush(&b, bs[0:1]);
let p: *io.stream = &b.vtable;
let p: io.stream = &b.vt;
let buf: [5]u8;
let z: i32 = putstr("ab cd", buf[0:5], 0);
let r: (i32 | io.closed) = io.write(p, buf[0:5]);
let r: (size | io.error) = io.write(p, buf[0:5]);
match (r) {
case let n: i32 => { if (n != 5) { fail(); }; };
case io.closed => fail();
case let n: size => { if (n: i32 != 5) { fail(); }; };
case let e: io.error => fail();
};
// Space at index 2 triggers post-copy flush of all 5 bytes; the
// scan-then-copy-then-flush ordering writes everything, so mem.pos
// hits 5 in one shot.
// Space at index 2 triggers post-copy flush of all 5 bytes.
if (mem.pos != 5) { fail(); };
if (raw[2] != 32u8) { fail(); };
if (raw[4] != 100u8) { fail(); }; // 'd'
@@ -777,9 +691,9 @@ export fn main() i32 = {
signalled = 1; scanbytecases();
signalled = 2; scanlinecases();
signalled = 3; scanlineoverflow();
signalled = 4; scantokcases();
signalled = 4; scanbytescases();
signalled = 5; emptystream();
signalled = 6; closedsource();
signalled = 6; errorsource();
signalled = 7; boundarycases();
signalled = 8; multifill();
signalled = 9; streamsmallwrite();