forked from bobg/scp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
114 lines (109 loc) · 2.13 KB
/
node_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
package scp
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestPeers(t *testing.T) {
cases := []struct {
slices []string
want string
}{
{},
{
slices: []string{"a"},
want: "a",
},
{
slices: []string{"a", "a"},
want: "a",
},
{
slices: []string{"a b", "a c"},
want: "a b c",
},
{
slices: []string{"a b", "c d"},
want: "a b c d",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
var q []NodeIDSet
for _, slice := range tc.slices {
ns := toNodeIDSet(slice)
q = append(q, ns)
}
ch := make(chan *Msg)
n := NewNode("x", slicesToQSet(q), ch, nil)
got := n.Peers()
want := toNodeIDSet(tc.want)
if !reflect.DeepEqual(got, NodeIDSet(want)) {
t.Errorf("got %v, want %v", got, want)
}
})
}
}
func TestWeight(t *testing.T) {
cases := []struct {
slices []string
wantW float64
wantIs1 bool
}{
{
slices: []string{"a"},
},
{
slices: []string{"a", "b"},
},
{
slices: []string{"a b", "a z"},
wantW: 0.5,
},
{
slices: []string{"a b", "a c", "a d", "a z"},
wantW: 0.25,
wantIs1: false,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
var q []NodeIDSet
for _, slice := range tc.slices {
ns := toNodeIDSet(slice)
q = append(q, ns)
}
ch := make(chan *Msg)
n := NewNode("x", slicesToQSet(q), ch, nil)
_, is1 := n.Weight(n.ID)
if !is1 {
t.Errorf("got !is1, want is1 for n.Weight(n.ID)")
}
got, is1 := n.Weight("z")
if got != tc.wantW || is1 != tc.wantIs1 {
t.Errorf("got %f (%v), want %f (%v)", got, is1, tc.wantW, tc.wantIs1)
}
})
}
}
func toNodeIDSet(s string) NodeIDSet {
var result NodeIDSet
fields := strings.Fields(s)
for _, f := range fields {
result = result.Add(NodeID(f))
}
return result
}
func slicesToQSet(slices []NodeIDSet) QSet {
result := QSet{T: 1}
for _, slice := range slices {
sub := QSet{T: len(slice)}
for _, nodeID := range slice {
nodeID := nodeID
sub.M = append(sub.M, QSetMember{N: &nodeID})
}
result.M = append(result.M, QSetMember{Q: &sub})
}
return result
}