Files
ww/lib/errors/errno_test.ww
Hojun-Cho aadc6618f0 lib: banner purge + WHY-only comment sweep (rule 8)
Every // ---- section banner dies (132 -> 0): names carry the WHAT.
Narration deleted (filename restatements, run-with lines, what-the-
next-line-does); every ref/hare cite, task cite, divergence, ABI/
layout contract, and ownership qualifier kept (borrowed-view lines
restored where the sweep over-cut). Comment-only proven: all 442
walk-workdir .s and 32 import-probe .s byte-identical before/after;
libbyteid 56-roster all-ID.
2026-08-08 21:10:18 +09:00

101 lines
2.8 KiB
Plaintext

// Parallel `[N]T` arrays of (errno, expected-tag) rather than a
// `[N]struct{...}` table — the cstage cgen's chained `arr[i].field`
// store gap (task #6). [[errtag]] collapses each named-void variant to
// a small int so the row body is a single integer compare.
package errors_test;
import errors;
import os;
fn streq(a: str, b: str) bool = {
if (a.len != b.len) { return false; };
let i: i32 = 0;
for (i < a.len) {
if (a[i] != b[i]) { return false; };
i += 1;
};
return true;
};
// errtag — the union discriminant as a stable int. opaque_ is 99 so a
// misrouted named condition can't masquerade as the unmapped tail.
fn errtag(e: errors.error) int = {
match (e) {
case errors.busy => return 1;
case errors.exists => return 2;
case errors.invalid => return 3;
case errors.noaccess => return 4;
case errors.noentry => return 5;
case errors.overflow => return 6;
case errors.unsupported => return 7;
case errors.timeout => return 8;
case errors.cancelled => return 9;
case errors.refused => return 10;
case errors.interrupted => return 11;
case errors.again => return 12;
case errors.netunreachable => return 13;
case let o: errors.opaque_ => return 99;
};
};
@test fn mapping() void = {
let ins: [12]os.errno;
ins[0] = os.ECONNREFUSED;
ins[1] = os.ECANCELED;
ins[2] = os.EOVERFLOW;
ins[3] = os.EACCES;
ins[4] = os.EINVAL;
ins[5] = os.EEXIST;
ins[6] = os.ENOENT;
ins[7] = os.ETIMEDOUT;
ins[8] = os.EBUSY;
ins[9] = os.EINTR;
ins[10] = os.EAGAIN;
ins[11] = os.ENETUNREACH;
let exp: [12]int;
exp[0] = 10; // refused
exp[1] = 9; // cancelled
exp[2] = 6; // overflow
exp[3] = 4; // noaccess
exp[4] = 3; // invalid
exp[5] = 2; // exists
exp[6] = 5; // noentry
exp[7] = 8; // timeout
exp[8] = 1; // busy
exp[9] = 11; // interrupted
exp[10] = 12; // again
exp[11] = 13; // netunreachable
let i: i32 = 0;
for (i < 12) {
// store-to-local, not inline errtag(errors.errno(ins[i])): the
// inline form hits #222 (large >4-eightbyte union sret-arg
// clobber, cs==ww). store-then-consume is the idiomatic shape
// regardless — it is how errno is actually used (cf
// lib/io/stream.ww:53, and Hare's errors/rt.ha callers).
let er: errors.error = errors.errno(ins[i]);
assert(!(errtag(er) != exp[i]));
i += 1;
};
};
@test fn opaquetail() void = {
// EIO (5) is outside the mapped set, so it wraps opaque_.
let e: errors.error = errors.errno(5);
assert(!(errtag(e) != 99));
match (e) {
case let o: errors.opaque_ =>
assert(!(!streq((*o.strerror)(&o.data), "Unknown error")));
case =>
abort();
};
};
@test fn strerrortext() void = {
assert(!(!streq(os.strerror(os.ENOENT), "No such file or directory")));
assert(!(!streq(os.strerror(os.EINVAL), "Invalid argument")));
assert(!(!streq(os.strerror(5), "Unknown error")));
};