-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpagerank_test.go
238 lines (198 loc) · 4.71 KB
/
pagerank_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
package pagerank
import (
"fmt"
"math"
"math/rand"
"runtime"
"testing"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func round(f float64) float64 {
return math.Floor(f*10+0.5) / 10
}
func toPercentage(f float64) float64 {
tenPow3 := math.Pow(10, 3)
return round(100 * (f * tenPow3) / tenPow3)
}
func assertRank(t *testing.T, pageRank Interface, expected map[int]float64) {
const tolerance = 0.0001
pageRank.Rank(0.85, tolerance, func(label int, rank float64) {
rankAsPercentage := toPercentage(rank)
if math.Abs(rankAsPercentage-expected[label]) > tolerance {
t.Error("Rank for", label, "should be", expected[label], "but was", rankAsPercentage)
}
})
}
func assertEqual(t *testing.T, actual, expected interface{}) {
if actual != expected {
t.Error("Should be", expected, "but was", actual)
}
}
func assert(t *testing.T, actual bool) {
if !actual {
t.Error("Should be true")
}
}
func TestRound(t *testing.T) {
assertEqual(t, round(0.6666666), 0.7)
}
func TestRankToPercentage(t *testing.T) {
assertEqual(t, toPercentage(0.6666666), 66.7)
}
func TestShouldEnterTheBlock(t *testing.T) {
pageRank := New()
pageRank.Link(0, 1)
entered := false
pageRank.Rank(0.85, 0.0001, func(_ int, _ float64) {
entered = true
})
assert(t, entered)
}
func TestShouldBePossibleToRecalculateTheRanksAfterANewLinkIsAdded(t *testing.T) {
pageRank := New()
pageRank.Link(0, 1)
assertRank(t, pageRank, map[int]float64{0: 35.1, 1: 64.9})
pageRank.Link(1, 2)
assertRank(t, pageRank, map[int]float64{0: 18.4, 1: 34.1, 2: 47.4})
}
func TestShouldBePossibleToClearTheGraph(t *testing.T) {
pageRank := New()
pageRank.Link(0, 1)
pageRank.Link(1, 2)
pageRank.Clear()
pageRank.Link(0, 1)
assertRank(t, pageRank, map[int]float64{0: 35.1, 1: 64.9})
}
func TestShouldNotFailWhenCalculatingTheRankOfAnEmptyGraph(t *testing.T) {
pageRank := New()
pageRank.Rank(0.85, 0.0001, func(label int, rank float64) {
t.Error("This should not be seen")
})
}
func TestShouldReturnCorrectResultsWhenHavingADanglingNode(t *testing.T) {
pageRank := New()
//node 2 is a dangling node because it has no outbound links
pageRank.Link(0, 2)
pageRank.Link(1, 2)
expectedRank := map[int]float64{
0: 21.3,
1: 21.3,
2: 57.4,
}
assertRank(t, pageRank, expectedRank)
}
func TestShouldNotChangeTheGraphWhenAddingTheSameLinkManyTimes(t *testing.T) {
pageRank := New()
pageRank.Link(0, 2)
pageRank.Link(0, 2)
pageRank.Link(0, 2)
pageRank.Link(1, 2)
pageRank.Link(1, 2)
expectedRank := map[int]float64{
0: 21.3,
1: 21.3,
2: 57.4,
}
assertRank(t, pageRank, expectedRank)
}
func TestShouldReturnCorrectResultsForAStarGraph(t *testing.T) {
pageRank := New()
pageRank.Link(0, 2)
pageRank.Link(1, 2)
pageRank.Link(2, 2)
expectedRank := map[int]float64{
0: 5,
1: 5,
2: 90,
}
assertRank(t, pageRank, expectedRank)
}
func TestShouldBeUniformForACircularGraph(t *testing.T) {
pageRank := New()
pageRank.Link(0, 1)
pageRank.Link(1, 2)
pageRank.Link(2, 3)
pageRank.Link(3, 4)
pageRank.Link(4, 0)
expectedRank := map[int]float64{
0: 20,
1: 20,
2: 20,
3: 20,
4: 20,
}
assertRank(t, pageRank, expectedRank)
}
func TestShouldReturnCorrectResultsForAConvergingGraph(t *testing.T) {
pageRank := New()
pageRank.Link(0, 1)
pageRank.Link(0, 2)
pageRank.Link(1, 2)
pageRank.Link(2, 2)
expectedRank := map[int]float64{
0: 5,
1: 7.1,
2: 87.9,
}
assertRank(t, pageRank, expectedRank)
}
func TestShouldCorrectlyReproduceTheWikipediaExample(t *testing.T) {
//http://en.wikipedia.org/wiki/File:PageRanks-Example.svg
pageRank := New()
pageRank.Link(1, 2)
pageRank.Link(2, 1)
pageRank.Link(3, 0)
pageRank.Link(3, 1)
pageRank.Link(4, 3)
pageRank.Link(4, 1)
pageRank.Link(4, 5)
pageRank.Link(5, 4)
pageRank.Link(5, 1)
pageRank.Link(6, 1)
pageRank.Link(6, 4)
pageRank.Link(7, 1)
pageRank.Link(7, 4)
pageRank.Link(8, 1)
pageRank.Link(8, 4)
pageRank.Link(9, 4)
pageRank.Link(10, 4)
expectedRank := map[int]float64{
0: 3.3, //a
1: 38.4, //b
2: 34.3, //c
3: 3.9, //d
4: 8.1, //e
5: 3.9, //f
6: 1.6, //g
7: 1.6, //h
8: 1.6, //i
9: 1.6, //j
10: 1.6, //k
}
assertRank(t, pageRank, expectedRank)
}
func BenchmarkOneHundredThousand(b *testing.B) {
n := 100_000
pageRank := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for from := 0; from < n; from++ {
for j := 0; j < rand.Intn(400); j++ {
too := rand.Intn(n)
to := too
if too > 80000 {
to = rand.Intn(3)
}
pageRank.Link(from, to)
}
}
}
result := make([]float64, n)
pageRank.Rank(0.85, 0.001, func(key int, val float64) {
result[key] = val
})
fmt.Println("5 first values are", result[0], ",", result[1], ",", result[2], ",", result[3], ",", result[4], " Size:", len(result))
pageRank.Clear()
}