-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtree_test.go
95 lines (81 loc) · 2.32 KB
/
tree_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
package merkletree
import (
"testing"
"golang.org/x/net/context"
)
func makeTestConfig(of *TestObjFactory, m ChildIndex, n ChildIndex) Config {
return NewConfig(SHA512Hasher{}, m, n, of)
}
func newTestMemTree(of *TestObjFactory, m ChildIndex, n ChildIndex) (t *Tree, me *MemEngine) {
me = NewMemEngine()
t = NewTree(me, makeTestConfig(of, m, n))
return t, me
}
func testSimpleBuild(t *testing.T, numElem int, m ChildIndex, n ChildIndex) {
of := NewTestObjFactory()
objs := of.Mproduce(numElem)
sm := NewSortedMapFromList(objs)
tree, _ := newTestMemTree(of, m, n)
if err := tree.Build(context.TODO(), sm, nil); err != nil {
t.Fatalf("Error in build: %v", err)
}
findAll(t, tree, objs)
}
func testUpsertBuild(t *testing.T, numElem int, m ChildIndex, n ChildIndex) {
of := NewTestObjFactory()
objs := of.Mproduce(numElem)
tree, _ := newTestMemTree(of, m, n)
for _, obj := range objs {
if err := tree.Upsert(context.TODO(), obj, nil); err != nil {
t.Fatalf("Error in upsert: %v\n", err)
}
}
findAll(t, tree, objs)
}
func TestSimpleBuild4by16(t *testing.T) {
for i := 0; i < 10; i++ {
testSimpleBuild(t, 1024, ChildIndex(4), ChildIndex(16))
}
}
func TestSimpleBuild2by4(t *testing.T) {
for i := 0; i < 10; i++ {
testSimpleBuild(t, 1024, ChildIndex(2), ChildIndex(4))
}
}
func TestSimpleBuild256by256(t *testing.T) {
for i := 0; i < 2; i++ {
testSimpleBuild(t, 8192, ChildIndex(256), ChildIndex(256))
}
}
func TestSimpleBuildSmall(t *testing.T) {
testSimpleBuild(t, 1, ChildIndex(2), ChildIndex(2))
}
func findAll(t *testing.T, tree *Tree, objs []KeyValuePair) {
for i, kvp := range objs {
v, _, err := tree.Find(context.TODO(), kvp.Key)
if err != nil {
t.Fatalf("Find for obj %d yielded an error: %v", i, err)
}
if v == nil {
t.Fatalf("Find for obj %d with key %x returned no results", i, kvp.Key)
}
if !deepEqual(v, kvp.Value) {
t.Fatalf("Didn't get object equality for %d: %+v != %+v", i, v, kvp.Value)
}
}
}
func TestUpsert4by16(t *testing.T) {
for i := 0; i < 10; i++ {
testUpsertBuild(t, 1024, ChildIndex(4), ChildIndex(16))
}
}
func TestUpsert2by4(t *testing.T) {
for i := 0; i < 10; i++ {
testUpsertBuild(t, 1024, ChildIndex(2), ChildIndex(4))
}
}
func TestUpsertBuild256by256(t *testing.T) {
for i := 0; i < 2; i++ {
testUpsertBuild(t, 8192, ChildIndex(256), ChildIndex(256))
}
}