-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_test.go
254 lines (241 loc) · 6.69 KB
/
trie_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
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mpf
import (
"testing"
)
// NOTE: this hash and entry list comes from the aiken-lang/merkel-patricia-forestry tests
var fruitsExpectedHash string = "4acd78f345a686361df77541b2e0b533f53362e36620a1fdd3a13e0b61a3b078"
var fruitsTestEntries = []struct {
key string
value string
}{
{key: `apple[uid: 58]`, value: `🍎`},
{key: `apricot[uid: 0]`, value: `🤷`},
{key: `banana[uid: 218]`, value: `🍌`},
{key: `blueberry[uid: 0]`, value: `🫐`},
{key: `cherry[uid: 0]`, value: `🍒`},
{key: `coconut[uid: 0]`, value: `🥥`},
{key: `cranberry[uid: 0]`, value: `🤷`},
{key: `fig[uid: 68267]`, value: `🤷`},
{key: `grapefruit[uid: 0]`, value: `🤷`},
{key: `grapes[uid: 0]`, value: `🍇`},
{key: `guava[uid: 344]`, value: `🤷`},
{key: `kiwi[uid: 0]`, value: `🥝`},
{key: `kumquat[uid: 0]`, value: `🤷`},
{key: `lemon[uid: 0]`, value: `🍋`},
{key: `lime[uid: 0]`, value: `🤷`},
{key: `mango[uid: 0]`, value: `🥭`},
{key: `orange[uid: 0]`, value: `🍊`},
{key: `papaya[uid: 0]`, value: `🤷`},
{key: `passionfruit[uid: 0]`, value: `🤷`},
{key: `peach[uid: 0]`, value: `🍑`},
{key: `pear[uid: 0]`, value: `🍐`},
{key: `pineapple[uid: 12577]`, value: `🍍`},
{key: `plum[uid: 15492]`, value: `🤷`},
{key: `pomegranate[uid: 0]`, value: `🤷`},
{key: `raspberry[uid: 0]`, value: `🤷`},
{key: `strawberry[uid: 2532]`, value: `🍓`},
{key: `tangerine[uid: 11]`, value: `🍊`},
{key: `tomato[uid: 83468]`, value: `🍅`},
{key: `watermelon[uid: 0]`, value: `🍉`},
{key: `yuzu[uid: 0]`, value: `🤷`},
}
func TestTrieEmpty(t *testing.T) {
trie := NewTrie()
if trie.Hash() != NullHash {
t.Errorf(
"empty trie does not have expected hash: got %s, expected null hash",
trie.Hash().String(),
)
}
if trie.size != 0 {
t.Errorf(
"empty trie does not have expected size: got %d, expected 0",
trie.size,
)
}
}
func TestTrieHashChanges(t *testing.T) {
trie := NewTrie()
hash0 := trie.Hash().String()
trie.Set([]byte("abcd"), []byte("1"))
hash1 := trie.Hash().String()
if hash0 == hash1 {
t.Errorf(
"hash did not change after insert: old %s, new %s",
hash0,
hash1,
)
return
}
trie.Set([]byte("bcde"), []byte("2"))
hash2 := trie.Hash().String()
if hash1 == hash2 {
t.Errorf(
"hash did not change after insert: old %s, new %s",
hash1,
hash2,
)
}
}
func TestTrieDelete(t *testing.T) {
trie := NewTrie()
trie.Set([]byte("abcd"), []byte("1"))
hash1 := trie.Hash().String()
trie.Set([]byte("bcde"), []byte("2"))
hash2 := trie.Hash().String()
if err := trie.Delete([]byte("bcde")); err != nil {
t.Errorf("unexpected error: %s", err)
return
}
hash3 := trie.Hash().String()
if hash2 == hash3 {
t.Errorf("hash did not change after delete")
return
}
if hash1 != hash3 {
t.Errorf(
"hash is different before and after set/delete of key: got %s, expected %s",
hash3,
hash1,
)
}
}
func TestTrieGet(t *testing.T) {
testKey := []byte{0xaa, 0xff}
testVal := []byte("1")
trie := NewTrie()
trie.Set(testKey, testVal)
tmpVal, err := trie.Get(testKey)
if err != nil {
t.Errorf("unexpected error getting key: %s", err)
return
}
if string(tmpVal) != string(testVal) {
t.Errorf(
"did not get expected value for key: got %x, expected %x",
tmpVal,
testVal,
)
}
}
func TestTrieHas(t *testing.T) {
testKey := []byte{0xaa, 0xff}
trie := NewTrie()
if trie.Has(testKey) {
t.Errorf("has key when shouldn't")
return
}
trie.Set(testKey, []byte("1"))
if !trie.Has(testKey) {
t.Errorf("does not have key when should")
}
}
func TestTrieSetTwice(t *testing.T) {
expectedRootHash := "eb258590dda64098b24091629f9dbcaf7e6e55011f9a411deb9e5a9793f0d83f"
trie := NewTrie()
trie.Set([]byte{0xab, 0xcd}, []byte{0x01, 0x23})
hash1 := trie.Hash()
if hash1.String() != expectedRootHash {
t.Errorf(
"did not get expected root hash: got %s, expected %s",
trie.Hash().String(),
expectedRootHash,
)
}
trie.Set([]byte{0xab, 0xcd}, []byte{0x01, 0x23})
hash2 := trie.Hash()
if hash1 != hash2 {
t.Errorf(
"root hash changed when setting same value: got %s, expected %s",
hash2,
hash1,
)
}
}
func TestTrieExpectedHash(t *testing.T) {
expectedRootHash1 := "eb258590dda64098b24091629f9dbcaf7e6e55011f9a411deb9e5a9793f0d83f"
trie := NewTrie()
trie.Set([]byte{0xab, 0xcd}, []byte{0x01, 0x23})
if trie.Hash().String() != expectedRootHash1 {
t.Errorf(
"did not get expected root hash: got %s, expected %s",
trie.Hash().String(),
expectedRootHash1,
)
}
expectedRootHash2 := "6eddba467ac9132f619b06f6bc8577ae4a3a7d64632fe4d7d24b0ad9e58769b4"
trie.Set([]byte{0xaa, 0xff}, []byte{0x45, 0x67})
if trie.Hash().String() != expectedRootHash2 {
t.Errorf(
"did not get expected root hash: got %s, expected %s",
trie.Hash().String(),
expectedRootHash2,
)
}
}
func TestTrieFruitsExpectedHash(t *testing.T) {
trie := NewTrie()
for _, entry := range fruitsTestEntries {
trie.Set([]byte(entry.key), []byte(entry.value))
}
if trie.Hash().String() != fruitsExpectedHash {
t.Errorf(
"did not get expected root hash: got %s, expected %s",
trie.Hash().String(),
fruitsExpectedHash,
)
}
}
func TestTrieFruitsGet(t *testing.T) {
trie := NewTrie()
for _, entry := range fruitsTestEntries {
trie.Set([]byte(entry.key), []byte(entry.value))
}
for _, entry := range fruitsTestEntries {
tmpVal, err := trie.Get([]byte(entry.key))
if err != nil {
t.Fatalf("unexpected error getting key: %s", err)
}
if string(tmpVal) != entry.value {
t.Fatalf(
"did not get expected value: got %x, expected %x",
tmpVal,
entry.value,
)
}
}
}
func TestTrieFruitsSetDeleteConsistentHash(t *testing.T) {
trie := NewTrie()
var hashes []Hash
for _, entry := range fruitsTestEntries {
hashes = append(hashes, trie.Hash())
trie.Set([]byte(entry.key), []byte(entry.value))
}
for i := len(fruitsTestEntries) - 1; i >= 0; i-- {
entry := fruitsTestEntries[i]
if err := trie.Delete([]byte(entry.key)); err != nil {
t.Fatalf("unexpected error deleting key: %s", err)
}
if trie.Hash() != hashes[i] {
t.Fatalf(
"did not get expected hash: got %s, expected %s",
trie.Hash().String(),
hashes[i].String(),
)
}
}
}