Skip to content

Commit

Permalink
Additional edge case for Issue 50
Browse files Browse the repository at this point in the history
I fixed another issue that was possible before but missed this case
which is more common. Haaving an asset with no disks is now handled
properly without blowing everything up.
  • Loading branch information
michaeljs1990 committed Apr 6, 2020
1 parent ded875c commit 3426411
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion collins/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ func BytesToHumanSize(size float64) string {
getSize := Round(math.Pow(1024, base-math.Floor(base)), .5, 2)

var getSuffix string
if int(math.Floor(base)) > len(suffix) {
if size == 0 {
// We use math.Log of size to get the base. In the case that size is 0
// math.Log is undefined and returns -Infinity. This handles that edge
// case by manually setting the suffix to the first one.
getSuffix = suffix[0]
} else if int(math.Floor(base)) > len(suffix) {
// Wow you have more than a YB of storage/memory good for you
// your asset likely is messed up :P
getSuffix = "Unknown"
} else {
getSuffix = suffix[int(math.Floor(base))]
}

if size == 0 {
return "0 " + string(getSuffix)
}

return strconv.FormatFloat(getSize, 'f', -1, 64) + " " + string(getSuffix)
}

Expand Down
7 changes: 6 additions & 1 deletion collins/commands/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func TestUniqueOrderedSet(t *testing.T) {
}

func TestBytesToHumanSize(t *testing.T) {
out := BytesToHumanSize(12)
out := BytesToHumanSize(0)
if out != "0 B" {
t.Error("Bytes to human was suppose to be 0 B but was " + out)
}

out = BytesToHumanSize(12)
if out != "12 B" {
t.Error("Bytes to human was suppose to be 12 B but was " + out)
}
Expand Down

0 comments on commit 3426411

Please sign in to comment.