// indextest — exercises bytes.index/rindex, u8 and []u8 arms. A // failing row aborts via the assert/abort builtin (task #5 @test // conversion). Vectors mirror ref/hare/bytes/index.ha. package bytes_test; import bytes; // ref/hare/bytes/index.ha:112. @test fn index_byte_cases() void = { let a: [4]u8; a[0] = 1u8; a[1] = 3u8; a[2] = 3u8; a[3] = 7u8; match (bytes.index(a[0:4], 1u8)) { case let i: i32 => { assert(!(i != 0)); }; case void => abort(); }; match (bytes.index(a[0:4], 3u8)) { case let i: i32 => { assert(!(i != 1)); }; case void => abort(); }; match (bytes.index(a[0:4], 7u8)) { case let i: i32 => { assert(!(i != 3)); }; case void => abort(); }; match (bytes.index(a[0:4], 42u8)) { case let i: i32 => abort(); case void => void; }; let z: [1]u8; match (bytes.index(z[0:0], 42u8)) { case let i: i32 => abort(); case void => void; }; }; // ref/hare/bytes/index.ha:128-139. Vector strings copied verbatim where // representable as ASCII byte sequences. @test fn index_slice_cases() void = { let h1: [4]u8; h1[0] = 1u8; h1[1] = 42u8; h1[2] = 24u8; h1[3] = 0u8; let n1: [2]u8; n1[0] = 42u8; n1[1] = 24u8; match (bytes.index(h1[0:3], n1[0:2])) { case let i: i32 => { assert(!(i != 1)); }; case void => abort(); }; let h2: [4]u8; h2[0] = 1u8; h2[1] = 3u8; h2[2] = 3u8; h2[3] = 7u8; let n2: [2]u8; n2[0] = 3u8; n2[1] = 3u8; match (bytes.index(h2[0:4], n2[0:2])) { case let i: i32 => { assert(!(i != 1)); }; case void => abort(); }; // needle longer than haystack — void let h3: [3]u8; h3[0] = 1u8; h3[1] = 2u8; h3[2] = 3u8; let n3: [4]u8; n3[0] = 1u8; n3[1] = 2u8; n3[2] = 3u8; n3[3] = 4u8; match (bytes.index(h3[0:3], n3[0:4])) { case let i: i32 => abort(); case void => void; }; // len(haystack) == len(needle), match — offset 0 let h4: [2]u8; h4[0] = 42u8; h4[1] = 20u8; let n4: [2]u8; n4[0] = 42u8; n4[1] = 20u8; match (bytes.index(h4[0:2], n4[0:2])) { case let i: i32 => { assert(!(i != 0)); }; case void => abort(); }; // len(haystack) == len(needle), no match — void let h5: [4]u8; h5[0] = 1u8; h5[1] = 1u8; h5[2] = 1u8; h5[3] = 2u8; let n5: [4]u8; n5[0] = 1u8; n5[1] = 1u8; n5[2] = 1u8; n5[3] = 3u8; match (bytes.index(h5[0:4], n5[0:4])) { case let i: i32 => abort(); case void => void; }; // Partial-prefix recovery — needle [1,1,2] aligns at i=1 after the // false-start at i=0 ([1,1,1] mismatches at byte 2). Pins the // naive scanner's restart discipline. let h6: [4]u8; h6[0] = 1u8; h6[1] = 1u8; h6[2] = 1u8; h6[3] = 2u8; let n6s: [3]u8; n6s[0] = 1u8; n6s[1] = 1u8; n6s[2] = 2u8; match (bytes.index(h6[0:4], n6s[0:3])) { case let i: i32 => { assert(!(i != 1)); }; case void => abort(); }; // Same shape, longer haystack with no match anywhere. let h7: [5]u8; h7[0] = 1u8; h7[1] = 1u8; h7[2] = 1u8; h7[3] = 3u8; h7[4] = 2u8; let n7: [4]u8; n7[0] = 1u8; n7[1] = 1u8; n7[2] = 1u8; n7[3] = 2u8; match (bytes.index(h7[0:5], n7[0:4])) { case let i: i32 => abort(); case void => void; }; // empty needle — Hare returns 0 (ref/hare/bytes/index.ha:63). let z: [1]u8; let zn: [1]u8; match (bytes.index(h2[0:4], zn[0:0])) { case let i: i32 => { assert(!(i != 0)); }; case void => abort(); }; // empty haystack, non-empty needle — void match (bytes.index(z[0:0], n3[0:3])) { case let i: i32 => abort(); case void => void; }; // single-byte slice needle — should semantically equal u8 arm let n6: [1]u8; n6[0] = 7u8; match (bytes.index(h2[0:4], n6[0:1])) { case let i: i32 => { assert(!(i != 3)); }; case void => abort(); }; }; // ref/hare/bytes/index.ha:118. @test fn rindex_byte_cases() void = { let a: [4]u8; a[0] = 1u8; a[1] = 3u8; a[2] = 3u8; a[3] = 7u8; match (bytes.rindex(a[0:4], 3u8)) { case let i: i32 => { assert(!(i != 2)); }; case void => abort(); }; match (bytes.rindex(a[0:4], 42u8)) { case let i: i32 => abort(); case void => void; }; let z: [1]u8; match (bytes.rindex(z[0:0], 42u8)) { case let i: i32 => abort(); case void => void; }; }; // ref/hare/bytes/index.ha:123-125. Distinguishes from index when the // needle appears more than once. @test fn rindex_slice_cases() void = { let a: [4]u8; a[0] = 1u8; a[1] = 1u8; a[2] = 1u8; a[3] = 2u8; let n11: [2]u8; n11[0] = 1u8; n11[1] = 1u8; match (bytes.rindex(a[0:4], n11[0:2])) { case let i: i32 => { assert(!(i != 1)); }; case void => abort(); }; let n12: [2]u8; n12[0] = 1u8; n12[1] = 2u8; match (bytes.rindex(a[0:4], n12[0:2])) { case let i: i32 => { assert(!(i != 2)); }; case void => abort(); }; // absent let n99: [2]u8; n99[0] = 9u8; n99[1] = 9u8; match (bytes.rindex(a[0:4], n99[0:2])) { case let i: i32 => abort(); case void => void; }; };