~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/dustin/go-humanize/comma_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package humanize
 
2
 
 
3
import (
 
4
        "math/big"
 
5
        "testing"
 
6
)
 
7
 
 
8
func TestCommas(t *testing.T) {
 
9
        testList{
 
10
                {"0", Comma(0), "0"},
 
11
                {"10", Comma(10), "10"},
 
12
                {"100", Comma(100), "100"},
 
13
                {"1,000", Comma(1000), "1,000"},
 
14
                {"10,000", Comma(10000), "10,000"},
 
15
                {"100,000", Comma(100000), "100,000"},
 
16
                {"10,000,000", Comma(10000000), "10,000,000"},
 
17
                {"10,100,000", Comma(10100000), "10,100,000"},
 
18
                {"10,010,000", Comma(10010000), "10,010,000"},
 
19
                {"10,001,000", Comma(10001000), "10,001,000"},
 
20
                {"123,456,789", Comma(123456789), "123,456,789"},
 
21
                {"maxint", Comma(9.223372e+18), "9,223,372,000,000,000,000"},
 
22
                {"minint", Comma(-9.223372e+18), "-9,223,372,000,000,000,000"},
 
23
                {"-123,456,789", Comma(-123456789), "-123,456,789"},
 
24
                {"-10,100,000", Comma(-10100000), "-10,100,000"},
 
25
                {"-10,010,000", Comma(-10010000), "-10,010,000"},
 
26
                {"-10,001,000", Comma(-10001000), "-10,001,000"},
 
27
                {"-10,000,000", Comma(-10000000), "-10,000,000"},
 
28
                {"-100,000", Comma(-100000), "-100,000"},
 
29
                {"-10,000", Comma(-10000), "-10,000"},
 
30
                {"-1,000", Comma(-1000), "-1,000"},
 
31
                {"-100", Comma(-100), "-100"},
 
32
                {"-10", Comma(-10), "-10"},
 
33
        }.validate(t)
 
34
}
 
35
 
 
36
func BenchmarkCommas(b *testing.B) {
 
37
        for i := 0; i < b.N; i++ {
 
38
                Comma(1234567890)
 
39
        }
 
40
}
 
41
 
 
42
func BenchmarkBigCommas(b *testing.B) {
 
43
        for i := 0; i < b.N; i++ {
 
44
                BigComma(big.NewInt(1234567890))
 
45
        }
 
46
}
 
47
 
 
48
func bigComma(i int64) string {
 
49
        return BigComma(big.NewInt(i))
 
50
}
 
51
 
 
52
func TestBigCommas(t *testing.T) {
 
53
        testList{
 
54
                {"0", bigComma(0), "0"},
 
55
                {"10", bigComma(10), "10"},
 
56
                {"100", bigComma(100), "100"},
 
57
                {"1,000", bigComma(1000), "1,000"},
 
58
                {"10,000", bigComma(10000), "10,000"},
 
59
                {"100,000", bigComma(100000), "100,000"},
 
60
                {"10,000,000", bigComma(10000000), "10,000,000"},
 
61
                {"10,100,000", bigComma(10100000), "10,100,000"},
 
62
                {"10,010,000", bigComma(10010000), "10,010,000"},
 
63
                {"10,001,000", bigComma(10001000), "10,001,000"},
 
64
                {"123,456,789", bigComma(123456789), "123,456,789"},
 
65
                {"maxint", bigComma(9.223372e+18), "9,223,372,000,000,000,000"},
 
66
                {"minint", bigComma(-9.223372e+18), "-9,223,372,000,000,000,000"},
 
67
                {"-123,456,789", bigComma(-123456789), "-123,456,789"},
 
68
                {"-10,100,000", bigComma(-10100000), "-10,100,000"},
 
69
                {"-10,010,000", bigComma(-10010000), "-10,010,000"},
 
70
                {"-10,001,000", bigComma(-10001000), "-10,001,000"},
 
71
                {"-10,000,000", bigComma(-10000000), "-10,000,000"},
 
72
                {"-100,000", bigComma(-100000), "-100,000"},
 
73
                {"-10,000", bigComma(-10000), "-10,000"},
 
74
                {"-1,000", bigComma(-1000), "-1,000"},
 
75
                {"-100", bigComma(-100), "-100"},
 
76
                {"-10", bigComma(-10), "-10"},
 
77
        }.validate(t)
 
78
}
 
79
 
 
80
func TestVeryBigCommas(t *testing.T) {
 
81
        tests := []struct{ in, exp string }{
 
82
                {
 
83
                        "84889279597249724975972597249849757294578485",
 
84
                        "84,889,279,597,249,724,975,972,597,249,849,757,294,578,485",
 
85
                },
 
86
                {
 
87
                        "-84889279597249724975972597249849757294578485",
 
88
                        "-84,889,279,597,249,724,975,972,597,249,849,757,294,578,485",
 
89
                },
 
90
        }
 
91
        for _, test := range tests {
 
92
                n, _ := (&big.Int{}).SetString(test.in, 10)
 
93
                got := BigComma(n)
 
94
                if test.exp != got {
 
95
                        t.Errorf("Expected %q, got %q", test.exp, got)
 
96
                }
 
97
        }
 
98
}