~kyrofa/unity-scope-snappy/integration-tests_previews

« back to all changes in this revision

Viewing changes to internal/github.com/dustin/go-humanize/bigbytes_test.go

  • Committer: Tarmac
  • Author(s): Kyle Fazzari
  • Date: 2015-06-18 12:13:38 UTC
  • mfrom: (8.5.3 unity-scope-snappy)
  • Revision ID: tarmac-20150618121338-2xo6u8zz2q83zmr2
Add snap package size to previews.

Approved by Xavi Garcia, PS Jenkins bot.

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 TestBigByteParsing(t *testing.T) {
 
9
        tests := []struct {
 
10
                in  string
 
11
                exp uint64
 
12
        }{
 
13
                {"42", 42},
 
14
                {"42MB", 42000000},
 
15
                {"42MiB", 44040192},
 
16
                {"42mb", 42000000},
 
17
                {"42mib", 44040192},
 
18
                {"42MIB", 44040192},
 
19
                {"42 MB", 42000000},
 
20
                {"42 MiB", 44040192},
 
21
                {"42 mb", 42000000},
 
22
                {"42 mib", 44040192},
 
23
                {"42 MIB", 44040192},
 
24
                {"42.5MB", 42500000},
 
25
                {"42.5MiB", 44564480},
 
26
                {"42.5 MB", 42500000},
 
27
                {"42.5 MiB", 44564480},
 
28
                // No need to say B
 
29
                {"42M", 42000000},
 
30
                {"42Mi", 44040192},
 
31
                {"42m", 42000000},
 
32
                {"42mi", 44040192},
 
33
                {"42MI", 44040192},
 
34
                {"42 M", 42000000},
 
35
                {"42 Mi", 44040192},
 
36
                {"42 m", 42000000},
 
37
                {"42 mi", 44040192},
 
38
                {"42 MI", 44040192},
 
39
                {"42.5M", 42500000},
 
40
                {"42.5Mi", 44564480},
 
41
                {"42.5 M", 42500000},
 
42
                {"42.5 Mi", 44564480},
 
43
                // Large testing, breaks when too much larger than
 
44
                // this.
 
45
                {"12.5 EB", uint64(12.5 * float64(EByte))},
 
46
                {"12.5 E", uint64(12.5 * float64(EByte))},
 
47
                {"12.5 EiB", uint64(12.5 * float64(EiByte))},
 
48
        }
 
49
 
 
50
        for _, p := range tests {
 
51
                got, err := ParseBigBytes(p.in)
 
52
                if err != nil {
 
53
                        t.Errorf("Couldn't parse %v: %v", p.in, err)
 
54
                } else {
 
55
                        if got.Uint64() != p.exp {
 
56
                                t.Errorf("Expected %v for %v, got %v",
 
57
                                        p.exp, p.in, got)
 
58
                        }
 
59
                }
 
60
        }
 
61
}
 
62
 
 
63
func TestBigByteErrors(t *testing.T) {
 
64
        got, err := ParseBigBytes("84 JB")
 
65
        if err == nil {
 
66
                t.Errorf("Expected error, got %v", got)
 
67
        }
 
68
        got, err = ParseBigBytes("")
 
69
        if err == nil {
 
70
                t.Errorf("Expected error parsing nothing")
 
71
        }
 
72
}
 
73
 
 
74
func bbyte(in uint64) string {
 
75
        return BigBytes((&big.Int{}).SetUint64(in))
 
76
}
 
77
 
 
78
func bibyte(in uint64) string {
 
79
        return BigIBytes((&big.Int{}).SetUint64(in))
 
80
}
 
81
 
 
82
func TestBigBytes(t *testing.T) {
 
83
        testList{
 
84
                {"bytes(0)", bbyte(0), "0B"},
 
85
                {"bytes(1)", bbyte(1), "1B"},
 
86
                {"bytes(803)", bbyte(803), "803B"},
 
87
                {"bytes(999)", bbyte(999), "999B"},
 
88
 
 
89
                {"bytes(1024)", bbyte(1024), "1.0KB"},
 
90
                {"bytes(1MB - 1)", bbyte(MByte - Byte), "1000KB"},
 
91
 
 
92
                {"bytes(1MB)", bbyte(1024 * 1024), "1.0MB"},
 
93
                {"bytes(1GB - 1K)", bbyte(GByte - KByte), "1000MB"},
 
94
 
 
95
                {"bytes(1GB)", bbyte(GByte), "1.0GB"},
 
96
                {"bytes(1TB - 1M)", bbyte(TByte - MByte), "1000GB"},
 
97
 
 
98
                {"bytes(1TB)", bbyte(TByte), "1.0TB"},
 
99
                {"bytes(1PB - 1T)", bbyte(PByte - TByte), "999TB"},
 
100
 
 
101
                {"bytes(1PB)", bbyte(PByte), "1.0PB"},
 
102
                {"bytes(1PB - 1T)", bbyte(EByte - PByte), "999PB"},
 
103
 
 
104
                {"bytes(1EB)", bbyte(EByte), "1.0EB"},
 
105
                // Overflows.
 
106
                // {"bytes(1EB - 1P)", Bytes((KByte*EByte)-PByte), "1023EB"},
 
107
 
 
108
                {"bytes(0)", bibyte(0), "0B"},
 
109
                {"bytes(1)", bibyte(1), "1B"},
 
110
                {"bytes(803)", bibyte(803), "803B"},
 
111
                {"bytes(1023)", bibyte(1023), "1023B"},
 
112
 
 
113
                {"bytes(1024)", bibyte(1024), "1.0KiB"},
 
114
                {"bytes(1MB - 1)", bibyte(MiByte - IByte), "1024KiB"},
 
115
 
 
116
                {"bytes(1MB)", bibyte(1024 * 1024), "1.0MiB"},
 
117
                {"bytes(1GB - 1K)", bibyte(GiByte - KiByte), "1024MiB"},
 
118
 
 
119
                {"bytes(1GB)", bibyte(GiByte), "1.0GiB"},
 
120
                {"bytes(1TB - 1M)", bibyte(TiByte - MiByte), "1024GiB"},
 
121
 
 
122
                {"bytes(1TB)", bibyte(TiByte), "1.0TiB"},
 
123
                {"bytes(1PB - 1T)", bibyte(PiByte - TiByte), "1023TiB"},
 
124
 
 
125
                {"bytes(1PB)", bibyte(PiByte), "1.0PiB"},
 
126
                {"bytes(1PB - 1T)", bibyte(EiByte - PiByte), "1023PiB"},
 
127
 
 
128
                {"bytes(1EiB)", bibyte(EiByte), "1.0EiB"},
 
129
                // Overflows.
 
130
                // {"bytes(1EB - 1P)", bibyte((KIByte*EIByte)-PiByte), "1023EB"},
 
131
 
 
132
                {"bytes(5.5GiB)", bibyte(5.5 * GiByte), "5.5GiB"},
 
133
 
 
134
                {"bytes(5.5GB)", bbyte(5.5 * GByte), "5.5GB"},
 
135
        }.validate(t)
 
136
}
 
