libgo: update to Go 1.11
[official-gcc.git] / libgo / go / strconv / itoa_test.go
blobb5ee3aa82803706ac623c66a498ebe7b6178d61f
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.
5 package strconv_test
7 import (
8 . "strconv"
9 "testing"
12 type itob64Test struct {
13 in int64
14 base int
15 out string
18 var itob64tests = []itob64Test{
19 {0, 10, "0"},
20 {1, 10, "1"},
21 {-1, 10, "-1"},
22 {12345678, 10, "12345678"},
23 {-987654321, 10, "-987654321"},
24 {1<<31 - 1, 10, "2147483647"},
25 {-1<<31 + 1, 10, "-2147483647"},
26 {1 << 31, 10, "2147483648"},
27 {-1 << 31, 10, "-2147483648"},
28 {1<<31 + 1, 10, "2147483649"},
29 {-1<<31 - 1, 10, "-2147483649"},
30 {1<<32 - 1, 10, "4294967295"},
31 {-1<<32 + 1, 10, "-4294967295"},
32 {1 << 32, 10, "4294967296"},
33 {-1 << 32, 10, "-4294967296"},
34 {1<<32 + 1, 10, "4294967297"},
35 {-1<<32 - 1, 10, "-4294967297"},
36 {1 << 50, 10, "1125899906842624"},
37 {1<<63 - 1, 10, "9223372036854775807"},
38 {-1<<63 + 1, 10, "-9223372036854775807"},
39 {-1 << 63, 10, "-9223372036854775808"},
41 {0, 2, "0"},
42 {10, 2, "1010"},
43 {-1, 2, "-1"},
44 {1 << 15, 2, "1000000000000000"},
46 {-8, 8, "-10"},
47 {057635436545, 8, "57635436545"},
48 {1 << 24, 8, "100000000"},
50 {16, 16, "10"},
51 {-0x123456789abcdef, 16, "-123456789abcdef"},
52 {1<<63 - 1, 16, "7fffffffffffffff"},
53 {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
54 {-1 << 63, 2, "-1000000000000000000000000000000000000000000000000000000000000000"},
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"},
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)
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)
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)
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)
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)
98 type uitob64Test struct {
99 in uint64
100 base int
101 out string
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"},
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)
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)
129 var varlenUints = []struct {
130 in uint64
131 out string
133 {1, "1"},
134 {12, "12"},
135 {123, "123"},
136 {1234, "1234"},
137 {12345, "12345"},
138 {123456, "123456"},
139 {1234567, "1234567"},
140 {12345678, "12345678"},
141 {123456789, "123456789"},
142 {1234567890, "1234567890"},
143 {12345678901, "12345678901"},
144 {123456789012, "123456789012"},
145 {1234567890123, "1234567890123"},
146 {12345678901234, "12345678901234"},
147 {123456789012345, "123456789012345"},
148 {1234567890123456, "1234567890123456"},
149 {12345678901234567, "12345678901234567"},
150 {123456789012345678, "123456789012345678"},
151 {1234567890123456789, "1234567890123456789"},
152 {12345678901234567890, "12345678901234567890"},
155 func TestFormatUintVarlen(t *testing.T) {
156 for _, test := range varlenUints {
157 s := FormatUint(test.in, 10)
158 if s != test.out {
159 t.Errorf("FormatUint(%v, 10) = %v want %v", test.in, s, test.out)
164 func BenchmarkFormatInt(b *testing.B) {
165 for i := 0; i < b.N; i++ {
166 for _, test := range itob64tests {
167 s := FormatInt(test.in, test.base)
168 BenchSink += len(s)
173 func BenchmarkAppendInt(b *testing.B) {
174 dst := make([]byte, 0, 30)
175 for i := 0; i < b.N; i++ {
176 for _, test := range itob64tests {
177 dst = AppendInt(dst[:0], test.in, test.base)
178 BenchSink += len(dst)
183 func BenchmarkFormatUint(b *testing.B) {
184 for i := 0; i < b.N; i++ {
185 for _, test := range uitob64tests {
186 s := FormatUint(test.in, test.base)
187 BenchSink += len(s)
192 func BenchmarkAppendUint(b *testing.B) {
193 dst := make([]byte, 0, 30)
194 for i := 0; i < b.N; i++ {
195 for _, test := range uitob64tests {
196 dst = AppendUint(dst[:0], test.in, test.base)
197 BenchSink += len(dst)
202 func BenchmarkFormatIntSmall(b *testing.B) {
203 smallInts := []int64{7, 42}
204 for _, smallInt := range smallInts {
205 b.Run(Itoa(int(smallInt)), func(b *testing.B) {
206 for i := 0; i < b.N; i++ {
207 s := FormatInt(smallInt, 10)
208 BenchSink += len(s)
214 func BenchmarkAppendIntSmall(b *testing.B) {
215 dst := make([]byte, 0, 30)
216 const smallInt = 42
217 for i := 0; i < b.N; i++ {
218 dst = AppendInt(dst[:0], smallInt, 10)
219 BenchSink += len(dst)
223 func BenchmarkAppendUintVarlen(b *testing.B) {
224 for _, test := range varlenUints {
225 b.Run(test.out, func(b *testing.B) {
226 dst := make([]byte, 0, 30)
227 for j := 0; j < b.N; j++ {
228 dst = AppendUint(dst[:0], test.in, 10)
229 BenchSink += len(dst)
235 var BenchSink int // make sure compiler cannot optimize away benchmarks