-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
135 lines (105 loc) · 2.84 KB
/
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright Chrono Technologies LLC
// SPDX-License-Identifier: MIT
package arc
import (
"bytes"
"errors"
"testing"
)
func TestForEachChild(t *testing.T) {
subject := node{}
expectedKeys := [][]byte{
[]byte("apple"),
[]byte("banana"),
[]byte("cherry"),
[]byte("durian"),
}
subject.addChild(&node{key: expectedKeys[2]})
subject.addChild(&node{key: expectedKeys[0]})
subject.addChild(&node{key: expectedKeys[3]})
subject.addChild(&node{key: expectedKeys[1]})
subject.forEachChild(func(idx int, n *node) error {
got := n.key
want := expectedKeys[idx]
if !bytes.Equal(got, want) {
t.Fatalf("unexpected node, got:%q, want:%q", got, want)
}
return nil
})
}
func TestFindChild(t *testing.T) {
subject := node{}
expectedKeys := [][]byte{
[]byte("apple"),
[]byte("banana"),
[]byte("cherry"),
[]byte("durian"),
}
for _, key := range expectedKeys {
subject.addChild(&node{key: key})
}
for _, key := range expectedKeys {
node, err := subject.findChild(key)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !bytes.Equal(node.key, key) {
t.Fatalf("unexpected node, got:%q, want:%q", node.key, key)
}
}
for _, key := range [][]byte{[]byte("apricot"), []byte("crisp")} {
_, err := subject.findChild(key)
if !errors.Is(err, ErrKeyNotFound) {
t.Fatalf("unexpected error, got:%v, want:%v", err, ErrKeyNotFound)
}
}
}
func TestRemoveChild(t *testing.T) {
subject := node{}
expectedKeys := [][]byte{
[]byte("apple"),
[]byte("banana"),
[]byte("cherry"),
[]byte("durian"),
}
for _, key := range expectedKeys {
subject.addChild(&node{key: key})
}
// Test basic removal operations.
{
// Node exists before removal.
if _, err := subject.findChild(expectedKeys[2]); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := subject.removeChild(&node{key: expectedKeys[2]}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Child count has been updated.
if subject.numChildren != len(expectedKeys)-1 {
t.Fatalf("unexpected numChildren, got:%d, want:%d", subject.numChildren, len(expectedKeys)-1)
}
// Node unavailable after removal.
if _, err := subject.findChild(expectedKeys[2]); err != ErrKeyNotFound {
t.Fatalf("unexpected error: %v", err)
}
}
// Test removal of non-existent node.
{
if err := subject.removeChild(&node{key: []byte("bogus")}); err != ErrKeyNotFound {
t.Fatalf("unexpected error: %v", err)
}
}
}
func TestPrependKey(t *testing.T) {
subject := &node{key: []byte("child")}
prefix := []byte("parent-")
expected := []byte("parent-child")
subject.prependKey(prefix)
if !bytes.Equal(subject.key, expected) {
t.Errorf("unexpected result, got:%q, want:%q", subject.key, expected)
}
subject.prependKey(nil)
if !bytes.Equal(subject.key, expected) {
t.Errorf("unexpected result, got:%q, want:%q", subject.key, expected)
}
}