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