-
Notifications
You must be signed in to change notification settings - Fork 69
/
nibbles_test.go
90 lines (82 loc) · 1.53 KB
/
nibbles_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestIsNibble(t *testing.T) {
for i := 0; i < 20; i++ {
isNibble := i >= 0 && i < 16
require.Equal(t, isNibble, IsNibble(byte(i)), i)
}
}
func TestToPrefixed(t *testing.T) {
cases := []struct {
ns []Nibble
isLeafNode bool
expected []Nibble
}{
{
[]Nibble{1},
false,
[]Nibble{1, 1},
},
{
[]Nibble{1, 2},
false,
[]Nibble{0, 0, 1, 2},
},
{
[]Nibble{1},
true,
[]Nibble{3, 1},
},
{
[]Nibble{1, 2},
true,
[]Nibble{2, 0, 1, 2},
},
{
[]Nibble{5, 0, 6},
true,
[]Nibble{3, 5, 0, 6},
},
{
[]Nibble{14, 3},
false,
[]Nibble{0, 0, 14, 3},
},
{
[]Nibble{9, 3, 6, 5},
true,
[]Nibble{2, 0, 9, 3, 6, 5},
},
{
[]Nibble{1, 3, 3, 5},
true,
[]Nibble{2, 0, 1, 3, 3, 5},
},
{
[]Nibble{7},
true,
[]Nibble{3, 7},
},
}
for _, c := range cases {
require.Equal(t,
c.expected,
ToPrefixed(c.ns, c.isLeafNode))
}
}
func TestFromBytes(t *testing.T) {
// [1, 100] -> ['0x01', '0x64']
require.Equal(t, []Nibble{0, 1, 6, 4}, FromBytes([]byte{1, 100}))
}
func TestToBytes(t *testing.T) {
bytes := []byte{0, 1, 2, 3}
require.Equal(t, bytes, ToBytes(FromBytes(bytes)))
}
func TestPrefixMatchedLen(t *testing.T) {
require.Equal(t, 3, PrefixMatchedLen([]Nibble{0, 1, 2, 3}, []Nibble{0, 1, 2}))
require.Equal(t, 4, PrefixMatchedLen([]Nibble{0, 1, 2, 3}, []Nibble{0, 1, 2, 3}))
require.Equal(t, 4, PrefixMatchedLen([]Nibble{0, 1, 2, 3}, []Nibble{0, 1, 2, 3, 4}))
}