package node import ( "fmt" "testing" ) // 测试 BytesToHumanReadable 函数 func TestBytesToHumanReadable(t *testing.T) { tests := []struct { num float64 suffix string want string }{ {1024, "B", "1.0 KiB"}, {1024 * 1024, "B", "1.0 MiB"}, {1024 * 1024 * 1024, "B", "1.0 GiB"}, {1024 * 1024 * 1024 * 1024, "B", "1.0 TiB"}, {1024 * 1024 * 1024 * 1024 * 1024, "B", "1.0 PiB"}, {1024 * 1024 * 1024 * 1024 * 1024 * 1024, "B", "1.0 EiB"}, {1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, "B", "1.0 ZiB"}, {1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, "B", "1.0 YiB"}, {512, "B", "512.0 B"}, } for _, tt := range tests { t.Run(fmt.Sprintf("%f_%s", tt.num, tt.suffix), func(t *testing.T) { got := BytesToHumanReadable(tt.num, tt.suffix) if got != tt.want { t.Errorf("BytesToHumanReadable(%f, %s) = %s; want %s", tt.num, tt.suffix, got, tt.want) } }) } } // 测试 FormatSize 函数 func TestFormatSize(t *testing.T) { tests := []struct { size int64 want string }{ {1024, " 1.0 KiB"}, {1024 * 1024, " 1.0 MiB"}, {1024 * 1024 * 1024, " 1.0 GiB"}, {1024 * 1024 * 1024 * 1024, " 1.0 TiB"}, {512, " 512.0 B"}, } for _, tt := range tests { t.Run(fmt.Sprintf("%d", tt.size), func(t *testing.T) { got := FormatSize(tt.size) if got != tt.want { t.Errorf("FormatSize(%d) = %s; want %s", tt.size, got, tt.want) } }) } }