camper/pkg/template/humanize_test.go

60 lines
1005 B
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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