refactor: extract thirdparty/unit

This commit is contained in:
Brian Tiger Chow 2015-01-20 04:23:06 -08:00
parent 121061b645
commit de2cb5d8c7
2 changed files with 72 additions and 0 deletions

46
thirdparty/unit/unit.go vendored Normal file
View File

@ -0,0 +1,46 @@
package unit
import "fmt"
type Information int64
const (
_ Information = iota // ignore first value by assigning to blank identifier
KB = 1 << (10 * iota)
MB
GB
TB
PB
EB
)
func (i Information) String() string {
tmp := int64(i)
// default
var d int64 = tmp
symbol := "B"
switch {
case i > EB:
d = tmp / EB
symbol = "EB"
case i > PB:
d = tmp / PB
symbol = "PB"
case i > TB:
d = tmp / TB
symbol = "TB"
case i > GB:
d = tmp / GB
symbol = "GB"
case i > MB:
d = tmp / MB
symbol = "MB"
case i > KB:
d = tmp / KB
symbol = "KB"
}
return fmt.Sprintf("%d %s", d, symbol)
}

26
thirdparty/unit/unit_test.go vendored Normal file
View File

@ -0,0 +1,26 @@
package unit
import "testing"
// and the award for most meta goes to...
func TestByteSizeUnit(t *testing.T) {
if 1*KB != 1*1024 {
t.Fatal(1 * KB)
}
if 1*MB != 1*1024*1024 {
t.Fail()
}
if 1*GB != 1*1024*1024*1024 {
t.Fail()
}
if 1*TB != 1*1024*1024*1024*1024 {
t.Fail()
}
if 1*PB != 1*1024*1024*1024*1024*1024 {
t.Fail()
}
if 1*EB != 1*1024*1024*1024*1024*1024*1024 {
t.Fail()
}
}