forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_test.go
52 lines (46 loc) · 1 KB
/
hex_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hex
import (
"encoding/hex"
"math"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeDecodeBig(t *testing.T) {
b := big.NewInt(math.MaxInt64)
e := EncodeBig(b)
d := DecodeBig(e)
assert.Equal(t, b.Uint64(), d.Uint64())
}
// Define a struct for test cases
type TestCase struct {
input string
output []byte
err error
}
// Unit test function
func TestDecodeHex(t *testing.T) {
testCases := []TestCase{
{"0", []byte{0}, nil},
{"00", []byte{0}, nil},
{"0x0", []byte{0}, nil},
{"0x00", []byte{0}, nil},
{"1", []byte{1}, nil},
{"01", []byte{1}, nil},
{"", []byte{}, hex.ErrLength},
{"0x", []byte{}, hex.ErrLength},
{"zz", []byte{}, hex.InvalidByteError('z')},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
output, err := DecodeHex(tc.input)
if tc.err != nil {
require.Error(t, tc.err, err)
} else {
require.NoError(t, err)
}
require.Equal(t, output, tc.output)
})
}
}