Files
util/humanbytes_test.go
clawbot 500768ad32 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
2026-09-05 03:27:53 +00:00

34 lines
847 B
Go

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)
}
})
}
}