Fix SizeStr index out of band

add test for it

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-01-06 15:27:19 +01:00
parent 8f1c2b7aa9
commit 2d22f9c3d3
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 10 additions and 1 deletions

View File

@ -131,7 +131,7 @@ func (bi BigInt) SizeStr() string {
den := big.NewRat(1, 1024)
var i int
for f, _ := r.Float64(); f >= 1024 && 1 < len(sizeUnits); f, _ = r.Float64() {
for f, _ := r.Float64(); f >= 1024 && i+1 < len(sizeUnits); f, _ = r.Float64() {
i++
r = r.Mul(r, den)
}

View File

@ -2,6 +2,7 @@ package types
import (
"bytes"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
@ -69,3 +70,11 @@ func TestSizeStr(t *testing.T) {
assert.Equal(t, c.out, NewInt(c.in).SizeStr(), "input %+v, produced wrong result", c)
}
}
func TestSizeStrBig(t *testing.T) {
ZiB := big.NewInt(50000)
ZiB = ZiB.Lsh(ZiB, 70)
assert.Equal(t, "5e+04 ZiB", BigInt{Int: ZiB}.SizeStr(), "inout %+v, produced wrong result", ZiB)
}