1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strconv_test 6 7 import ( 8 "math" 9 "math/rand" 10 "reflect" 11 . "strconv" 12 "strings" 13 "testing" 14 "time" 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 // NaNs 46 {"nan", "NaN", nil}, 47 {"NaN", "NaN", nil}, 48 {"NAN", "NaN", nil}, 49 50 // Infs 51 {"inf", "+Inf", nil}, 52 {"-Inf", "-Inf", nil}, 53 {"+INF", "+Inf", nil}, 54 {"-Infinity", "-Inf", nil}, 55 {"+INFINITY", "+Inf", nil}, 56 {"Infinity", "+Inf", nil}, 57 58 // largest float64 59 {"1.7976931348623157e308", "1.7976931348623157e+308", nil}, 60 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil}, 61 // next float64 - too large 62 {"1.7976931348623159e308", "+Inf", ErrRange}, 63 {"-1.7976931348623159e308", "-Inf", ErrRange}, 64 // the border is ...158079 65 // borderline - okay 66 {"1.7976931348623158e308", "1.7976931348623157e+308", nil}, 67 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil}, 68 // borderline - too large 69 {"1.797693134862315808e308", "+Inf", ErrRange}, 70 {"-1.797693134862315808e308", "-Inf", ErrRange}, 71 72 // a little too large 73 {"1e308", "1e+308", nil}, 74 {"2e308", "+Inf", ErrRange}, 75 {"1e309", "+Inf", ErrRange}, 76 77 // way too large 78 {"1e310", "+Inf", ErrRange}, 79 {"-1e310", "-Inf", ErrRange}, 80 {"1e400", "+Inf", ErrRange}, 81 {"-1e400", "-Inf", ErrRange}, 82 {"1e400000", "+Inf", ErrRange}, 83 {"-1e400000", "-Inf", ErrRange}, 84 85 // denormalized 86 {"1e-305", "1e-305", nil}, 87 {"1e-306", "1e-306", nil}, 88 {"1e-307", "1e-307", nil}, 89 {"1e-308", "1e-308", nil}, 90 {"1e-309", "1e-309", nil}, 91 {"1e-310", "1e-310", nil}, 92 {"1e-322", "1e-322", nil}, 93 // smallest denormal 94 {"5e-324", "5e-324", nil}, 95 {"4e-324", "5e-324", nil}, 96 {"3e-324", "5e-324", nil}, 97 // too small 98 {"2e-324", "0", nil}, 99 // way too small 100 {"1e-350", "0", nil}, 101 {"1e-400000", "0", nil}, 102 103 // try to overflow exponent 104 {"1e-4294967296", "0", nil}, 105 {"1e+4294967296", "+Inf", ErrRange}, 106 {"1e-18446744073709551616", "0", nil}, 107 {"1e+18446744073709551616", "+Inf", ErrRange}, 108 109 // Parse errors 110 {"1e", "0", ErrSyntax}, 111 {"1e-", "0", ErrSyntax}, 112 {".e-1", "0", ErrSyntax}, 113 {"1\x00.2", "0", ErrSyntax}, 114 115 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 116 {"2.2250738585072012e-308", "2.2250738585072014e-308", nil}, 117 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 118 {"2.2250738585072011e-308", "2.225073858507201e-308", nil}, 119 120 // A very large number (initially wrongly parsed by the fast algorithm). 121 {"4.630813248087435e+307", "4.630813248087435e+307", nil}, 122 123 // A different kind of very large number. 124 {"22.222222222222222", "22.22222222222222", nil}, 125 {"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil}, 126 127 // Exactly halfway between 1 and math.Nextafter(1, 2). 128 // Round to even (down). 129 {"1.00000000000000011102230246251565404236316680908203125", "1", nil}, 130 // Slightly lower; still round down. 131 {"1.00000000000000011102230246251565404236316680908203124", "1", nil}, 132 // Slightly higher; round up. 133 {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil}, 134 // Slightly higher, but you have to read all the way to the end. 135 {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil}, 136 } 137 138 var atof32tests = []atofTest{ 139 // Exactly halfway between 1 and the next float32. 140 // Round to even (down). 141 {"1.000000059604644775390625", "1", nil}, 142 // Slightly lower. 143 {"1.000000059604644775390624", "1", nil}, 144 // Slightly higher. 145 {"1.000000059604644775390626", "1.0000001", nil}, 146 // Slightly higher, but you have to read all the way to the end. 147 {"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil}, 148 149 // largest float32: (1<<128) * (1 - 2^-24) 150 {"340282346638528859811704183484516925440", "3.4028235e+38", nil}, 151 {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil}, 152 // next float32 - too large 153 {"3.4028236e38", "+Inf", ErrRange}, 154 {"-3.4028236e38", "-Inf", ErrRange}, 155 // the border is 3.40282356779...e+38 156 // borderline - okay 157 {"3.402823567e38", "3.4028235e+38", nil}, 158 {"-3.402823567e38", "-3.4028235e+38", nil}, 159 // borderline - too large 160 {"3.4028235678e38", "+Inf", ErrRange}, 161 {"-3.4028235678e38", "-Inf", ErrRange}, 162 163 // Denormals: less than 2^-126 164 {"1e-38", "1e-38", nil}, 165 {"1e-39", "1e-39", nil}, 166 {"1e-40", "1e-40", nil}, 167 {"1e-41", "1e-41", nil}, 168 {"1e-42", "1e-42", nil}, 169 {"1e-43", "1e-43", nil}, 170 {"1e-44", "1e-44", nil}, 171 {"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45 172 {"5e-45", "6e-45", nil}, 173 // Smallest denormal 174 {"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45 175 {"2e-45", "1e-45", nil}, 176 177 // 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27) 178 // is an exact power of two that needs 8 decimal digits to be correctly 179 // parsed back. 180 // The float32 before is 16777215p+68 = 4.95175986e+27 181 // The halfway is 4.951760009. A bad algorithm that thinks the previous 182 // float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27. 183 {"4951760157141521099596496896", "4.9517602e+27", nil}, 184 } 185 186 type atofSimpleTest struct { 187 x float64 188 s string 189 } 190 191 var ( 192 atofRandomTests []atofSimpleTest 193 benchmarksRandomBits [1024]string 194 benchmarksRandomNormal [1024]string 195 ) 196 197 func init() { 198 // The atof routines return NumErrors wrapping 199 // the error and the string. Convert the table above. 200 for i := range atoftests { 201 test := &atoftests[i] 202 if test.err != nil { 203 test.err = &NumError{"ParseFloat", test.in, test.err} 204 } 205 } 206 for i := range atof32tests { 207 test := &atof32tests[i] 208 if test.err != nil { 209 test.err = &NumError{"ParseFloat", test.in, test.err} 210 } 211 } 212 213 // Generate random inputs for tests and benchmarks 214 rand.Seed(time.Now().UnixNano()) 215 if testing.Short() { 216 atofRandomTests = make([]atofSimpleTest, 100) 217 } else { 218 atofRandomTests = make([]atofSimpleTest, 10000) 219 } 220 for i := range atofRandomTests { 221 n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) 222 x := math.Float64frombits(n) 223 s := FormatFloat(x, 'g', -1, 64) 224 atofRandomTests[i] = atofSimpleTest{x, s} 225 } 226 227 for i := range benchmarksRandomBits { 228 bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) 229 x := math.Float64frombits(bits) 230 benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64) 231 } 232 233 for i := range benchmarksRandomNormal { 234 x := rand.NormFloat64() 235 benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64) 236 } 237 } 238 239 func testAtof(t *testing.T, opt bool) { 240 oldopt := SetOptimize(opt) 241 for i := 0; i < len(atoftests); i++ { 242 test := &atoftests[i] 243 out, err := ParseFloat(test.in, 64) 244 outs := FormatFloat(out, 'g', -1, 64) 245 if outs != test.out || !reflect.DeepEqual(err, test.err) { 246 t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v", 247 test.in, out, err, test.out, test.err) 248 } 249 250 if float64(float32(out)) == out { 251 out, err := ParseFloat(test.in, 32) 252 out32 := float32(out) 253 if float64(out32) != out { 254 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32)) 255 continue 256 } 257 outs := FormatFloat(float64(out32), 'g', -1, 32) 258 if outs != test.out || !reflect.DeepEqual(err, test.err) { 259 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v", 260 test.in, out32, err, test.out, test.err, out) 261 } 262 } 263 } 264 for _, test := range atof32tests { 265 out, err := ParseFloat(test.in, 32) 266 out32 := float32(out) 267 if float64(out32) != out { 268 t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32)) 269 continue 270 } 271 outs := FormatFloat(float64(out32), 'g', -1, 32) 272 if outs != test.out || !reflect.DeepEqual(err, test.err) { 273 t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v", 274 test.in, out32, err, test.out, test.err, out) 275 } 276 } 277 SetOptimize(oldopt) 278 } 279 280 func TestAtof(t *testing.T) { testAtof(t, true) } 281 282 func TestAtofSlow(t *testing.T) { testAtof(t, false) } 283 284 func TestAtofRandom(t *testing.T) { 285 for _, test := range atofRandomTests { 286 x, _ := ParseFloat(test.s, 64) 287 switch { 288 default: 289 t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x) 290 case x == test.x: 291 case math.IsNaN(test.x) && math.IsNaN(x): 292 } 293 } 294 t.Logf("tested %d random numbers", len(atofRandomTests)) 295 } 296 297 var roundTripCases = []struct { 298 f float64 299 s string 300 }{ 301 // Issue 2917. 302 // This test will break the optimized conversion if the 303 // FPU is using 80-bit registers instead of 64-bit registers, 304 // usually because the operating system initialized the 305 // thread with 80-bit precision and the Go runtime didn't 306 // fix the FP control word. 307 {8865794286000691 << 39, "4.87402195346389e+27"}, 308 {8865794286000692 << 39, "4.8740219534638903e+27"}, 309 } 310 311 func TestRoundTrip(t *testing.T) { 312 for _, tt := range roundTripCases { 313 old := SetOptimize(false) 314 s := FormatFloat(tt.f, 'g', -1, 64) 315 if s != tt.s { 316 t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s) 317 } 318 f, err := ParseFloat(tt.s, 64) 319 if f != tt.f || err != nil { 320 t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f) 321 } 322 SetOptimize(true) 323 s = FormatFloat(tt.f, 'g', -1, 64) 324 if s != tt.s { 325 t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s) 326 } 327 f, err = ParseFloat(tt.s, 64) 328 if f != tt.f || err != nil { 329 t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f) 330 } 331 SetOptimize(old) 332 } 333 } 334 335 // TestRoundTrip32 tries a fraction of all finite positive float32 values. 336 func TestRoundTrip32(t *testing.T) { 337 step := uint32(997) 338 if testing.Short() { 339 step = 99991 340 } 341 count := 0 342 for i := uint32(0); i < 0xff<<23; i += step { 343 f := math.Float32frombits(i) 344 if i&1 == 1 { 345 f = -f // negative 346 } 347 s := FormatFloat(float64(f), 'g', -1, 32) 348 349 parsed, err := ParseFloat(s, 32) 350 parsed32 := float32(parsed) 351 switch { 352 case err != nil: 353 t.Errorf("ParseFloat(%q, 32) gave error %s", s, err) 354 case float64(parsed32) != parsed: 355 t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32) 356 case parsed32 != f: 357 t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f) 358 } 359 count++ 360 } 361 t.Logf("tested %d float32's", count) 362 } 363 364 func BenchmarkAtof64Decimal(b *testing.B) { 365 for i := 0; i < b.N; i++ { 366 ParseFloat("33909", 64) 367 } 368 } 369 370 func BenchmarkAtof64Float(b *testing.B) { 371 for i := 0; i < b.N; i++ { 372 ParseFloat("339.7784", 64) 373 } 374 } 375 376 func BenchmarkAtof64FloatExp(b *testing.B) { 377 for i := 0; i < b.N; i++ { 378 ParseFloat("-5.09e75", 64) 379 } 380 } 381 382 func BenchmarkAtof64Big(b *testing.B) { 383 for i := 0; i < b.N; i++ { 384 ParseFloat("123456789123456789123456789", 64) 385 } 386 } 387 388 func BenchmarkAtof64RandomBits(b *testing.B) { 389 for i := 0; i < b.N; i++ { 390 ParseFloat(benchmarksRandomBits[i%1024], 64) 391 } 392 } 393 394 func BenchmarkAtof64RandomFloats(b *testing.B) { 395 for i := 0; i < b.N; i++ { 396 ParseFloat(benchmarksRandomNormal[i%1024], 64) 397 } 398 } 399 400 func BenchmarkAtof32Decimal(b *testing.B) { 401 for i := 0; i < b.N; i++ { 402 ParseFloat("33909", 32) 403 } 404 } 405 406 func BenchmarkAtof32Float(b *testing.B) { 407 for i := 0; i < b.N; i++ { 408 ParseFloat("339.778", 32) 409 } 410 } 411 412 func BenchmarkAtof32FloatExp(b *testing.B) { 413 for i := 0; i < b.N; i++ { 414 ParseFloat("12.3456e32", 32) 415 } 416 } 417 418 var float32strings [4096]string 419 420 func BenchmarkAtof32Random(b *testing.B) { 421 n := uint32(997) 422 for i := range float32strings { 423 n = (99991*n + 42) % (0xff << 23) 424 float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32) 425 } 426 b.ResetTimer() 427 for i := 0; i < b.N; i++ { 428 ParseFloat(float32strings[i%4096], 32) 429 } 430 } 431