4
4
"fmt"
5
5
"testing"
6
6
7
- "github.com/btcsuite/btcd/btcec"
8
- "github.com/btcsuite/btcutil"
7
+ "github.com/stretchr/testify/require"
9
8
)
10
9
11
10
func TestBetweennessCentralityMetricConstruction (t * testing.T ) {
@@ -14,173 +13,120 @@ func TestBetweennessCentralityMetricConstruction(t *testing.T) {
14
13
15
14
for _ , workers := range failing {
16
15
m , err := NewBetweennessCentralityMetric (workers )
17
- if m != nil || err == nil {
18
- t .Fatalf ("construction must fail with <= 0 workers" )
19
- }
16
+ require .Error (
17
+ t , err , "construction must fail with <= 0 workers" ,
18
+ )
19
+ require .Nil (t , m )
20
20
}
21
21
22
22
for _ , workers := range ok {
23
23
m , err := NewBetweennessCentralityMetric (workers )
24
- if m == nil || err != nil {
25
- t .Fatalf ("construction must succeed with >= 1 workers" )
26
- }
24
+ require .NoError (
25
+ t , err , "construction must succeed with >= 1 workers" ,
26
+ )
27
+ require .NotNil (t , m )
27
28
}
28
29
}
29
30
30
31
// Tests that empty graph results in empty centrality result.
31
32
func TestBetweennessCentralityEmptyGraph (t * testing.T ) {
32
33
centralityMetric , err := NewBetweennessCentralityMetric (1 )
33
- if err != nil {
34
- t .Fatalf ("construction must succeed with positive number of workers" )
35
- }
34
+ require .NoError (
35
+ t , err ,
36
+ "construction must succeed with positive number of workers" ,
37
+ )
36
38
37
39
for _ , chanGraph := range chanGraphs {
38
40
graph , cleanup , err := chanGraph .genFunc ()
39
41
success := t .Run (chanGraph .name , func (t1 * testing.T ) {
40
- if err != nil {
41
- t1 .Fatalf ("unable to create graph: %v" , err )
42
- }
42
+ require .NoError (t , err , "unable to create graph" )
43
+
43
44
if cleanup != nil {
44
45
defer cleanup ()
45
46
}
46
47
47
- if err := centralityMetric .Refresh (graph ); err != nil {
48
- t .Fatalf ("unexpected failure during metric refresh: %v" , err )
49
- }
48
+ err := centralityMetric .Refresh (graph )
49
+ require .NoError (t , err )
50
50
51
51
centrality := centralityMetric .GetMetric (false )
52
- if len (centrality ) > 0 {
53
- t .Fatalf ("expected empty metric, got: %v" , len (centrality ))
54
- }
52
+ require .Equal (t , 0 , len (centrality ))
55
53
56
54
centrality = centralityMetric .GetMetric (true )
57
- if len (centrality ) > 0 {
58
- t .Fatalf ("expected empty metric, got: %v" , len (centrality ))
59
- }
60
-
55
+ require .Equal (t , 0 , len (centrality ))
61
56
})
62
57
if ! success {
63
58
break
64
59
}
65
60
}
66
61
}
67
62
68
- // testGraphDesc is a helper type to describe a test graph.
69
- type testGraphDesc struct {
70
- nodes int
71
- edges map [int ][]int
72
- }
73
-
74
- // buildTestGraph builds a test graph from a passed graph desriptor.
75
- func buildTestGraph (t * testing.T ,
76
- graph testGraph , desc testGraphDesc ) map [int ]* btcec.PublicKey {
77
-
78
- nodes := make (map [int ]* btcec.PublicKey )
79
-
80
- for i := 0 ; i < desc .nodes ; i ++ {
81
- key , err := graph .addRandNode ()
82
- if err != nil {
83
- t .Fatalf ("cannot create random node" )
84
- }
85
-
86
- nodes [i ] = key
87
- }
88
-
89
- const chanCapacity = btcutil .SatoshiPerBitcoin
90
- for u , neighbors := range desc .edges {
91
- for _ , v := range neighbors {
92
- _ , _ , err := graph .addRandChannel (nodes [u ], nodes [v ], chanCapacity )
93
- if err != nil {
94
- t .Fatalf ("unexpected error adding random channel: %v" , err )
95
- }
96
- }
97
- }
98
-
99
- return nodes
100
- }
101
-
102
63
// Test betweenness centrality calculating using an example graph.
103
64
func TestBetweennessCentralityWithNonEmptyGraph (t * testing.T ) {
104
- graphDesc := testGraphDesc {
105
- nodes : 9 ,
106
- edges : map [int ][]int {
107
- 0 : {1 , 2 , 3 },
108
- 1 : {2 },
109
- 2 : {3 },
110
- 3 : {4 , 5 },
111
- 4 : {5 , 6 , 7 },
112
- 5 : {6 , 7 },
113
- 6 : {7 , 8 },
114
- },
115
- }
116
-
117
65
workers := []int {1 , 3 , 9 , 100 }
118
66
119
- results := []struct {
67
+ tests := []struct {
120
68
normalize bool
121
69
centrality []float64
122
70
}{
123
71
{
124
- normalize : true ,
125
- centrality : []float64 {
126
- 0.2 , 0.0 , 0.2 , 1.0 , 0.4 , 0.4 , 7.0 / 15.0 , 0.0 , 0.0 ,
127
- },
72
+ normalize : true ,
73
+ centrality : normalizedTestGraphCentrality ,
128
74
},
129
75
{
130
- normalize : false ,
131
- centrality : []float64 {
132
- 3.0 , 0.0 , 3.0 , 15.0 , 6.0 , 6.0 , 7.0 , 0.0 , 0.0 ,
133
- },
76
+ normalize : false ,
77
+ centrality : testGraphCentrality ,
134
78
},
135
79
}
136
80
137
81
for _ , numWorkers := range workers {
138
82
for _ , chanGraph := range chanGraphs {
139
83
numWorkers := numWorkers
140
84
graph , cleanup , err := chanGraph .genFunc ()
141
- if err != nil {
142
- t .Fatalf ("unable to create graph: %v" , err )
143
- }
85
+ require .NoError (t , err , "unable to create graph" )
86
+
144
87
if cleanup != nil {
145
88
defer cleanup ()
146
89
}
147
90
148
- testName := fmt .Sprintf ("%v %d workers" , chanGraph .name , numWorkers )
91
+ testName := fmt .Sprintf (
92
+ "%v %d workers" , chanGraph .name , numWorkers ,
93
+ )
94
+
149
95
success := t .Run (testName , func (t1 * testing.T ) {
150
- centralityMetric , err := NewBetweennessCentralityMetric (
96
+ metric , err := NewBetweennessCentralityMetric (
151
97
numWorkers ,
152
98
)
153
- if err != nil {
154
- t .Fatalf ("construction must succeed with " +
155
- "positive number of workers" )
156
- }
99
+ require .NoError (
100
+ t , err ,
101
+ "construction must succeed with " +
102
+ "positive number of workers" ,
103
+ )
157
104
158
- graphNodes := buildTestGraph (t1 , graph , graphDesc )
159
- if err := centralityMetric .Refresh (graph ); err != nil {
160
- t1 .Fatalf ("error while calculating betweeness centrality" )
161
- }
162
- for _ , expected := range results {
163
- expected := expected
164
- centrality := centralityMetric .GetMetric (expected .normalize )
105
+ graphNodes := buildTestGraph (
106
+ t1 , graph , centralityTestGraph ,
107
+ )
165
108
166
- if len (centrality ) != graphDesc .nodes {
167
- t .Fatalf ("expected %v values, got: %v" ,
168
- graphDesc .nodes , len (centrality ))
169
- }
109
+ err = metric .Refresh (graph )
110
+ require .NoError (t , err )
170
111
171
- for node , nodeCentrality := range expected .centrality {
172
- nodeID := NewNodeID (graphNodes [node ])
173
- calculatedCentrality , ok := centrality [nodeID ]
174
- if ! ok {
175
- t1 .Fatalf ("no result for node: %x (%v)" ,
176
- nodeID , node )
177
- }
178
-
179
- if nodeCentrality != calculatedCentrality {
180
- t1 .Errorf ("centrality for node: %v " +
181
- "should be %v, got: %v" ,
182
- node , nodeCentrality , calculatedCentrality )
183
- }
112
+ for _ , expected := range tests {
113
+ expected := expected
114
+ centrality := metric .GetMetric (
115
+ expected .normalize ,
116
+ )
117
+
118
+ require .Equal (t ,
119
+ centralityTestGraph .nodes ,
120
+ len (centrality ),
121
+ )
122
+
123
+ for i , c := range expected .centrality {
124
+ nodeID := NewNodeID (
125
+ graphNodes [i ],
126
+ )
127
+ result , ok := centrality [nodeID ]
128
+ require .True (t , ok )
129
+ require .Equal (t , c , result )
184
130
}
185
131
}
186
132
})
0 commit comments