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.
14 type atofTest struct {
20 var atoftests = []atofTest{
24 {"1x", "0", ErrSyntax},
25 {"1.1.", "0", ErrSyntax},
26 {"1e23", "1e+23", nil},
27 {"1E23", "1e+23", nil},
28 {"100000000000000000000000", "1e+23", nil},
29 {"1e-100", "1e-100", nil},
30 {"123456700", "1.234567e+08", nil},
31 {"99999999999999974834176", "9.999999999999997e+22", nil},
32 {"100000000000000000000001", "1.0000000000000001e+23", nil},
33 {"100000000000000008388608", "1.0000000000000001e+23", nil},
34 {"100000000000000016777215", "1.0000000000000001e+23", nil},
35 {"100000000000000016777216", "1.0000000000000003e+23", nil},
37 {"-0.1", "-0.1", nil},
39 {"1e-20", "1e-20", nil},
40 {"625e-3", "0.625", nil},
49 {"-Inf", "-Inf", nil},
50 {"+INF", "+Inf", nil},
51 {"-Infinity", "-Inf", nil},
52 {"+INFINITY", "+Inf", nil},
53 {"Infinity", "+Inf", nil},
56 {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
57 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
58 // next float64 - too large
59 {"1.7976931348623159e308", "+Inf", ErrRange},
60 {"-1.7976931348623159e308", "-Inf", ErrRange},
61 // the border is ...158079
63 {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
64 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
65 // borderline - too large
66 {"1.797693134862315808e308", "+Inf", ErrRange},
67 {"-1.797693134862315808e308", "-Inf", ErrRange},
70 {"1e308", "1e+308", nil},
71 {"2e308", "+Inf", ErrRange},
72 {"1e309", "+Inf", ErrRange},
75 {"1e310", "+Inf", ErrRange},
76 {"-1e310", "-Inf", ErrRange},
77 {"1e400", "+Inf", ErrRange},
78 {"-1e400", "-Inf", ErrRange},
79 {"1e400000", "+Inf", ErrRange},
80 {"-1e400000", "-Inf", ErrRange},
83 {"1e-305", "1e-305", nil},
84 {"1e-306", "1e-306", nil},
85 {"1e-307", "1e-307", nil},
86 {"1e-308", "1e-308", nil},
87 {"1e-309", "1e-309", nil},
88 {"1e-310", "1e-310", nil},
89 {"1e-322", "1e-322", nil},
91 {"5e-324", "5e-324", nil},
92 {"4e-324", "5e-324", nil},
93 {"3e-324", "5e-324", nil},
98 {"1e-400000", "0", nil},
100 // try to overflow exponent
101 {"1e-4294967296", "0", nil},
102 {"1e+4294967296", "+Inf", ErrRange},
103 {"1e-18446744073709551616", "0", nil},
104 {"1e+18446744073709551616", "+Inf", ErrRange},
107 {"1e", "0", ErrSyntax},
108 {"1e-", "0", ErrSyntax},
109 {".e-1", "0", ErrSyntax},
111 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
112 {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
113 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
114 {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
118 // The atof routines return NumErrors wrapping
119 // the error and the string. Convert the table above.
120 for i := range atoftests {
121 test := &atoftests[i]
123 test.err = &NumError{test.in, test.err}
128 func testAtof(t *testing.T, opt bool) {
129 oldopt := SetOptimize(opt)
130 for i := 0; i < len(atoftests); i++ {
131 test := &atoftests[i]
132 out, err := Atof64(test.in)
133 outs := Ftoa64(out, 'g', -1)
134 if outs != test.out || !reflect.DeepEqual(err, test.err) {
135 t.Errorf("Atof64(%v) = %v, %v want %v, %v",
136 test.in, out, err, test.out, test.err)
139 out, err = AtofN(test.in, 64)
140 outs = FtoaN(out, 'g', -1, 64)
141 if outs != test.out || !reflect.DeepEqual(err, test.err) {
142 t.Errorf("AtofN(%v, 64) = %v, %v want %v, %v",
143 test.in, out, err, test.out, test.err)
146 if float64(float32(out)) == out {
147 out32, err := Atof32(test.in)
148 outs := Ftoa32(out32, 'g', -1)
149 if outs != test.out || !reflect.DeepEqual(err, test.err) {
150 t.Errorf("Atof32(%v) = %v, %v want %v, %v # %v",
151 test.in, out32, err, test.out, test.err, out)
154 out, err := AtofN(test.in, 32)
156 outs = FtoaN(float64(out32), 'g', -1, 32)
157 if outs != test.out || !reflect.DeepEqual(err, test.err) {
158 t.Errorf("AtofN(%v, 32) = %v, %v want %v, %v # %v",
159 test.in, out32, err, test.out, test.err, out)
166 func TestAtof(t *testing.T) { testAtof(t, true) }
168 func TestAtofSlow(t *testing.T) { testAtof(t, false) }
170 func BenchmarkAtof64Decimal(b *testing.B) {
171 for i := 0; i < b.N; i++ {
176 func BenchmarkAtof64Float(b *testing.B) {
177 for i := 0; i < b.N; i++ {
182 func BenchmarkAtof64FloatExp(b *testing.B) {
183 for i := 0; i < b.N; i++ {
188 func BenchmarkAtof64Big(b *testing.B) {
189 for i := 0; i < b.N; i++ {
190 Atof64("123456789123456789123456789")