74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package progress
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHumanSize(t *testing.T) {
|
|
tests := []struct {
|
|
n int64
|
|
human bool
|
|
want string
|
|
}{
|
|
{500, true, "500B"},
|
|
{1536, true, "1.5KiB"},
|
|
{1 << 20, true, "1.0MiB"},
|
|
{5 * 1 << 20, true, "5.0MiB"},
|
|
{20 * 1 << 20, true, "20MiB"},
|
|
{1 << 30, true, "1.0GiB"},
|
|
{2048, false, "2048B"},
|
|
// abbrevSize bumps to the next unit once the quotient reaches 922, so
|
|
// 922*1024 bytes reads as 0.9MiB rather than 922KiB.
|
|
{922 * 1024, true, "0.9MiB"},
|
|
{921 * 1024, true, "921KiB"},
|
|
// The unit ladder is capped at Gi, so a terabyte overflows GiB.
|
|
{1 << 40, true, "1024.0GiB"},
|
|
}
|
|
for _, tc := range tests {
|
|
if got := humanSize(tc.n, tc.human); got != tc.want {
|
|
t.Errorf("humanSize(%d, %v) = %q, want %q", tc.n, tc.human, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSecfmt(t *testing.T) {
|
|
tests := []struct {
|
|
d time.Duration
|
|
want string
|
|
}{
|
|
{0, "0s"},
|
|
{30 * time.Second, "30s"},
|
|
{90 * time.Second, "1m30s"},
|
|
{120 * time.Second, "2m"},
|
|
{3600 * time.Second, "1h"},
|
|
{3661 * time.Second, "1h1m1s"},
|
|
{3720 * time.Second, "1h2m"},
|
|
{-1 * time.Second, "--"},
|
|
}
|
|
for _, tc := range tests {
|
|
if got := secfmt(tc.d); got != tc.want {
|
|
t.Errorf("secfmt(%v) = %q, want %q", tc.d, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSpeedNoSuffix(t *testing.T) {
|
|
// The DL:/UL: fields show the bare abbreviated size with no "/s".
|
|
if got := speed(1536, true); got != "1.5KiB" {
|
|
t.Errorf("speed(1536,true) = %q, want %q", got, "1.5KiB")
|
|
}
|
|
if got := speed(2048, false); got != "2048B" {
|
|
t.Errorf("speed(2048,false) = %q, want %q", got, "2048B")
|
|
}
|
|
}
|
|
|
|
func TestPercent(t *testing.T) {
|
|
if got := percent(50, 100); got != 50 {
|
|
t.Errorf("percent(50,100) = %d, want 50", got)
|
|
}
|
|
if got := percent(1, 0); got != 0 {
|
|
t.Errorf("percent(1,0) = %d, want 0", got)
|
|
}
|
|
}
|