mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
fix: fix util test FormatBigMetric
This commit is contained in:
parent
dd84d22b56
commit
8bd4400ba8
2 changed files with 105 additions and 2 deletions
|
|
@ -417,11 +417,16 @@ func marshalIntoFormat(data interface{}, format string) ([]byte, error) {
|
||||||
func FormatBigMetric(number int64, baseUnitName string) string {
|
func FormatBigMetric(number int64, baseUnitName string) string {
|
||||||
memoryUnits := []string{"KB", "MB", "GB", "TB"}
|
memoryUnits := []string{"KB", "MB", "GB", "TB"}
|
||||||
cpuUnits := []string{"µs", "ms", "s", "m", "h"}
|
cpuUnits := []string{"µs", "ms", "s", "m", "h"}
|
||||||
const unit = 1000
|
var unit int64
|
||||||
|
if baseUnitName == "bytes" {
|
||||||
|
unit = 1024
|
||||||
|
} else {
|
||||||
|
unit = 1000
|
||||||
|
}
|
||||||
if number < unit {
|
if number < unit {
|
||||||
return fmt.Sprintf("%d", number)
|
return fmt.Sprintf("%d", number)
|
||||||
}
|
}
|
||||||
div, exp := int64(unit), 0
|
div, exp := int64(unit), 1
|
||||||
for n := number / unit; n >= unit; n /= unit {
|
for n := number / unit; n >= unit; n /= unit {
|
||||||
div *= unit
|
div *= unit
|
||||||
exp++
|
exp++
|
||||||
|
|
|
||||||
|
|
@ -486,3 +486,101 @@ func TestSetObjectFieldByPath(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFormatBigMetric(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
number int64
|
||||||
|
baseUnitName string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
// Bytes tests
|
||||||
|
{
|
||||||
|
name: "bytes: small value",
|
||||||
|
number: 500,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "500",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bytes: KB",
|
||||||
|
number: 1500,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1.465 KB",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bytes: MB",
|
||||||
|
number: 1500000,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1.431 MB",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bytes: GB",
|
||||||
|
number: 1500000000,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1.397 GB",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bytes: TB",
|
||||||
|
number: 1500000000000,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1.364 TB",
|
||||||
|
},
|
||||||
|
// Nanoseconds tests
|
||||||
|
{
|
||||||
|
name: "nanoseconds: small value",
|
||||||
|
number: 500,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "500",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nanoseconds: µs",
|
||||||
|
number: 1500,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "1.500 µs",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nanoseconds: ms",
|
||||||
|
number: 1500000,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "1.500 ms",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nanoseconds: s",
|
||||||
|
number: 1500000000,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "1.500 s",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nanoseconds: m",
|
||||||
|
number: 1500000000000,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "1.500 m",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nanoseconds: h",
|
||||||
|
number: 1500000000000000,
|
||||||
|
baseUnitName: "nanoseconds",
|
||||||
|
expected: "1.500 h",
|
||||||
|
},
|
||||||
|
// Exact boundaries
|
||||||
|
{
|
||||||
|
name: "bytes: exact 1024",
|
||||||
|
number: 1024,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1.000 KB",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bytes: 1023",
|
||||||
|
number: 1023,
|
||||||
|
baseUnitName: "bytes",
|
||||||
|
expected: "1023",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := FormatBigMetric(tt.number, tt.baseUnitName)
|
||||||
|
assert.EqualValues(t, tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue