lib+test: bufio bstream writer half (closes #18)
bstream wraps *io.stream with caller-supplied rbuf/wbuf; init, flush, setflush, unread, isbuffered, bread, bwrite, bclose mirror ref/hare/bufio/stream.ha modulo the bstream-vs-stream rename (cross-module type collision, see task #21). Default flush byte-set is "\n" (Hare flag::NONE default at stream.ha:75). Drops the prior ww-only FLUSH_ON_WRITE flag for Hare's flush []u8 + setflush byte-set. bufiotest.ww adds 10 @test fns: small-write, auto-flush, line-flush default, manual-flush, isbuffered, unread (post-read and pre-read budgets), scanner-over-bstream unread, flush-empty no-op, bclose flushes, setflush custom byte.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
// bufiotest — exercises lib/bufio. Run with `out/bin/ww run lib/bufio/bufiotest.ww`.
|
||||
//
|
||||
// Each @test enumerates parallel `[N]T` arrays of inputs and
|
||||
// expectations, then iterates one body across them. Parallel arrays
|
||||
// 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.
|
||||
// memiotest leans on. The bstream @tests are one scenario per fn
|
||||
// (the ww-stdlib idiom): the "table" is the fn list in main, not
|
||||
// a row array.
|
||||
|
||||
use bufio;
|
||||
use bytes;
|
||||
@@ -400,14 +402,393 @@ fn closedstream(s: *io.stream) void = {
|
||||
bufio.finish(&sc);
|
||||
};
|
||||
|
||||
// ---- bstream init + flush round-trip ---------------------------------
|
||||
|
||||
@test fn bstreamsmallwrite() void = {
|
||||
let raw: [16]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:16]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [8]u8;
|
||||
let wb: [8]u8;
|
||||
bufio.init(&b, &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]);
|
||||
match (r) {
|
||||
case let n: i32 => { if (n != 5) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 0) { fail(); };
|
||||
|
||||
let fr: (void | io.closed) = bufio.flush(&b);
|
||||
match (fr) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 5) { fail(); };
|
||||
if (raw[0] != 104u8) { fail(); };
|
||||
if (raw[4] != 111u8) { fail(); };
|
||||
};
|
||||
|
||||
// ---- write larger than wbuf → auto-flush -----------------------------
|
||||
|
||||
@test fn bstreamautoflush() void = {
|
||||
let raw: [32]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:32]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &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.
|
||||
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]);
|
||||
match (r) {
|
||||
case let n: i32 => { if (n != 10) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
// Two full flushes happened (8B); 2B still pending.
|
||||
if (mem.pos != 8) { fail(); };
|
||||
|
||||
let fr: (void | io.closed) = bufio.flush(&b);
|
||||
match (fr) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 10) { fail(); };
|
||||
if (raw[0] != 97u8) { fail(); }; // 'a'
|
||||
if (raw[9] != 106u8) { fail(); }; // 'j'
|
||||
};
|
||||
|
||||
// ---- 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 bstreamlineflush() void = {
|
||||
let raw: [32]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:32]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [16]u8;
|
||||
bufio.init(&b, &m, rb[0:4], wb[0:16]);
|
||||
|
||||
let p: *io.stream = &b.vtable;
|
||||
|
||||
// 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]);
|
||||
match (r1) {
|
||||
case let n: i32 => { if (n != 5) { fail(); }; };
|
||||
case io.closed => 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]);
|
||||
match (r2) {
|
||||
case let n: i32 => { if (n != 1) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 6) { fail(); };
|
||||
if (raw[0] != 104u8) { fail(); };
|
||||
if (raw[5] != 10u8) { fail(); };
|
||||
};
|
||||
|
||||
// ---- 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 bstreamflushonwrite() void = {
|
||||
let raw: [32]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:32]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [16]u8;
|
||||
bufio.init(&b, &m, rb[0:4], wb[0:16]);
|
||||
let nilbs: [1]u8;
|
||||
bufio.setflush(&b, nilbs[0:0]);
|
||||
|
||||
let p: *io.stream = &b.vtable;
|
||||
let h: [2]u8;
|
||||
h[0] = 65u8; h[1] = 66u8; // "AB"
|
||||
let r1: (i32 | io.closed) = io.write(p, h[0:2]);
|
||||
match (r1) {
|
||||
case let n: i32 => { if (n != 2) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
let f1: (void | io.closed) = bufio.flush(&b);
|
||||
match (f1) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 2) { fail(); };
|
||||
if (raw[0] != 65u8) { fail(); };
|
||||
|
||||
let r2: (i32 | io.closed) = io.write(p, h[0:1]);
|
||||
match (r2) {
|
||||
case let n: i32 => { if (n != 1) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
let f2: (void | io.closed) = bufio.flush(&b);
|
||||
match (f2) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 3) { fail(); };
|
||||
if (raw[2] != 65u8) { fail(); };
|
||||
};
|
||||
|
||||
// ---- isbuffered: bstream → true, plain memio → false -----------------
|
||||
|
||||
@test fn bstreamisbuffered() void = {
|
||||
let raw: [8]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:8]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &m, rb[0:4], wb[0:4]);
|
||||
|
||||
if (!bufio.isbuffered(&b.vtable)) { fail(); };
|
||||
if (bufio.isbuffered(&m)) { fail(); };
|
||||
};
|
||||
|
||||
// ---- bread + unread: pushed-back bytes come out first ----------------
|
||||
|
||||
@test fn bstreamunread() 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 b: bufio.bstream;
|
||||
let rb: [8]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &m, rb[0:8], wb[0:4]);
|
||||
|
||||
let p: *io.stream = &b.vtable;
|
||||
|
||||
// Pull 3 bytes through bread — fills rbuf from src (8B), serves
|
||||
// 3 of them; rstart=3 after.
|
||||
let out: [4]u8;
|
||||
let r1: (i32 | io.eof | io.closed) = io.read(p, out[0:3]);
|
||||
match (r1) {
|
||||
case let z: i32 => { if (z != 3) { fail(); }; };
|
||||
case io.eof => fail();
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (out[0] != 65u8) { fail(); }; // 'A'
|
||||
if (out[2] != 67u8) { fail(); }; // 'C'
|
||||
|
||||
// Push back 2 bytes; next read returns them first.
|
||||
let push: [2]u8;
|
||||
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]);
|
||||
match (r2) {
|
||||
case let z: i32 => { if (z != 2) { fail(); }; };
|
||||
case io.eof => fail();
|
||||
case io.closed => 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]);
|
||||
match (r3) {
|
||||
case let z: i32 => { if (z != 4) { fail(); }; };
|
||||
case io.eof => fail();
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (out[0] != 68u8) { fail(); }; // 'D'
|
||||
if (out[3] != 71u8) { fail(); }; // 'G'
|
||||
};
|
||||
|
||||
// ---- scanner over a bstream-wrapped src: pre-read unread + scanline --
|
||||
|
||||
// Push three bytes into a freshly-init'd bstream (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
|
||||
// bstream. The scanner pulls "XYZ" from rbuf first, refills "hello\n"
|
||||
// from src, and scanline returns "XYZhello".
|
||||
@test fn bstreamscannerunread() 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 b: bufio.bstream;
|
||||
let rb: [16]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &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 sbuf: [32]u8;
|
||||
let sc: bufio.scanner;
|
||||
bufio.newscanner(&sc, p, sbuf[0:32]);
|
||||
|
||||
let lr: (str | io.eof | io.closed | bufio.overflow) = bufio.scanline(&sc);
|
||||
match (lr) {
|
||||
case let v: str => {
|
||||
if (v.len != 8) { fail(); }; // "XYZhello"
|
||||
if (v[0] != 88u8) { fail(); }; // 'X'
|
||||
if (v[7] != 111u8) { fail(); }; // 'o'
|
||||
};
|
||||
case io.eof => fail();
|
||||
case io.closed => fail();
|
||||
case bufio.overflow => fail();
|
||||
};
|
||||
bufio.finish(&sc);
|
||||
};
|
||||
|
||||
// ---- flush() on empty wbuf is a no-op --------------------------------
|
||||
|
||||
@test fn bstreamflushempty() void = {
|
||||
let raw: [8]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:8]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &m, rb[0:4], wb[0:4]);
|
||||
|
||||
let r: (void | io.closed) = bufio.flush(&b);
|
||||
match (r) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 0) { fail(); };
|
||||
};
|
||||
|
||||
// ---- bclose drains pending wbuf then forwards close to src ----------
|
||||
|
||||
// io.close on the bstream vtable must run the flush path (Hare
|
||||
// stream.ha:188-202). Buffer two bytes, route io.close through the
|
||||
// bstream, and confirm both reach src.
|
||||
@test fn bstreamcloseflushes() void = {
|
||||
let raw: [8]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:8]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [4]u8;
|
||||
bufio.init(&b, &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 h: [2]u8;
|
||||
h[0] = 80u8; h[1] = 81u8; // "PQ"
|
||||
let r: (i32 | io.closed) = io.write(p, h[0:2]);
|
||||
match (r) {
|
||||
case let n: i32 => { if (n != 2) { fail(); }; };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 0) { fail(); }; // still buffered
|
||||
|
||||
let cr: (void | io.closed) = io.close(p);
|
||||
match (cr) {
|
||||
case void => { };
|
||||
case io.closed => fail();
|
||||
};
|
||||
if (mem.pos != 2) { fail(); };
|
||||
if (raw[0] != 80u8) { fail(); };
|
||||
if (raw[1] != 81u8) { fail(); };
|
||||
};
|
||||
|
||||
// ---- 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 bstreamsetflushcustom() void = {
|
||||
let raw: [16]u8;
|
||||
let mem: memio.state;
|
||||
let m: io.stream;
|
||||
memio.fixed(&mem, &m, raw[0:16]);
|
||||
|
||||
let b: bufio.bstream;
|
||||
let rb: [4]u8;
|
||||
let wb: [16]u8;
|
||||
bufio.init(&b, &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 buf: [5]u8;
|
||||
let z: i32 = putstr("ab cd", buf[0:5], 0);
|
||||
let r: (i32 | io.closed) = io.write(p, buf[0:5]);
|
||||
match (r) {
|
||||
case let n: i32 => { if (n != 5) { fail(); }; };
|
||||
case io.closed => 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.
|
||||
if (mem.pos != 5) { fail(); };
|
||||
if (raw[2] != 32u8) { fail(); };
|
||||
if (raw[4] != 100u8) { fail(); }; // 'd'
|
||||
};
|
||||
|
||||
export fn main() i32 = {
|
||||
signalled = 1; scanbytecases();
|
||||
signalled = 2; scanlinecases();
|
||||
signalled = 3; scanlineoverflow();
|
||||
signalled = 4; scantokcases();
|
||||
signalled = 5; emptystream();
|
||||
signalled = 6; closedsource();
|
||||
signalled = 7; boundarycases();
|
||||
signalled = 8; multifill();
|
||||
signalled = 1; scanbytecases();
|
||||
signalled = 2; scanlinecases();
|
||||
signalled = 3; scanlineoverflow();
|
||||
signalled = 4; scantokcases();
|
||||
signalled = 5; emptystream();
|
||||
signalled = 6; closedsource();
|
||||
signalled = 7; boundarycases();
|
||||
signalled = 8; multifill();
|
||||
signalled = 9; bstreamsmallwrite();
|
||||
signalled = 10; bstreamautoflush();
|
||||
signalled = 11; bstreamlineflush();
|
||||
signalled = 12; bstreamflushonwrite();
|
||||
signalled = 13; bstreamisbuffered();
|
||||
signalled = 14; bstreamunread();
|
||||
signalled = 15; bstreamscannerunread();
|
||||
signalled = 16; bstreamflushempty();
|
||||
signalled = 17; bstreamcloseflushes();
|
||||
signalled = 18; bstreamsetflushcustom();
|
||||
return 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user