camper/pkg/template/humanize_test.go

60 lines
1005 B
Go
Raw Normal View History

/*
* SPDX-FileCopyrightText: 20122015 Dustin Sallings <dustin@spy.net>
* SPDX-License-Identifier: MIT
*/
package template
import "testing"
func testHumanizeBytes(t *testing.T, in int64, exp string) {
if actual := humanizeBytes(in); actual != exp {
t.Errorf("humanizeBytes(%d) got %v expected %v", in, actual, exp)
}
}
func TestBytes(t *testing.T) {
const (
IByte = 1 << (iota * 10)
KiByte
MiByte
GiByte
TiByte
PiByte
EiByte
)
tests := []struct {
in int64
exp string
}{
{0, "0 B"},
{1, "1 B"},
{803, "803 B"},
{1023, "1023 B"},
{1024, "1.0 KiB"},
{MiByte - IByte, "1024 KiB"},
{1024 * 1024, "1.0 MiB"},
{GiByte - KiByte, "1024 MiB"},
{GiByte, "1.0 GiB"},
{TiByte - MiByte, "1024 GiB"},
{TiByte, "1.0 TiB"},
{PiByte - TiByte, "1023 TiB"},
{PiByte, "1.0 PiB"},
{EiByte - PiByte, "1023 PiB"},
{EiByte, "1.0 EiB"},
{5.5 * GiByte, "5.5 GiB"},
}
for _, tt := range tests {
testHumanizeBytes(t, tt.in, tt.exp)
}
}