Files
ww/lib/encoding/hex/hextest.ww
Hojun-Cho e3f49234f9 lib/encoding/hex: align to Hare io-streaming surface
The old buffer surface (encodedsize/decodedsize + encode(dst,src) i32 +
decode(dst,src) (i32|invalid)) does not exist in Hare — it predates the
#94 io vtable and mis-cited hex.ha:175 while implementing a different
signature. Replace it with Hare's real surface
(ref/hare/encoding/hex/hex.ha):

  - newencoder(out: io.handle) (:28) — write-only encoder stream.
  - encode(out: io.handle, in) (size | io.error) (:91).
  - encodestr(in) str (:68).
  - decodestr(s) ([]u8 | errors.invalid) (:175).

Divergences (documented at-site):

  - The streaming DECODER (newdecoder/decode_reader, :120,:129) is
    DEFERRED to #247, blocked on #199b: Hare's decode_reader returns
    errors::invalid, which fits Hare's io::error (spreads
    ...errors::error). ww's io.error (lib/io/types.ww:55-62) does not
    carry errors.invalid, and io.read's (size|eof|error) can't propagate
    it, so a hex decoder *stream* can't faithfully report invalid hex
    through io.read yet. decodestr ships as a direct transform meanwhile.
  - nomem dropped from encodestr/decodestr returns (ww memio.dynamic has
    no failure path — same memio.string rule-9 carve-out, memio.ww:208).
  - The local hex.invalid type is deleted in favor of errors.invalid
    (that was the original divergence).
  - encode uses a single io.write rather than Hare's io::writeall (ww has
    none — fmt.fprint:498-501: callers drive write-all over raw io.write;
    encode_writer is whole-slice so a single write is equivalent).
  - dump (:212) deferred: ww has no default-arg support and fmt's
    formattable lacks u64 (#209), so the address column can't be ported
    faithfully yet.

hex is now import-bearing, so it moves off the 900_stdlib standalone-
compile list (like fmt/os/strings/bufio/bytes/errors before it); coverage
stays at 979_hex_run.c. The stale "mirrors lib/encoding/hex.encode"
comments in lib/encoding/utf8/utf8.ww are updated, which regenerates the
6 selfhost combined.ww (5 cmd + test/smoke) (comment-only, byte-id-neutral).
2026-06-02 00:40:04 +09:00

177 lines
4.6 KiB
Plaintext

// hextest — exercises lib/encoding/hex's io-streaming surface. Run with
// `out/bin/ww run lib/encoding/hex/hextest.ww`. Mirrors Hare's hex
// @test fns (ref/hare/encoding/hex/hex.ha:82,96,194) plus a full-byte
// round-trip. Same signalled-then-fail()-with-+10 pattern as the rest of
// the 9xx stdlib tests; non-zero exit pinpoints the failing scenario.
//
// The streaming decoder (newdecoder) is deferred (#247), so the decode
// side is exercised through decodestr only.
package hex;
import bytes;
import errors;
import hex;
import io;
import memio;
import os;
let signalled: i32 = 0;
fn fail() void = { os.exit(signalled + 10); };
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;
};
fn cafebabe() [8]u8 = {
let r: [8]u8;
r[0] = 0xCAu8; r[1] = 0xFEu8; r[2] = 0xBAu8; r[3] = 0xBEu8;
r[4] = 0xDEu8; r[5] = 0xADu8; r[6] = 0xF0u8; r[7] = 0x0Du8;
return r;
};
// ---- encodestr ---- ref/hare/encoding/hex/hex.ha:82
@test fn encodestr_basic() void = {
let in: [8]u8 = cafebabe();
if (!streq(hex.encodestr(in[0:8]), "cafebabedeadf00d")) { fail(); };
};
// 0x00 → "00" guards a sign-extend / signed-shift on the high nibble.
@test fn encodestr_zero() void = {
let in: [1]u8; in[0] = 0u8;
if (!streq(hex.encodestr(in[0:1]), "00")) { fail(); };
};
// 0xFF → "ff" guards an off-by-one in the digit lookup.
@test fn encodestr_ff() void = {
let in: [1]u8; in[0] = 0xFFu8;
if (!streq(hex.encodestr(in[0:1]), "ff")) { fail(); };
};
@test fn encodestr_empty() void = {
let in: [1]u8;
if (hex.encodestr(in[0:0]).len != 0) { fail(); };
};
// ---- encode (io.handle sink) ---- ref/hare/encoding/hex/hex.ha:96
@test fn encode_stream() void = {
let in: [8]u8 = cafebabe();
let out: memio.stream = memio.dynamic();
match (hex.encode(&out.vt, in[0:8])) {
case let n: size => { if (n: i32 != 16) { fail(); }; };
case let e: io.error => fail();
};
if (!streq(memio.string(&out), "cafebabedeadf00d")) { fail(); };
};
// ---- decodestr round-trip ---- ref/hare/encoding/hex/hex.ha:194
@test fn decodestr_lower() void = {
match (hex.decodestr("cafebabedeadf00d")) {
case let b: []u8 => {
let want: [8]u8 = cafebabe();
if (!bytes.equal(b, want[0:8])) { fail(); };
};
case let e: errors.invalid => fail();
};
};
// Mixed/upper case must decode too; the encoder is lowercase-only but
// the decoder accepts both per ref/hare/encoding/hex/README:13.
@test fn decodestr_upper() void = {
match (hex.decodestr("CAFEBABEDEADF00D")) {
case let b: []u8 => {
let want: [8]u8 = cafebabe();
if (!bytes.equal(b, want[0:8])) { fail(); };
};
case let e: errors.invalid => fail();
};
};
@test fn decodestr_mixed() void = {
match (hex.decodestr("CaFeBaBeDeAdF00d")) {
case let b: []u8 => {
let want: [8]u8 = cafebabe();
if (!bytes.equal(b, want[0:8])) { fail(); };
};
case let e: errors.invalid => fail();
};
};
@test fn decodestr_empty() void = {
match (hex.decodestr("")) {
case let b: []u8 => { if (b.len != 0) { fail(); }; };
case let e: errors.invalid => fail();
};
};
// ---- decodestr error cases ---- ref/hare/encoding/hex/hex.ha:154,199
//
// Odd length and non-hex chars both return errors.invalid.
@test fn decodestr_odd() void = {
match (hex.decodestr("abc")) {
case let b: []u8 => fail();
case let e: errors.invalid => void;
};
};
@test fn decodestr_bad() void = {
match (hex.decodestr("zz00")) { // 'z' isn't hex
case let b: []u8 => fail();
case let e: errors.invalid => void;
};
};
@test fn decodestr_bad_mid() void = {
match (hex.decodestr("aabbgg")) { // 'g' isn't hex
case let b: []u8 => fail();
case let e: errors.invalid => void;
};
};
// ---- round-trip every byte value 0..255 --------------------------------
@test fn roundtrip_all_bytes() void = {
let src: [256]u8;
let i: i32 = 0;
for (i < 256) {
src[i] = i: u8;
i += 1;
};
let s: str = hex.encodestr(src[0:256]);
if (s.len != 512) { fail(); };
match (hex.decodestr(s)) {
case let b: []u8 => {
if (b.len != 256) { fail(); };
if (!bytes.equal(b, src[0:256])) { fail(); };
};
case let e: errors.invalid => fail();
};
};
export fn main() i32 = {
signalled = 1; encodestr_basic();
signalled = 2; encodestr_zero();
signalled = 3; encodestr_ff();
signalled = 4; encodestr_empty();
signalled = 5; encode_stream();
signalled = 6; decodestr_lower();
signalled = 7; decodestr_upper();
signalled = 8; decodestr_mixed();
signalled = 9; decodestr_empty();
signalled = 10; decodestr_odd();
signalled = 11; decodestr_bad();
signalled = 12; decodestr_bad_mid();
signalled = 13; roundtrip_all_bytes();
return 0;
};