From 2cba9b3868724e40bc507e0d402b805cc1680650 Mon Sep 17 00:00:00 2001 From: Philipp Dorschner Date: Mon, 14 Mar 2022 11:53:12 +0100 Subject: [PATCH 1/2] HumanReadable: respect zero value input --- convert/bytes_common.go | 7 +++++++ convert/bytes_iec_test.go | 1 + convert/bytes_si_test.go | 1 + 3 files changed, 9 insertions(+) diff --git a/convert/bytes_common.go b/convert/bytes_common.go index 28ea9ac..4cf3400 100644 --- a/convert/bytes_common.go +++ b/convert/bytes_common.go @@ -68,13 +68,20 @@ func ParseBytes(value string) (ByteAny, error) { // Meant as a universal function to be used by the implementations, with base and a list of unit names. // // A special behavior is that resulting values smaller than 2 are displayed with the lower exponent. +// If the input value is 0, humanReadable will always return "0MB" // // Examples: // 1073741824B -> 1000KB // 2147483648B -> 2MB +// 0 -> 0MB // func humanReadable(b uint64, units []string, base float64) (float64, string) { + if b == 0 { + return 0, "MB" + } + exponent := math.Log(float64(b)) / math.Log(base) + // Round to the unit scaled exponent unitExponent := math.Floor(exponent) diff --git a/convert/bytes_iec_test.go b/convert/bytes_iec_test.go index e83c3c3..b0fc83e 100644 --- a/convert/bytes_iec_test.go +++ b/convert/bytes_iec_test.go @@ -6,6 +6,7 @@ import ( ) func TestBytesIEC_HumanReadable(t *testing.T) { + assert.Equal(t, "0MB", BytesIEC(0).HumanReadable()) assert.Equal(t, "999B", BytesIEC(999).HumanReadable()) assert.Equal(t, "999KiB", BytesIEC(999*1024).HumanReadable()) assert.Equal(t, "999MiB", BytesIEC(999*1024*1024).HumanReadable()) diff --git a/convert/bytes_si_test.go b/convert/bytes_si_test.go index eb9c488..ec5bc23 100644 --- a/convert/bytes_si_test.go +++ b/convert/bytes_si_test.go @@ -6,6 +6,7 @@ import ( ) func TestBytesSI_HumanReadable(t *testing.T) { + assert.Equal(t, "0MB", BytesSI(0).HumanReadable()) assert.Equal(t, "999B", BytesSI(999).HumanReadable()) assert.Equal(t, "999KB", BytesSI(999*1000).HumanReadable()) assert.Equal(t, "999MB", BytesSI(999*1000*1000).HumanReadable()) From 502c7e030be0ad4ad033d06778919a45cc086acf Mon Sep 17 00:00:00 2001 From: Philipp Dorschner Date: Mon, 14 Mar 2022 13:48:16 +0100 Subject: [PATCH 2/2] HumanReadable: change default value to B --- convert/bytes_common.go | 4 ++-- convert/bytes_iec_test.go | 2 +- convert/bytes_si_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/convert/bytes_common.go b/convert/bytes_common.go index 4cf3400..0ade0c5 100644 --- a/convert/bytes_common.go +++ b/convert/bytes_common.go @@ -68,7 +68,7 @@ func ParseBytes(value string) (ByteAny, error) { // Meant as a universal function to be used by the implementations, with base and a list of unit names. // // A special behavior is that resulting values smaller than 2 are displayed with the lower exponent. -// If the input value is 0, humanReadable will always return "0MB" +// If the input value is 0, humanReadable will always return "0B" // // Examples: // 1073741824B -> 1000KB @@ -77,7 +77,7 @@ func ParseBytes(value string) (ByteAny, error) { // func humanReadable(b uint64, units []string, base float64) (float64, string) { if b == 0 { - return 0, "MB" + return 0, "B" } exponent := math.Log(float64(b)) / math.Log(base) diff --git a/convert/bytes_iec_test.go b/convert/bytes_iec_test.go index b0fc83e..2beb17b 100644 --- a/convert/bytes_iec_test.go +++ b/convert/bytes_iec_test.go @@ -6,7 +6,7 @@ import ( ) func TestBytesIEC_HumanReadable(t *testing.T) { - assert.Equal(t, "0MB", BytesIEC(0).HumanReadable()) + assert.Equal(t, "0B", BytesIEC(0).HumanReadable()) assert.Equal(t, "999B", BytesIEC(999).HumanReadable()) assert.Equal(t, "999KiB", BytesIEC(999*1024).HumanReadable()) assert.Equal(t, "999MiB", BytesIEC(999*1024*1024).HumanReadable()) diff --git a/convert/bytes_si_test.go b/convert/bytes_si_test.go index ec5bc23..98d8b6e 100644 --- a/convert/bytes_si_test.go +++ b/convert/bytes_si_test.go @@ -6,7 +6,7 @@ import ( ) func TestBytesSI_HumanReadable(t *testing.T) { - assert.Equal(t, "0MB", BytesSI(0).HumanReadable()) + assert.Equal(t, "0B", BytesSI(0).HumanReadable()) assert.Equal(t, "999B", BytesSI(999).HumanReadable()) assert.Equal(t, "999KB", BytesSI(999*1000).HumanReadable()) assert.Equal(t, "999MB", BytesSI(999*1000*1000).HumanReadable())