From 8b2510fdca57793e9562c671fea06971083b7eb2 Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Mon, 24 Jun 2024 21:29:00 -0400 Subject: [PATCH] Coverage complete I want to spend some time documenting what I've built here before sharing with the public, but my work here is done. --- src/elements.zig | 30 ++++++++++++++++++++---------- src/test-runeset.zig | 38 ++++---------------------------------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/elements.zig b/src/elements.zig index 6056801..961c5d5 100644 --- a/src/elements.zig +++ b/src/elements.zig @@ -118,7 +118,7 @@ pub const Rune = packed struct(u32) { .c = 0, .d = 0, }, - .follow => return null, + .follow => unreachable, // nbytes is null for this case .lead => { switch (nB) { 2 => { @@ -385,15 +385,7 @@ pub const Mask = struct { /// Return the next element of the Mask. /// It is illegal to pass this function a nonexistent element. pub inline fn after(self: Mask, cu: CodeUnit) ?CodeUnit { - if (safeMode) { - if (!self.isIn(cu)) { - std.debug.print( - "{} with body {d}, byte {d}, not in 0x{x:0>16}\n", - .{ cu.kind, cu.body, cu.byte(), self.m }, - ); - std.debug.assert(false); - } - } + std.debug.assert(self.isIn(cu)); if (cu.body == 63) return null; const kind = cu.kind; var next: u6 = cu.body + 1; @@ -730,3 +722,21 @@ test "rune tests" { try expect(rD.equalToCodepoint('🤔')); try expectError(error.CodepointTooHigh, Rune.fromCodepoint(0x110000)); } + +test "invalid Rune tests" { + try expectEqual(null, Rune.fromSlice("\x81abc")); + try expectEqual(null, Rune.fromSlice("\x9f0")); + try expectEqual(null, Rune.fromSlice("\xcer")); + try expectEqual(null, Rune.fromSlice("\xff\xff\xff")); + try expectEqual(null, Rune.fromSlice("\xe2✓")); + try expectEqual(null, Rune.fromSlice("\xf0\x9f\x98q")); + try expectError( + error.InvalidUnicode, + (Rune{ + .a = 0xff, + .b = 0xff, + .c = 0, + .d = 0, + }).toCodepoint(), + ); +} diff --git a/src/test-runeset.zig b/src/test-runeset.zig index 80ca534..85a96ee 100644 --- a/src/test-runeset.zig +++ b/src/test-runeset.zig @@ -127,39 +127,10 @@ fn verifySetIteration(set: RuneSet) !void { } codeunits += rune.byteCount(); rune_count += 1; - if (lastRune.rawInt() == rune.rawInt()) { - std.debug.print( - "Saw {u} and {u}\n", - .{ lastRune.toCodepoint() catch unreachable, rune.toCodepoint() catch unreachable }, - ); - std.debug.print( - "rune bytes: {x} {x} {x} {x}\n", - .{ rune.a, rune.b, rune.c, rune.d }, - ); - try expect(false); - } else { - lastRune = rune; - } + lastRune = rune; const runeArray = rune.toByteArray(); const matchedBytes = set.matchOne(&runeArray).?; const byteCount = rune.byteCount(); - if (safemode and byteCount != matchedBytes) { - const rune_point = rune.toCodepoint(); - if (rune_point) |r| { - std.debug.print("rune {u} not a member of set\n", .{r}); - std.debug.print( - "rune bytes: {x} {x} {x} {x}\n", - .{ rune.a, rune.b, rune.c, rune.d }, - ); - std.debug.print("set length {d}, idx {d}\n", .{ set.body.len, setIter.idx }); - } else |_| { - std.debug.print( - "rune is invalid! {x} {x} {x} {x}\n", - .{ rune.a, rune.b, rune.c, rune.d }, - ); - } - } - // std.debug.print("{u}", .{rune.toCodepoint() catch unreachable}); try expectEqual(byteCount, matchedBytes); } std.debug.print("\n", .{}); @@ -371,11 +342,10 @@ fn verifySetsOfTwoLRstrings(L: LRstrings, R: LRstrings, alloc: Allocator) !void //| Test Suite -test "workshop" { +test "iterator edge cases" { const allocator = std.testing.allocator; - const g_set = try RuneSet.createFromConstString(greek.str, allocator); - defer g_set.deinit(allocator); - try verifySetsOfTwoLRstrings(deseret, greek, allocator); + try withStringVerifySetProperties("abcdefghijklmnopqrstuvwxyz", allocator); + try withStringVerifySetProperties("012345" ++ greek.str, allocator); } test "verify sets of LRstrings data" {