itoa_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. package strconv_test
  5. import (
  6. . "strconv"
  7. "testing"
  8. )
  9. type itob64Test struct {
  10. in int64
  11. base int
  12. out string
  13. }
  14. var itob64tests = []itob64Test{
  15. {0, 10, "0"},
  16. {1, 10, "1"},
  17. {-1, 10, "-1"},
  18. {12345678, 10, "12345678"},
  19. {-987654321, 10, "-987654321"},
  20. {1<<31 - 1, 10, "2147483647"},
  21. {-1<<31 + 1, 10, "-2147483647"},
  22. {1 << 31, 10, "2147483648"},
  23. {-1 << 31, 10, "-2147483648"},
  24. {1<<31 + 1, 10, "2147483649"},
  25. {-1<<31 - 1, 10, "-2147483649"},
  26. {1<<32 - 1, 10, "4294967295"},
  27. {-1<<32 + 1, 10, "-4294967295"},
  28. {1 << 32, 10, "4294967296"},
  29. {-1 << 32, 10, "-4294967296"},
  30. {1<<32 + 1, 10, "4294967297"},
  31. {-1<<32 - 1, 10, "-4294967297"},
  32. {1 << 50, 10, "1125899906842624"},
  33. {1<<63 - 1, 10, "9223372036854775807"},
  34. {-1<<63 + 1, 10, "-9223372036854775807"},
  35. {-1 << 63, 10, "-9223372036854775808"},
  36. {0, 2, "0"},
  37. {10, 2, "1010"},
  38. {-1, 2, "-1"},
  39. {1 << 15, 2, "1000000000000000"},
  40. {-8, 8, "-10"},
  41. {057635436545, 8, "57635436545"},
  42. {1 << 24, 8, "100000000"},
  43. {16, 16, "10"},
  44. {-0x123456789abcdef, 16, "-123456789abcdef"},
  45. {1<<63 - 1, 16, "7fffffffffffffff"},
  46. {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
  47. {-1 << 63, 2, "-1000000000000000000000000000000000000000000000000000000000000000"},
  48. {16, 17, "g"},
  49. {25, 25, "10"},
  50. {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
  51. {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
  52. }
  53. func TestItoa(t *testing.T) {
  54. for _, test := range itob64tests {
  55. s := FormatInt(test.in, test.base)
  56. if s != test.out {
  57. t.Errorf("FormatInt(%v, %v) = %v want %v",
  58. test.in, test.base, s, test.out)
  59. }
  60. x := AppendInt([]byte("abc"), test.in, test.base)
  61. if string(x) != "abc"+test.out {
  62. t.Errorf("AppendInt(%q, %v, %v) = %q want %v",
  63. "abc", test.in, test.base, x, test.out)
  64. }
  65. if test.in >= 0 {
  66. s := FormatUint(uint64(test.in), test.base)
  67. if s != test.out {
  68. t.Errorf("FormatUint(%v, %v) = %v want %v",
  69. test.in, test.base, s, test.out)
  70. }
  71. x := AppendUint(nil, uint64(test.in), test.base)
  72. if string(x) != test.out {
  73. t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
  74. "abc", uint64(test.in), test.base, x, test.out)
  75. }
  76. }
  77. if test.base == 10 && int64(int(test.in)) == test.in {
  78. s := Itoa(int(test.in))
  79. if s != test.out {
  80. t.Errorf("Itoa(%v) = %v want %v",
  81. test.in, s, test.out)
  82. }
  83. }
  84. }
  85. }
  86. type uitob64Test struct {
  87. in uint64
  88. base int
  89. out string
  90. }
  91. var uitob64tests = []uitob64Test{
  92. {1<<63 - 1, 10, "9223372036854775807"},
  93. {1 << 63, 10, "9223372036854775808"},
  94. {1<<63 + 1, 10, "9223372036854775809"},
  95. {1<<64 - 2, 10, "18446744073709551614"},
  96. {1<<64 - 1, 10, "18446744073709551615"},
  97. {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
  98. }
  99. func TestUitoa(t *testing.T) {
  100. for _, test := range uitob64tests {
  101. s := FormatUint(test.in, test.base)
  102. if s != test.out {
  103. t.Errorf("FormatUint(%v, %v) = %v want %v",
  104. test.in, test.base, s, test.out)
  105. }
  106. x := AppendUint([]byte("abc"), test.in, test.base)
  107. if string(x) != "abc"+test.out {
  108. t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
  109. "abc", test.in, test.base, x, test.out)
  110. }
  111. }
  112. }
  113. var varlenUints = []struct {
  114. in uint64
  115. out string
  116. }{
  117. {1, "1"},
  118. {12, "12"},
  119. {123, "123"},
  120. {1234, "1234"},
  121. {12345, "12345"},
  122. {123456, "123456"},
  123. {1234567, "1234567"},
  124. {12345678, "12345678"},
  125. {123456789, "123456789"},
  126. {1234567890, "1234567890"},
  127. {12345678901, "12345678901"},
  128. {123456789012, "123456789012"},
  129. {1234567890123, "1234567890123"},
  130. {12345678901234, "12345678901234"},
  131. {123456789012345, "123456789012345"},
  132. {1234567890123456, "1234567890123456"},
  133. {12345678901234567, "12345678901234567"},
  134. {123456789012345678, "123456789012345678"},
  135. {1234567890123456789, "1234567890123456789"},
  136. {12345678901234567890, "12345678901234567890"},
  137. }
  138. func TestFormatUintVarlen(t *testing.T) {
  139. for _, test := range varlenUints {
  140. s := FormatUint(test.in, 10)
  141. if s != test.out {
  142. t.Errorf("FormatUint(%v, 10) = %v want %v", test.in, s, test.out)
  143. }
  144. }
  145. }
  146. func BenchmarkFormatInt(b *testing.B) {
  147. for i := 0; i < b.N; i++ {
  148. for _, test := range itob64tests {
  149. s := FormatInt(test.in, test.base)
  150. BenchSink += len(s)
  151. }
  152. }
  153. }
  154. func BenchmarkAppendInt(b *testing.B) {
  155. dst := make([]byte, 0, 30)
  156. for i := 0; i < b.N; i++ {
  157. for _, test := range itob64tests {
  158. dst = AppendInt(dst[:0], test.in, test.base)
  159. BenchSink += len(dst)
  160. }
  161. }
  162. }
  163. func BenchmarkFormatUint(b *testing.B) {
  164. for i := 0; i < b.N; i++ {
  165. for _, test := range uitob64tests {
  166. s := FormatUint(test.in, test.base)
  167. BenchSink += len(s)
  168. }
  169. }
  170. }
  171. func BenchmarkAppendUint(b *testing.B) {
  172. dst := make([]byte, 0, 30)
  173. for i := 0; i < b.N; i++ {
  174. for _, test := range uitob64tests {
  175. dst = AppendUint(dst[:0], test.in, test.base)
  176. BenchSink += len(dst)
  177. }
  178. }
  179. }
  180. func BenchmarkFormatIntSmall(b *testing.B) {
  181. smallInts := []int64{7, 42}
  182. for _, smallInt := range smallInts {
  183. b.Run(Itoa(int(smallInt)), func(b *testing.B) {
  184. for i := 0; i < b.N; i++ {
  185. s := FormatInt(smallInt, 10)
  186. BenchSink += len(s)
  187. }
  188. })
  189. }
  190. }
  191. func BenchmarkAppendIntSmall(b *testing.B) {
  192. dst := make([]byte, 0, 30)
  193. const smallInt = 42
  194. for i := 0; i < b.N; i++ {
  195. dst = AppendInt(dst[:0], smallInt, 10)
  196. BenchSink += len(dst)
  197. }
  198. }
  199. func BenchmarkAppendUintVarlen(b *testing.B) {
  200. for _, test := range varlenUints {
  201. b.Run(test.out, func(b *testing.B) {
  202. dst := make([]byte, 0, 30)
  203. for j := 0; j < b.N; j++ {
  204. dst = AppendUint(dst[:0], test.in, 10)
  205. BenchSink += len(dst)
  206. }
  207. })
  208. }
  209. }
  210. var BenchSink int // make sure compiler cannot optimize away benchmarks