Skip to content

Commit 0e1b302

Browse files
committed
Add more float tests
1 parent 6091de8 commit 0e1b302

File tree

2 files changed

+129
-48
lines changed

2 files changed

+129
-48
lines changed

src/utils/msgpack/msgpack_test_bool.zig

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ test "readBool: wrong type" {
2323
}
2424

2525
test "writeBool: false" {
26-
var output = std.ArrayList(u8).init(std.testing.allocator);
27-
defer output.deinit();
28-
try msgpack.pack(bool, output.writer(), false);
29-
try std.testing.expectEqualSlices(u8, &packed_false, output.items);
26+
var buffer: [16]u8 = undefined;
27+
var stream = std.io.fixedBufferStream(&buffer);
28+
try msgpack.pack(bool, stream.writer(), false);
29+
try std.testing.expectEqualSlices(u8, &packed_false, stream.getWritten());
3030
}
3131

3232
test "writeBool: true" {
33-
var output = std.ArrayList(u8).init(std.testing.allocator);
34-
defer output.deinit();
35-
try msgpack.pack(bool, output.writer(), true);
36-
try std.testing.expectEqualSlices(u8, &packed_true, output.items);
33+
var buffer: [16]u8 = undefined;
34+
var stream = std.io.fixedBufferStream(&buffer);
35+
try msgpack.pack(bool, stream.writer(), true);
36+
try std.testing.expectEqualSlices(u8, &packed_true, stream.getWritten());
3737
}
3838

3939
test "writeBool: null" {
40-
var output = std.ArrayList(u8).init(std.testing.allocator);
41-
defer output.deinit();
42-
try msgpack.pack(?bool, output.writer(), null);
43-
try std.testing.expectEqualSlices(u8, &packed_null, output.items);
40+
var buffer: [16]u8 = undefined;
41+
var stream = std.io.fixedBufferStream(&buffer);
42+
try msgpack.pack(?bool, stream.writer(), null);
43+
try std.testing.expectEqualSlices(u8, &packed_null, stream.getWritten());
4444
}
+117-36
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,150 @@
11
const std = @import("std");
22
const msgpack = @import("msgpack.zig");
33

4+
const packed_null = [_]u8{0xc0};
5+
const packed_float32_zero = [_]u8{ 0xca, 0x00, 0x00, 0x00, 0x00 };
6+
const packed_float64_zero = [_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
7+
const packed_float32_pi = [_]u8{ 0xca, 0x40, 0x49, 0x0f, 0xdb };
8+
const packed_float64_pi = [_]u8{ 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18 };
9+
const packed_float32_nan = [_]u8{ 0xca, 0x7f, 0xc0, 0x00, 0x00 };
10+
const packed_float64_nan = [_]u8{ 0xcb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
11+
const packed_float32_inf = [_]u8{ 0xca, 0x7f, 0x80, 0x00, 0x00 };
12+
const packed_float64_inf = [_]u8{ 0xcb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
13+
414
const float_types = [_]type{ f32, f64 };
515

616
test "readFloat: null" {
7-
const buffer = [_]u8{0xc0};
817
inline for (float_types) |T| {
9-
var stream = std.io.fixedBufferStream(&buffer);
10-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
11-
try std.testing.expectEqual(null, try unpacker.read(?T));
18+
var stream = std.io.fixedBufferStream(&packed_null);
19+
try std.testing.expectEqual(null, try msgpack.unpack(?T, stream.reader(), .{}));
20+
}
21+
}
22+
23+
test "readFloat: float32 (zero)" {
24+
inline for (float_types) |T| {
25+
var stream = std.io.fixedBufferStream(&packed_float32_zero);
26+
const value = try msgpack.unpack(T, stream.reader(), .{});
27+
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(f32, @floatCast(value)));
28+
}
29+
}
30+
31+
test "readFloat: float64 (zero)" {
32+
inline for (float_types) |T| {
33+
var stream = std.io.fixedBufferStream(&packed_float64_zero);
34+
const value = try msgpack.unpack(T, stream.reader(), .{});
35+
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(T, @floatCast(value)));
1236
}
1337
}
1438

1539
test "readFloat: float32 (pi)" {
1640
inline for (float_types) |T| {
17-
const buffer = [_]u8{ 0xca, 0x40, 0x49, 0x0f, 0xdb };
18-
var stream = std.io.fixedBufferStream(&buffer);
19-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
20-
const value = try unpacker.read(T);
41+
var stream = std.io.fixedBufferStream(&packed_float32_pi);
42+
const value = try msgpack.unpack(T, stream.reader(), .{});
2143
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(f32, @floatCast(value)));
2244
}
2345
}
2446

25-
test "readFloat: float32 (zero)" {
47+
test "readFloat: float64 (pi)" {
2648
inline for (float_types) |T| {
27-
const buffer = [_]u8{ 0xca, 0x00, 0x00, 0x00, 0x00 };
28-
var stream = std.io.fixedBufferStream(&buffer);
29-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
30-
const value = try unpacker.read(T);
31-
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(f32, @floatCast(value)));
49+
var stream = std.io.fixedBufferStream(&packed_float64_pi);
50+
const value = try msgpack.unpack(T, stream.reader(), .{});
51+
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(T, @floatCast(value)));
3252
}
3353
}
3454

35-
test "readFloat: float64 (pi)" {
55+
test "readFloat: float32 (nan)" {
3656
inline for (float_types) |T| {
37-
const buffer = [_]u8{ 0xcb, 0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18 };
38-
var stream = std.io.fixedBufferStream(&buffer);
39-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
40-
const value = try unpacker.read(T);
41-
try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(if (@bitSizeOf(T) >= 64) f64 else f32, @floatCast(value)));
57+
var stream = std.io.fixedBufferStream(&packed_float32_nan);
58+
const value = try msgpack.unpack(T, stream.reader(), .{});
59+
try std.testing.expect(std.math.isNan(value));
4260
}
4361
}
4462

