Files
got/progress/format_test.go
Hojun-Cho 10672f3816 all: cut bloat found in the pike audit
Dead, vestigial, and over-built code with no behavior change (except the
bool-vocabulary trim, a deliberate behavior tightening):

- httpdl: drop the vestigial sort+copy in validSegs (work-stealing was
  removed in 270812d, so segments always tile [0,total) ascending now);
  validate order in place, which also rejects a scrambled sidecar. Inline
  the endOff() accessor to s.end.
- cli: shrink the boolean vocabulary to true/false, which is all that's
  accepted. The invented yes/no/1/0/on/off spellings are gone, so e.g.
  --enable-dht=yes (or yes in got.conf) is now a usage error.
- download: remove the dead, never-called Stat.Done() method.
- progress: drop speed(), a pure alias of humanSize; call humanSize directly.
- main: replace the jobsHolder mutex box with atomic.Pointer[[]job] for the
  forced-exit session save; the capability stays, the lock and type go.
- bt: collapse isAddrInUse's X||X (missinggo.IsAddrInUse is that exact
  string match); drop the now-unused missinggo import (tidy -> indirect).
- option: note that prealloc and falloc are identical here.
2026-06-21 15:45:56 +09:00

64 lines
1.5 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 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)
}
}