-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteriornode_test.go
281 lines (234 loc) · 9.41 KB
/
interiornode_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package mpt
import (
"bytes"
"encoding/hex"
"testing"
)
func TestInteriorNodeKeyValue(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected SetValue to panic, but it did not")
}
}()
l1, _ := NewEmptyLeafNode()
l2, _ := NewEmptyLeafNode()
in, err := NewInteriorNode(l1, l2)
if err != nil {
t.Error(err.Error())
}
n := in.GetValue()
if n != nil {
t.Error("Expected GetValue to return nil, but it did not")
}
n = in.GetKey()
if n != nil {
t.Error("Expected GetKey to return nil, but it did not")
}
in.SetValue([]byte{})
}
func TestInteriorNodeChildren(t *testing.T) {
in, err := NewInteriorNode(nil, nil)
if err != nil {
t.Error(err.Error())
}
if in.HasLeft() {
t.Error("Expected hasLeft to return false, got true")
}
if in.HasRight() {
t.Error("Expected hasRight to return false, got true")
}
l1, _ := NewEmptyLeafNode()
l2, _ := NewEmptyLeafNode()
in.SetLeftChild(l1)
in.SetRightChild(l2)
if !in.GetLeftChild().Equals(l1) {
t.Error("GetLeftChild did not equal the input")
}
if !in.GetRightChild().Equals(l2) {
t.Error("GetRightChild did not equal the input")
}
if !in.HasLeft() {
t.Error("Expected hasLeft to return true, got false")
}
if !in.HasRight() {
t.Error("Expected hasRight to return true, got false")
}
}
func TestInterorNodeEquals(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
in2, _ := NewInteriorNode(dln, dln2)
in3, _ := NewInteriorNode(dln2, dln)
in4, _ := NewInteriorNode(nil, dln)
in5, _ := NewInteriorNode(dln2, nil)
in6, _ := NewInteriorNode(nil, nil)
if !in.Equals(in2) {
t.Error("Expected two interior nodes with the same children to n1.Equals(n2) == true (got false)")
}
if in.Equals(in3) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
if in.Equals(dln) {
t.Error("Expected interior node and dictionaryleafnode to n1.Equals(n2) == false (got true)")
}
if in3.Equals(in4) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
if in3.Equals(in5) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
if in4.Equals(in3) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
if in6.Equals(in4) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
if in5.Equals(in6) {
t.Error("Expected two interior nodes with different children to n1.Equals(n2) == false (got true)")
}
}
func TestInterorNodeGetHash(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
h, _ := hex.DecodeString("b0a16687f6701fea2b317cf84642a7de3093b34312e78b24da6cee66aee1fb23") // Expected hash
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
if in.CountHashesRequiredForGetHash() != 3 {
t.Error("Expected CountHashesRequiredForGetHash to be 3, but it was not")
}
dh := in.GetHash()
if !bytes.Equal(dh, h) {
t.Errorf("GetHash should return %x, but got %x", h, dh)
}
if in.CountHashesRequiredForGetHash() != 0 {
t.Error("Expected CountHashesRequiredForGetHash to be 0, but it was not")
}
}
func testSerializeEqual(in *InteriorNode, t *testing.T) {
b := in.Bytes()
n, err := NodeFromBytes(b)
if err != nil {
t.Error(err.Error())
}
in2, ok := n.(*InteriorNode)
if !ok {
t.Error("Failed to deserialize InteriorNode")
return
}
if !in2.Equals(in) {
t.Error("Deserialized node did not equal input")
}
}
func TestInteriorNodeSerialize(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
testSerializeEqual(in, t)
// Test with left node nil
in.SetLeftChild(nil)
testSerializeEqual(in, t)
// Test with right node nil
in.SetLeftChild(dln)
in.SetRightChild(nil)
testSerializeEqual(in, t)
// Test with both nil
in.SetLeftChild(nil)
in.SetRightChild(nil)
testSerializeEqual(in, t)
// Test invalid byte slices
_, err := NewInteriorNodeFromBytes([]byte{}) // Empty byte slice
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
_, err = NewInteriorNodeFromBytes([]byte{0xFF, 0xAB, 0x00, 0x00, 0x00}) // Length for left, but no bytes with actual data
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
_, err = NewInteriorNodeFromBytes([]byte{0xFF}) // No data for left nor right
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
_, err = NewInteriorNodeFromBytes([]byte{0xFF, 0x00, 0x00, 0x00, 0x00}) // zero-length key
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
_, err = NewInteriorNodeFromBytes([]byte{0xFF, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00}) // invalid type for left node
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
_, err = NewInteriorNodeFromBytes([]byte{0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF}) // invalid type for right node
if err == nil {
t.Error("NewInteriorNodeFromBytes with invalid data should have returned an error, but did not")
}
}
func TestInteriorNodeCounts(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
if in.EmptyLeafNodesInSubtree() != 0 {
t.Error("Expected EmptyLeafNodesInSubtree to return 0, but it did not")
}
if in.InteriorNodesInSubtree() != 1 {
t.Error("Expected InteriorNodesInSubtree to return 1, but it did not")
}
if in.NodesInSubtree() != 3 {
t.Error("Expected NodesInSubtree to return 3, but it did not")
}
if in.NonEmptyLeafNodesInSubtree() != 2 {
t.Error("Expected NonEmptyLeafNodesInSubtree to return 2, but it did not")
}
}
func TestInteriorNodeChanged(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
if !in.Changed() {
t.Error("Expected changed for a new node to be true, but it was not")
}
in.MarkUnchangedAll()
if in.Changed() {
t.Error("Expected changed after call to MarkUnchangedAll to be false, but it was not")
}
in.MarkChangedAll()
if !in.Changed() {
t.Error("Expected changed after call to MarkChangedAll to be true, but it was not")
}
}
func TestInteriorNodeProperties(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("2134567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
dln, _ := NewDictionaryLeafNode(k1, v1)
dln2, _ := NewDictionaryLeafNode(k2, v2)
in, _ := NewInteriorNode(dln, dln2)
if in.IsEmpty() {
t.Error("Expected IsEmpty for an InteriorNode to be false, but it was not")
}
if in.IsStub() {
t.Error("Expected IsStub for an InteriorNode to be false, but it was not")
}
if in.IsLeaf() {
t.Error("Expected IsLeaf for an InteriorNode to be false, but it was not")
}
}