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

Subversion Repositories openrisc

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

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
        "reflect"
9
        . "strconv"
10
        "testing"
11
)
12
 
13
type atoui64Test struct {
14
        in  string
15
        out uint64
16
        err error
17
}
18
 
19
var atoui64tests = []atoui64Test{
20
        {"", 0, ErrSyntax},
21
        {"0", 0, nil},
22
        {"1", 1, nil},
23
        {"12345", 12345, nil},
24
        {"012345", 12345, nil},
25
        {"12345x", 0, ErrSyntax},
26
        {"98765432100", 98765432100, nil},
27
        {"18446744073709551615", 1<<64 - 1, nil},
28
        {"18446744073709551616", 1<<64 - 1, ErrRange},
29
        {"18446744073709551620", 1<<64 - 1, ErrRange},
30
}
31
 
32
var btoui64tests = []atoui64Test{
33
        {"", 0, ErrSyntax},
34
        {"0", 0, nil},
35
        {"1", 1, nil},
36
        {"12345", 12345, nil},
37
        {"012345", 012345, nil},
38
        {"0x12345", 0x12345, nil},
39
        {"0X12345", 0x12345, nil},
40
        {"12345x", 0, ErrSyntax},
41
        {"98765432100", 98765432100, nil},
42
        {"18446744073709551615", 1<<64 - 1, nil},
43
        {"18446744073709551616", 1<<64 - 1, ErrRange},
44
        {"18446744073709551620", 1<<64 - 1, ErrRange},
45
        {"0xFFFFFFFFFFFFFFFF", 1<<64 - 1, nil},
46
        {"0x10000000000000000", 1<<64 - 1, ErrRange},
47
        {"01777777777777777777777", 1<<64 - 1, nil},
48
        {"01777777777777777777778", 0, ErrSyntax},
49
        {"02000000000000000000000", 1<<64 - 1, ErrRange},
50
        {"0200000000000000000000", 1 << 61, nil},
51
}
52
 
53
type atoi64Test struct {
54
        in  string
55
        out int64
56
        err error
57
}
58
 
59
var atoi64tests = []atoi64Test{
60
        {"", 0, ErrSyntax},
61
        {"0", 0, nil},
62
        {"-0", 0, nil},
63
        {"1", 1, nil},
64
        {"-1", -1, nil},
65
        {"12345", 12345, nil},
66
        {"-12345", -12345, nil},
67
        {"012345", 12345, nil},
68
        {"-012345", -12345, nil},
69
        {"98765432100", 98765432100, nil},
70
        {"-98765432100", -98765432100, nil},
71
        {"9223372036854775807", 1<<63 - 1, nil},
72
        {"-9223372036854775807", -(1<<63 - 1), nil},
73
        {"9223372036854775808", 1<<63 - 1, ErrRange},
74
        {"-9223372036854775808", -1 << 63, nil},
75
        {"9223372036854775809", 1<<63 - 1, ErrRange},
76
        {"-9223372036854775809", -1 << 63, ErrRange},
77
}
78
 
79
var btoi64tests = []atoi64Test{
80
        {"", 0, ErrSyntax},
81
        {"0", 0, nil},
82
        {"-0", 0, nil},
83
        {"1", 1, nil},
84
        {"-1", -1, nil},
85
        {"12345", 12345, nil},
86
        {"-12345", -12345, nil},
87
        {"012345", 012345, nil},
88
        {"-012345", -012345, nil},
89
        {"0x12345", 0x12345, nil},
90
        {"-0X12345", -0x12345, nil},
91
        {"12345x", 0, ErrSyntax},
92
        {"-12345x", 0, ErrSyntax},
93
        {"98765432100", 98765432100, nil},
94
        {"-98765432100", -98765432100, nil},
95
        {"9223372036854775807", 1<<63 - 1, nil},
96
        {"-9223372036854775807", -(1<<63 - 1), nil},
97
        {"9223372036854775808", 1<<63 - 1, ErrRange},
98
        {"-9223372036854775808", -1 << 63, nil},
99
        {"9223372036854775809", 1<<63 - 1, ErrRange},
100
        {"-9223372036854775809", -1 << 63, ErrRange},
101
}
102
 
