OSDN Git Service

libgo: Update to weekly.2011-11-01.
[pf3gnuchains/gcc-fork.git] / libgo / go / strconv / atof_test.go
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         "os"
9         "reflect"
10         . "strconv"
11         "testing"
12 )
13
14 type atofTest struct {
15         in  string
16         out string
17         err os.Error
18 }
19
20 var atoftests = []atofTest{
21         {"", "0", ErrSyntax},
22         {"1", "1", nil},
23         {"+1", "1", nil},
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},
36         {"-1", "-1", nil},
37         {"-0.1", "-0.1", nil},
38         {"-0", "-0", nil},
39         {"1e-20", "1e-20", nil},
40         {"625e-3", "0.625", nil},
41
42         // NaNs
43         {"nan", "NaN", nil},
44         {"NaN", "NaN", nil},
45         {"NAN", "NaN", nil},
46
47         // Infs
48         {"inf", "+Inf", nil},
49         {"-Inf", "-Inf", nil},
50         {"+INF", "+Inf", nil},
51         {"-Infinity", "-Inf", nil},
52         {"+INFINITY", "+Inf", nil},
53         {"Infinity", "+Inf", nil},
54
55         // largest float64
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
62         // borderline - okay
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},
68
69         // a little too large
70         {"1e308", "1e+308", nil},
71         {"2e308", "+Inf", ErrRange},
72         {"1e309", "+Inf", ErrRange},
73
74         // way too large
75         {"1e310", "+Inf", ErrRange},
76         {"-1e310", "-Inf", ErrRange},
77         {"1e400", "+Inf", ErrRange},
78         {"-1e400", "-Inf", ErrRange},
79         {"1e400000", "+Inf", ErrRange},
80         {"-1e400000", "-Inf", ErrRange},
81
82         // denormalized
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},
90         // smallest denormal
91         {"5e-324", "5e-324", nil},
92         {"4e-324", "5e-324", nil},
93         {"3e-324", "5e-324", nil},
94         // too small
95         {"2e-324", "0", nil},
96         // way too small
97         {"1e-350", "0", nil},
98         {"1e-400000", "0", nil},
99
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},
105
106         // Parse errors
107         {"1e", "0", ErrSyntax},
108         {"1e-", "0", ErrSyntax},
109         {".e-1", "0", ErrSyntax},
110
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},
115 }
116
117 func init() {
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]
122                 if test.err != nil {
123                         test.err = &NumError{test.in, test.err}
124                 }
125         }
126 }
127
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)
137                 }
138
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)
144                 }
145
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)
152                         }
153
154                         out, err := AtofN(test.in, 32)
155                         out32 = float32(out)
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)
160                         }
161                 }
162         }
163         SetOptimize(oldopt)
164 }
165
166 func TestAtof(t *testing.T) { testAtof(t, true) }
167
168 func TestAtofSlow(t *testing.T) { testAtof(t, false) }
169
170 func BenchmarkAtof64Decimal(b *testing.B) {
171         for i := 0; i < b.N; i++ {
172                 Atof64("33909")
173         }
174 }
175
176 func BenchmarkAtof64Float(b *testing.B) {
177         for i := 0; i < b.N; i++ {
178                 Atof64("339.7784")
179         }
180 }
181
182 func BenchmarkAtof64FloatExp(b *testing.B) {
183         for i := 0; i < b.N; i++ {
184                 Atof64("-5.09e75")
185         }
186 }
187
188 func BenchmarkAtof64Big(b *testing.B) {
189         for i := 0; i < b.N; i++ {
190                 Atof64("123456789123456789123456789")
191         }
192 }