@cryptotaxi247 / kubo / commits / de2cb5d8c

refactor: extract thirdparty/unit

Brian Tiger Chow committed Jan 20, 2015 at 04:23 UTC de2cb5d8c75bc0dcec29f086d40ddc8a662cc950
2 files changed +72
thirdparty/unit/unit.go new
+46
@@ -0,0 +1,46 @@
1 +package unit
2 +
3 +import "fmt"
4 +
5 +type Information int64
6 +
7 +const (
8 + _ Information = iota // ignore first value by assigning to blank identifier
9 + KB = 1 << (10 * iota)
10 + MB
11 + GB
12 + TB
13 + PB
14 + EB
15 +)
16 +
17 +func (i Information) String() string {
18 +
19 + tmp := int64(i)
20 +
21 + // default
22 + var d int64 = tmp
23 + symbol := "B"
24 +
25 + switch {
26 + case i > EB:
27 + d = tmp / EB
28 + symbol = "EB"
29 + case i > PB:
30 + d = tmp / PB
31 + symbol = "PB"
32 + case i > TB:
33 + d = tmp / TB
34 + symbol = "TB"
35 + case i > GB:
36 + d = tmp / GB
37 + symbol = "GB"
38 + case i > MB:
39 + d = tmp / MB
40 + symbol = "MB"
41 + case i > KB:
42 + d = tmp / KB
43 + symbol = "KB"
44 + }
45 + return fmt.Sprintf("%d %s", d, symbol)
46 +}
thirdparty/unit/unit_test.go new
+26
@@ -0,0 +1,26 @@
1 +package unit
2 +
3 +import "testing"
4 +
5 +// and the award for most meta goes to...
6 +
7 +func TestByteSizeUnit(t *testing.T) {
8 + if 1*KB != 1*1024 {
9 + t.Fatal(1 * KB)
10 + }
11 + if 1*MB != 1*1024*1024 {
12 + t.Fail()
13 + }
14 + if 1*GB != 1*1024*1024*1024 {
15 + t.Fail()
16 + }
17 + if 1*TB != 1*1024*1024*1024*1024 {
18 + t.Fail()
19 + }
20 + if 1*PB != 1*1024*1024*1024*1024*1024 {
21 + t.Fail()
22 + }
23 + if 1*EB != 1*1024*1024*1024*1024*1024*1024 {
24 + t.Fail()
25 + }
26 +}