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