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