zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit da4acf9a485bf1658194c8cb8593dd142677138d (tree)
parent 2e60d4d06400eef07ff05d5552b69961fa9e63f9
Author: Marc Tiehuis <marc@tiehu.is>
Date:   Sat,  9 Mar 2024 22:22:55 +1300

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

Diffstat:
Mlib/std/fmt.zig | 4++++
Mlib/std/fmt/ryu128.zig | 14++++++++++++++
Mtest/cases/safety/slice sentinel mismatch - floats.zig | 2+-
3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig @@ -784,6 +784,10 @@ fn formatFloatValue( } } +test { + _ = &ryu128; +} + pub const Case = enum { lower, upper }; fn formatSliceHexImpl(comptime case: Case) type { diff --git a/lib/std/fmt/ryu128.zig b/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" { diff --git a/test/cases/safety/slice sentinel mismatch - floats.zig b/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);