Commit de2cb5d8 authored by Brian Tiger Chow's avatar Brian Tiger Chow

refactor: extract thirdparty/unit

parent 121061b6
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)
}
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()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment