hermez-node/common/float40_test.go
common/float40_test.goRaw
1package common
2
3import (
4 "fmt"
5 "math/big"
6 "testing"
7
8 "github.com/hermeznetwork/tracerr"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestConversionsFloat40(t *testing.T) {
14 testVector := map[Float40]string{
15 6*0x800000000 + 123: "123000000",
16 2*0x800000000 + 4545: "454500",
17 30*0x800000000 + 10235: "10235000000000000000000000000000000",
18 0x000000000: "0",
19 0x800000000: "0",
20 0x0001: "1",
21 0x0401: "1025",
22 0x800000000 + 1: "10",
23 0xFFFFFFFFFF: "343597383670000000000000000000000000000000",
24 }
25
26 for test := range testVector {
27 fix, err := test.BigInt()
28 require.NoError(t, err)
29 assert.Equal(t, fix.String(), testVector[test])
30
31 bi, ok := new(big.Int).SetString(testVector[test], 10)
32 require.True(t, ok)
33
34 fl, err := NewFloat40(bi)
35 assert.NoError(t, err)
36
37 fx2, err := fl.BigInt()
38 require.NoError(t, err)
39 assert.Equal(t, fx2.String(), testVector[test])
40 }
41}
42
43func TestExpectError(t *testing.T) {
44 testVector := map[string]error{
45 "9922334455000000000000000000000000000000": nil,
46 "9922334455000000000000000000000000000001": ErrFloat40NotEnoughPrecission,
47 "9922334454999999999999999999999999999999": ErrFloat40NotEnoughPrecission,
48 "42949672950000000000000000000000000000000": nil,
49 "99223344556573838487575": ErrFloat40NotEnoughPrecission,
50 "992233445500000000000000000000000000000000": ErrFloat40E31,
51 "343597383670000000000000000000000000000000": nil,
52 "343597383680000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
53 "343597383690000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
54 "343597383700000000000000000000000000000000": ErrFloat40E31,
55 }
56 for test := range testVector {
57 bi, ok := new(big.Int).SetString(test, 10)
58 require.True(t, ok)
59 _, err := NewFloat40(bi)
60 assert.Equal(t, testVector[test], tracerr.Unwrap(err))
61 }
62}
63
64func TestNewFloat40Floor(t *testing.T) {
65 testVector := map[string][]string{
66 // []int contains [Float40 value, Flot40 Floor value], when
67 // Float40 value is expected to be 0, is because is expected to
68 // be an error
69 "9922334455000000000000000000000000000000": {
70 "1040714485495", "1040714485495", "9922334455000000000000000000000000000000"},
71 "9922334455000000000000000000000000000001": { // Floor [2] will be same as prev line
72 "0", "1040714485495", "9922334455000000000000000000000000000000"},
73 "9922334454999999999999999999999999999999": {
74 "0", "1040714485494", "9922334454000000000000000000000000000000"},
75 "42949672950000000000000000000000000000000": {
76 "1069446856703", "1069446856703", "42949672950000000000000000000000000000000"},
77 "99223344556573838487575": {
78 "0", "456598933239", "99223344550000000000000"},
79 "992233445500000000000000000000000000000000": {
80 "0", "0", "0"}, // e>31, returns 0, err
81 "343597383670000000000000000000000000000000": {
82 "1099511627775", "1099511627775", "343597383670000000000000000000000000000000"},
83 "343597383680000000000000000000000000000000": {
84 "0", "0", "0"}, // e>31, returns 0, err
85 "1157073197879933027": {
86 "0", "286448638922", "1157073197800000000"},
87 }
88 for test := range testVector {
89 bi, ok := new(big.Int).SetString(test, 10)
90 require.True(t, ok)
91 f40, err := NewFloat40(bi)
92 if f40 == 0 {
93 assert.Error(t, err)
94 } else {
95 assert.NoError(t, err)
96 }
97 assert.Equal(t, testVector[test][0], fmt.Sprint(uint64(f40)))
98
99 f40, err = NewFloat40Floor(bi)
100 if f40 == 0 {
101 assert.Equal(t, ErrFloat40E31, tracerr.Unwrap(err))
102 } else {
103 assert.NoError(t, err)
104 }
105 assert.Equal(t, testVector[test][1], fmt.Sprint(uint64(f40)))
106
107 bi2, err := f40.BigInt()
108 require.NoError(t, err)
109 assert.Equal(t, fmt.Sprint(testVector[test][2]), bi2.String())
110 }
111}
112
113func BenchmarkFloat40(b *testing.B) {
114 newBigInt := func(s string) *big.Int {
115 bigInt, ok := new(big.Int).SetString(s, 10)
116 if !ok {
117 panic("Can not convert string to *big.Int")
118 }
119 return bigInt
120 }
121 type pair struct {
122 Float40 Float40
123 BigInt *big.Int
124 }
125 testVector := []pair{
126 {6*0x800000000 + 123, newBigInt("123000000")},
127 {2*0x800000000 + 4545, newBigInt("454500")},
128 {30*0x800000000 + 10235, newBigInt("10235000000000000000000000000000000")},
129 {0x000000000, newBigInt("0")},
130 {0x800000000, newBigInt("0")},
131 {0x0001, newBigInt("1")},
132 {0x0401, newBigInt("1025")},
133 {0x800000000 + 1, newBigInt("10")},
134 {0xFFFFFFFFFF, newBigInt("343597383670000000000000000000000000000000")},
135 }
136 b.Run("NewFloat40()", func(b *testing.B) {
137 for i := 0; i < b.N; i++ {
138 _, _ = NewFloat40(testVector[i%len(testVector)].BigInt)
139 }
140 })
141 b.Run("Float40.BigInt()", func(b *testing.B) {
142 for i := 0; i < b.N; i++ {
143 _, _ = testVector[i%len(testVector)].Float40.BigInt()
144 }
145 })
146}