forked from spacemeshos/merkle-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treecache_test.go
212 lines (155 loc) · 5.07 KB
/
treecache_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
package merkle
import (
"errors"
"github.com/stretchr/testify/require"
"io"
"testing"
)
func TestNewTreeCache(t *testing.T) {
}
var someError = errors.New("some error")
type seekErrorReader struct{}
func (seekErrorReader) Seek(index uint64) error { return someError }
func (seekErrorReader) ReadNext() ([]byte, error) { panic("implement me") }
func (seekErrorReader) Width() uint64 { return 3 }
type readErrorReader struct{}
func (readErrorReader) Seek(index uint64) error { return nil }
func (readErrorReader) ReadNext() ([]byte, error) { return nil, someError }
func (readErrorReader) Width() uint64 { return 8 }
type seekEOFReader struct{}
func (seekEOFReader) Seek(index uint64) error { return io.EOF }
func (seekEOFReader) ReadNext() ([]byte, error) { panic("implement me") }
func (seekEOFReader) Width() uint64 { return 1 }
type widthReader struct{ width uint64 }
func (r widthReader) Seek(index uint64) error { return nil }
func (r widthReader) ReadNext() ([]byte, error) { return nil, someError }
func (r widthReader) Width() uint64 { return r.width }
func TestNewTreeCacheErrors(t *testing.T) {
r := require.New(t)
var readers map[uint]NodeReader
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.Error(err)
r.Equal("reader for base layer must be included", err.Error())
r.Nil(treeCache)
}
func TestNewTreeCache2(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.Error(err)
r.Equal("reader for base layer must be included", err.Error())
r.Nil(treeCache)
}
func TestNewTreeCache3(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = seekErrorReader{}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
nodePos := position{}
node, err := treeCache.GetNode(nodePos)
r.Error(err)
r.Equal("while seeking to position <h: 0 i: 0> in cache: some error", err.Error())
r.Nil(node)
}
func TestNewTreeCache4(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = readErrorReader{}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
nodePos := position{}
node, err := treeCache.GetNode(nodePos)
r.Error(err)
r.Equal("while reading from cache: some error", err.Error())
r.Nil(node)
}
func TestNewTreeCache5(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = seekErrorReader{}
readers[1] = seekEOFReader{}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
nodePos := position{height: 1}
node, err := treeCache.GetNode(nodePos)
r.Error(err)
r.Equal("while seeking to position <h: 0 i: 0> in cache: some error", err.Error())
r.Nil(node)
}
func TestNewTreeCache6(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = seekErrorReader{}
readers[1] = widthReader{width: 1}
readers[2] = seekEOFReader{}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
nodePos := position{height: 2}
node, err := treeCache.GetNode(nodePos)
r.Error(err)
r.Equal("while calculating ephemeral node at position <h: 1 i: 1>: while seeking to position <h: 0 i: 10> in cache: some error", err.Error())
r.Nil(node)
}
func TestNewTreeCache7(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = widthReader{width: 2}
readers[1] = seekEOFReader{}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
nodePos := position{height: 1}
node, err := treeCache.GetNode(nodePos)
r.Error(err)
r.Equal("while traversing subtree for root: while reading a leaf: some error", err.Error())
r.Nil(node)
}
func TestNewTreeCacheStructureSuccess(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = widthReader{width: 4}
readers[1] = widthReader{width: 2}
readers[2] = widthReader{width: 1}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.NoError(err)
r.NotNil(treeCache)
}
func TestNewTreeCacheStructureFail(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = widthReader{width: 3}
readers[1] = widthReader{width: 2}
readers[2] = widthReader{width: 1}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.Error(err)
r.Equal("reader at layer 1 has width 2 instead of 1", err.Error())
r.Nil(treeCache)
}
func TestNewTreeCacheStructureFail2(t *testing.T) {
r := require.New(t)
readers := make(map[uint]NodeReader)
readers[0] = widthReader{width: 4}
readers[1] = widthReader{width: 1}
readers[2] = widthReader{width: 1}
treeCache, err := NewTreeCache(readers, GetSha256Parent)
r.Error(err)
r.Equal("reader at layer 1 has width 1 instead of 2", err.Error())
r.Nil(treeCache)
}
func TestPosition_isAncestorOf(t *testing.T) {
lower := position{
index: 0,
height: 0,
}
higher := position{
index: 0,
height: 1,
}
isAncestor := lower.isAncestorOf(higher)
require.False(t, isAncestor)
}