|
1 | 1 | const std = @import("std");
|
2 | 2 | const msgpack = @import("msgpack.zig");
|
3 | 3 |
|
| 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 | + |
4 | 14 | const float_types = [_]type{ f32, f64 };
|
5 | 15 |
|
6 | 16 | test "readFloat: null" {
|
7 |
| - const buffer = [_]u8{0xc0}; |
8 | 17 | 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))); |
12 | 36 | }
|
13 | 37 | }
|
14 | 38 |
|
15 | 39 | test "readFloat: float32 (pi)" {
|
16 | 40 | 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(), .{}); |
21 | 43 | try std.testing.expectApproxEqAbs(std.math.pi, value, std.math.floatEpsAt(f32, @floatCast(value)));
|
22 | 44 | }
|
23 | 45 | }
|
24 | 46 |
|
25 |
| -test "readFloat: float32 (zero)" { |
| 47 | +test "readFloat: float64 (pi)" { |
26 | 48 | 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))); |
32 | 52 | }
|
33 | 53 | }
|
34 | 54 |
|
35 |
| -test "readFloat: float64 (pi)" { |
| 55 | +test "readFloat: float32 (nan)" { |
36 | 56 | 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)); |
42 | 60 | }
|
43 | 61 | }
|
44 | 62 |
|
45 |
| -test "readFloat: float64 (zero)" { |
| 63 | +test "readFloat: float64 (nan)" { |
46 | 64 | 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)); |
52 | 76 | }
|
53 | 77 | }
|
54 | 78 |
|
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; |
57 | 117 | 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()); |
61 | 120 | }
|
62 | 121 |
|
63 |
| -test "writeFloat: float64" { |
64 |
| - var buffer: [100]u8 = undefined; |
| 122 | +test "writeFloat: float64 (nan)" { |
| 123 | + var buffer: [16]u8 = undefined; |
65 | 124 | 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 | + } |
69 | 150 | }
|
0 commit comments