-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding_test.go
More file actions
75 lines (69 loc) · 1.65 KB
/
encoding_test.go
File metadata and controls
75 lines (69 loc) · 1.65 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mpt
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncoding(t *testing.T) {
for i := 0; i < 10000; i++ {
bytes := randomBytes()
nibbles := bytesToNibbles(bytes)
recovered := nibblesToBytes(nibbles)
assert.Equal(t, bytes, recovered)
}
}
func TestEncDecLeafKey(t *testing.T) {
keyNibbles := randomNibbles()
keyBytes, flag := encodeKey(keyNibbles, leafType)
if len(keyNibbles)%2 == 0 {
assert.Equal(t, leafType, flag)
} else {
assert.Equal(t, leafWithPad, flag)
}
nibbles, nodeType := decodeKey(flag, keyBytes)
assert.Equal(t, leafType, nodeType)
assert.Equal(t, keyNibbles, nibbles)
}
func TestEncDecExtKey(t *testing.T) {
keyNibbles := randomNibbles()
keyBytes, flag := encodeKey(keyNibbles, extType)
if len(keyNibbles)%2 == 0 {
assert.Equal(t, extType, flag)
} else {
assert.Equal(t, extWithPad, flag)
}
nibbles, nodeType := decodeKey(flag, keyBytes)
assert.Equal(t, extType, nodeType)
assert.Equal(t, keyNibbles, nibbles)
}
func TestMatchingLength(t *testing.T) {
cases := []struct {
a []byte
b []byte
commonLength int
}{
{
a: []byte{0x01, 0x02, 0x03},
b: []byte{0x01, 0x02, 0x03},
commonLength: 3,
},
{
a: []byte{0x01, 0x02, 0x03, 0x04},
b: []byte{0x01, 0x02, 0x03},
commonLength: 3,
},
{
a: []byte{0x01, 0x02, 0x03},
b: []byte{0x01, 0x02, 0x03, 0x04},
commonLength: 3,
},
{
a: []byte{},
b: []byte{0x01, 0x02, 0x03},
commonLength: 0,
},
}
for _, c := range cases {
ml := matchingLength(c.a, c.b)
assert.Equal(t, ml, c.commonLength)
}
}