-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_test.go
60 lines (52 loc) · 1.18 KB
/
bucket_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
package kademlia
import (
"math/big"
"testing"
)
type testContact struct {
id *big.Int
}
func (c *testContact) ID() *big.Int {
return c.id
}
func mustNewTestContactFromString(s string) Contact {
id, ok := new(big.Int).SetString(s, 16)
if !ok {
panic("invalid contact id")
}
return &testContact{id: id}
}
func TestEmptyBucketDepth(t *testing.T) {
const (
B = 3
k = 1
)
b := newBucket(new(big.Int), bitMask(B))
if b.depth() != 0 {
t.Fail()
}
}
func TestBucketDepth(t *testing.T) {
const (
B = 17
k = 7
)
b := newBucket(new(big.Int), bitMask(B))
for _, test := range []struct {
input string
expected int
}{
{expected: 16, input: "3faa"}, // 00111111 10101010
{expected: 5, input: "3855"}, // 00111000 01010101
{expected: 4, input: "30aa"}, // 00110000 10101010
{expected: 3, input: "2055"}, // 00100000 01010101
{expected: 2, input: "00aa"}, // 00000000 10101010
{expected: 1, input: "4055"}, // 01000000 01010101
{expected: 0, input: "80aa"}, // 10000000 10101010
} {
b.put(mustNewTestContactFromString(test.input), k)
if d := b.depth(); d != test.expected {
t.Errorf("expected %d, got %d, %s", test.expected, d, test.input)
}
}
}