bump from 3 to 4 digits in format-string

Fixes #1993
This commit is contained in:
laser 2020-06-19 08:51:06 -07:00
parent ffa7be86fe
commit 5e77b5da9f
2 changed files with 25 additions and 2 deletions

View File

@ -76,7 +76,7 @@ func SizeStr(bi BigInt) string {
}
f, _ := r.Float64()
return fmt.Sprintf("%.3g %s", f, byteSizeUnits[i])
return fmt.Sprintf("%.4g %s", f, byteSizeUnits[i])
}
var deciUnits = []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"}

View File

@ -3,7 +3,12 @@ package types
import (
"bytes"
"math/big"
"math/rand"
"strings"
"testing"
"time"
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
)
@ -60,8 +65,10 @@ func TestSizeStr(t *testing.T) {
}{
{0, "0 B"},
{1, "1 B"},
{1016, "1016 B"},
{1024, "1 KiB"},
{2000, "1.95 KiB"},
{1000 * 1024, "1000 KiB"},
{2000, "1.953 KiB"},
{5 << 20, "5 MiB"},
{11 << 60, "11 EiB"},
}
@ -71,6 +78,22 @@ func TestSizeStr(t *testing.T) {
}
}
func TestSizeStrUnitsSymmetry(t *testing.T) {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
for i := 0; i < 1000000; i++ {
n := r.Uint64()
l := strings.ReplaceAll(units.BytesSize(float64(n)), " ", "")
r := strings.ReplaceAll(SizeStr(NewInt(n)), " ", "")
assert.NotContains(t, l, "e+")
assert.NotContains(t, r, "e+")
assert.Equal(t, l, r, "wrong formatting for %d", n)
}
}
func TestSizeStrBig(t *testing.T) {
ZiB := big.NewInt(50000)
ZiB = ZiB.Lsh(ZiB, 70)