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

Subversion Repositories openrisc

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

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
        "runtime"
9
        . "strconv"
10
        "testing"
11
)
12
 
13
type itob64Test struct {
14
        in   int64
15
        base int
16
        out  string
17
}
18
 
19
var itob64tests = []itob64Test{
20
        {0, 10, "0"},
21
        {1, 10, "1"},
22
        {-1, 10, "-1"},
23
        {12345678, 10, "12345678"},
24
        {-987654321, 10, "-987654321"},
25
        {1<<31 - 1, 10, "2147483647"},
26
        {-1<<31 + 1, 10, "-2147483647"},
27
        {1 << 31, 10, "2147483648"},
28
        {-1 << 31, 10, "-2147483648"},
29
        {1<<31 + 1, 10, "2147483649"},
30
        {-1<<31 - 1, 10, "-2147483649"},
31
        {1<<32 - 1, 10, "4294967295"},
32
        {-1<<32 + 1, 10, "-4294967295"},
33
        {1 << 32, 10, "4294967296"},
34
        {-1 << 32, 10, "-4294967296"},
35
        {1<<32 + 1, 10, "4294967297"},
36
        {-1<<32 - 1, 10, "-4294967297"},
37
        {1 << 50, 10, "1125899906842624"},
38
        {1<<63 - 1, 10, "9223372036854775807"},
39
        {-1<<63 + 1, 10, "-9223372036854775807"},
40
        {-1 << 63, 10, "-9223372036854775808"},
41
 
42
        {0, 2, "0"},
43
        {10, 2, "1010"},
44
        {-1, 2, "-1"},
45
        {1 << 15, 2, "1000000000000000"},
46
 
47
        {-8, 8, "-10"},
48
        {057635436545, 8, "57635436545"},
49
        {1 << 24, 8, "100000000"},
50
 
51
        {16, 16, "10"},
52
        {-0x123456789abcdef, 16, "-123456789abcdef"},
53
        {1<<63 - 1, 16, "7fffffffffffffff"},
54
        {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
55
 
56
        {16, 17, "g"},
57
        {25, 25, "10"},
58
        {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
59
        {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
60
}
61
 
62
func TestItoa(t *testing.T) {
63
        for _, test := range itob64tests {
64
                s := FormatInt(test.in, test.base)
65
                if s != test.out {
66
                        t.Errorf("FormatInt(%v, %v) = %v want %v",
67
                                test.in, test.base, s, test.out)
68
                }
69
                x := AppendInt([]byte("abc"), test.in, test.base)
70
                if string(x) != "abc"+test.out {
71
                        t.Errorf("AppendInt(%q, %v, %v) = %q want %v",
72
                                "abc", test.in, test.base, x, test.out)
73
                }
74
 
75
                if test.in >= 0 {
76
                        s := FormatUint(uint64(test.in), test.base)
77
                        if s != test.out {
78
                                t.Errorf("FormatUint(%v, %v) = %v want %v",
79
                                        test.in, test.base, s, test.out)
80
                        }
81
                        x := AppendUint(nil, uint64(test.in), test.base)
82
                        if string(x) != test.out {
83
                                t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
84
                                        "abc", uint64(test.in), test.base, x, test.out)
85
                        }
86
                }
87
 
88
                if test.base == 10 && int64(int(test.in)) == test.in {
89
                        s := Itoa(int(test.in))
90
                        if s != test.out {
91
                                t.Errorf("Itoa(%v) = %v want %v",
92
                                        test.in, s, test.out)
93
                        }
94
                }
95
        }
96
}
97
 
98
type uitob64Test struct {
99
        in   uint64
100
        base int
101
        out  string
102
}
103
 
104
var uitob64tests = []uitob64Test{
105
        {1<<63 - 1, 10, "9223372036854775807"},
106
        {1 << 63, 10, "9223372036854775808"},
107
        {1<<63 + 1, 10, "9223372036854775809"},
108
        {1<<64 - 2, 10, "18446744073709551614"},
109
        {1<<64 - 1, 10, "18446744073709551615"},
110
        {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
111
}
112
 
113
func TestUitoa(t *testing.T) {
114
        for _, test := range uitob64tests {
115
                s := FormatUint(test.in, test.base)
116
                if s != test.out {
117
                        t.Errorf("FormatUint(%v, %v) = %v want %v",
118
                                test.in, test.base, s, test.out)
119
                }
120
                x := AppendUint([]byte("abc"), test.in, test.base)
121
                if string(x) != "abc"+test.out {
122
                        t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
123
                                "abc", test.in, test.base, x, test.out)
124
                }
125
 
126
        }
127
}
128
 
129
func numAllocations(f func()) int {
130
        memstats := new(runtime.MemStats)
131
        runtime.ReadMemStats(memstats)
132
        n0 := memstats.Mallocs
133
        f()
134
        runtime.ReadMemStats(memstats)
135
        return int(memstats.Mallocs - n0)
136
}
137
 
138
/* This test relies on escape analysis which gccgo does not yet do.
139
 
140
var globalBuf [64]byte
141
 
142
func TestAppendUintDoesntAllocate(t *testing.T) {
143
        n := numAllocations(func() {
144
                var buf [64]byte
145
                AppendInt(buf[:0], 123, 10)
146
        })
147
        want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better
148
        if n != want {
149
                t.Errorf("with local buffer, did %d allocations, want %d", n, want)
150
        }
151
        n = numAllocations(func() {
152
                AppendInt(globalBuf[:0], 123, 10)
153
        })
154
        if n != 0 {
155
                t.Errorf("with reused buffer, did %d allocations, want 0", n)
156
        }
157
}
158
 
159
*/
160
 
161
func BenchmarkFormatInt(b *testing.B) {
162
        for i := 0; i < b.N; i++ {
163
                for _, test := range itob64tests {
164
                        FormatInt(test.in, test.base)
165
                }
166
        }
167
}
168
 
169
func BenchmarkAppendInt(b *testing.B) {
170
        dst := make([]byte, 0, 30)
171
        for i := 0; i < b.N; i++ {
172
                for _, test := range itob64tests {
173
                        AppendInt(dst, test.in, test.base)
174
                }
175
        }
176
}
177
 
178
func BenchmarkFormatUint(b *testing.B) {
179
        for i := 0; i < b.N; i++ {
180
                for _, test := range uitob64tests {
181
                        FormatUint(test.in, test.base)
182
                }
183
        }
184
}
185
 
186
func BenchmarkAppendUint(b *testing.B) {
187
        dst := make([]byte, 0, 30)
188
        for i := 0; i < b.N; i++ {
189
                for _, test := range uitob64tests {
190
                        AppendUint(dst, test.in, test.base)
191
                }
192
        }
193
}

powered by: WebSVN 2.1.0

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