forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fast_node_test.go
58 lines (51 loc) · 1.32 KB
/
fast_node_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
53
54
55
56
57
58
package iavl
import (
"bytes"
"encoding/hex"
"testing"
"github.com/stretchr/testify/require"
)
func TestFastNode_encodedSize(t *testing.T) {
fastNode := &FastNode{
key: randBytes(10),
versionLastUpdatedAt: 1,
value: randBytes(20),
}
expectedSize := 1 + len(fastNode.value) + 1
require.Equal(t, expectedSize, fastNode.encodedSize())
}
func TestFastNode_encode_decode(t *testing.T) {
testcases := map[string]struct {
node *FastNode
expectHex string
expectError bool
}{
"nil": {nil, "", true},
"empty": {&FastNode{}, "0000", false},
"inner": {&FastNode{
key: []byte{0x4},
versionLastUpdatedAt: 1,
value: []byte{0x2},
}, "020102", false},
}
for name, tc := range testcases {
tc := tc
t.Run(name, func(t *testing.T) {
var buf bytes.Buffer
err := tc.node.writeBytes(&buf)
if tc.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tc.expectHex, hex.EncodeToString(buf.Bytes()))
node, err := DeserializeFastNode(tc.node.key, buf.Bytes())
require.NoError(t, err)
// since value and leafHash are always decoded to []byte{} we augment the expected struct here
if tc.node.value == nil {
tc.node.value = []byte{}
}
require.Equal(t, tc.node, node)
})
}
}