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", os.EINVAL},
25 {"1.1.", "0", os.EINVAL},
26 {"1e23", "1e+23", nil},
27 {"100000000000000000000000", "1e+23", nil},
28 {"1e-100", "1e-100", nil},
29 {"123456700", "1.234567e+08", nil},
30 {"99999999999999974834176", "9.999999999999997e+22", nil},
31 {"100000000000000000000001", "1.0000000000000001e+23", nil},
32 {"100000000000000008388608", "1.0000000000000001e+23", nil},
33 {"100000000000000016777215", "1.0000000000000001e+23", nil},
34 {"100000000000000016777216", "1.0000000000000003e+23", nil},
37 {"1e-20", "1e-20", nil},
38 {"625e-3", "0.625", nil},
41 {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
42 {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
43 // next float64 - too large
44 {"1.7976931348623159e308", "+Inf", os.ERANGE},
45 {"-1.7976931348623159e308", "-Inf", os.ERANGE},
46 // the border is ...158079
48 {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
49 {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
50 // borderline - too large
51 {"1.797693134862315808e308", "+Inf", os.ERANGE},
52 {"-1.797693134862315808e308", "-Inf", os.ERANGE},
55 {"1e308", "1e+308", nil},
56 {"2e308", "+Inf", os.ERANGE},
57 {"1e309", "+Inf", os.ERANGE},
60 {"1e310", "+Inf", os.ERANGE},
61 {"-1e310", "-Inf", os.ERANGE},
62 {"1e400", "+Inf", os.ERANGE},
63 {"-1e400", "-Inf", os.ERANGE},
64 {"1e400000", "+Inf", os.ERANGE},
65 {"-1e400000", "-Inf", os.ERANGE},
68 {"1e-305", "1e-305", nil},
69 {"1e-306", "1e-306", nil},
70 {"1e-307", "1e-307", nil},
71 {"1e-308", "1e-308", nil},
72 {"1e-309", "1e-309", nil},
73 {"1e-310", "1e-310", nil},
74 {"1e-322", "1e-322", nil},
76 {"5e-324", "5e-324", nil},
77 {"4e-324", "5e-324", nil},
78 {"3e-324", "5e-324", nil},
83 {"1e-400000", "0", nil},
85 // try to overflow exponent
86 {"1e-4294967296", "0", nil},
87 {"1e+4294967296", "+Inf", os.ERANGE},
88 {"1e-18446744073709551616", "0", nil},
89 {"1e+18446744073709551616", "+Inf", os.ERANGE},
92 {"1e", "0", os.EINVAL},
93 {"1e-", "0", os.EINVAL},
94 {".e-1", "0", os.EINVAL},
98 // The atof routines return NumErrors wrapping
99 // the error and the string. Convert the table above.
100 for i := range atoftests {
101 test := &atoftests[i]
103 test.err = &NumError{test.in, test.err}
108 func testAtof(t *testing.T, opt bool) {
109 oldopt := SetOptimize(opt)
110 for i := 0; i < len(atoftests); i++ {
111 test := &atoftests[i]
112 out, err := Atof64(test.in)
113 outs := Ftoa64(out, 'g', -1)
114 if outs != test.out || !reflect.DeepEqual(err, test.err) {
115 t.Errorf("Atof64(%v) = %v, %v want %v, %v",
116 test.in, out, err, test.out, test.err)
119 out, err = AtofN(test.in, 64)
120 outs = FtoaN(out, 'g', -1, 64)
121 if outs != test.out || !reflect.DeepEqual(err, test.err) {
122 t.Errorf("AtofN(%v, 64) = %v, %v want %v, %v",
123 test.in, out, err, test.out, test.err)
126 if float64(float32(out)) == out {
127 out32, err := Atof32(test.in)
128 outs := Ftoa32(out32, 'g', -1)
129 if outs != test.out || !reflect.DeepEqual(err, test.err) {
130 t.Errorf("Atof32(%v) = %v, %v want %v, %v # %v",
131 test.in, out32, err, test.out, test.err, out)
134 out, err := AtofN(test.in, 32)
136 outs = FtoaN(float64(out32), 'g', -1, 32)
137 if outs != test.out || !reflect.DeepEqual(err, test.err) {
138 t.Errorf("AtofN(%v, 32) = %v, %v want %v, %v # %v",
139 test.in, out32, err, test.out, test.err, out)
143 if FloatSize == 64 || float64(float32(out)) == out {
144 outf, err := Atof(test.in)
145 outs := Ftoa(outf, 'g', -1)
146 if outs != test.out || !reflect.DeepEqual(err, test.err) {
147 t.Errorf("Ftoa(%v) = %v, %v want %v, %v # %v",
148 test.in, outf, err, test.out, test.err, out)
155 func TestAtof(t *testing.T) { testAtof(t, true) }
157 func TestAtofSlow(t *testing.T) { testAtof(t, false) }
159 func BenchmarkAtofDecimal(b *testing.B) {
160 for i := 0; i < b.N; i++ {
165 func BenchmarkAtofFloat(b *testing.B) {
166 for i := 0; i < b.N; i++ {
171 func BenchmarkAtofFloatExp(b *testing.B) {
172 for i := 0; i < b.N; i++ {
177 func BenchmarkAtofBig(b *testing.B) {
178 for i := 0; i < b.N; i++ {
179 Atof("123456789123456789123456789")