45-
test "readFloat: float64 (zero)" {
63+
test "readFloat: float64 (nan)" {
4664
inline for (float_types) |T| {
47-
const buffer = [_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
48-
var stream = std.io.fixedBufferStream(&buffer);
49-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
50-
const value = try unpacker.read(T);
51-
try std.testing.expectApproxEqAbs(0.0, value, std.math.floatEpsAt(if (@bitSizeOf(T) >= 64) f64 else f32, @floatCast(value)));
65+
var stream = std.io.fixedBufferStream(&packed_float64_nan);
66+
const value = try msgpack.unpack(T, stream.reader(), .{});
67+
try std.testing.expect(std.math.isNan(value));
68+
}
69+
}
70+
71+
test "readFloat: float32 (inf)" {
72+
inline for (float_types) |T| {
73+
var stream = std.io.fixedBufferStream(&packed_float32_inf);
74+
const value = try msgpack.unpack(T, stream.reader(), .{});
75+
try std.testing.expect(std.math.isInf(value));
5276
}
5377
}
5478

55-
test "writeFloat: float32" {
56-
var buffer: [100]u8 = undefined;
79+
test "readFloat: float64 (inf)" {
80+
inline for (float_types) |T| {
81+
var stream = std.io.fixedBufferStream(&packed_float64_inf);
82+
const value = try msgpack.unpack(T, stream.reader(), .{});
83+
try std.testing.expect(std.math.isInf(value));
84+
}
85+
}
86+
87+
test "writeFloat: float32 (pi)" {
88+
var buffer: [16]u8 = undefined;
89+
var stream = std.io.fixedBufferStream(&buffer);
90+
try msgpack.pack(f32, stream.writer(), std.math.pi);
91+
try std.testing.expectEqualSlices(u8, &packed_float32_pi, stream.getWritten());
92+
}
93+
94+
test "writeFloat: float64 (pi)" {
95+
var buffer: [16]u8 = undefined;
96+
var stream = std.io.fixedBufferStream(&buffer);
97+
try msgpack.pack(f64, stream.writer(), std.math.pi);
98+
try std.testing.expectEqualSlices(u8, &packed_float64_pi, stream.getWritten());
99+
}
100+
101+
test "writeFloat: float32 (zero)" {
102+
var buffer: [16]u8 = undefined;
103+
var stream = std.io.fixedBufferStream(&buffer);
104+
try msgpack.pack(f32, stream.writer(), 0.0);
105+
try std.testing.expectEqualSlices(u8, &packed_float32_zero, stream.getWritten());
106+
}
107+
108+
test "writeFloat: float64 (zero)" {
109+
var buffer: [16]u8 = undefined;
110+
var stream = std.io.fixedBufferStream(&buffer);
111+
try msgpack.pack(f64, stream.writer(), 0.0);
112+
try std.testing.expectEqualSlices(u8, &packed_float64_zero, stream.getWritten());
113+
}
114+
115+
test "writeFloat: float32 (nan)" {
116+
var buffer: [16]u8 = undefined;
57117
var stream = std.io.fixedBufferStream(&buffer);
58-
var packer = msgpack.packer(stream.writer());
59-
try packer.writeFloat(f32, std.math.pi);
60-
try std.testing.expectEqualSlices(u8, &.{ 0xca, 0x40, 0x49, 0x0f, 0xdb }, stream.getWritten());
118+
try msgpack.pack(f32, stream.writer(), std.math.nan(f32));
119+
try std.testing.expectEqualSlices(u8, &packed_float32_nan, stream.getWritten());
61120
}
62121

63-
test "writeFloat: float64" {
64-
var buffer: [100]u8 = undefined;
122+
test "writeFloat: float64 (nan)" {
123+
var buffer: [16]u8 = undefined;
65124
var stream = std.io.fixedBufferStream(&buffer);
66-
var packer = msgpack.packer(stream.writer());
67-
try packer.writeFloat(f64, std.math.pi);
68-
try std.testing.expectEqualSlices(u8, &.{ 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18 }, stream.getWritten());
125+
try msgpack.pack(f64, stream.writer(), std.math.nan(f64));
126+
try std.testing.expectEqualSlices(u8, &packed_float64_nan, stream.getWritten());
127+
}
128+
129+
test "writeFloat: float32 (inf)" {
130+
var buffer: [16]u8 = undefined;
131+
var stream = std.io.fixedBufferStream(&buffer);
132+
try msgpack.pack(f32, stream.writer(), std.math.inf(f32));
133+
try std.testing.expectEqualSlices(u8, &packed_float32_inf, stream.getWritten());
134+
}
135+
136+
test "writeFloat: float64 (inf)" {
137+
var buffer: [16]u8 = undefined;
138+
var stream = std.io.fixedBufferStream(&buffer);
139+
try msgpack.pack(f64, stream.writer(), std.math.inf(f64));
140+
try std.testing.expectEqualSlices(u8, &packed_float64_inf, stream.getWritten());
141+
}
142+
143+
test "writeFloat: null" {
144+
inline for (float_types) |T| {
145+
var buffer: [16]u8 = undefined;
146+
var stream = std.io.fixedBufferStream(&buffer);
147+
try msgpack.pack(?T, stream.writer(), null);
148+
try std.testing.expectEqualSlices(u8, &packed_null, stream.getWritten());
149+
}
69150
}

0 commit comments

Comments
 (0)