OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [strconv/] [atof_test.go] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "testing"
13
        "time"
14
)
15
 
16
type atofTest struct {
17
        in  string
18
        out string
19
        err error
20
}
21
 
22
var atoftests = []atofTest{
23
        {"", "0", ErrSyntax},
24
        {"1", "1", nil},
25
        {"+1", "1", nil},
26
        {"1x", "0", ErrSyntax},
27
        {"1.1.", "0", ErrSyntax},
28
        {"1e23", "1e+23", nil},
29
        {"1E23", "1e+23", nil},
30
        {"100000000000000000000000", "1e+23", nil},
31
        {"1e-100", "1e-100", nil},
32
        {"123456700", "1.234567e+08", nil},
33
        {"99999999999999974834176", "9.999999999999997e+22", nil},
34
        {"100000000000000000000001", "1.0000000000000001e+23", nil},
35
        {"100000000000000008388608", "1.0000000000000001e+23", nil},
36
        {"100000000000000016777215", "1.0000000000000001e+23", nil},
37
        {"100000000000000016777216", "1.0000000000000003e+23", nil},
38
        {"-1", "-1", nil},
39
        {"-0.1", "-0.1", nil},
40
        {"-0", "-0", nil},
41
        {"1e-20", "1e-20", nil},
42
        {"625e-3", "0.625", nil},
43
 
44
        // NaNs
45
        {"nan", "NaN", nil},
46
        {"NaN", "NaN", nil},
47
        {"NAN", "NaN", nil},
48
 
49
        // Infs
50
        {"inf", "+Inf", nil},
51
        {"-Inf", "-Inf", nil},
52
        {"+INF", "+Inf", nil},
53
        {"-Infinity", "-Inf", nil},
54
        {"+INFINITY", "+Inf", nil},
55
        {"Infinity", "+Inf", nil},
56
 
57
        // largest float64
58
        {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
59
        {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
60
        // next float64 - too large
61
        {"1.7976931348623159e308", "+Inf", ErrRange},
62
        {"-1.7976931348623159e308", "-Inf", ErrRange},
63
        // the border is ...158079
64
        // borderline - okay
65
        {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
66
        {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
67
        // borderline - too large
68
        {"1.797693134862315808e308", "+Inf", ErrRange},
69
        {"-1.797693134862315808e308", "-Inf", ErrRange},
70
 
71
        // a little too large
72
        {"1e308", "1e+308", nil},
73
        {"2e308", "+Inf", ErrRange},
74
        {"1e309", "+Inf", ErrRange},
75
 
76
        // way too large
77
        {"1e310", "+Inf", ErrRange},
78
        {"-1e310", "-Inf", ErrRange},
79
        {"1e400", "+Inf", ErrRange},
80
        {"-1e400", "-Inf", ErrRange},
81
        {"1e400000", "+Inf", ErrRange},
82
        {"-1e400000", "-Inf", ErrRange},
83
 
84
        // denormalized
85
        {"1e-305", "1e-305", nil},
86
        {"1e-306", "1e-306", nil},
87
        {"1e-307", "1e-307", nil},
88
        {"1e-308", "1e-308", nil},
89
        {"1e-309", "1e-309", nil},
90
        {"1e-310", "1e-310", nil},
91
        {"1e-322", "1e-322", nil},
92
        // smallest denormal
93
        {"5e-324", "5e-324", nil},
94
        {"4e-324", "5e-324", nil},
95
        {"3e-324", "5e-324", nil},
96
        // too small
97
        {"2e-324", "0", nil},
98
        // way too small
99
        {"1e-350", "0", nil},
100
        {"1e-400000", "0", nil},
101
 
102
        // try to overflow exponent
103
        {"1e-4294967296", "0", nil},
104
        {"1e+4294967296", "+Inf", ErrRange},
105
        {"1e-18446744073709551616", "0", nil},
106
        {"1e+18446744073709551616", "+Inf", ErrRange},
107
 
108
        // Parse errors
109
        {"1e", "0", ErrSyntax},
110
        {"1e-", "0", ErrSyntax},
111
        {".e-1", "0", ErrSyntax},
112
 
113
        // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
114
        {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
115
        // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
116
        {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
117
 
118
        // A very large number (initially wrongly parsed by the fast algorithm).
119
        {"4.630813248087435e+307", "4.630813248087435e+307", nil},
120
}
121
 
122
type atofSimpleTest struct {
123
        x float64
124
        s string
125
}
126
 
127
var (
128
        atofRandomTests        []atofSimpleTest
129
        benchmarksRandomBits   [1024]string
130
        benchmarksRandomNormal [1024]string
131
)
132
 
133
func init() {
134
        // The atof routines return NumErrors wrapping
135
        // the error and the string.  Convert the table above.
136
        for i := range atoftests {
137
                test := &atoftests[i]
138
                if test.err != nil {
139
                        test.err = &NumError{"ParseFloat", test.in, test.err}
140
                }
141
        }
142
 
143
        // Generate random inputs for tests and benchmarks
144
        rand.Seed(time.Now().UnixNano())
145
        if testing.Short() {
146
                atofRandomTests = make([]atofSimpleTest, 100)
147
        } else {
148
                atofRandomTests = make([]atofSimpleTest, 10000)
149
        }
150
        for i := range atofRandomTests {
151
                n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
152
                x := math.Float64frombits(n)
153
                s := FormatFloat(x, 'g', -1, 64)
154
                atofRandomTests[i] = atofSimpleTest{x, s}
155
        }
156
 
157
        for i := range benchmarksRandomBits {
158
                bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
159
                x := math.Float64frombits(bits)
160
                benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
161
        }
162
 
163
        for i := range benchmarksRandomNormal {
164
                x := rand.NormFloat64()
165
                benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
166
        }
167
}
168
 
169
func testAtof(t *testing.T, opt bool) {
170
        oldopt := SetOptimize(opt)
171
        for i := 0; i < len(atoftests); i++ {
172
                test := &atoftests[i]
173
                out, err := ParseFloat(test.in, 64)
174
                outs := FormatFloat(out, 'g', -1, 64)
175
                if outs != test.out || !reflect.DeepEqual(err, test.err) {
176
                        t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
177
                                test.in, out, err, test.out, test.err)
178
                }
179
 
180
                if float64(float32(out)) == out {
181
                        out, err := ParseFloat(test.in, 32)
182
                        out32 := float32(out)
183
                        if float64(out32) != out {
184
                                t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
185
                                continue
186
                        }
187
                        outs := FormatFloat(float64(out32), 'g', -1, 32)
188
                        if outs != test.out || !reflect.DeepEqual(err, test.err) {
189
                                t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v  # %v",
190
                                        test.in, out32, err, test.out, test.err, out)
191
                        }
192
                }
193
        }
194
        SetOptimize(oldopt)
195
}
196
 
197
func TestAtof(t *testing.T) { testAtof(t, true) }
198
 
199
func TestAtofSlow(t *testing.T) { testAtof(t, false) }
200
 
201
func TestAtofRandom(t *testing.T) {
202
        for _, test := range atofRandomTests {
203
                x, _ := ParseFloat(test.s, 64)
204
                switch {
205
                default:
206
                        t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
207
                case x == test.x:
208
                case math.IsNaN(test.x) && math.IsNaN(x):
209
                }
210
        }
211
        t.Logf("tested %d random numbers", len(atofRandomTests))
212
}
213
 
214
func BenchmarkAtof64Decimal(b *testing.B) {
215
        for i := 0; i < b.N; i++ {
216
                ParseFloat("33909", 64)
217
        }
218
}
219
 
220
func BenchmarkAtof64Float(b *testing.B) {
221
        for i := 0; i < b.N; i++ {
222
                ParseFloat("339.7784", 64)
223
        }
224
}
225
 
226
func BenchmarkAtof64FloatExp(b *testing.B) {
227
        for i := 0; i < b.N; i++ {
228
                ParseFloat("-5.09e75", 64)
229
        }
230
}
231
 
232
func BenchmarkAtof64Big(b *testing.B) {
233
        for i := 0; i < b.N; i++ {
234
                ParseFloat("123456789123456789123456789", 64)
235
        }
236
}
237
 
238
func BenchmarkAtof64RandomBits(b *testing.B) {
239
        for i := 0; i < b.N; i++ {
240
                ParseFloat(benchmarksRandomBits[i%1024], 64)
241
        }
242
}
243
 
244
func BenchmarkAtof64RandomFloats(b *testing.B) {
245
        for i := 0; i < b.N; i++ {
246
                ParseFloat(benchmarksRandomNormal[i%1024], 64)
247
        }
248
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.