-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatof256_test.go
More file actions
312 lines (291 loc) · 8.18 KB
/
atof256_test.go
File metadata and controls
312 lines (291 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package floats
import (
"math"
"strconv"
"testing"
)
var parseFloat256Tests = []struct {
input string
want Float256
err error
}{
{"0", exact256(0), nil},
{"-0", exact256(math.Copysign(0, -1)), nil},
{"1", exact256(1.0), nil},
{"1.5", exact256(1.5), nil},
{"-2.75", exact256(-2.75), nil},
{"10", exact256(10), nil},
{"100", exact256(100), nil},
{"1000", exact256(1000), nil},
{"10000", exact256(10000), nil},
{
// largest normal number
"1.61132571748576047361957211845200501064402387454966951747637125049607183e+78913",
Float256{
0x7fff_efff_ffff_ffff, 0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff,
},
nil,
},
{
// smallest positive normal number
"2.48242795146434978829932822291387172367768770607964686927095329791378756e-78913",
Float256{
0x0000_1000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0000,
},
nil,
},
{
// largest subnormal number
"2.48242795146434978829932822291387172367768770607964686927095329791378754e-78913",
Float256{
0x0000_0fff_ffff_ffff, 0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff,
},
nil,
},
{
// smallest positive subnormal number
"2e-78984",
Float256{
0x0000_0000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0001,
},
nil,
},
{
"5.26013590154837350724098988288012866555033980282317385949828090306873215e+210",
Float256{
0x402b_b000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0000,
},
nil,
},
// next float256 - too large
{
// largest normal number
"+1.611325717485760473619572118452005010644023874549669517476371250496071831e+78913",
exact256(math.Inf(1)),
strconv.ErrRange,
},
{
"-1.611325717485760473619572118452005010644023874549669517476371250496071831e+78913",
exact256(math.Inf(-1)),
strconv.ErrRange,
},
// Hexadecimal floating-point.
{"0x1p+0", exact256(1.0), nil},
{"0x1p1", exact256(2.0), nil},
{"0x1.8p+1", exact256(3.0), nil},
{"0x1p-1", exact256(0.5), nil},
{"-0x2p3", exact256(-16), nil},
{"0x0.fp4", exact256(15), nil},
{"0x1e2", exact256(0), strconv.ErrSyntax}, // missing 'p' exponent
{"1p2", exact256(0), strconv.ErrSyntax}, // missing '0x' prefix
// Rounding
{
"0x1.000000000000000000000000000000000000000000000000000000000008p+00",
exact256(1.0), nil, // round down
},
{
"0x1.00000000000000000000000000000000000000000000000000000000000800001p+00",
Float256{
0x3fff_f000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0001,
},
nil, // round up
},
{
"0x1.000000000000000000000000000000000000000000000000000000000017fffffp+00",
Float256{
0x3fff_f000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0001,
},
nil, // round down
},
{
"0x1.000000000000000000000000000000000000000000000000000000000018p+00",
Float256{
0x3fff_f000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0002,
},
nil, // round up
},
{
"0x1.fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8p+00",
exact256(2.0), nil, // round up
},
// NaNs
{"nan", exact256(math.NaN()), nil},
{"NaN", exact256(math.NaN()), nil},
{"NAN", exact256(math.NaN()), nil},
// Infs
{"Inf", exact256(math.Inf(1)), nil},
{"-Inf", exact256(math.Inf(-1)), nil},
{"+INF", exact256(math.Inf(1)), nil},
{"-Infinity", exact256(math.Inf(-1)), nil},
{"+INFINITY", exact256(math.Inf(1)), nil},
{"Infinity", exact256(math.Inf(1)), nil},
// try to overflow exponent
{"1e-4294967296", exact256(0), nil},
{"1e+4294967296", exact256(math.Inf(1)), strconv.ErrRange},
{"1e-18446744073709551616", exact256(0), nil},
{"1e+18446744073709551616", exact256(math.Inf(1)), strconv.ErrRange},
{"0x1p-4294967296", exact256(0), nil},
{"0x1p+4294967296", exact256(math.Inf(1)), strconv.ErrRange},
{"0x1p-18446744073709551616", exact256(0), nil},
{"0x1p+18446744073709551616", exact256(math.Inf(1)), strconv.ErrRange},
// Parse errors
{"1e", exact256(0), strconv.ErrSyntax},
{"1e-", exact256(0), strconv.ErrSyntax},
{".e-1", exact256(0), strconv.ErrSyntax},
{"1\x00.2", exact256(0), strconv.ErrSyntax},
{"0x", exact256(0), strconv.ErrSyntax},
{"0x.", exact256(0), strconv.ErrSyntax},
{"0x1", exact256(0), strconv.ErrSyntax},
{"0x.1", exact256(0), strconv.ErrSyntax},
{"0x1p", exact256(0), strconv.ErrSyntax},
{"0x.1p", exact256(0), strconv.ErrSyntax},
{"0x1p+", exact256(0), strconv.ErrSyntax},
{"0x.1p+", exact256(0), strconv.ErrSyntax},
{"0x1p-", exact256(0), strconv.ErrSyntax},
{"0x.1p-", exact256(0), strconv.ErrSyntax},
{"0x1p+2", exact256(4), nil},
{"0x.1p+2", exact256(0.25), nil},
{"0x1p-2", exact256(0.25), nil},
{"0x.1p-2", exact256(0.015625), nil},
}
func TestParseFloat256(t *testing.T) {
for _, tt := range parseFloat256Tests {
got, err := ParseFloat256(tt.input)
if err != nil {
numErr, ok := err.(*strconv.NumError)
if !ok {
t.Errorf("ParseFloat256(%q) unexpected error type: %v", tt.input, err)
continue
}
if numErr.Func != "ParseFloat256" {
t.Errorf("ParseFloat256(%q) unexpected Func in NumError: got %q, want %q", tt.input, numErr.Func, "ParseFloat256")
}
if numErr.Num != tt.input {
t.Errorf("ParseFloat256(%q) unexpected Num in NumError: got %q, want %q", tt.input, numErr.Num, tt.input)
}
err = numErr.Err
}
if !eq256(got, tt.want) || err != tt.err {
t.Errorf("ParseFloat256(%q) = (%v, %v) want (%v, %v)", tt.input, got, err, tt.want, tt.err)
}
}
}
func FuzzParseFloat256(f *testing.F) {
for _, tt := range parseFloat256Tests {
f.Add(tt.input)
}
f.Fuzz(func(t *testing.T, input string) {
f0, err := ParseFloat256(input)
if err != nil {
return
}
s := f0.String()
f1, err := ParseFloat256(s)
if err != nil {
t.Fatal(err)
}
if !eq256(f0, f1) {
t.Fatalf("ParseFloat256(%q) = %v; after String() = %q and ParseFloat256 = %v", input, f0, s, f1)
}
})
}
func BenchmarkParseFloat256_Decimal(b *testing.B) {
for b.Loop() {
_, err := ParseFloat256("33909")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseFloat256_BigDecimal(b *testing.B) {
for b.Loop() {
_, err := ParseFloat256("220855883097298041197912187592864814478435487109452369765200775161577472") // = 1 << 237
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseFloat256_Float(b *testing.B) {
for b.Loop() {
_, err := ParseFloat256("339.778")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseFloat256_FloatExp(b *testing.B) {
for b.Loop() {
_, err := ParseFloat256("-5.09e-3")
if err != nil {
b.Fatal(err)
}
}
}
func TestFloat256_UnmarshalJSON(t *testing.T) {
tests := []struct {
input string
want Float256
}{
{"0", exact256(0)},
{"1.5", exact256(1.5)},
{"-2.75", exact256(-2.75)},
}
for _, tt := range tests {
var f Float256
err := f.UnmarshalJSON([]byte(tt.input))
if err != nil {
t.Errorf("Float256.UnmarshalJSON(%q) unexpected error: %v", tt.input, err)
continue
}
if !eq256(f, tt.want) {
t.Errorf("Float256.UnmarshalJSON(%q) = %v; want %v", tt.input, f, tt.want)
}
}
// invalid input does not modify the receiver
f := exact256(1.5)
err := f.UnmarshalJSON([]byte("invalid"))
if err == nil {
t.Errorf("Float256.UnmarshalJSON(%q) expected error, got nil", "invalid")
}
if !eq256(f, exact256(1.5)) {
t.Errorf("Float256.UnmarshalJSON(%q) modified receiver on error: got %v, want %v", "invalid", f, exact256(1.5))
}
}
func TestFloat256_UnmarshalText(t *testing.T) {
tests := []struct {
input string
want Float256
}{
{"0", exact256(0)},
{"1.5", exact256(1.5)},
{"-2.75", exact256(-2.75)},
}
for _, tt := range tests {
var f Float256
err := f.UnmarshalText([]byte(tt.input))
if err != nil {
t.Errorf("Float256.UnmarshalText(%q) unexpected error: %v", tt.input, err)
continue
}
if !eq256(f, tt.want) {
t.Errorf("Float256.UnmarshalText(%q) = %v; want %v", tt.input, f, tt.want)
}
}
// invalid input does not modify the receiver
f := exact256(1.5)
err := f.UnmarshalText([]byte("invalid"))
if err == nil {
t.Errorf("Float256.UnmarshalText(%q) expected error, got nil", "invalid")
}
if !eq256(f, exact256(1.5)) {
t.Errorf("Float256.UnmarshalText(%q) modified receiver on error: got %v, want %v", "invalid", f, exact256(1.5))
}
}