-
Notifications
You must be signed in to change notification settings - Fork 5
/
partitioner_test.go
415 lines (367 loc) · 9.41 KB
/
partitioner_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package handel
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPartitionerBinTreeSize(t *testing.T) {
n := 17
reg := FakeRegistry(n)
type sizeTest struct {
id int32
level int
isErr bool
exp int
}
var tests = []sizeTest{
{1, 0, false, 1},
{1, 1, false, 1},
{1, 2, false, 2},
{1, 3, false, 4},
{1, 4, false, 8},
// 1 because 17 is alone in his group (only node > 16 )
{1, 5, false, 1},
{1, 6, false, 17},
// -- 17's point of view
{16, 0, false, 1},
{16, 5, false, n - 1},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
ct := NewBinPartitioner(test.id, reg, DefaultLogger)
size := ct.Size(test.level)
require.Equal(t, test.exp, size)
}
}
func TestPartitionerBinIndexAtLevel(t *testing.T) {
n := 13
reg := FakeRegistry(n)
type indexTest struct {
partID int32
id int32
level int
err bool
exp int
}
var tests = []indexTest{
// "left side" id should be same
{5, 1, 3, false, 1},
// "right side" should be shifted
{1, 5, 3, false, 1},
// invalid level
{1, 1, 10, true, 1},
// invalid id for this level
{1, 5, 2, true, 1},
}
for i, test := range tests {
t.Logf(" -- test %d --", i)
part := NewBinPartitioner(test.partID, reg, DefaultLogger)
res, err := part.IndexAtLevel(test.id, test.level)
if err != nil {
if test.err {
require.Error(t, err)
continue
}
t.Fatal("expected no error", err)
}
require.Equal(t, test.exp, res)
}
}
func TestPartitionerBinTreeCombine(t *testing.T) {
n := 17
reg := FakeRegistry(n)
type combineTest struct {
id int32
sigs []*incomingSig
level int
isErr bool
exp *MultiSignature
}
sig3 := &fakeSig{true}
bs3 := NewWilffBitset(n / 2)
for i := 0; i < bs3.BitLength(); i++ {
bs3.Set(i, true)
}
sig2 := &fakeSig{true}
bs2 := NewWilffBitset(pow2(3 - 1))
// only the level-2 bits are set
for i := 2; i < 4; i++ {
bs2.Set(i, true)
}
bs0 := NewWilffBitset(1)
bs0.Set(0, true)
// final signature should have level 4 and bit length 8
// with first bit set to false
pairs3 := incomingSigs(0, 2, 3)
final4 := finalIncomingSig(4, 8)
final4.ms.BitSet.Set(0, false)
fullBs := NewWilffBitset(n)
for i := 0; i < n; i++ {
fullBs.Set(i, true)
}
var tests = []combineTest{
// from last node
{16, incomingSigs(0), 1, false, &MultiSignature{Signature: sig3, BitSet: bs0}},
// error in the level requested
{16, incomingSigs(0, 5), 3, true, &MultiSignature{Signature: sig3, BitSet: fullBs}},
// contributions from last node + all previous nodes
{16, incomingSigs(0, 5), 6, false, &MultiSignature{Signature: sig3, BitSet: fullBs}},
// all good, we should have the first half of signature returned (
{1, incomingSigs(0, 1, 2, 3), 4, false, &MultiSignature{Signature: sig3, BitSet: bs3}},
// only one to combine
{1, incomingSigs(2), 3, false, &MultiSignature{Signature: sig2, BitSet: bs2}},
{1, nil, 0, true, nil},
// with holes
{1, pairs3, 4, false, final4.ms},
}
for i, test := range tests {
ct := NewBinPartitioner(test.id, reg, DefaultLogger)
t.Logf(" -- test %d -- ", i)
ms := ct.Combine(test.sigs, test.level, NewWilffBitset)
if ms == nil {
if test.isErr {
continue
}
require.NotNil(t, ms)
}
require.Equal(t, test.exp.Signature, ms.Signature)
require.Equal(t, test.exp.BitSet.BitLength(), ms.BitSet.BitLength())
bs1 := test.exp.BitSet
bs2 := ms.BitSet
for i := 0; i < bs1.BitLength(); i++ {
require.Equal(t, bs1.Get(i), bs2.Get(i))
}
}
}
func TestPartitionerBinTreeCombineFull(t *testing.T) {
n := 17
reg := FakeRegistry(n)
sig3 := &fakeSig{true}
bs3 := NewWilffBitset(n)
for i := 0; i < pow2(3); i++ {
bs3.Set(i, true)
}
sig2 := &fakeSig{true}
bs2 := NewWilffBitset(n)
// only the second sig is there so no 0,1
for i := 2; i < pow2(2); i++ {
bs2.Set(i, true)
}
pairs3 := incomingSigs(0, 1, 2, 3, 4)
// 4-1 -> because that's how you compute the size of a level
// -1 -> to just spread out holes to other levels and leave this one still
// having one contribution
for i := 0; i < pow2(4-1)-1; i++ {
pairs3[4].ms.BitSet.Set(i, false)
}
pairs3[3].ms.BitSet.Set(1, false)
pairs3[3].ms.BitSet.Set(2, false)
final4 := finalIncomingSig(4, n)
final4.ms.BitSet.Set(5, false)
final4.ms.BitSet.Set(6, false)
for i := 8; i < 15; i++ {
final4.ms.BitSet.Set(i, false)
}
final4.ms.BitSet.Set(16, false) // no signature of the last node included
bs := func(is ...int) BitSet {
fullBs := NewWilffBitset(n)
if len(is) == 0 {
for i := 0; i < n; i++ {
fullBs.Set(i, true)
}
return fullBs
}
for _, i := range is {
fullBs.Set(i, true)
}
return fullBs
}
type combineTest struct {
id int32
sigs []*incomingSig
isErr bool
exp *MultiSignature
}
var tests = []combineTest{
// from last node
{16, incomingSigs(0), false, &MultiSignature{Signature: sig3, BitSet: bs(16)}},
// error in the level requested
{16, incomingSigs(0, 5), true, &MultiSignature{Signature: sig3, BitSet: bs()}},
// contributions from last node + all previous nodes
{16, incomingSigs(0, 5), false, &MultiSignature{Signature: sig3, BitSet: bs()}},
// all good, we should have the first half of signature returned (
{1, incomingSigs(0, 1, 2, 3), false, &MultiSignature{Signature: sig3, BitSet: bs3}},
// only one to combine
{1, incomingSigs(2), false, &MultiSignature{Signature: sig2, BitSet: bs2}},
{1, nil, true, nil},
// with holes
{1, pairs3, false, final4.ms},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
ct := NewBinPartitioner(test.id, reg, DefaultLogger)
res := ct.CombineFull(test.sigs, NewWilffBitset)
if res == nil {
if test.exp == nil {
continue
}
t.Fatal("should not have got nil output")
}
require.Equal(t, test.exp.Signature, res.Signature)
require.Equal(t, test.exp.BitSet.BitLength(), res.BitSet.BitLength())
require.Equal(t, n, res.BitSet.BitLength())
bs1 := test.exp.BitSet
bs2 := res.BitSet
for i := 0; i < bs1.BitLength(); i++ {
require.Equal(t, bs1.Get(i), bs2.Get(i))
}
}
}
func TestPartitionerBinTreeMaxLevel(t *testing.T) {
type maxLevelTest struct {
n int
exp int
}
var tests = []maxLevelTest{
{8, 3}, {16, 4}, {2, 1},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
reg := FakeRegistry(test.n)
ct := NewBinPartitioner(1, reg, DefaultLogger)
require.Equal(t, test.exp, ct.MaxLevel())
}
}
func TestPartitionerBinLevels(t *testing.T) {
type levelsTest struct {
n int
id int32
levels []int
}
i := func(is ...int) []int {
return is
}
tests := []levelsTest{
{4, 1, i(1, 2)},
{5, 1, i(1, 2, 3)},
{5, 4, i(3)},
}
for i, test := range tests {
t.Logf(" -- test %d --", i)
reg := FakeRegistry(test.n)
ct := NewBinPartitioner(test.id, reg, DefaultLogger).(*binomialPartitioner)
levels := ct.Levels()
require.Equal(t, test.levels, levels)
for _, lvl := range levels {
_, err := ct.IdentitiesAt(lvl)
require.NoError(t, err)
}
}
}
func TestPartitionerBinTreeRangeAt(t *testing.T) {
n := 17
reg := FakeRegistry(n)
type rangeTest struct {
id int32
level int
isErr bool
from int
to int
}
tests := []rangeTest{
{1, 0, false, 1, 2},
{1, 1, false, 0, 1},
{1, 2, false, 2, 4},
{1, 3, false, 4, 8},
{1, 4, false, 8, 16},
{1, 5, false, 16, 17},
{16, 0, false, 16, 17},
{16, 1, true, 16, 17},
{16, 2, true, 16, 17},
{16, 3, true, 16, 17},
{16, 4, true, 16, 17},
{16, 5, false, 0, 16},
{1, 7, true, 0, 0},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
ct := NewBinPartitioner(test.id, reg, DefaultLogger).(*binomialPartitioner)
_ids, err := ct.IdentitiesAt(test.level)
if test.isErr {
require.Error(t, err)
}
min, max, err := ct.rangeLevel(test.level)
if test.isErr {
require.Error(t, err)
continue
}
require.Equal(t, test.from, min)
require.Equal(t, test.to, max)
expected, ok := reg.Identities(test.from, test.to)
require.True(t, ok)
require.Equal(t, expected, _ids)
}
}
func TestPartitionerBinTreeRangeAtInverse(t *testing.T) {
n := 17
reg := FakeRegistry(n)
type rangeTest struct {
id int32
level int
isErr bool
from int
to int
}
tests := []rangeTest{
// test for id in the lower part of the ID space => complete power of
// two
{1, 0, false, 1, 2},
{1, 1, false, 1, 2},
{1, 2, false, 0, 2},
{1, 3, false, 0, 4},
{1, 4, false, 0, 8},
{1, 5, false, 0, 16},
//special high level where we take the whole ID space
{1, 6, false, 0, 17},
{1, 7, true, 0, 0},
// test after the power of two - 16 - so the levels should be all equal
// size = 1
{16, 0, false, 16, 17},
{16, 1, false, 16, 17},
{16, 2, false, 16, 17},
{16, 3, false, 16, 17},
{16, 4, false, 16, 17},
{16, 5, false, 16, 17},
// special high level where we take the whole ID space
{16, 6, false, 0, 17},
{16, 7, true, 16, 17},
}
for i, test := range tests {
t.Logf(" -- test %d -- ", i)
ct := NewBinPartitioner(test.id, reg, DefaultLogger).(*binomialPartitioner)
min, max, err := ct.rangeLevelInverse(test.level)
if test.isErr {
require.Error(t, err)
continue
}
require.Equal(t, test.from, min)
require.Equal(t, test.to, max)
}
}
func TestIsSet(t *testing.T) {
type setTest struct {
nb uint
idx uint
expected bool
}
tests := []setTest{
{0, 0, false},
{2, 0, false},
{2, 1, true},
{7, 2, true},
{7, 4, false},
}
for i, test := range tests {
res := isSet(test.nb, test.idx)
require.Equal(t, test.expected, res, "%d - failed: %v", i, test)
}
}