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.
12 type itob64Test
struct {
18 var itob64tests
= []itob64Test
{
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"},
44 {1 << 15, 2, "1000000000000000"},
47 {057635436545, 8, "57635436545"},
48 {1 << 24, 8, "100000000"},
51 {-0x123456789abcdef, 16, "-123456789abcdef"},
52 {1<<63 - 1, 16, "7fffffffffffffff"},
53 {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
57 {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
58 {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
61 func TestItoa(t
*testing
.T
) {
62 for _
, test
:= range itob64tests
{
63 s
:= FormatInt(test
.in
, test
.base
)
65 t
.Errorf("FormatInt(%v, %v) = %v want %v",
66 test
.in
, test
.base
, s
, test
.out
)
68 x
:= AppendInt([]byte("abc"), test
.in
, test
.base
)
69 if string(x
) != "abc"+test
.out
{
70 t
.Errorf("AppendInt(%q, %v, %v) = %q want %v",
71 "abc", test
.in
, test
.base
, x
, test
.out
)
75 s
:= FormatUint(uint64(test
.in
), test
.base
)
77 t
.Errorf("FormatUint(%v, %v) = %v want %v",
78 test
.in
, test
.base
, s
, test
.out
)
80 x
:= AppendUint(nil, uint64(test
.in
), test
.base
)
81 if string(x
) != test
.out
{
82 t
.Errorf("AppendUint(%q, %v, %v) = %q want %v",
83 "abc", uint64(test
.in
), test
.base
, x
, test
.out
)
87 if test
.base
== 10 && int64(int(test
.in
)) == test
.in
{
88 s
:= Itoa(int(test
.in
))
90 t
.Errorf("Itoa(%v) = %v want %v",
97 type uitob64Test
struct {
103 var uitob64tests
= []uitob64Test
{
104 {1<<63 - 1, 10, "9223372036854775807"},
105 {1 << 63, 10, "9223372036854775808"},
106 {1<<63 + 1, 10, "9223372036854775809"},
107 {1<<64 - 2, 10, "18446744073709551614"},
108 {1<<64 - 1, 10, "18446744073709551615"},
109 {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
112 func TestUitoa(t
*testing
.T
) {
113 for _
, test
:= range uitob64tests
{
114 s
:= FormatUint(test
.in
, test
.base
)
116 t
.Errorf("FormatUint(%v, %v) = %v want %v",
117 test
.in
, test
.base
, s
, test
.out
)
119 x
:= AppendUint([]byte("abc"), test
.in
, test
.base
)
120 if string(x
) != "abc"+test
.out
{
121 t
.Errorf("AppendUint(%q, %v, %v) = %q want %v",
122 "abc", test
.in
, test
.base
, x
, test
.out
)
128 func BenchmarkFormatInt(b
*testing
.B
) {
129 for i
:= 0; i
< b
.N
; i
++ {
130 for _
, test
:= range itob64tests
{
131 FormatInt(test
.in
, test
.base
)
136 func BenchmarkAppendInt(b
*testing
.B
) {
137 dst
:= make([]byte, 0, 30)
138 for i
:= 0; i
< b
.N
; i
++ {
139 for _
, test
:= range itob64tests
{
140 AppendInt(dst
, test
.in
, test
.base
)
145 func BenchmarkFormatUint(b
*testing
.B
) {
146 for i
:= 0; i
< b
.N
; i
++ {
147 for _
, test
:= range uitob64tests
{
148 FormatUint(test
.in
, test
.base
)
153 func BenchmarkAppendUint(b
*testing
.B
) {
154 dst
:= make([]byte, 0, 30)
155 for i
:= 0; i
< b
.N
; i
++ {
156 for _
, test
:= range uitob64tests
{
157 AppendUint(dst
, test
.in
, test
.base
)