From d82d271a7f665f3be3fe3594b558c2a65b3a9926 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Thu, 12 May 2022 22:05:40 -0700 Subject: [PATCH] Add tests for Uuid --- src/util.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/util.zig b/src/util.zig index b41cfe6..ee3f79e 100644 --- a/src/util.zig +++ b/src/util.zig @@ -88,6 +88,44 @@ pub const Uuid = struct { } }; +test "parse uuid" { + try std.testing.expectEqual( + Uuid.Nil, + try Uuid.parse("00000000-0000-0000-0000-000000000000"), + ); + + try std.testing.expectEqual( + Uuid{ + .data = @bitCast([16]u8, @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7)), + }, + try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b"), + ); +} + +test "format uuid" { + var buf = [1]u8{0} ** 36; + var fbs = std.io.fixedBufferStream(&buf); + try Uuid.format(Uuid.Nil, "", .{}, fbs.writer()); + try std.testing.expectEqualStrings("00000000-0000-0000-0000-000000000000", &buf); + + fbs.reset(); + try Uuid.format(Uuid{ + .data = @bitCast([16]u8, @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7)), + }, "", .{}, fbs.writer()); + try std.testing.expectEqualStrings("f75f5160-12d3-42c2-a81d-ad2245b7a74b", &buf); +} + +test "roundtrip random uuid" { + const uuid = Uuid.randV4(getRandom()); + + var buf: [36]u8 = undefined; + var fbs = std.io.fixedBufferStream(&buf); + try Uuid.format(uuid, "", .{}, fbs.writer()); + + const parsed = try Uuid.parse(&buf); + + try std.testing.expectEqual(uuid, parsed); +} pub const ciutf8 = struct { const Hash = std.hash.Wyhash; const View = std.unicode.Utf8View;