You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.1 KiB

  1. package humanize
  2. import (
  3. "math"
  4. "math/big"
  5. "testing"
  6. )
  7. func TestCommas(t *testing.T) {
  8. testList{
  9. {"0", Comma(0), "0"},
  10. {"10", Comma(10), "10"},
  11. {"100", Comma(100), "100"},
  12. {"1,000", Comma(1000), "1,000"},
  13. {"10,000", Comma(10000), "10,000"},
  14. {"100,000", Comma(100000), "100,000"},
  15. {"10,000,000", Comma(10000000), "10,000,000"},
  16. {"10,100,000", Comma(10100000), "10,100,000"},
  17. {"10,010,000", Comma(10010000), "10,010,000"},
  18. {"10,001,000", Comma(10001000), "10,001,000"},
  19. {"123,456,789", Comma(123456789), "123,456,789"},
  20. {"maxint", Comma(9.223372e+18), "9,223,372,000,000,000,000"},
  21. {"math.maxint", Comma(math.MaxInt64), "9,223,372,036,854,775,807"},
  22. {"math.minint", Comma(math.MinInt64), "-9,223,372,036,854,775,808"},
  23. {"minint", Comma(-9.223372e+18), "-9,223,372,000,000,000,000"},
  24. {"-123,456,789", Comma(-123456789), "-123,456,789"},
  25. {"-10,100,000", Comma(-10100000), "-10,100,000"},
  26. {"-10,010,000", Comma(-10010000), "-10,010,000"},
  27. {"-10,001,000", Comma(-10001000), "-10,001,000"},
  28. {"-10,000,000", Comma(-10000000), "-10,000,000"},
  29. {"-100,000", Comma(-100000), "-100,000"},
  30. {"-10,000", Comma(-10000), "-10,000"},
  31. {"-1,000", Comma(-1000), "-1,000"},
  32. {"-100", Comma(-100), "-100"},
  33. {"-10", Comma(-10), "-10"},
  34. }.validate(t)
  35. }
  36. func TestCommafs(t *testing.T) {
  37. testList{
  38. {"0", Commaf(0), "0"},
  39. {"10.11", Commaf(10.11), "10.11"},
  40. {"100", Commaf(100), "100"},
  41. {"1,000", Commaf(1000), "1,000"},
  42. {"10,000", Commaf(10000), "10,000"},
  43. {"100,000", Commaf(100000), "100,000"},
  44. {"834,142.32", Commaf(834142.32), "834,142.32"},
  45. {"10,000,000", Commaf(10000000), "10,000,000"},
  46. {"10,100,000", Commaf(10100000), "10,100,000"},
  47. {"10,010,000", Commaf(10010000), "10,010,000"},
  48. {"10,001,000", Commaf(10001000), "10,001,000"},
  49. {"123,456,789", Commaf(123456789), "123,456,789"},
  50. {"maxf64", Commaf(math.MaxFloat64), "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000"},
  51. {"minf64", Commaf(math.SmallestNonzeroFloat64), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005"},
  52. {"-123,456,789", Commaf(-123456789), "-123,456,789"},
  53. {"-10,100,000", Commaf(-10100000), "-10,100,000"},
  54. {"-10,010,000", Commaf(-10010000), "-10,010,000"},
  55. {"-10,001,000", Commaf(-10001000), "-10,001,000"},
  56. {"-10,000,000", Commaf(-10000000), "-10,000,000"},
  57. {"-100,000", Commaf(-100000), "-100,000"},
  58. {"-10,000", Commaf(-10000), "-10,000"},
  59. {"-1,000", Commaf(-1000), "-1,000"},
  60. {"-100.11", Commaf(-100.11), "-100.11"},
  61. {"-10", Commaf(-10), "-10"},
  62. }.validate(t)
  63. }
  64. func BenchmarkCommas(b *testing.B) {
  65. for i := 0; i < b.N; i++ {
  66. Comma(1234567890)
  67. }
  68. }
  69. func BenchmarkCommaf(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. Commaf(1234567890.83584)
  72. }
  73. }
  74. func BenchmarkBigCommas(b *testing.B) {
  75. for i := 0; i < b.N; i++ {
  76. BigComma(big.NewInt(1234567890))
  77. }
  78. }
  79. func bigComma(i int64) string {
  80. return BigComma(big.NewInt(i))
  81. }
  82. func TestBigCommas(t *testing.T) {
  83. testList{
  84. {"0", bigComma(0), "0"},
  85. {"10", bigComma(10), "10"},
  86. {"100", bigComma(100), "100"},
  87. {"1,000", bigComma(1000), "1,000"},
  88. {"10,000", bigComma(10000), "10,000"},
  89. {"100,000", bigComma(100000), "100,000"},
  90. {"10,000,000", bigComma(10000000), "10,000,000"},
  91. {"10,100,000", bigComma(10100000), "10,100,000"},
  92. {"10,010,000", bigComma(10010000), "10,010,000"},
  93. {"10,001,000", bigComma(10001000), "10,001,000"},
  94. {"123,456,789", bigComma(123456789), "123,456,789"},
  95. {"maxint", bigComma(9.223372e+18), "9,223,372,000,000,000,000"},
  96. {"minint", bigComma(-9.223372e+18), "-9,223,372,000,000,000,000"},
  97. {"-123,456,789", bigComma(-123456789), "-123,456,789"},
  98. {"-10,100,000", bigComma(-10100000), "-10,100,000"},
  99. {"-10,010,000", bigComma(-10010000), "-10,010,000"},
  100. {"-10,001,000", bigComma(-10001000), "-10,001,000"},
  101. {"-10,000,000", bigComma(-10000000), "-10,000,000"},
  102. {"-100,000", bigComma(-100000), "-100,000"},
  103. {"-10,000", bigComma(-10000), "-10,000"},
  104. {"-1,000", bigComma(-1000), "-1,000"},
  105. {"-100", bigComma(-100), "-100"},
  106. {"-10", bigComma(-10), "-10"},
  107. }.validate(t)
  108. }
  109. func TestVeryBigCommas(t *testing.T) {
  110. tests := []struct{ in, exp string }{
  111. {
  112. "84889279597249724975972597249849757294578485",
  113. "84,889,279,597,249,724,975,972,597,249,849,757,294,578,485",
  114. },
  115. {
  116. "-84889279597249724975972597249849757294578485",
  117. "-84,889,279,597,249,724,975,972,597,249,849,757,294,578,485",
  118. },
  119. }
  120. for _, test := range tests {
  121. n, _ := (&big.Int{}).SetString(test.in, 10)
  122. got := BigComma(n)
  123. if test.exp != got {
  124. t.Errorf("Expected %q, got %q", test.exp, got)
  125. }
  126. }
  127. }