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