-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
pagerank_test.go
132 lines (104 loc) · 2.56 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
package pagerank
import (
"reflect"
"testing"
)
func TestEmpty(t *testing.T) {
graph := NewGraph()
actual := map[uint32]float64{}
expected := map[uint32]float64{}
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})
if reflect.DeepEqual(actual, expected) != true {
t.Error("Expected", expected, "but got", actual)
}
}
func TestSimple(t *testing.T) {
graph := NewGraph()
graph.Link(1, 2, 1.0)
graph.Link(1, 3, 1.0)
graph.Link(2, 3, 1.0)
graph.Link(2, 4, 1.0)
graph.Link(3, 1, 1.0)
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.32721836185043207,
2: 0.2108699481253495,
3: 0.3004897566512289,
4: 0.16142193337298952,
}
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})
if reflect.DeepEqual(actual, expected) != true {
t.Error("Expected", expected, "but got", actual)
}
}
func TestWeighted(t *testing.T) {
graph := NewGraph()
graph.Link(1, 2, 1.0)
graph.Link(1, 3, 2.0)
graph.Link(2, 3, 3.0)
graph.Link(2, 4, 4.0)
graph.Link(3, 1, 5.0)
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.34983779905464363,
2: 0.1688733284604475,
3: 0.3295121849483849,
4: 0.15177668753652385,
}
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})
if reflect.DeepEqual(actual, expected) != true {
t.Error("Expected", expected, "but got", actual)
}
}
func TestDuplicates(t *testing.T) {
graph := NewGraph()
graph.Link(1, 2, 1.0)
graph.Link(1, 3, 2.0)
graph.Link(2, 3, 3.0)
graph.Link(2, 4, 4.0)
graph.Link(3, 1, 5.0)
graph.Link(1, 2, 6.0)
graph.Link(1, 3, 7.0)
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.3312334209098247,
2: 0.19655848316544225,
3: 0.3033555769882879,
4: 0.168852518936445,
}
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})
if reflect.DeepEqual(actual, expected) != true {
t.Error("Expected", expected, "but got", actual)
}
}
func TestDuplicatesAfterReset(t *testing.T) {
graph := NewGraph()
graph.Link(1, 2, 1.0)
graph.Link(1, 3, 2.0)
graph.Link(2, 3, 3.0)
graph.Link(2, 4, 4.0)
graph.Link(3, 1, 5.0)
graph.Reset()
graph.Link(1, 2, 6.0)
graph.Link(1, 3, 7.0)
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.25974019022001016,
2: 0.3616383883769191,
3: 0.3786214214030706,
}
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})
if reflect.DeepEqual(actual, expected) != true {
t.Error("Expected", expected, "but got", actual)
}
}