arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/utils/utils_test.go
utils/utils_test.goRaw
  1package utils
  2
  3import (
  4	"math/big"
  5	"testing"
  6
  7	"github.com/stretchr/testify/assert"
  8)
  9
 10func TestConversions(t *testing.T) {
 11
 12	testVector := map[Float16]string{
 13		0x307B: "123000000",
 14		0x1DC6: "454500",
 15		0xFFFF: "10235000000000000000000000000000000",
 16		0x0000: "0",
 17		0x0400: "0",
 18		0x0001: "1",
 19		0x0401: "1",
 20		0x0800: "0",
 21		0x0c00: "5",
 22		0x0801: "10",
 23		0x0c01: "15",
 24	}
 25
 26	for test := range testVector {
 27
 28		fix := test.BigInt()
 29
 30		assert.Equal(t, fix.String(), testVector[test])
 31
 32		bi := big.NewInt(0)
 33		bi.SetString(testVector[test], 10)
 34
 35		fl, err := NewFloat16(bi)
 36		assert.Equal(t, nil, err)
 37
 38		fx2 := fl.BigInt()
 39		assert.Equal(t, fx2.String(), testVector[test])
 40
 41	}
 42
 43}
 44
 45func TestFloorFix2Float(t *testing.T) {
 46
 47	testVector := map[string]Float16{
 48		"87999990000000000": 0x776f,
 49		"87950000000000001": 0x776f,
 50		"87950000000000000": 0x776f,
 51		"87949999999999999": 0x736f,
 52	}
 53
 54	for test := range testVector {
 55
 56		bi := big.NewInt(0)
 57		bi.SetString(test, 10)
 58
 59		testFloat := NewFloat16Floor(bi)
 60
 61		assert.Equal(t, testFloat, testVector[test])
 62
 63	}
 64
 65}
 66
 67func TestConversionLosses(t *testing.T) {
 68	a := big.NewInt(1000)
 69	b, err := NewFloat16(a)
 70	assert.Equal(t, nil, err)
 71	c := b.BigInt()
 72	assert.Equal(t, c, a)
 73
 74	a = big.NewInt(1024)
 75	b, err = NewFloat16(a)
 76	assert.Equal(t, ErrRoundingLoss, err)
 77	c = b.BigInt()
 78	assert.NotEqual(t, c, a)
 79
 80	a = big.NewInt(32767)
 81	b, err = NewFloat16(a)
 82	assert.Equal(t, ErrRoundingLoss, err)
 83	c = b.BigInt()
 84	assert.NotEqual(t, c, a)
 85
 86	a = big.NewInt(32768)
 87	b, err = NewFloat16(a)
 88	assert.Equal(t, ErrRoundingLoss, err)
 89	c = b.BigInt()
 90	assert.NotEqual(t, c, a)
 91
 92	a = big.NewInt(65536000)
 93	b, err = NewFloat16(a)
 94	assert.Equal(t, ErrRoundingLoss, err)
 95	c = b.BigInt()
 96	assert.NotEqual(t, c, a)
 97
 98}
 99
100func BenchmarkFloat16(b *testing.B) {
101	newBigInt := func(s string) *big.Int {
102		bigInt, ok := new(big.Int).SetString(s, 10)
103		if !ok {
104			panic("Bad big int")
105		}
106		return bigInt
107	}
108	type pair struct {
109		Float16 Float16
110		BigInt  *big.Int
111	}
112	testVector := []pair{
113		pair{0x307B, newBigInt("123000000")},
114		pair{0x1DC6, newBigInt("454500")},
115		pair{0xFFFF, newBigInt("10235000000000000000000000000000000")},
116		pair{0x0000, newBigInt("0")},
117		pair{0x0400, newBigInt("0")},
118		pair{0x0001, newBigInt("1")},
119		pair{0x0401, newBigInt("1")},
120		pair{0x0800, newBigInt("0")},
121		pair{0x0c00, newBigInt("5")},
122		pair{0x0801, newBigInt("10")},
123		pair{0x0c01, newBigInt("15")},
124	}
125	b.Run("floorFix2Float()", func(b *testing.B) {
126		for i := 0; i < b.N; i++ {
127			NewFloat16Floor(testVector[i%len(testVector)].BigInt)
128		}
129	})
130	b.Run("NewFloat16()", func(b *testing.B) {
131		for i := 0; i < b.N; i++ {
132			NewFloat16(testVector[i%len(testVector)].BigInt) //nolint:errcheck
133		}
134	})
135	b.Run("Float16.BigInt()", func(b *testing.B) {
136		for i := 0; i < b.N; i++ {
137			testVector[i%len(testVector)].Float16.BigInt()
138		}
139	})
140}