This commit is contained in:
loveuer
2026-01-28 10:28:13 +08:00
parent 507a67e455
commit 3ee0c9c098
29 changed files with 2852 additions and 0 deletions

78
tool/human/size_test.go Normal file
View File

@@ -0,0 +1,78 @@
package human
import (
"fmt"
"testing"
"time"
)
func TestSize(t *testing.T) {
tests := []struct {
input int64
expected string
}{
{0, "0 B"},
{1, "0 B"},
{1023, "0 B"},
{1024, "1 KB"},
{1536, "1.50 KB"},
{1024 * 1024, "1 MB"},
{1024 * 1024 * 1024, "1 GB"},
{1024 * 1024 * 1024 * 1024, "1 TB"},
{-1024, "-1 KB"},
}
for _, tt := range tests {
result := Size(tt.input)
if result != tt.expected {
t.Errorf("Size(%d) = %s, want %s", tt.input, result, tt.expected)
}
}
}
func TestDuration(t *testing.T) {
tests := []struct {
input time.Duration
expected string
}{
{0, "0s"},
{time.Second, "1s"},
{time.Minute, "1m"},
{time.Hour, "1h"},
{24 * time.Hour, "1d"},
{25 * time.Hour, "1d 1h"},
{90 * time.Minute, "1h 30m"},
{time.Hour + time.Minute + 34*time.Second + 230*time.Millisecond, "1h 1m 34.23s"},
{1356*24*time.Hour + 2*time.Hour + 55*time.Minute + 34*time.Second + 230*time.Millisecond, "1356d 2h 55m 34.23s"},
{-time.Hour, "-1h"},
}
for _, tt := range tests {
result := Duration(tt.input)
if result != tt.expected {
t.Errorf("Duration(%v) = %s, want %s", tt.input, result, tt.expected)
}
}
}
func ExampleSize() {
fmt.Println(Size(1024))
fmt.Println(Size(1024 * 1024))
fmt.Println(Size(1536))
// Output:
// 1 KB
// 1 MB
// 1.50 KB
}
func ExampleDuration() {
fmt.Println(Duration(time.Hour))
fmt.Println(Duration(25 * time.Hour))
fmt.Println(Duration(90 * time.Minute))
fmt.Println(Duration(1356*24*time.Hour + 2*time.Hour + 55*time.Minute + 34*time.Second + 230*time.Millisecond))
// Output:
// 1h
// 1d 1h
// 1h 30m
// 1356d 2h 55m 34.23s
}