137
 
 
138
func TestVeryBigBytes(t *testing.T) {
 
139
        b, _ := (&big.Int{}).SetString("15347691069326346944512", 10)
 
140
        s := BigBytes(b)
 
141
        if s != "15ZB" {
 
142
                t.Errorf("Expected 15ZB, got %v", s)
 
143
        }
 
144
        s = BigIBytes(b)
 
145
        if s != "13ZiB" {
 
146
                t.Errorf("Expected 13ZiB, got %v", s)
 
147
        }
 
148
 
 
149
        b, _ = (&big.Int{}).SetString("15716035654990179271180288", 10)
 
150
        s = BigBytes(b)
 
151
        if s != "16YB" {
 
152
                t.Errorf("Expected 16YB, got %v", s)
 
153
        }
 
154
        s = BigIBytes(b)
 
155
        if s != "13YiB" {
 
156
                t.Errorf("Expected 13YiB, got %v", s)
 
157
        }
 
158
}
 
159
 
 
160
func TestVeryVeryBigBytes(t *testing.T) {
 
161
        b, _ := (&big.Int{}).SetString("16093220510709943573688614912", 10)
 
162
        s := BigBytes(b)
 
163
        if s != "16093YB" {
 
164
                t.Errorf("Expected 16093YB, got %v", s)
 
165
        }
 
166
        s = BigIBytes(b)
 
167
        if s != "13312YiB" {
 
168
                t.Errorf("Expected 13312YiB, got %v", s)
 
169
        }
 
170
}
 
171
 
 
172
func TestParseVeryBig(t *testing.T) {
 
173
        tests := []struct {
 
174
                in  string
 
175
                out string
 
176
        }{
 
177
                {"16ZB", "16000000000000000000000"},
 
178
                {"16ZiB", "18889465931478580854784"},
 
179
                {"16.5ZB", "16500000000000000000000"},
 
180
                {"16.5ZiB", "19479761741837286506496"},
 
181
                {"16Z", "16000000000000000000000"},
 
182
                {"16Zi", "18889465931478580854784"},
 
183
                {"16.5Z", "16500000000000000000000"},
 
184
                {"16.5Zi", "19479761741837286506496"},
 
185
 
 
186
                {"16YB", "16000000000000000000000000"},
 
187
                {"16YiB", "19342813113834066795298816"},
 
188
                {"16.5YB", "16500000000000000000000000"},
 
189
                {"16.5YiB", "19947276023641381382651904"},
 
190
                {"16Y", "16000000000000000000000000"},
 
191
                {"16Yi", "19342813113834066795298816"},
 
192
                {"16.5Y", "16500000000000000000000000"},
 
193
                {"16.5Yi", "19947276023641381382651904"},
 
194
        }
 
195
 
 
196
        for _, test := range tests {
 
197
                x, err := ParseBigBytes(test.in)
 
198
                if err != nil {
 
199
                        t.Errorf("Error parsing %q: %v", test.in, err)
 
200
                        continue
 
201
                }
 
202
 
 
203
                if x.String() != test.out {
 
204
                        t.Errorf("Expected %q for %q, got %v", test.out, test.in, x)
 
205
                }
 
206
        }
 
207
}
 
208
 
 
209
func BenchmarkParseBigBytes(b *testing.B) {
 
210
        for i := 0; i < b.N; i++ {
 
211
                ParseBigBytes("16.5Z")
 
212
        }
 
213
}
 
214
 
 
215
func BenchmarkBigBytes(b *testing.B) {
 
216
        for i := 0; i < b.N; i++ {
 
217
                bibyte(16.5 * GByte)
 
218
        }
 
219
}