1
2
3
4
5 package strconv_test
6
7 import (
8 . "internal/strconv"
9 "math"
10 "math/rand"
11 "reflect"
12 "strings"
13 "sync"
14 "testing"
15 )
16
17 type atofTest struct {
18 in string
19 out string
20 err error
21 }
22
23 var atoftests = []atofTest{
24 {"", "0", ErrSyntax},
25 {"1", "1", nil},
26 {"+1", "1", nil},
27 {"1x", "0", ErrSyntax},
28 {"1.1.", "0", ErrSyntax},
29 {"1e23", "1e+23", nil},
30 {"1E23", "1e+23", nil},
31 {"100000000000000000000000", "1e+23", nil},
32 {"1e-100", "1e-100", nil},
33 {"123456700", "1.234567e+08", nil},
34 {"99999999999999974834176", "9.999999999999997e+22", nil},
35 {"100000000000000000000001", "1.0000000000000001e+23", nil},
36 {"100000000000000008388608", "1.0000000000000001e+23", nil},
37 {"100000000000000016777215", "1.0000000000000001e+23", nil},
38 {"100000000000000016777216", "1.0000000000000003e+23", nil},
39 {"-1", "-1", nil},
40 {"-0.1", "-0.1", nil},
41 {"-0", "-0", nil},
42 {"1e-20", "1e-20", nil},
43 {"625e-3", "0.625", nil},
44
45
46 {"0x1p0", "1", nil},
47 {"0x1p1", "2", nil},
48 {"0x1p-1", "0.5", nil},
49 {"0x1ep-1", "15", nil},
50 {"-0x1ep-1", "-15", nil},
51 {"-0x1_ep-1", "-15", nil},
52 {"0x1p-200", "6.223015277861142e-61", nil},
53 {"0x1p200", "1.6069380442589903e+60", nil},
54 {"0x1fFe2.p0", "131042", nil},
55 {"0x1fFe2.P0", "131042", nil},
56 {"-0x2p3", "-16", nil},
57 {"0x0.fp4", "15", nil},
58 {"0x0.fp0", "0.9375", nil},
59 {"0x1e2", "0", ErrSyntax},
60 {"1p2", "0", ErrSyntax},
61
62
63 {"0", "0", nil},
64 {"0e0", "0", nil},
65 {"-0e0", "-0", nil},
66 {"+0e0", "0", nil},
67 {"0e-0", "0", nil},
68 {"-0e-0", "-0", nil},
69 {"+0e-0", "0", nil},
70 {"0e+0", "0", nil},
71 {"-0e+0", "-0", nil},
72 {"+0e+0", "0", nil},
73 {"0e+01234567890123456789", "0", nil},
74 {"0.00e-01234567890123456789", "0", nil},
75 {"-0e+01234567890123456789", "-0", nil},
76 {"-0.00e-01234567890123456789", "-0", nil},
77 {"0x0p+01234567890123456789", "0", nil},
78 {"0x0.00p-01234567890123456789", "0", nil},
79 {"-0x0p+01234567890123456789", "-0", nil},
80 {"-0x0.00p-01234567890123456789", "-0", nil},
81
82 {"0e291", "0", nil},
83 {"0e292", "0", nil},
84 {"0e347", "0", nil},
85 {"0e348", "0", nil},
86 {"-0e291", "-0", nil},
87 {"-0e292", "-0", nil},
88 {"-0e347", "-0", nil},
89 {"-0e348", "-0", nil},
90 {"0x0p126", "0", nil},
91 {"0x0p127", "0", nil},
92 {"0x0p128", "0", nil},
93 {"0x0p129", "0", nil},
94 {"0x0p130", "0", nil},
95 {"0x0p1022", "0", nil},
96 {"0x0p1023", "0", nil},
97 {"0x0p1024", "0", nil},
98 {"0x0p1025", "0", nil},
99 {"0x0p1026", "0", nil},
100 {"-0x0p126", "-0", nil},
101 {"-0x0p127", "-0", nil},
102 {"-0x0p128", "-0", nil},
103 {"-0x0p129", "-0", nil},
104 {"-0x0p130", "-0", nil},
105 {"-0x0p1022", "-0", nil},
106 {"-0x0p1023", "-0", nil},
107 {"-0x0p1024", "-0", nil},
108 {"-0x0p1025", "-0", nil},
109 {"-0x0p1026", "-0", nil},
110
111
112 {"nan", "NaN", nil},
113 {"NaN", "NaN", nil},
114 {"NAN", "NaN", nil},
115
116
117 {"inf", "+Inf", nil},
118 {"-Inf", "-Inf", nil},
119 {"+INF", "+Inf", nil},
120 {"-Infinity", "-Inf", nil},
121 {"+INFINITY", "+Inf", nil},
122 {"Infinity", "+Inf", nil},
123
124
125 {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
126 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
127 {"0x1.fffffffffffffp1023", "1.7976931348623157e+308", nil},
128 {"-0x1.fffffffffffffp1023", "-1.7976931348623157e+308", nil},
129 {"0x1fffffffffffffp+971", "1.7976931348623157e+308", nil},
130 {"-0x1fffffffffffffp+971", "-1.7976931348623157e+308", nil},
131 {"0x.1fffffffffffffp1027", "1.7976931348623157e+308", nil},
132 {"-0x.1fffffffffffffp1027", "-1.7976931348623157e+308", nil},
133
134
135 {"1.7976931348623159e308", "+Inf", ErrRange},
136 {"-1.7976931348623159e308", "-Inf", ErrRange},
137 {"0x1p1024", "+Inf", ErrRange},
138 {"-0x1p1024", "-Inf", ErrRange},
139 {"0x2p1023", "+Inf", ErrRange},
140 {"-0x2p1023", "-Inf", ErrRange},
141 {"0x.1p1028", "+Inf", ErrRange},
142 {"-0x.1p1028", "-Inf", ErrRange},
143 {"0x.2p1027", "+Inf", ErrRange},
144 {"-0x.2p1027", "-Inf", ErrRange},
145
146
147
148 {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
149 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
150 {"0x1.fffffffffffff7fffp1023", "1.7976931348623157e+308", nil},
151 {"-0x1.fffffffffffff7fffp1023", "-1.7976931348623157e+308", nil},
152
153 {"1.797693134862315808e308", "+Inf", ErrRange},
154 {"-1.797693134862315808e308", "-Inf", ErrRange},
155 {"0x1.fffffffffffff8p1023", "+Inf", ErrRange},
156 {"-0x1.fffffffffffff8p1023", "-Inf", ErrRange},
157 {"0x1fffffffffffff.8p+971", "+Inf", ErrRange},
158 {"-0x1fffffffffffff8p+967", "-Inf", ErrRange},
159 {"0x.1fffffffffffff8p1027", "+Inf", ErrRange},
160 {"-0x.1fffffffffffff9p1027", "-Inf", ErrRange},
161
162
163 {"1e308", "1e+308", nil},
164 {"2e308", "+Inf", ErrRange},
165 {"1e309", "+Inf", ErrRange},
166 {"0x1p1025", "+Inf", ErrRange},
167
168
169 {"1e310", "+Inf", ErrRange},
170 {"-1e310", "-Inf", ErrRange},
171 {"1e400", "+Inf", ErrRange},
172 {"-1e400", "-Inf", ErrRange},
173 {"1e400000", "+Inf", ErrRange},
174 {"-1e400000", "-Inf", ErrRange},
175 {"0x1p1030", "+Inf", ErrRange},
176 {"0x1p2000", "+Inf", ErrRange},
177 {"0x1p2000000000", "+Inf", ErrRange},
178 {"-0x1p1030", "-Inf", ErrRange},
179 {"-0x1p2000", "-Inf", ErrRange},
180 {"-0x1p2000000000", "-Inf", ErrRange},
181
182
183 {"1e-305", "1e-305", nil},
184 {"1e-306", "1e-306", nil},
185 {"1e-307", "1e-307", nil},
186 {"1e-308", "1e-308", nil},
187 {"1e-309", "1e-309", nil},
188 {"1e-310", "1e-310", nil},
189 {"1e-322", "1e-322", nil},
190
191 {"5e-324", "5e-324", nil},
192 {"4e-324", "5e-324", nil},
193 {"3e-324", "5e-324", nil},
194
195 {"2e-324", "0", nil},
196
197 {"1e-350", "0", nil},
198 {"1e-400000", "0", nil},
199 {"1e-345", "0", nil},
200 {"1e-343", "0", nil},
201 {"9.999999999999999999e-343", "0", nil},
202
203
204 {"0x2.00000000000000p-1010", "1.8227805048890994e-304", nil},
205 {"0x1.fffffffffffff0p-1010", "1.8227805048890992e-304", nil},
206 {"0x1.fffffffffffff7p-1010", "1.8227805048890992e-304", nil},
207 {"0x1.fffffffffffff8p-1010", "1.8227805048890994e-304", nil},
208 {"0x1.fffffffffffff9p-1010", "1.8227805048890994e-304", nil},
209
210 {"0x2.00000000000000p-1022", "4.450147717014403e-308", nil},
211 {"0x1.fffffffffffff0p-1022", "4.4501477170144023e-308", nil},
212 {"0x1.fffffffffffff7p-1022", "4.4501477170144023e-308", nil},
213 {"0x1.fffffffffffff8p-1022", "4.450147717014403e-308", nil},
214 {"0x1.fffffffffffff9p-1022", "4.450147717014403e-308", nil},
215
216 {"0x1.00000000000000p-1022", "2.2250738585072014e-308", nil},
217 {"0x0.fffffffffffff0p-1022", "2.225073858507201e-308", nil},
218 {"0x0.ffffffffffffe0p-1022", "2.2250738585072004e-308", nil},
219 {"0x0.ffffffffffffe7p-1022", "2.2250738585072004e-308", nil},
220 {"0x1.ffffffffffffe8p-1023", "2.225073858507201e-308", nil},
221 {"0x1.ffffffffffffe9p-1023", "2.225073858507201e-308", nil},
222
223 {"0x0.00000003fffff0p-1022", "2.072261e-317", nil},
224 {"0x0.00000003456780p-1022", "1.694649e-317", nil},
225 {"0x0.00000003456787p-1022", "1.694649e-317", nil},
226 {"0x0.00000003456788p-1022", "1.694649e-317", nil},
227 {"0x0.00000003456790p-1022", "1.6946496e-317", nil},
228 {"0x0.00000003456789p-1022", "1.6946496e-317", nil},
229
230 {"0x0.0000000345678800000000000000000000000001p-1022", "1.6946496e-317", nil},
231
232 {"0x0.000000000000f0p-1022", "7.4e-323", nil},
233 {"0x0.00000000000060p-1022", "3e-323", nil},
234 {"0x0.00000000000058p-1022", "3e-323", nil},
235 {"0x0.00000000000057p-1022", "2.5e-323", nil},
236 {"0x0.00000000000050p-1022", "2.5e-323", nil},
237
238 {"0x0.00000000000010p-1022", "5e-324", nil},
239 {"0x0.000000000000081p-1022", "5e-324", nil},
240 {"0x0.00000000000008p-1022", "0", nil},
241 {"0x0.00000000000007fp-1022", "0", nil},
242
243
244 {"1e-4294967296", "0", nil},
245 {"1e+4294967296", "+Inf", ErrRange},
246 {"1e-18446744073709551616", "0", nil},
247 {"1e+18446744073709551616", "+Inf", ErrRange},
248 {"0x1p-4294967296", "0", nil},
249 {"0x1p+4294967296", "+Inf", ErrRange},
250 {"0x1p-18446744073709551616", "0", nil},
251 {"0x1p+18446744073709551616", "+Inf", ErrRange},
252
253
254 {"1e", "0", ErrSyntax},
255 {"1e-", "0", ErrSyntax},
256 {".e-1", "0", ErrSyntax},
257 {"1\x00.2", "0", ErrSyntax},
258 {"0x", "0", ErrSyntax},
259 {"0x.", "0", ErrSyntax},
260 {"0x1", "0", ErrSyntax},
261 {"0x.1", "0", ErrSyntax},
262 {"0x1p", "0", ErrSyntax},
263 {"0x.1p", "0", ErrSyntax},
264 {"0x1p+", "0", ErrSyntax},
265 {"0x.1p+", "0", ErrSyntax},
266 {"0x1p-", "0", ErrSyntax},
267 {"0x.1p-", "0", ErrSyntax},
268 {"0x1p+2", "4", nil},
269 {"0x.1p+2", "0.25", nil},
270 {"0x1p-2", "0.25", nil},
271 {"0x.1p-2", "0.015625", nil},
272
273
274 {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
275
276 {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
277
278
279 {"4.630813248087435e+307", "4.630813248087435e+307", nil},
280
281
282 {"22.222222222222222", "22.22222222222222", nil},
283 {"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
284 {"0x1.1111111111111p222", "7.18931911124017e+66", nil},
285 {"0x2.2222222222222p221", "7.18931911124017e+66", nil},
286 {"0x2." + strings.Repeat("2", 4000) + "p221", "7.18931911124017e+66", nil},
287
288
289
290 {"1.00000000000000011102230246251565404236316680908203125", "1", nil},
291 {"0x1.00000000000008p0", "1", nil},
292
293 {"1.00000000000000011102230246251565404236316680908203124", "1", nil},
294 {"0x1.00000000000007Fp0", "1", nil},
295
296 {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
297 {"0x1.000000000000081p0", "1.0000000000000002", nil},
298 {"0x1.00000000000009p0", "1.0000000000000002", nil},
299
300 {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
301 {"0x1.00000000000008" + strings.Repeat("0", 10000) + "1p0", "1.0000000000000002", nil},
302
303
304
305 {"1.00000000000000033306690738754696212708950042724609375", "1.0000000000000004", nil},
306 {"0x1.00000000000018p0", "1.0000000000000004", nil},
307
308
309
310 {"1090544144181609348671888949248", "1.0905441441816093e+30", nil},
311
312 {"1090544144181609348835077142190", "1.0905441441816094e+30", nil},
313
314
315 {"1_23.50_0_0e+1_2", "1.235e+14", nil},
316 {"-_123.5e+12", "0", ErrSyntax},
317 {"+_123.5e+12", "0", ErrSyntax},
318 {"_123.5e+12", "0", ErrSyntax},
319 {"1__23.5e+12", "0", ErrSyntax},
320 {"123_.5e+12", "0", ErrSyntax},
321 {"123._5e+12", "0", ErrSyntax},
322 {"123.5_e+12", "0", ErrSyntax},
323 {"123.5__0e+12", "0", ErrSyntax},
324 {"123.5e_+12", "0", ErrSyntax},
325 {"123.5e+_12", "0", ErrSyntax},
326 {"123.5e_-12", "0", ErrSyntax},
327 {"123.5e-_12", "0", ErrSyntax},
328 {"123.5e+1__2", "0", ErrSyntax},
329 {"123.5e+12_", "0", ErrSyntax},
330
331 {"0x_1_2.3_4_5p+1_2", "74565", nil},
332 {"-_0x12.345p+12", "0", ErrSyntax},
333 {"+_0x12.345p+12", "0", ErrSyntax},
334 {"_0x12.345p+12", "0", ErrSyntax},
335 {"0x__12.345p+12", "0", ErrSyntax},
336 {"0x1__2.345p+12", "0", ErrSyntax},
337 {"0x12_.345p+12", "0", ErrSyntax},
338 {"0x12._345p+12", "0", ErrSyntax},
339 {"0x12.3__45p+12", "0", ErrSyntax},
340 {"0x12.345_p+12", "0", ErrSyntax},
341 {"0x12.345p_+12", "0", ErrSyntax},
342 {"0x12.345p+_12", "0", ErrSyntax},
343 {"0x12.345p_-12", "0", ErrSyntax},
344 {"0x12.345p-_12", "0", ErrSyntax},
345 {"0x12.345p+1__2", "0", ErrSyntax},
346 {"0x12.345p+12_", "0", ErrSyntax},
347
348 {"1e100x", "0", ErrSyntax},
349 {"1e1000x", "0", ErrSyntax},
350 }
351
352 var atof32tests = []atofTest{
353
354 {"0x1p-100", "7.888609e-31", nil},
355 {"0x1p100", "1.2676506e+30", nil},
356
357
358
359 {"1.000000059604644775390625", "1", nil},
360 {"0x1.000001p0", "1", nil},
361
362 {"1.000000059604644775390624", "1", nil},
363 {"0x1.0000008p0", "1", nil},
364 {"0x1.000000fp0", "1", nil},
365
366 {"1.000000059604644775390626", "1.0000001", nil},
367 {"0x1.000002p0", "1.0000001", nil},
368 {"0x1.0000018p0", "1.0000001", nil},
369 {"0x1.0000011p0", "1.0000001", nil},
370
371 {"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil},
372 {"0x1.000001" + strings.Repeat("0", 10000) + "1p0", "1.0000001", nil},
373
374
375 {"340282346638528859811704183484516925440", "3.4028235e+38", nil},
376 {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
377 {"0x.ffffffp128", "3.4028235e+38", nil},
378 {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
379 {"-0x.ffffffp128", "-3.4028235e+38", nil},
380
381 {"3.4028236e38", "+Inf", ErrRange},
382 {"-3.4028236e38", "-Inf", ErrRange},
383 {"0x1.0p128", "+Inf", ErrRange},
384 {"-0x1.0p128", "-Inf", ErrRange},
385
386
387 {"3.402823567e38", "3.4028235e+38", nil},
388 {"-3.402823567e38", "-3.4028235e+38", nil},
389 {"0x.ffffff7fp128", "3.4028235e+38", nil},
390 {"-0x.ffffff7fp128", "-3.4028235e+38", nil},
391
392 {"3.4028235678e38", "+Inf", ErrRange},
393 {"-3.4028235678e38", "-Inf", ErrRange},
394 {"0x.ffffff8p128", "+Inf", ErrRange},
395 {"-0x.ffffff8p128", "-Inf", ErrRange},
396
397
398 {"1e-38", "1e-38", nil},
399 {"1e-39", "1e-39", nil},
400 {"1e-40", "1e-40", nil},
401 {"1e-41", "1e-41", nil},
402 {"1e-42", "1e-42", nil},
403 {"1e-43", "1e-43", nil},
404 {"1e-44", "1e-44", nil},
405 {"6e-45", "6e-45", nil},
406 {"5e-45", "6e-45", nil},
407
408
409 {"1e-45", "1e-45", nil},
410 {"2e-45", "1e-45", nil},
411 {"3e-45", "3e-45", nil},
412
413
414 {"0x0.89aBcDp-125", "1.2643093e-38", nil},
415 {"0x0.8000000p-125", "1.1754944e-38", nil},
416 {"0x0.1234560p-125", "1.671814e-39", nil},
417 {"0x0.1234567p-125", "1.671814e-39", nil},
418 {"0x0.1234568p-125", "1.671814e-39", nil},
419 {"0x0.1234569p-125", "1.671815e-39", nil},
420 {"0x0.1234570p-125", "1.671815e-39", nil},
421 {"0x0.0000010p-125", "1e-45", nil},
422 {"0x0.00000081p-125", "1e-45", nil},
423 {"0x0.0000008p-125", "0", nil},
424 {"0x0.0000007p-125", "0", nil},
425
426 {"1e-70", "0", nil},
427 {"1e-65", "0", nil},
428 {"1e-64", "0", nil},
429 {"9.999999999999999999e-64", "0", nil},
430
431
432
433
434
435
436
437 {"4951760157141521099596496896", "4.9517602e+27", nil},
438 }
439
440 type atofSimpleTest struct {
441 x float64
442 s string
443 }
444
445 var (
446 atofOnce sync.Once
447 atofRandomTests []atofSimpleTest
448 benchmarksRandomBits [1024]string
449 benchmarksRandomNormal [1024]string
450 )
451
452 func initAtof() {
453 atofOnce.Do(initAtofOnce)
454 }
455
456 func initAtofOnce() {
457
458 if testing.Short() {
459 atofRandomTests = make([]atofSimpleTest, 100)
460 } else {
461 atofRandomTests = make([]atofSimpleTest, 10000)
462 }
463 for i := range atofRandomTests {
464 n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
465 x := math.Float64frombits(n)
466 s := FormatFloat(x, 'g', -1, 64)
467 atofRandomTests[i] = atofSimpleTest{x, s}
468 }
469
470 for i := range benchmarksRandomBits {
471 bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
472 x := math.Float64frombits(bits)
473 benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
474 }
475
476 for i := range benchmarksRandomNormal {
477 x := rand.NormFloat64()
478 benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
479 }
480 }
481
482 func TestParseFloatPrefix(t *testing.T) {
483 for i := range atoftests {
484 test := &atoftests[i]
485 if test.err != nil {
486 continue
487 }
488
489
490
491 for _, suffix := range []string{" ", "q", "+", "-", "<", "=", ">", "(", ")", "i", "init"} {
492 in := test.in + suffix
493 _, n, err := parseFloatPrefix(in, 64)
494 if err != nil {
495 t.Errorf("ParseFloatPrefix(%q, 64): err = %v; want no error", in, err)
496 }
497 if n != len(test.in) {
498 t.Errorf("ParseFloatPrefix(%q, 64): n = %d; want %d", in, n, len(test.in))
499 }
500 }
501 }
502 }
503
504 func testAtof(t *testing.T, opt bool) {
505 initAtof()
506 oldopt := SetOptimize(opt)
507 for i := 0; i < len(atoftests); i++ {
508 test := &atoftests[i]
509 out, err := ParseFloat(test.in, 64)
510 outs := FormatFloat(out, 'g', -1, 64)
511 if outs != test.out || !reflect.DeepEqual(err, test.err) {
512 t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
513 test.in, out, err, test.out, test.err)
514 }
515
516 if float64(float32(out)) == out {
517 out, err := ParseFloat(test.in, 32)
518 out32 := float32(out)
519 if float64(out32) != out {
520 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
521 continue
522 }
523 outs := FormatFloat(float64(out32), 'g', -1, 32)
524 if outs != test.out || !reflect.DeepEqual(err, test.err) {
525 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
526 test.in, out32, err, test.out, test.err, out)
527 }
528 }
529 }
530 for _, test := range atof32tests {
531 out, err := ParseFloat(test.in, 32)
532 out32 := float32(out)
533 if float64(out32) != out {
534 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
535 continue
536 }
537 outs := FormatFloat(float64(out32), 'g', -1, 32)
538 if outs != test.out || !reflect.DeepEqual(err, test.err) {
539 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
540 test.in, out32, err, test.out, test.err, out)
541 }
542 }
543 SetOptimize(oldopt)
544 }
545
546 func TestAtof(t *testing.T) { testAtof(t, true) }
547
548 func TestAtofSlow(t *testing.T) { testAtof(t, false) }
549
550 func TestAtofRandom(t *testing.T) {
551 initAtof()
552 for _, test := range atofRandomTests {
553 x, _ := ParseFloat(test.s, 64)
554 switch {
555 default:
556 t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
557 case x == test.x:
558 case math.IsNaN(test.x) && math.IsNaN(x):
559 }
560 }
561 t.Logf("tested %d random numbers", len(atofRandomTests))
562 }
563
564 var roundTripCases = []struct {
565 f float64
566 s string
567 }{
568
569
570
571
572
573
574 {8865794286000691 << 39, "4.87402195346389e+27"},
575 {8865794286000692 << 39, "4.8740219534638903e+27"},
576 }
577
578 func TestRoundTrip(t *testing.T) {
579 for _, tt := range roundTripCases {
580 old := SetOptimize(false)
581 s := FormatFloat(tt.f, 'g', -1, 64)
582 if s != tt.s {
583 t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
584 }
585 f, err := ParseFloat(tt.s, 64)
586 if f != tt.f || err != nil {
587 t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
588 }
589 SetOptimize(true)
590 s = FormatFloat(tt.f, 'g', -1, 64)
591 if s != tt.s {
592 t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
593 }
594 f, err = ParseFloat(tt.s, 64)
595 if f != tt.f || err != nil {
596 t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
597 }
598 SetOptimize(old)
599 }
600 }
601
602
603 func TestRoundTrip32(t *testing.T) {
604 step := uint32(997)
605 if testing.Short() {
606 step = 99991
607 }
608 count := 0
609 for i := uint32(0); i < 0xff<<23; i += step {
610 f := math.Float32frombits(i)
611 if i&1 == 1 {
612 f = -f
613 }
614 s := FormatFloat(float64(f), 'g', -1, 32)
615
616 parsed, err := ParseFloat(s, 32)
617 parsed32 := float32(parsed)
618 switch {
619 case err != nil:
620 t.Errorf("ParseFloat(%q, 32) gave error %s", s, err)
621 case float64(parsed32) != parsed:
622 t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32)
623 case parsed32 != f:
624 t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f)
625 }
626 count++
627 }
628 t.Logf("tested %d float32's", count)
629 }
630
631
632
633 func TestParseFloatIncorrectBitSize(t *testing.T) {
634 const s = "1.5e308"
635 const want = 1.5e308
636
637 for _, bitSize := range []int{0, 10, 100, 128} {
638 f, err := ParseFloat(s, bitSize)
639 if err != nil {
640 t.Fatalf("ParseFloat(%q, %d) gave error %s", s, bitSize, err)
641 }
642 if f != want {
643 t.Fatalf("ParseFloat(%q, %d) = %g (expected %g)", s, bitSize, f, want)
644 }
645 }
646 }
647
648 func BenchmarkAtof64Decimal(b *testing.B) {
649 for i := 0; i < b.N; i++ {
650 ParseFloat("33909", 64)
651 }
652 }
653
654 func BenchmarkAtof64Float(b *testing.B) {
655 for i := 0; i < b.N; i++ {
656 ParseFloat("339.7784", 64)
657 }
658 }
659
660 func BenchmarkAtof64FloatExp(b *testing.B) {
661 for i := 0; i < b.N; i++ {
662 ParseFloat("-5.09e75", 64)
663 }
664 }
665
666 func BenchmarkAtof64Big(b *testing.B) {
667 for i := 0; i < b.N; i++ {
668 ParseFloat("123456789123456789123456789", 64)
669 }
670 }
671
672 func BenchmarkAtof64RandomBits(b *testing.B) {
673 initAtof()
674 b.ResetTimer()
675 for i := 0; i < b.N; i++ {
676 ParseFloat(benchmarksRandomBits[i%1024], 64)
677 }
678 }
679
680 func BenchmarkAtof64RandomFloats(b *testing.B) {
681 initAtof()
682 b.ResetTimer()
683 for i := 0; i < b.N; i++ {
684 ParseFloat(benchmarksRandomNormal[i%1024], 64)
685 }
686 }
687
688 func BenchmarkAtof64RandomLongFloats(b *testing.B) {
689 initAtof()
690 samples := make([]string, len(atofRandomTests))
691 for i, t := range atofRandomTests {
692 samples[i] = FormatFloat(t.x, 'g', 20, 64)
693 }
694 b.ResetTimer()
695 idx := 0
696 for i := 0; i < b.N; i++ {
697 ParseFloat(samples[idx], 64)
698 idx++
699 if idx == len(samples) {
700 idx = 0
701 }
702 }
703 }
704
705 func BenchmarkAtof32Decimal(b *testing.B) {
706 for i := 0; i < b.N; i++ {
707 ParseFloat("33909", 32)
708 }
709 }
710
711 func BenchmarkAtof32Float(b *testing.B) {
712 for i := 0; i < b.N; i++ {
713 ParseFloat("339.778", 32)
714 }
715 }
716
717 func BenchmarkAtof32FloatExp(b *testing.B) {
718 for i := 0; i < b.N; i++ {
719 ParseFloat("12.3456e32", 32)
720 }
721 }
722
723 func BenchmarkAtof32Random(b *testing.B) {
724 n := uint32(997)
725 var float32strings [4096]string
726 for i := range float32strings {
727 n = (99991*n + 42) % (0xff << 23)
728 float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32)
729 }
730 b.ResetTimer()
731 for i := 0; i < b.N; i++ {
732 ParseFloat(float32strings[i%4096], 32)
733 }
734 }
735
736 func BenchmarkAtof32RandomLong(b *testing.B) {
737 n := uint32(997)
738 var float32strings [4096]string
739 for i := range float32strings {
740 n = (99991*n + 42) % (0xff << 23)
741 float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', 20, 32)
742 }
743 b.ResetTimer()
744 for i := 0; i < b.N; i++ {
745 ParseFloat(float32strings[i%4096], 32)
746 }
747 }
748
View as plain text