103
type atoui32Test struct {
104
        in  string
105
        out uint32
106
        err error
107
}
108
 
109
var atoui32tests = []atoui32Test{
110
        {"", 0, ErrSyntax},
111
        {"0", 0, nil},
112
        {"1", 1, nil},
113
        {"12345", 12345, nil},
114
        {"012345", 12345, nil},
115
        {"12345x", 0, ErrSyntax},
116
        {"987654321", 987654321, nil},
117
        {"4294967295", 1<<32 - 1, nil},
118
        {"4294967296", 1<<32 - 1, ErrRange},
119
}
120
 
121
type atoi32Test struct {
122
        in  string
123
        out int32
124
        err error
125
}
126
 
127
var atoi32tests = []atoi32Test{
128
        {"", 0, ErrSyntax},
129
        {"0", 0, nil},
130
        {"-0", 0, nil},
131
        {"1", 1, nil},
132
        {"-1", -1, nil},
133
        {"12345", 12345, nil},
134
        {"-12345", -12345, nil},
135
        {"012345", 12345, nil},
136
        {"-012345", -12345, nil},
137
        {"12345x", 0, ErrSyntax},
138
        {"-12345x", 0, ErrSyntax},
139
        {"987654321", 987654321, nil},
140
        {"-987654321", -987654321, nil},
141
        {"2147483647", 1<<31 - 1, nil},
142
        {"-2147483647", -(1<<31 - 1), nil},
143
        {"2147483648", 1<<31 - 1, ErrRange},
144
        {"-2147483648", -1 << 31, nil},
145
        {"2147483649", 1<<31 - 1, ErrRange},
146
        {"-2147483649", -1 << 31, ErrRange},
147
}
148
 
149
func init() {
150
        // The atoi routines return NumErrors wrapping
151
        // the error and the string.  Convert the tables above.
152
        for i := range atoui64tests {
153
                test := &atoui64tests[i]
154
                if test.err != nil {
155
                        test.err = &NumError{"ParseUint", test.in, test.err}
156
                }
157
        }
158
        for i := range btoui64tests {
159
                test := &btoui64tests[i]
160
                if test.err != nil {
161
                        test.err = &NumError{"ParseUint", test.in, test.err}
162
                }
163
        }
164
        for i := range atoi64tests {
165
                test := &atoi64tests[i]
166
                if test.err != nil {
167
                        test.err = &NumError{"ParseInt", test.in, test.err}
168
                }
169
        }
170
        for i := range btoi64tests {
171
                test := &btoi64tests[i]
172
                if test.err != nil {
173
                        test.err = &NumError{"ParseInt", test.in, test.err}
174
                }
175
        }
176
        for i := range atoui32tests {
177
                test := &atoui32tests[i]
178
                if test.err != nil {
179
                        test.err = &NumError{"ParseUint", test.in, test.err}
180
                }
181
        }
182
        for i := range atoi32tests {
183
                test := &atoi32tests[i]
184
                if test.err != nil {
185
                        test.err = &NumError{"ParseInt", test.in, test.err}
186
                }
187
        }
188
}
189
 
190
func TestParseUint64(t *testing.T) {
191
        for i := range atoui64tests {
192
                test := &atoui64tests[i]
193
                out, err := ParseUint(test.in, 10, 64)
194
                if test.out != out || !reflect.DeepEqual(test.err, err) {
195
                        t.Errorf("Atoui64(%q) = %v, %v want %v, %v",
196
                                test.in, out, err, test.out, test.err)
197
                }
198
        }
199
}
200
 
