-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_test.go
65 lines (54 loc) · 1.44 KB
/
path_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
package graph
import "testing"
func TestFindShortestPaths(t *testing.T) {
g := NewGraph()
a := g.NewVertex("a")
b := g.NewVertex("b")
c := g.NewVertex("c")
d := g.NewVertex("d")
a.AddEdge(b, int64(1))
a.AddEdge(c, int64(2))
b.AddEdge(a, int64(1))
b.AddEdge(c, int64(3))
b.AddEdge(d, int64(1))
c.AddEdge(a, int64(1))
c.AddEdge(b, int64(3))
c.AddEdge(d, int64(3))
d.AddEdge(b, int64(1))
d.AddEdge(c, int64(1))
// Find the shortest paths to vertex a
pm := g.FindShortestPaths(a)
// Check that the PathInfo for d is valid
pathToD := pm[d]
if pathToD == nil {
t.Fatalf("no shortest path info to vertex d\n")
}
if pathToD.dist != 2 {
t.Fatalf("shortest path dist to d incorrect (was %d)\n", pathToD.dist)
}
// Check that the path back to d is valid
vlist := []*Vertex{d, b, a}
pi := pm[vlist[0]]
for i, v := range vlist {
if pi == nil {
t.Fatalf("Path for vertex %s on shortest path is nil\n", v.Value.(string))
}
if pi.vertex != v {
t.Fatalf("Path for vertex %s does not map to correct vertex\n", v.Value.(string))
}
// Check source vertex's
if i == len(vlist)-1 {
if pi.prev != nil {
t.Errorf("Path of source vertex does not end chain\n")
}
if pi.dist != 0 {
t.Errorf("Path of source vertex does not have distance 0 (actual %d)\n", pi.dist)
}
} else {
if pi.prev == nil {
t.Errorf("Path for vertex %s does not link to previous vertex\n", v.Value.(string))
}
pi = pi.prev
}
}
}