forked from celestiaorg/smt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
56 lines (45 loc) · 1.06 KB
/
iterator_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
package smt
import (
"crypto/sha256"
"github.com/stretchr/testify/require"
"testing"
)
type kvs struct{ k, v string }
var testdata = []kvs{
{"aardvark", "c"},
{"bar", "b"},
{"barb", "bd"},
{"bars", "be"},
{"fab", "z"},
{"foo", "a"},
{"foos", "aa"},
{"food", "ab"},
{"jars", "d"},
}
func TestIterator_LeafNodes(t *testing.T) {
nodeStore := NewSimpleMap()
valueStore := NewSimpleMap()
// Initialise the tree
tree := NewSparseMerkleTree(nodeStore, valueStore, sha256.New())
// Add test data to tree
for _, entry := range testdata {
_, err := tree.Update([]byte(entry.k), []byte(entry.v))
require.NoError(t, err)
}
it := tree.NewIterator()
leafNodes := make(map[string]string)
for it.Next() {
if it.Leaf() {
value, err := it.LeafValue()
require.NoError(t, err)
leafNodes[string(it.LeafKey())] = string(value)
}
}
for _, entry := range testdata {
value, ok := leafNodes[string(tree.th.path([]byte(entry.k)))]
// check for the key
require.True(t, ok, "key not found")
// check for the value
require.Equal(t, value, entry.v)
}
}