itoa_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. {16, 17, "g"},
  48. {25, 25, "10"},
  49. {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
  50. {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
  51. }
  52. func TestItoa(t *testing.T) {
  53. for _, test := range itob64tests {
  54. s := FormatInt(test.in, test.base)
  55. if s != test.out {
  56. t.Errorf("FormatInt(%v, %v) = %v want %v",
  57. test.in, test.base, s, test.out)
  58. }
  59. x := AppendInt([]byte("abc"), test.in, test.base)
  60. if string(x) != "abc"+test.out {
  61. t.Errorf("AppendInt(%q, %v, %v) = %q want %v",
  62. "abc", test.in, test.base, x, test.out)
  63. }
  64. if test.in >= 0 {
  65. s := FormatUint(uint64(test.in), test.base)
  66. if s != test.out {
  67. t.Errorf("FormatUint(%v, %v) = %v want %v",
  68. test.in, test.base, s, test.out)
  69. }
  70. x := AppendUint(nil, uint64(test.in), test.base)
  71. if string(x) != test.out {
  72. t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
  73. "abc", uint64(test.in), test.base, x, test.out)
  74. }
  75. }
  76. if test.base == 10 && int64(int(test.in)) == test.in {
  77. s := Itoa(int(test.in))
  78. if s != test.out {
  79. t.Errorf("Itoa(%v) = %v want %v",
  80. test.in, s, test.out)
  81. }
  82. }
  83. }
  84. }
  85. type uitob64Test struct {
  86. in uint64
  87. base int
  88. out string
  89. }
  90. var uitob64tests = []uitob64Test{
  91. {1<<63 - 1, 10, "9223372036854775807"},
  92. {1 << 63, 10, "9223372036854775808"},
  93. {1<<63 + 1, 10, "9223372036854775809"},
  94. {1<<64 - 2, 10, "18446744073709551614"},
  95. {1<<64 - 1, 10, "18446744073709551615"},
  96. {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
  97. }
  98. func TestUitoa(t *testing.T) {
  99. for _, test := range uitob64tests {
  100. s := FormatUint(test.in, test.base)
  101. if s != test.out {
  102. t.Errorf("FormatUint(%v, %v) = %v want %v",
  103. test.in, test.base, s, test.out)
  104. }
  105. x := AppendUint([]byte("abc"), test.in, test.base)
  106. if string(x) != "abc"+test.out {
  107. t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
  108. "abc", test.in, test.base, x, test.out)
  109. }
  110. }
  111. }
  112. func BenchmarkFormatInt(b *testing.B) {
  113. for i := 0; i < b.N; i++ {
  114. for _, test := range itob64tests {
  115. FormatInt(test.in, test.base)
  116. }
  117. }
  118. }
  119. func BenchmarkAppendInt(b *testing.B) {
  120. dst := make([]byte, 0, 30)
  121. for i := 0; i < b.N; i++ {
  122. for _, test := range itob64tests {
  123. AppendInt(dst, test.in, test.base)
  124. }
  125. }
  126. }
  127. func BenchmarkFormatUint(b *testing.B) {
  128. for i := 0; i < b.N; i++ {
  129. for _, test := range uitob64tests {
  130. FormatUint(test.in, test.base)
  131. }
  132. }
  133. }
  134. func BenchmarkAppendUint(b *testing.B) {
  135. dst := make([]byte, 0, 30)
  136. for i := 0; i < b.N; i++ {
  137. for _, test := range uitob64tests {
  138. AppendUint(dst, test.in, test.base)
  139. }
  140. }
  141. }