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