25 renames (git mv, content untouched). _test.ww is what the package coordinator's test detection and the sep loader's canonical exclusion key on; the old *test.ww spellings survived only through the line-leading-@test compatibility scan. Consumers updated in place: LIBRARY_TESTS, the libbyteid roster, the 901/974/975/976 carriers that copy or invoke these files, and the check.c/check.ww + path/ftos comments that cite them. Closes the open-driver-work migration bullet.
1080 lines
30 KiB
Plaintext
1080 lines
30 KiB
Plaintext
// 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. 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_test;
|
|
|
|
import bufio;
|
|
import bytes;
|
|
import encoding.utf8;
|
|
import io;
|
|
import memio;
|
|
|
|
// putstr — copy the bytes of `s` into `into` starting at `off`,
|
|
// returning the new offset.
|
|
fn putstr(s: str, into: []u8, off: i32) i32 = {
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
into[off + i] = s[i];
|
|
i += 1;
|
|
};
|
|
return off + s.len;
|
|
};
|
|
|
|
// ---- 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 errread(s: io.stream, buf: []u8) (size | io.eof | io.error) = {
|
|
let nm: nomem; let e: io.error = nm; return e;
|
|
};
|
|
fn errwrite(s: io.stream, buf: []u8) (size | io.error) = {
|
|
let nm: nomem; let e: io.error = nm; return e;
|
|
};
|
|
fn errsource() io.stream = {
|
|
errvt.reader = (&errread): *io.reader;
|
|
errvt.writer = (&errwrite): *io.writer;
|
|
return &errvt;
|
|
};
|
|
|
|
// ---- a close-recording source for the no-propagate test ---------------
|
|
//
|
|
// closevt forwards writes to closesink (so a flush is observable) and
|
|
// records whether its closer fired in srcclosed.
|
|
let srcclosed: bool = false;
|
|
let closevt: io.vtable;
|
|
let closesink: memio.stream;
|
|
|
|
fn closewrite(s: io.stream, buf: []u8) (size | io.error) = {
|
|
return io.write(&closesink.vt, buf);
|
|
};
|
|
fn closeclose(s: io.stream) (void | io.error) = {
|
|
srcclosed = true;
|
|
return void;
|
|
};
|
|
fn closesource() io.stream = {
|
|
closevt.writer = (&closewrite): *io.writer;
|
|
closevt.closer = (&closeclose): *io.closer;
|
|
return &closevt;
|
|
};
|
|
|
|
// ---- scanbyte: drain four bytes through a 2-byte window ---------------
|
|
|
|
@test fn scanbytecases() void = {
|
|
let raw: [4]u8;
|
|
raw[0] = 11u8; raw[1] = 22u8; raw[2] = 33u8; raw[3] = 44u8;
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:4]);
|
|
let m: io.stream = &mem.vt;
|
|
let buf: [2]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:2]);
|
|
|
|
// (wantbyte, weof)
|
|
let want: [6]u8;
|
|
let weof: [6]i32;
|
|
want[0]=11u8; weof[0]=0;
|
|
want[1]=22u8; weof[1]=0;
|
|
want[2]=33u8; weof[2]=0;
|
|
want[3]=44u8; weof[3]=0;
|
|
want[4]=0u8; weof[4]=1; // past end → eof
|
|
want[5]=0u8; weof[5]=1; // stay at eof
|
|
|
|
let i: i32 = 0;
|
|
for (i < 6) {
|
|
let r: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
|
|
match (r) {
|
|
case let b: u8 => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(b != want[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- scanline: 3 lines + tail fragment + extra calls stay at EOF ------
|
|
|
|
@test fn scanlinecases() void = {
|
|
let src: [32]u8;
|
|
let n: i32 = putstr("foo\nbar\nbaz\ntrailing", src[0:32], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
let buf: [32]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:32]);
|
|
|
|
// (wantlen, wantfirst, weof, wovr)
|
|
let wl: [5]i32;
|
|
let wf: [5]u8;
|
|
let weof: [5]i32;
|
|
let wovr: [5]i32;
|
|
wl[0]=3; wf[0]=102u8; weof[0]=0; wovr[0]=0; // foo
|
|
wl[1]=3; wf[1]=98u8; weof[1]=0; wovr[1]=0; // bar
|
|
wl[2]=3; wf[2]=98u8; weof[2]=0; wovr[2]=0; // baz
|
|
wl[3]=0; wf[3]=0u8; weof[3]=1; wovr[3]=0; // trailing → eof
|
|
wl[4]=0; wf[4]=0u8; weof[4]=1; wovr[4]=0; // stay eof
|
|
|
|
let i: i32 = 0;
|
|
for (i < 5) {
|
|
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r) {
|
|
case let v: str => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(wovr[i] != 0));
|
|
assert(!(v.len != wl[i]));
|
|
assert(!(v.len > 0 && v[0] != wf[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => { assert(!(wovr[i] == 0)); };
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- scanline: line longer than buffer → overflow --------------------
|
|
|
|
@test fn scanlineoverflow() void = {
|
|
let src: [32]u8;
|
|
let n: i32 = putstr("ABCDEFGHI\n", src[0:32], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
// Buffer too small for the 9-byte payload + delim.
|
|
let buf: [4]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:4]);
|
|
|
|
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r) {
|
|
case let v: str => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => { };
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- scanbytes: multi-delim hit, delim-at-start, EOF before delim ----
|
|
|
|
@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.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
let buf: [8]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
|
|
|
|
// (wantlen, wantfirst, weof)
|
|
let wl: [5]i32;
|
|
let wf: [5]u8;
|
|
let weof: [5]i32;
|
|
wl[0]=0; wf[0]=0u8; weof[0]=0;
|
|
wl[1]=1; wf[1]=97u8; weof[1]=0; // 'a'
|
|
wl[2]=0; wf[2]=0u8; weof[2]=0;
|
|
wl[3]=1; wf[3]=98u8; weof[3]=0; // 'b'
|
|
wl[4]=0; wf[4]=0u8; weof[4]=1; // 'c' discarded
|
|
|
|
let i: i32 = 0;
|
|
for (i < 5) {
|
|
let r: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 44u8); // ','
|
|
match (r) {
|
|
case let v: []u8 => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(v.len != wl[i]));
|
|
assert(!(v.len > 0 && v[0] != wf[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- empty stream: every variant returns eof immediately --------------
|
|
|
|
@test fn emptystream() void = {
|
|
let raw: [1]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:0]);
|
|
let m: io.stream = &mem.vt;
|
|
let buf: [8]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
|
|
|
|
let r1: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
|
|
match (r1) {
|
|
case let b: u8 => abort();
|
|
case io.eof => { };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r2) {
|
|
case let v: str => abort();
|
|
case io.eof => { };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
let r3: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 10u8);
|
|
match (r3) {
|
|
case let v: []u8 => abort();
|
|
case io.eof => { };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- io.error surfacing on a stream that returns error ----------------
|
|
|
|
@test fn errorsource() void = {
|
|
let m: io.stream = errsource();
|
|
let buf: [8]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
|
|
|
|
let r1: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
|
|
match (r1) {
|
|
case let b: u8 => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r2) {
|
|
case let v: str => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
let r3: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc, 32u8);
|
|
match (r3) {
|
|
case let v: []u8 => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- boundary: empty line + idempotent EOF on scanbytes --------------
|
|
|
|
@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.stream = memio.fixed(s1[0:n1]);
|
|
let m1: io.stream = &m1mem.vt;
|
|
let b1: [8]u8;
|
|
let sc1: bufio.scanner = bufio.newscannerbuf(m1, b1[0:8]);
|
|
|
|
let r1: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc1);
|
|
match (r1) {
|
|
case let v: str => { assert(!(v.len != 0)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc1);
|
|
match (r2) {
|
|
case let v: str => {
|
|
assert(!(v.len != 3));
|
|
assert(!(v[0] != 102u8)); // 'f'
|
|
};
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc1);
|
|
|
|
// ---- scanbytes idempotent EOF ---------------------------------
|
|
let s2: [8]u8;
|
|
let n2: i32 = putstr("ab", s2[0:8], 0);
|
|
let m2mem: memio.stream = memio.fixed(s2[0:n2]);
|
|
let m2: io.stream = &m2mem.vt;
|
|
let b2: [8]u8;
|
|
let sc2: bufio.scanner = bufio.newscannerbuf(m2, b2[0:8]);
|
|
|
|
let t1: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc2, 44u8);
|
|
match (t1) {
|
|
case let v: []u8 => abort();
|
|
case io.eof => { };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
let t2: ([]u8 | io.eof | io.error | bufio.overflow) = bufio.scanbytes(&sc2, 44u8);
|
|
match (t2) {
|
|
case let v: []u8 => abort();
|
|
case io.eof => { };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc2);
|
|
};
|
|
|
|
// ---- multi-fill: window smaller than longest token, shift path --------
|
|
|
|
@test fn multifill() void = {
|
|
let src: [32]u8;
|
|
let n: i32 = putstr("abcd\nxy\n", src[0:32], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
let buf: [5]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:5]);
|
|
|
|
// (wantlen, wantfirst, weof)
|
|
let wl: [3]i32;
|
|
let wf: [3]u8;
|
|
let weof: [3]i32;
|
|
wl[0]=4; wf[0]=97u8; weof[0]=0; // abcd
|
|
wl[1]=2; wf[1]=120u8; weof[1]=0; // xy
|
|
wl[2]=0; wf[2]=0u8; weof[2]=1; // eof
|
|
|
|
let i: i32 = 0;
|
|
for (i < 3) {
|
|
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r) {
|
|
case let v: str => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(v.len != wl[i]));
|
|
assert(!(v.len > 0 && v[0] != wf[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- stream init + flush round-trip ---------------------------------
|
|
|
|
@test fn streamsmallwrite() void = {
|
|
let raw: [16]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:16]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [8]u8;
|
|
let wb: [8]u8;
|
|
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.vt;
|
|
let r: (size | io.error) = io.write(p, buf[0:5]);
|
|
match (r) {
|
|
case let n: size => { assert(!(n: i32 != 5)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 0));
|
|
|
|
let fr: (void | io.error) = bufio.flush(&b);
|
|
match (fr) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 5));
|
|
assert(!(raw[0] != 104u8));
|
|
assert(!(raw[4] != 111u8));
|
|
};
|
|
|
|
// ---- write larger than wbuf → auto-flush -----------------------------
|
|
|
|
@test fn streamautoflush() void = {
|
|
let raw: [32]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:32]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [4]u8;
|
|
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.
|
|
let buf: [10]u8;
|
|
let z: i32 = putstr("abcdefghij", buf[0:10], 0);
|
|
let p: io.stream = &b.vt;
|
|
let r: (size | io.error) = io.write(p, buf[0:10]);
|
|
match (r) {
|
|
case let n: size => { assert(!(n: i32 != 10)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
// Two full flushes happened (8B); 2B still pending.
|
|
assert(!(mem.pos != 8));
|
|
|
|
let fr: (void | io.error) = bufio.flush(&b);
|
|
match (fr) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 10));
|
|
assert(!(raw[0] != 97u8)); // 'a'
|
|
assert(!(raw[9] != 106u8)); // 'j'
|
|
};
|
|
|
|
// ---- default flush byte-set: '\n' in payload triggers flush ---------
|
|
|
|
@test fn streamlineflush() void = {
|
|
let raw: [32]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:32]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [16]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:16]);
|
|
|
|
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: (size | io.error) = io.write(p, h[0:5]);
|
|
match (r1) {
|
|
case let n: size => { assert(!(n: i32 != 5)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 0));
|
|
|
|
// Second write: '\n' present, triggers flush of everything (6B).
|
|
let nl: [1]u8;
|
|
nl[0] = 10u8;
|
|
let r2: (size | io.error) = io.write(p, nl[0:1]);
|
|
match (r2) {
|
|
case let n: size => { assert(!(n: i32 != 1)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 6));
|
|
assert(!(raw[0] != 104u8));
|
|
assert(!(raw[5] != 10u8));
|
|
};
|
|
|
|
// ---- explicit-flush-per-write: caller drives the drain ---------------
|
|
|
|
@test fn streamflushonwrite() void = {
|
|
let raw: [32]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:32]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [16]u8;
|
|
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.vt;
|
|
let h: [2]u8;
|
|
h[0] = 65u8; h[1] = 66u8; // "AB"
|
|
let r1: (size | io.error) = io.write(p, h[0:2]);
|
|
match (r1) {
|
|
case let n: size => { assert(!(n: i32 != 2)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
let f1: (void | io.error) = bufio.flush(&b);
|
|
match (f1) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 2));
|
|
assert(!(raw[0] != 65u8));
|
|
|
|
let r2: (size | io.error) = io.write(p, h[0:1]);
|
|
match (r2) {
|
|
case let n: size => { assert(!(n: i32 != 1)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
let f2: (void | io.error) = bufio.flush(&b);
|
|
match (f2) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 3));
|
|
assert(!(raw[2] != 65u8));
|
|
};
|
|
|
|
// ---- isbuffered: stream → true, plain memio → false -----------------
|
|
|
|
@test fn streamisbuffered() void = {
|
|
let raw: [8]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:8]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
|
|
|
|
assert(!(!bufio.isbuffered(&b.vt)));
|
|
assert(!(bufio.isbuffered(m)));
|
|
};
|
|
|
|
// ---- bread tops up a partial pending buffer (Hare short-read) --------
|
|
//
|
|
// rbuf holds SOME pending bytes (fewer than requested) AND has spare
|
|
// capacity: Hare shifts the pending region to the front and reads more
|
|
// into the tail before serving (ref/hare/bufio/stream.ha:209). Pre-fix
|
|
// ww served only the short pending count and never topped up.
|
|
@test fn streamreadtopup() void = {
|
|
let raw: [16]u8;
|
|
let n: i32 = putstr("ABCDEFGHIJKLMNOP", raw[0:16], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [8]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:8], wb[0:4]);
|
|
|
|
let p: io.stream = &b.vt;
|
|
|
|
// First read fills rbuf (8B "ABCDEFGH"), serves 3 — 5 pending remain.
|
|
let out: [10]u8;
|
|
let r1: (size | io.eof | io.error) = io.read(p, out[0:3]);
|
|
match (r1) {
|
|
case let z: size => { assert(!(z: i32 != 3)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
|
|
// Second read wants 10 > 5 pending: Hare shifts "DEFGH" to the front,
|
|
// reads 3 more ("IJK") into the tail, serves 8. Pre-fix ww returned 5.
|
|
let r2: (size | io.eof | io.error) = io.read(p, out[0:10]);
|
|
match (r2) {
|
|
case let z: size => { assert(!(z: i32 != 8)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(out[0] != 68u8)); // 'D'
|
|
assert(!(out[7] != 75u8)); // 'K'
|
|
};
|
|
|
|
// ---- bread serves pending bytes when the top-up read hits EOF --------
|
|
//
|
|
// The load-bearing "EOF fatal only when avail==0" path
|
|
// (ref/hare/bufio/stream.ha:216): rbuf still holds pending bytes when
|
|
// the top-up io.read returns EOF — Hare drains the pending region, it
|
|
// does NOT propagate eof. Source is shorter than two reads so the
|
|
// second read's top-up io.read EOFs with bytes still buffered.
|
|
@test fn streamreadtopupeof() void = {
|
|
let raw: [5]u8;
|
|
let n: i32 = putstr("ABCDE", raw[0:5], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [8]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:8], wb[0:4]);
|
|
|
|
let p: io.stream = &b.vt;
|
|
|
|
// First read fills rbuf (5B "ABCDE", source drained), serves 3 —
|
|
// 2 pending remain.
|
|
let out: [10]u8;
|
|
let r1: (size | io.eof | io.error) = io.read(p, out[0:3]);
|
|
match (r1) {
|
|
case let z: size => { assert(!(z: i32 != 3)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
|
|
// Second read wants 10 > 2 pending: shift "DE" to the front, top-up
|
|
// io.read hits EOF (source drained). avail==2 != 0 so Hare drains the
|
|
// pending region — serves 2, NOT eof. "always fatal" would return eof.
|
|
let r2: (size | io.eof | io.error) = io.read(p, out[0:10]);
|
|
match (r2) {
|
|
case let z: size => { assert(!(z: i32 != 2)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(out[0] != 68u8)); // 'D'
|
|
assert(!(out[1] != 69u8)); // 'E'
|
|
};
|
|
|
|
// ---- bread + unread: pushed-back bytes come out first ----------------
|
|
|
|
@test fn streamunread() void = {
|
|
let raw: [8]u8;
|
|
let n: i32 = putstr("ABCDEFGH", raw[0:8], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [8]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:8], wb[0:4]);
|
|
|
|
let p: io.stream = &b.vt;
|
|
|
|
// Pull 3 bytes through bread — fills rbuf from src (8B), serves 3.
|
|
let out: [4]u8;
|
|
let r1: (size | io.eof | io.error) = io.read(p, out[0:3]);
|
|
match (r1) {
|
|
case let z: size => { assert(!(z: i32 != 3)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(out[0] != 65u8)); // 'A'
|
|
assert(!(out[2] != 67u8)); // '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: (size | io.eof | io.error) = io.read(p, out[0:2]);
|
|
match (r2) {
|
|
case let z: size => { assert(!(z: i32 != 2)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(out[0] != 88u8)); // 'X'
|
|
assert(!(out[1] != 89u8)); // 'Y'
|
|
|
|
// Subsequent read returns the original tail.
|
|
let r3: (size | io.eof | io.error) = io.read(p, out[0:4]);
|
|
match (r3) {
|
|
case let z: size => { assert(!(z: i32 != 4)); };
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(out[0] != 68u8)); // 'D'
|
|
assert(!(out[3] != 71u8)); // 'G'
|
|
};
|
|
|
|
// ---- scanner over a stream-wrapped src: pre-read unread + scanline --
|
|
|
|
@test fn streamscannerunread() void = {
|
|
let raw: [16]u8;
|
|
let n: i32 = putstr("hello\n", raw[0:16], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [16]u8;
|
|
let wb: [4]u8;
|
|
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.vt;
|
|
let sbuf: [32]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(p, sbuf[0:32]);
|
|
|
|
let lr: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (lr) {
|
|
case let v: str => {
|
|
assert(!(v.len != 8)); // "XYZhello"
|
|
assert(!(v[0] != 88u8)); // 'X'
|
|
assert(!(v[7] != 111u8)); // 'o'
|
|
};
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- flush() on empty wbuf is a no-op --------------------------------
|
|
|
|
@test fn streamflushempty() void = {
|
|
let raw: [8]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:8]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(m, rb[0:4], wb[0:4]);
|
|
|
|
let r: (void | io.error) = bufio.flush(&b);
|
|
match (r) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 0));
|
|
};
|
|
|
|
// ---- bclose drains pending wbuf then forwards close to src ----------
|
|
|
|
@test fn streamcloseflushes() void = {
|
|
let raw: [8]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:8]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [4]u8;
|
|
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.vt;
|
|
let h: [2]u8;
|
|
h[0] = 80u8; h[1] = 81u8; // "PQ"
|
|
let r: (size | io.error) = io.write(p, h[0:2]);
|
|
match (r) {
|
|
case let n: size => { assert(!(n: i32 != 2)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 0)); // still buffered
|
|
|
|
let cr: (void | io.error) = io.close(p);
|
|
match (cr) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(mem.pos != 2));
|
|
assert(!(raw[0] != 80u8));
|
|
assert(!(raw[1] != 81u8));
|
|
};
|
|
|
|
// ---- bclose flushes but does NOT close the underlying ----------------
|
|
//
|
|
// Hare's close_buffered closes src only under flag::MANAGED_HANDLE
|
|
// (ref/hare/bufio/stream.ha:194); init's default flag::NONE flushes
|
|
// only. The close-recording closesource lets us assert both: the
|
|
// buffered byte reaches the sink (flush) AND the closer never fires.
|
|
@test fn streamclosenopropagate() void = {
|
|
srcclosed = false;
|
|
let raw: [8]u8;
|
|
closesink = memio.fixed(raw[0:8]);
|
|
let src: io.stream = closesource();
|
|
|
|
let rb: [4]u8;
|
|
let wb: [4]u8;
|
|
let b: bufio.stream = bufio.init(src, 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.vt;
|
|
let h: [2]u8;
|
|
h[0] = 80u8; h[1] = 81u8; // "PQ"
|
|
let r: (size | io.error) = io.write(p, h[0:2]);
|
|
match (r) {
|
|
case let n: size => { assert(!(n: i32 != 2)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
assert(!(closesink.pos != 0)); // still buffered
|
|
|
|
let cr: (void | io.error) = io.close(p);
|
|
match (cr) {
|
|
case void => { };
|
|
case let e: io.error => abort();
|
|
};
|
|
// Flush happened: the 2 buffered bytes reached the sink.
|
|
assert(!(closesink.pos != 2));
|
|
assert(!(raw[0] != 80u8));
|
|
assert(!(raw[1] != 81u8));
|
|
// But the underlying was NOT closed (Hare flag::NONE default).
|
|
assert(!srcclosed);
|
|
};
|
|
|
|
// ---- setflush with a custom non-empty byte-set -----------------------
|
|
|
|
@test fn streamsetflushcustom() void = {
|
|
let raw: [16]u8;
|
|
let mem: memio.stream = memio.fixed(raw[0:16]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let rb: [4]u8;
|
|
let wb: [16]u8;
|
|
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.vt;
|
|
let buf: [5]u8;
|
|
let z: i32 = putstr("ab cd", buf[0:5], 0);
|
|
let r: (size | io.error) = io.write(p, buf[0:5]);
|
|
match (r) {
|
|
case let n: size => { assert(!(n: i32 != 5)); };
|
|
case let e: io.error => abort();
|
|
};
|
|
// Space at index 2 triggers post-copy flush of all 5 bytes.
|
|
assert(!(mem.pos != 5));
|
|
assert(!(raw[2] != 32u8));
|
|
assert(!(raw[4] != 100u8)); // 'd'
|
|
};
|
|
|
|
// ---- scanrune: ASCII + 2/3/4-byte UTF-8 + idempotent EOF --------------
|
|
|
|
@test fn scanrunecases() void = {
|
|
// "a" U+0061; "é" U+00E9 (C3 A9); "한" U+D55C (ED 95 9C);
|
|
// "😀" U+1F600 (F0 9F 98 80).
|
|
let raw: [10]u8;
|
|
raw[0] = 97u8;
|
|
raw[1] = 0xC3u8; raw[2] = 0xA9u8;
|
|
raw[3] = 0xEDu8; raw[4] = 0x95u8; raw[5] = 0x9Cu8;
|
|
raw[6] = 0xF0u8; raw[7] = 0x9Fu8; raw[8] = 0x98u8; raw[9] = 0x80u8;
|
|
|
|
let mem: memio.stream = memio.fixed(raw[0:10]);
|
|
let m: io.stream = &mem.vt;
|
|
// 8B window: the trailing 4-byte rune straddles a refill (shift path).
|
|
let buf: [8]u8;
|
|
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
|
|
|
|
// (wantrune, weof)
|
|
let want: [6]u32;
|
|
let weof: [6]i32;
|
|
want[0] = 97u32; weof[0] = 0;
|
|
want[1] = 233u32; weof[1] = 0;
|
|
want[2] = 54620u32; weof[2] = 0;
|
|
want[3] = 128512u32; weof[3] = 0;
|
|
want[4] = 0u32; weof[4] = 1; // past end → eof
|
|
want[5] = 0u32; weof[5] = 1; // stay at eof
|
|
|
|
let i: i32 = 0;
|
|
for (i < 6) {
|
|
let r: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc);
|
|
match (r) {
|
|
case let rn: rune => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(rn: u32 != want[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- scanrune: invalid sequences --------------------------------------
|
|
|
|
@test fn scanruneinvalid() void = {
|
|
// Bare continuation byte: utf8sz rejects the initial byte.
|
|
let r1raw: [1]u8;
|
|
r1raw[0] = 0x80u8;
|
|
let mem1: memio.stream = memio.fixed(r1raw[0:1]);
|
|
let m1: io.stream = &mem1.vt;
|
|
let b1: [8]u8;
|
|
let sc1: bufio.scanner = bufio.newscannerbuf(m1, b1[0:8]);
|
|
let r1: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc1);
|
|
match (r1) {
|
|
case let rn: rune => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc1);
|
|
|
|
// Truncated 2-byte sequence at EOF: pending < utf8sz → invalid
|
|
// (Hare scanner.ha:272-274).
|
|
let r2raw: [1]u8;
|
|
r2raw[0] = 0xC3u8;
|
|
let mem2: memio.stream = memio.fixed(r2raw[0:1]);
|
|
let m2: io.stream = &mem2.vt;
|
|
let b2: [8]u8;
|
|
let sc2: bufio.scanner = bufio.newscannerbuf(m2, b2[0:8]);
|
|
let r2: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc2);
|
|
match (r2) {
|
|
case let rn: rune => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc2);
|
|
|
|
// UTF-16 surrogate encoding (ED A0 80 = U+D800): utf8sz accepts the
|
|
// initial byte, the decode DFA rejects the continuation.
|
|
let r3raw: [3]u8;
|
|
r3raw[0] = 0xEDu8; r3raw[1] = 0xA0u8; r3raw[2] = 0x80u8;
|
|
let mem3: memio.stream = memio.fixed(r3raw[0:3]);
|
|
let m3: io.stream = &mem3.vt;
|
|
let b3: [8]u8;
|
|
let sc3: bufio.scanner = bufio.newscannerbuf(m3, b3[0:8]);
|
|
let r3: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc3);
|
|
match (r3) {
|
|
case let rn: rune => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => { };
|
|
case bufio.overflow => abort();
|
|
};
|
|
bufio.finish(&sc3);
|
|
};
|
|
|
|
// ---- newscanner: auto-grow from the empty ctor state ------------------
|
|
|
|
@test fn newscannergrow() void = {
|
|
let src: [16]u8;
|
|
let n: i32 = putstr("hello\nworld\n", src[0:16], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
// cap starts at 0; the first readahead grows to min(BUFSZ, maxread).
|
|
let sc: bufio.scanner = bufio.newscanner(m, 1024);
|
|
|
|
// (wantlen, wantfirst, weof)
|
|
let wl: [3]i32;
|
|
let wf: [3]u8;
|
|
let weof: [3]i32;
|
|
wl[0]=5; wf[0]=104u8; weof[0]=0; // hello
|
|
wl[1]=5; wf[1]=119u8; weof[1]=0; // world
|
|
wl[2]=0; wf[2]=0u8; weof[2]=1; // eof
|
|
|
|
let i: i32 = 0;
|
|
for (i < 3) {
|
|
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r) {
|
|
case let v: str => {
|
|
assert(!(weof[i] != 0));
|
|
assert(!(v.len != wl[i]));
|
|
assert(!(v.len > 0 && v[0] != wf[i]));
|
|
};
|
|
case io.eof => { assert(!(weof[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
bufio.finish(&sc);
|
|
|
|
// scanrune over a fresh newscanner (the regex search() shape).
|
|
let r2raw: [3]u8;
|
|
r2raw[0] = 0xC3u8; r2raw[1] = 0xA9u8; // é
|
|
r2raw[2] = 122u8; // 'z'
|
|
let mem2: memio.stream = memio.fixed(r2raw[0:3]);
|
|
let m2: io.stream = &mem2.vt;
|
|
let sc2: bufio.scanner = bufio.newscanner(m2, 1024);
|
|
|
|
let want: [3]u32;
|
|
let weof2: [3]i32;
|
|
want[0]=233u32; weof2[0]=0;
|
|
want[1]=122u32; weof2[1]=0;
|
|
want[2]=0u32; weof2[2]=1;
|
|
|
|
i = 0;
|
|
for (i < 3) {
|
|
let r: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc2);
|
|
match (r) {
|
|
case let rn: rune => {
|
|
assert(!(weof2[i] != 0));
|
|
assert(!(rn: u32 != want[i]));
|
|
};
|
|
case io.eof => { assert(!(weof2[i] == 0)); };
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => abort();
|
|
case bufio.overflow => abort();
|
|
};
|
|
i += 1;
|
|
};
|
|
bufio.finish(&sc2);
|
|
};
|
|
|
|
// ---- newscanner: maxread reached without a delimiter → overflow -------
|
|
|
|
@test fn newscanneroverflow() void = {
|
|
let src: [16]u8;
|
|
let n: i32 = putstr("ABCDEFGH\n", src[0:16], 0);
|
|
|
|
let mem: memio.stream = memio.fixed(src[0:n]);
|
|
let m: io.stream = &mem.vt;
|
|
let sc: bufio.scanner = bufio.newscanner(m, 4); // token (8B) > maxread
|
|
|
|
let r: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
|
|
match (r) {
|
|
case let v: str => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => { };
|
|
};
|
|
bufio.finish(&sc);
|
|
};
|
|
|
|
// ---- readahead can't-grow → overflow, not spin / nil-deref -----------
|
|
//
|
|
// drain F-B: a scanner that cannot buffer the next byte (zero-cap) makes
|
|
// readahead unable to make progress. Pre-fix it fell through silently to
|
|
// a zero-length io.read returning 0, so scanbyte spun forever (catB-144)
|
|
// and scanrune dereferenced the nil / zero-cap s.ptr[s.start]
|
|
// (catB-145). Both entry points must now surface bufio.overflow
|
|
// (ref/hare/bufio/scanner.ha:179-181). The table crosses the two
|
|
// zero-cap constructions — nil-ptr newscanner(,0) and zero-length
|
|
// newscannerbuf — with the two single-item entry points; every row is an
|
|
// overflow. Because the new code returns promptly, this test terminates
|
|
// where the pre-fix scanbyte path would hang.
|
|
@test fn readaheadcantgrow() void = {
|
|
let src: [4]u8;
|
|
src[0]=65u8; src[1]=66u8; src[2]=67u8; src[3]=68u8; // "ABCD"
|
|
|
|
// (ctor, entry): ctor 0=newscanner(,0) (nil ptr), 1=newscannerbuf
|
|
// (,[0:0]) (zero-len buffer); entry 0=scanbyte, 1=scanrune.
|
|
let ctor: [4]i32;
|
|
let entry: [4]i32;
|
|
ctor[0]=0; entry[0]=0; // newscanner + scanbyte
|
|
ctor[1]=0; entry[1]=1; // newscanner + scanrune
|
|
ctor[2]=1; entry[2]=0; // newscannerbuf + scanbyte
|
|
ctor[3]=1; entry[3]=1; // newscannerbuf + scanrune
|
|
|
|
let zbuf: [1]u8; // sliced to [0:0] for the zero-len construction
|
|
|
|
let i: i32 = 0;
|
|
for (i < 4) {
|
|
let mem: memio.stream = memio.fixed(src[0:4]);
|
|
let m: io.stream = &mem.vt;
|
|
|
|
let sc: bufio.scanner;
|
|
if (ctor[i] == 0) {
|
|
sc = bufio.newscanner(m, 0);
|
|
} else {
|
|
sc = bufio.newscannerbuf(m, zbuf[0:0]);
|
|
};
|
|
|
|
if (entry[i] == 0) {
|
|
let rb: (u8 | io.eof | io.error | bufio.overflow) =
|
|
bufio.scanbyte(&sc);
|
|
match (rb) {
|
|
case let b: u8 => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case bufio.overflow => { };
|
|
};
|
|
} else {
|
|
let rr: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) =
|
|
bufio.scanrune(&sc);
|
|
match (rr) {
|
|
case let rn: rune => abort();
|
|
case io.eof => abort();
|
|
case let e: io.error => abort();
|
|
case utf8.invalid => abort();
|
|
case bufio.overflow => { };
|
|
};
|
|
};
|
|
|
|
bufio.finish(&sc);
|
|
i += 1;
|
|
};
|
|
};
|