201
func TestParseUint64Base(t *testing.T) {
202
        for i := range btoui64tests {
203
                test := &btoui64tests[i]
204
                out, err := ParseUint(test.in, 0, 64)
205
                if test.out != out || !reflect.DeepEqual(test.err, err) {
206
                        t.Errorf("ParseUint(%q) = %v, %v want %v, %v",
207
                                test.in, out, err, test.out, test.err)
208
                }
209
        }
210
}
211
 
212
func TestParseInt64(t *testing.T) {
213
        for i := range atoi64tests {
214
                test := &atoi64tests[i]
215
                out, err := ParseInt(test.in, 10, 64)
216
                if test.out != out || !reflect.DeepEqual(test.err, err) {
217
                        t.Errorf("Atoi64(%q) = %v, %v want %v, %v",
218
                                test.in, out, err, test.out, test.err)
219
                }
220
        }
221
}
222
 
223
func TestParseInt64Base(t *testing.T) {
224
        for i := range btoi64tests {
225
                test := &btoi64tests[i]
226
                out, err := ParseInt(test.in, 0, 64)
227
                if test.out != out || !reflect.DeepEqual(test.err, err) {
228
                        t.Errorf("ParseInt(%q) = %v, %v want %v, %v",
229
                                test.in, out, err, test.out, test.err)
230
                }
231
        }
232
}
233
 
234
func TestParseUint(t *testing.T) {
235
        switch IntSize {
236
        case 32:
237
                for i := range atoui32tests {
238
                        test := &atoui32tests[i]
239
                        out, err := ParseUint(test.in, 10, 0)
240
                        if test.out != uint32(out) || !reflect.DeepEqual(test.err, err) {
241
                                t.Errorf("Atoui(%q) = %v, %v want %v, %v",
242
                                        test.in, out, err, test.out, test.err)
243
                        }
244
                }
245
        case 64:
246
                for i := range atoui64tests {
247
                        test := &atoui64tests[i]
248
                        out, err := ParseUint(test.in, 10, 0)
249
                        if test.out != uint64(out) || !reflect.DeepEqual(test.err, err) {
250
                                t.Errorf("Atoui(%q) = %v, %v want %v, %v",
251
                                        test.in, out, err, test.out, test.err)
252
                        }
253
                }
254
        }
255
}
256
 
257
func TestParseInt(t *testing.T) {
258
        switch IntSize {
259
        case 32:
260
                for i := range atoi32tests {
261
                        test := &atoi32tests[i]
262
                        out, err := ParseInt(test.in, 10, 0)
263
                        if test.out != int32(out) || !reflect.DeepEqual(test.err, err) {
264
                                t.Errorf("Atoi(%q) = %v, %v want %v, %v",
265
                                        test.in, out, err, test.out, test.err)
266
                        }
267
                }
268
        case 64:
269
                for i := range atoi64tests {
270
                        test := &atoi64tests[i]
271
                        out, err := ParseInt(test.in, 10, 0)
272
                        if test.out != int64(out) || !reflect.DeepEqual(test.err, err) {
273
                                t.Errorf("Atoi(%q) = %v, %v want %v, %v",
274
                                        test.in, out, err, test.out, test.err)
275
                        }
276
                }
277
        }
278
}
279
 
280
func BenchmarkAtoi(b *testing.B) {
281
        for i := 0; i < b.N; i++ {
282
                ParseInt("12345678", 10, 0)
283
        }
284
}
285
 
286
func BenchmarkAtoiNeg(b *testing.B) {
287
        for i := 0; i < b.N; i++ {
288
                ParseInt("-12345678", 10, 0)
289
        }
290
}
291
 
292
func BenchmarkAtoi64(b *testing.B) {
293
        for i := 0; i < b.N; i++ {
294
                ParseInt("12345678901234", 10, 64)
295
        }
296
}
297
 
298
func BenchmarkAtoi64Neg(b *testing.B) {
299
        for i := 0; i < b.N; i++ {
300
                ParseInt("-12345678901234", 10, 64)
301
        }
302
}

powered by: WebSVN 2.1.0

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