lib/bytes+test: Hare port (equal / index / rindex / contains / has{prefix,suffix} / reverse / zero)
Mirrors ref/hare/bytes/{equal,index,contains,reverse,zero}.ha for
the in-tree subset used by lib/encoding, lib/bufio, lib/memio;
converts 4 hextest sites from local beq to bytes.equal and drops
the now-dead beq in utf8test.
Surface:
- equal(a, b: []u8) bool
- index(s: []u8, needle: (u8 | []u8)) (i32 | void)
- rindex(s: []u8, needle: (u8 | []u8)) (i32 | void)
- contains(s: []u8, needle: (u8 | []u8)) bool
- hasprefix(s, pre: []u8) bool
- hassuffix(s, suf: []u8) bool
- reverse(s: []u8) void (already present, citation added)
- zero(s: []u8) void (already present, citation added)
Two documented Hare-fidelity gaps (cited in lib/bytes/bytes.ww
header, no in-tree caller demands them yet):
- index_slice / rindex_slice use naive O(n·m). Hare specialises
2/3/4-byte needles + falls back to Crochemore-Perrin two-way
(ref/hare/bytes/two_way.ha). Correctness equivalent.
- contains takes a single needle. Hare uses variadic
needle: (u8 | []u8)... (ref/hare/bytes/contains.ha:5).
Tests: 967_bytes_run drives lib/bytes/bytestest.ww via ww run.
Eight @test fns × table-driven row sets: equal (5), index_byte
(5), index_slice (10), rindex_byte (3), rindex_slice (3),
contains (4), hasprefix (6 verbatim from contains.ha:25),
hassuffix (6 verbatim from contains.ha:40).
Call-site conversions in the same commit (the conversions are
the proof the API is wired): lib/encoding/hex/hextest.ww drops
the local beq helper and 4 callers switch to bytes.equal;
lib/encoding/utf8/utf8test.ww drops the dead beq helper.
91/91 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// signalled-then-fail()-with-+10 pattern as the rest of the 9xx
|
||||
// stdlib tests; non-zero exit pinpoints the failing scenario.
|
||||
|
||||
use bytes;
|
||||
use hex;
|
||||
use os;
|
||||
|
||||
@@ -28,16 +29,6 @@ fn streq(buf: []u8, expect: str) bool = {
|
||||
return true;
|
||||
};
|
||||
|
||||
fn beq(a: []u8, b: []u8) 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;
|
||||
};
|
||||
|
||||
// ---- encodedsize / decodedsize -----------------------------------------
|
||||
|
||||
@test fn sizes() void = {
|
||||
@@ -109,7 +100,7 @@ fn beq(a: []u8, b: []u8) bool = {
|
||||
let want: [8]u8;
|
||||
want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8;
|
||||
want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8;
|
||||
if (!beq(dst[0:8], want[0:8])) { fail(); };
|
||||
if (!bytes.equal(dst[0:8], want[0:8])) { fail(); };
|
||||
};
|
||||
case let e: hex.invalid => { fail(); };
|
||||
};
|
||||
@@ -126,7 +117,7 @@ fn beq(a: []u8, b: []u8) bool = {
|
||||
let want: [8]u8;
|
||||
want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8;
|
||||
want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8;
|
||||
if (!beq(dst[0:8], want[0:8])) { fail(); };
|
||||
if (!bytes.equal(dst[0:8], want[0:8])) { fail(); };
|
||||
};
|
||||
case let e: hex.invalid => { fail(); };
|
||||
};
|
||||
@@ -146,7 +137,7 @@ fn beq(a: []u8, b: []u8) bool = {
|
||||
let want: [8]u8;
|
||||
want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8;
|
||||
want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8;
|
||||
if (!beq(dst[0:8], want[0:8])) { fail(); };
|
||||
if (!bytes.equal(dst[0:8], want[0:8])) { fail(); };
|
||||
};
|
||||
case let e: hex.invalid => { fail(); };
|
||||
};
|
||||
@@ -218,7 +209,7 @@ fn beq(a: []u8, b: []u8) bool = {
|
||||
match (r) {
|
||||
case let m: i32 => {
|
||||
if (m != 256) { fail(); };
|
||||
if (!beq(src[0:256], dec[0:256])) { fail(); };
|
||||
if (!bytes.equal(src[0:256], dec[0:256])) { fail(); };
|
||||
};
|
||||
case let e: hex.invalid => { fail(); };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user