| 1 | const std = @import("../std.zig"); |
| 2 | const testing = std.testing; |
| 3 | const fmt = std.fmt; |
| 4 | |
| 5 | // Hash using the specified hasher `H` asserting `expected == H(input)`. |
| 6 | pub fn assertEqualHash(comptime Hasher: anytype, comptime expected_hex: *const [Hasher.digest_length * 2:0]u8, input: []const u8) !void { |
| 7 | var h: [Hasher.digest_length]u8 = undefined; |
| 8 | Hasher.hash(input, &h, .{}); |
| 9 | |
| 10 | try assertEqual(expected_hex, &h); |
| 11 | } |
| 12 | |
| 13 | // Assert `expected` == hex(`input`) where `input` is a bytestring |
| 14 | pub fn assertEqual(comptime expected_hex: [:0]const u8, input: []const u8) !void { |
| 15 | var expected_bytes: [expected_hex.len / 2]u8 = undefined; |
| 16 | for (&expected_bytes, 0..) |*r, i| { |
| 17 | r.* = fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; |
| 18 | } |
| 19 | |
| 20 | try testing.expectEqualSlices(u8, &expected_bytes, input); |
| 21 | } |