From 1124f8d9941d033faf05f27a536986e9f3a16b30 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 10 Dec 2019 21:33:27 +0100 Subject: [PATCH] Add test cases License: MIT Signed-off-by: Jakub Sztandera --- cli/utils.go | 2 +- cli/utils_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cli/utils_test.go diff --git a/cli/utils.go b/cli/utils.go index a2a3a2ca0..09121f264 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -20,5 +20,5 @@ func SizeStr(size types.BigInt) string { } f, _ := r.Float64() - return fmt.Sprintf("%.3f %s", f, Units[i]) + return fmt.Sprintf("%.3g %s", f, Units[i]) } diff --git a/cli/utils_test.go b/cli/utils_test.go new file mode 100644 index 000000000..473e31923 --- /dev/null +++ b/cli/utils_test.go @@ -0,0 +1,27 @@ +package cli + +import ( + "testing" + + types "github.com/filecoin-project/lotus/chain/types" + "github.com/stretchr/testify/assert" +) + +func TestSizeStr(t *testing.T) { + cases := []struct { + in uint64 + out string + }{ + {0, "0 B"}, + {1, "1 B"}, + {1024, "1 KiB"}, + {2000, "1.95 KiB"}, + {5 << 20, "5 MiB"}, + {11 << 60, "11 EiB"}, + } + + for _, c := range cases { + assert.Equal(t, c.out, SizeStr(types.NewInt(c.in)), "input %+v, produced wrong result", c) + } + +}