OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[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", os.EINVAL},
22         {"1", "1", nil},
23         {"+1", "1", nil},
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},
35         {"-1", "-1", nil},
36         {"-0", "-0", nil},
37         {"1e-20", "1e-20", nil},
38         {"625e-3", "0.625", nil},
39
40         // largest float64
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
47         // borderline - okay
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},
53
54         // a little too large
55         {"1e308", "1e+308", nil},
56         {"2e308", "+Inf", os.ERANGE},
57         {"1e309", "+Inf", os.ERANGE},
58
59         // way too large
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},
66
67         // denormalized
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},
75         // smallest denormal
76         {"5e-324", "5e-324", nil},
77         {"4e-324", "5e-324", nil},
78         {"3e-324", "5e-324", nil},
79         // too small
80         {"2e-324", "0", nil},
81         // way too small
82         {"1e-350", "0", nil},
83         {"1e-400000", "0", nil},
84
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},
90
91         // Parse errors
92         {"1e", "0", os.EINVAL},
93         {"1e-", "0", os.EINVAL},
94         {".e-1", "0", os.EINVAL},
95 }
96
97 func init() {
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]
102                 if test.err != nil {
103                         test.err = &NumError{test.in, test.err}
104                 }
105         }
106 }
107
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)
117                 }
118
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)
124                 }
125
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)
132                         }
133
134                         out, err := AtofN(test.in, 32)
135                         out32 = float32(out)
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)
140                         }
141                 }
142
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)
149                         }
150                 }
151         }
152         SetOptimize(oldopt)
153 }
154
155 func TestAtof(t *testing.T) { testAtof(t, true) }
156
157 func TestAtofSlow(t *testing.T) { testAtof(t, false) }
158
159 func BenchmarkAtofDecimal(b *testing.B) {
160         for i := 0; i < b.N; i++ {
161                 Atof("33909")
162         }
163 }
164
165 func BenchmarkAtofFloat(b *testing.B) {
166         for i := 0; i < b.N; i++ {
167                 Atof("339.7784")
168         }
169 }
170
171 func BenchmarkAtofFloatExp(b *testing.B) {
172         for i := 0; i < b.N; i++ {
173                 Atof("-5.09e75")
174         }
175 }
176
177 func BenchmarkAtofBig(b *testing.B) {
178         for i := 0; i < b.N; i++ {
179                 Atof("123456789123456789123456789")
180         }
181 }