| 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 |
"fmt" |
| 9 |
. "internal/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 |
{-1 << 63, 2, "-1000000000000000000000000000000000000000000000000000000000000000"}, |
| 56 |
|
| 57 |
{16, 17, "g"}, |
| 58 |
{25, 25, "10"}, |
| 59 |
{(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"}, |
| 60 |
{(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"}, |
| 61 |
} |
| 62 |
|
| 63 |
func TestItoa(t *testing.T) { |
| 64 |
for _, test := range itob64tests { |
| 65 |
s := FormatInt(test.in, test.base) |
| 66 |
if s != test.out { |
| 67 |
t.Errorf("FormatInt(%v, %v) = %v want %v", |
| 68 |
test.in, test.base, s, test.out) |
| 69 |
} |
| 70 |
x := AppendInt([]byte("abc"), test.in, test.base) |
| 71 |
if string(x) != "abc"+test.out { |
| 72 |
t.Errorf("AppendInt(%q, %v, %v) = %q want %v", |
| 73 |
"abc", test.in, test.base, x, test.out) |
| 74 |
} |
| 75 |
|
| 76 |
if test.in >= 0 { |
| 77 |
s := FormatUint(uint64(test.in), test.base) |
| 78 |
if s != test.out { |
| 79 |
t.Errorf("FormatUint(%v, %v) = %v want %v", |
| 80 |
test.in, test.base, s, test.out) |
| 81 |
} |
| 82 |
x := AppendUint(nil, uint64(test.in), test.base) |
| 83 |
if string(x) != test.out { |
| 84 |
t.Errorf("AppendUint(%q, %v, %v) = %q want %v", |
| 85 |
"abc", uint64(test.in), test.base, x, test.out) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if test.base == 10 && int64(int(test.in)) == test.in { |
| 90 |
s := Itoa(int(test.in)) |
| 91 |
if s != test.out { |
| 92 |
t.Errorf("Itoa(%v) = %v want %v", |
| 93 |
test.in, s, test.out) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Override when base is illegal |
| 99 |
defer func() { |
| 100 |
if r := recover(); r == nil { |
| 101 |
t.Fatalf("expected panic due to illegal base") |
| 102 |
} |
| 103 |
}() |
| 104 |
FormatUint(12345678, 1) |
| 105 |
} |
| 106 |
|
| 107 |
type uitob64Test struct { |
| 108 |
in uint64 |
| 109 |
base int |
| 110 |
out string |
| 111 |
} |
| 112 |
|
| 113 |
var uitob64tests = []uitob64Test{ |
| 114 |
{1<<63 - 1, 10, "9223372036854775807"}, |
| 115 |
{1 << 63, 10, "9223372036854775808"}, |
| 116 |
{1<<63 + 1, 10, "9223372036854775809"}, |
| 117 |
{1<<64 - 2, 10, "18446744073709551614"}, |
| 118 |
{1<<64 - 1, 10, "18446744073709551615"}, |
| 119 |
{1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"}, |
| 120 |
} |
| 121 |
|
| 122 |
func TestUitoa(t *testing.T) { |
| 123 |
for _, test := range uitob64tests { |
| 124 |
s := FormatUint(test.in, test.base) |
| 125 |
if s != test.out { |
| 126 |
t.Errorf("FormatUint(%v, %v) = %v want %v", |
| 127 |
test.in, test.base, s, test.out) |
| 128 |
} |
| 129 |
x := AppendUint([]byte("abc"), test.in, test.base) |
| 130 |
if string(x) != "abc"+test.out { |
| 131 |
t.Errorf("AppendUint(%q, %v, %v) = %q want %v", |
| 132 |
"abc", test.in, test.base, x, test.out) |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
var varlenUints = []struct { |
| 139 |
in uint64 |
| 140 |
out string |
| 141 |
}{ |
| 142 |
{1, "1"}, |
| 143 |
{12, "12"}, |
| 144 |
{123, "123"}, |
| 145 |
{1234, "1234"}, |
| 146 |
{12345, "12345"}, |
| 147 |
{123456, "123456"}, |
| 148 |
{1234567, "1234567"}, |
| 149 |
{12345678, "12345678"}, |
| 150 |
{123456789, "123456789"}, |
| 151 |
{1234567890, "1234567890"}, |
| 152 |
{12345678901, "12345678901"}, |
| 153 |
{123456789012, "123456789012"}, |
| 154 |
{1234567890123, "1234567890123"}, |
| 155 |
{12345678901234, "12345678901234"}, |
| 156 |
{123456789012345, "123456789012345"}, |
| 157 |
{1234567890123456, "1234567890123456"}, |
| 158 |
{12345678901234567, "12345678901234567"}, |
| 159 |
{123456789012345678, "123456789012345678"}, |
| 160 |
{1234567890123456789, "1234567890123456789"}, |
| 161 |
{12345678901234567890, "12345678901234567890"}, |
| 162 |
} |
| 163 |
|
| 164 |
func TestFormatUintVarlen(t *testing.T) { |
| 165 |
for _, test := range varlenUints { |
| 166 |
s := FormatUint(test.in, 10) |
| 167 |
if s != test.out { |
| 168 |
t.Errorf("FormatUint(%v, 10) = %v want %v", test.in, s, test.out) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func BenchmarkFormatInt(b *testing.B) { |
| 174 |
for i := 0; i < b.N; i++ { |
| 175 |
for _, test := range itob64tests { |
| 176 |
s := FormatInt(test.in, test.base) |
| 177 |
BenchSink += len(s) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func BenchmarkAppendInt(b *testing.B) { |
| 183 |
dst := make([]byte, 0, 30) |
| 184 |
for i := 0; i < b.N; i++ { |
| 185 |
for _, test := range itob64tests { |
| 186 |
dst = AppendInt(dst[:0], test.in, test.base) |
| 187 |
BenchSink += len(dst) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func BenchmarkFormatUint(b *testing.B) { |
| 193 |
for i := 0; i < b.N; i++ { |
| 194 |
for _, test := range uitob64tests { |
| 195 |
s := FormatUint(test.in, test.base) |
| 196 |
BenchSink += len(s) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func BenchmarkAppendUint(b *testing.B) { |
| 202 |
dst := make([]byte, 0, 30) |
| 203 |
for i := 0; i < b.N; i++ { |
| 204 |
for _, test := range uitob64tests { |
| 205 |
dst = AppendUint(dst[:0], test.in, test.base) |
| 206 |
BenchSink += len(dst) |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func BenchmarkFormatIntSmall(b *testing.B) { |
| 212 |
smallInts := []int64{7, 42} |
| 213 |
for _, smallInt := range smallInts { |
| 214 |
b.Run(Itoa(int(smallInt)), func(b *testing.B) { |
| 215 |
for i := 0; i < b.N; i++ { |
| 216 |
s := FormatInt(smallInt, 10) |
| 217 |
BenchSink += len(s) |
| 218 |
} |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func BenchmarkAppendIntSmall(b *testing.B) { |
| 224 |
dst := make([]byte, 0, 30) |
| 225 |
const smallInt = 42 |
| 226 |
for i := 0; i < b.N; i++ { |
| 227 |
dst = AppendInt(dst[:0], smallInt, 10) |
| 228 |
BenchSink += len(dst) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func BenchmarkAppendUintVarlen(b *testing.B) { |
| 233 |
for _, test := range varlenUints { |
| 234 |
b.Run(fmt.Sprint("digits=", len(test.out)), func(b *testing.B) { |
| 235 |
dst := make([]byte, 0, 30) |
| 236 |
for j := 0; j < b.N; j++ { |
| 237 |
dst = AppendUint(dst[:0], test.in, 10) |
| 238 |
BenchSink += len(dst) |
| 239 |
} |
| 240 |
}) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
var BenchSink int // make sure compiler cannot optimize away benchmarks |