forked from sneak/util
add HumanBytes for printing a byte count readably
HumanBytes turns a byte count into a short string using powers of 1024, with whole bytes below the first threshold and one decimal place above it. Comes with a doc comment and table-driven tests. (closes #11) Model: opus-5
This commit is contained in:
33
humanbytes_test.go
Normal file
33
humanbytes_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHumanBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input uint64
|
||||
expected string
|
||||
}{
|
||||
{"zero", 0, "0 B"},
|
||||
{"one byte", 1, "1 B"},
|
||||
{"just below a kibibyte", 1023, "1023 B"},
|
||||
{"exactly a kibibyte", 1024, "1.0 KiB"},
|
||||
{"half a kibibyte more", 1536, "1.5 KiB"},
|
||||
{"rounds up into the next unit", 1048575, "1.0 MiB"},
|
||||
{"exactly a mebibyte", 1 << 20, "1.0 MiB"},
|
||||
{"a gibibyte", 1 << 30, "1.0 GiB"},
|
||||
{"a tebibyte", 1 << 40, "1.0 TiB"},
|
||||
{"a pebibyte", 1 << 50, "1.0 PiB"},
|
||||
{"an exbibyte", 1 << 60, "1.0 EiB"},
|
||||
{"the largest count there is", ^uint64(0), "16.0 EiB"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := HumanBytes(test.input)
|
||||
if got != test.expected {
|
||||
t.Errorf("expected %q got %q", test.expected, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user