Commit da4acf9a48

Marc Tiehuis <marc@tiehu.is>
2024-03-09 10:22:55
std.fmt: fix std-cases and perform round-trip check in ryu unit tests
1 parent 2e60d4d
Changed files (3)
lib/std/fmt/ryu128.zig
@@ -959,9 +959,23 @@ const POW5_INV_ERRORS: [154]u64 = .{
 // zig fmt: on
 
 fn check(comptime T: type, value: T, comptime expected: []const u8) !void {
+    const I = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = @bitSizeOf(T) } });
+
     var buf: [6000]u8 = undefined;
+    const value_bits: I = @bitCast(value);
     const s = try format(&buf, value, .{});
     try std.testing.expectEqualStrings(expected, s);
+
+    if (@bitSizeOf(T) != 80) {
+        const o = try std.fmt.parseFloat(T, s);
+        const o_bits: I = @bitCast(o);
+
+        if (std.math.isNan(value)) {
+            try std.testing.expect(std.math.isNan(o));
+        } else {
+            try std.testing.expectEqual(value_bits, o_bits);
+        }
+    }
 }
 
 test "format f32" {
lib/std/fmt.zig
@@ -784,6 +784,10 @@ fn formatFloatValue(
     }
 }
 
+test {
+    _ = &ryu128;
+}
+
 pub const Case = enum { lower, upper };
 
 fn formatSliceHexImpl(comptime case: Case) type {
test/cases/safety/slice sentinel mismatch - floats.zig
@@ -2,7 +2,7 @@ const std = @import("std");
 
 pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
     _ = stack_trace;
-    if (std.mem.eql(u8, message, "sentinel mismatch: expected 1.20000004e+00, found 4.0e+00")) {
+    if (std.mem.eql(u8, message, "sentinel mismatch: expected 1.2e0, found 4e0")) {
         std.process.exit(0);
     }
     std.process.exit(1);