std.fmt: fix std-cases and perform round-trip check in ryu unit tests

This commit is contained in:
Marc Tiehuis 2024-03-09 22:22:55 +13:00
parent 2e60d4d064
commit da4acf9a48
3 changed files with 19 additions and 1 deletions

View File

@ -784,6 +784,10 @@ fn formatFloatValue(
}
}
test {
_ = &ryu128;
}
pub const Case = enum { lower, upper };
fn formatSliceHexImpl(comptime case: Case) type {

View File

@ -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" {

View File

@ -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);