/
betweenness_centrality_test.go
138 lines (116 loc) · 2.9 KB
/
betweenness_centrality_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
package autopilot
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestBetweennessCentralityMetricConstruction(t *testing.T) {
failing := []int{-1, 0}
ok := []int{1, 10}
for _, workers := range failing {
m, err := NewBetweennessCentralityMetric(workers)
require.Error(
t, err, "construction must fail with <= 0 workers",
)
require.Nil(t, m)
}
for _, workers := range ok {
m, err := NewBetweennessCentralityMetric(workers)
require.NoError(
t, err, "construction must succeed with >= 1 workers",
)
require.NotNil(t, m)
}
}
// Tests that empty graph results in empty centrality result.
func TestBetweennessCentralityEmptyGraph(t *testing.T) {
centralityMetric, err := NewBetweennessCentralityMetric(1)
require.NoError(
t, err,
"construction must succeed with positive number of workers",
)
for _, chanGraph := range chanGraphs {
graph, cleanup, err := chanGraph.genFunc()
success := t.Run(chanGraph.name, func(t1 *testing.T) {
require.NoError(t, err, "unable to create graph")
if cleanup != nil {
defer cleanup()
}
err := centralityMetric.Refresh(graph)
require.NoError(t, err)
centrality := centralityMetric.GetMetric(false)
require.Equal(t, 0, len(centrality))
centrality = centralityMetric.GetMetric(true)
require.Equal(t, 0, len(centrality))
})
if !success {
break
}
}
}
// Test betweenness centrality calculating using an example graph.
func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
workers := []int{1, 3, 9, 100}
tests := []struct {
normalize bool
centrality []float64
}{
{
normalize: true,
centrality: normalizedTestGraphCentrality,
},
{
normalize: false,
centrality: testGraphCentrality,
},
}
for _, numWorkers := range workers {
for _, chanGraph := range chanGraphs {
numWorkers := numWorkers
graph, cleanup, err := chanGraph.genFunc()
require.NoError(t, err, "unable to create graph")
if cleanup != nil {
defer cleanup()
}
testName := fmt.Sprintf(
"%v %d workers", chanGraph.name, numWorkers,
)
success := t.Run(testName, func(t1 *testing.T) {
metric, err := NewBetweennessCentralityMetric(
numWorkers,
)
require.NoError(
t, err,
"construction must succeed with "+
"positive number of workers",
)
graphNodes := buildTestGraph(
t1, graph, centralityTestGraph,
)
err = metric.Refresh(graph)
require.NoError(t, err)
for _, expected := range tests {
expected := expected
centrality := metric.GetMetric(
expected.normalize,
)
require.Equal(t,
centralityTestGraph.nodes,
len(centrality),
)
for i, c := range expected.centrality {
nodeID := NewNodeID(
graphNodes[i],
)
result, ok := centrality[nodeID]
require.True(t, ok)
require.Equal(t, c, result)
}
}
})
if !success {
break
}
}
}
}