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;