github.com/andeya/ameda@v1.5.3/itoa62_test.go (about) 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 ameda_test 6 7 import ( 8 "testing" 9 10 . "github.com/andeya/ameda" 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 99 type uitob64Test struct { 100 in uint64 101 base int 102 out string 103 } 104 105 var uitob64tests = []uitob64Test{ 106 {1<<63 - 1, 10, "9223372036854775807"}, 107 {1 << 63, 10, "9223372036854775808"}, 108 {1<<63 + 1, 10, "9223372036854775809"}, 109 {1<<64 - 2, 10, "18446744073709551614"}, 110 {1<<64 - 1, 10, "18446744073709551615"}, 111 {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"}, 112 } 113 114 func TestUitoa(t *testing.T) { 115 for _, test := range uitob64tests { 116 s := FormatUint(test.in, test.base) 117 if s != test.out { 118 t.Errorf("FormatUint(%v, %v) = %v want %v", 119 test.in, test.base, s, test.out) 120 } 121 x := AppendUint([]byte("abc"), test.in, test.base) 122 if string(x) != "abc"+test.out { 123 t.Errorf("AppendUint(%q, %v, %v) = %q want %v", 124 "abc", test.in, test.base, x, test.out) 125 } 126 127 } 128 } 129 130 var varlenUints = []struct { 131 in uint64 132 out string 133 }{ 134 {1, "1"}, 135 {12, "12"}, 136 {123, "123"}, 137 {1234, "1234"}, 138 {12345, "12345"}, 139 {123456, "123456"}, 140 {1234567, "1234567"}, 141 {12345678, "12345678"}, 142 {123456789, "123456789"}, 143 {1234567890, "1234567890"}, 144 {12345678901, "12345678901"}, 145 {123456789012, "123456789012"}, 146 {1234567890123, "1234567890123"}, 147 {12345678901234, "12345678901234"}, 148 {123456789012345, "123456789012345"}, 149 {1234567890123456, "1234567890123456"}, 150 {12345678901234567, "12345678901234567"}, 151 {123456789012345678, "123456789012345678"}, 152 {1234567890123456789, "1234567890123456789"}, 153 {12345678901234567890, "12345678901234567890"}, 154 } 155 156 func TestFormatUintVarlen(t *testing.T) { 157 for _, test := range varlenUints { 158 s := FormatUint(test.in, 10) 159 if s != test.out { 160 t.Errorf("FormatUint(%v, 10) = %v want %v", test.in, s, test.out) 161 } 162 } 163 } 164 165 func BenchmarkFormatInt(b *testing.B) { 166 for i := 0; i < b.N; i++ { 167 for _, test := range itob64tests { 168 s := FormatInt(test.in, test.base) 169 BenchSink += len(s) 170 } 171 } 172 } 173 174 func BenchmarkAppendInt(b *testing.B) { 175 dst := make([]byte, 0, 30) 176 for i := 0; i < b.N; i++ { 177 for _, test := range itob64tests { 178 dst = AppendInt(dst[:0], test.in, test.base) 179 BenchSink += len(dst) 180 } 181 } 182 } 183 184 func BenchmarkFormatUint(b *testing.B) { 185 for i := 0; i < b.N; i++ { 186 for _, test := range uitob64tests { 187 s := FormatUint(test.in, test.base) 188 BenchSink += len(s) 189 } 190 } 191 } 192 193 func BenchmarkAppendUint(b *testing.B) { 194 dst := make([]byte, 0, 30) 195 for i := 0; i < b.N; i++ { 196 for _, test := range uitob64tests { 197 dst = AppendUint(dst[:0], test.in, test.base) 198 BenchSink += len(dst) 199 } 200 } 201 } 202 203 func BenchmarkFormatIntSmall(b *testing.B) { 204 smallInts := []int64{7, 42} 205 for _, smallInt := range smallInts { 206 b.Run(Itoa(int(smallInt)), func(b *testing.B) { 207 for i := 0; i < b.N; i++ { 208 s := FormatInt(smallInt, 10) 209 BenchSink += len(s) 210 } 211 }) 212 } 213 } 214 215 func BenchmarkAppendIntSmall(b *testing.B) { 216 dst := make([]byte, 0, 30) 217 const smallInt = 42 218 for i := 0; i < b.N; i++ { 219 dst = AppendInt(dst[:0], smallInt, 10) 220 BenchSink += len(dst) 221 } 222 } 223 224 func BenchmarkAppendUintVarlen(b *testing.B) { 225 for _, test := range varlenUints { 226 b.Run(test.out, func(b *testing.B) { 227 dst := make([]byte, 0, 30) 228 for j := 0; j < b.N; j++ { 229 dst = AppendUint(dst[:0], test.in, 10) 230 BenchSink += len(dst) 231 } 232 }) 233 } 234 } 235 236 var BenchSink int // make sure compiler cannot optimize away benchmarks