-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathdijkstra_test.go
51 lines (47 loc) · 928 Bytes
/
dijkstra_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
package graph
import (
"testing"
)
var tc_dijkstra = []struct {
name string
edges [][]int
node0 int
node1 int
expected int
}{
{
"straight line graph",
[][]int{{0, 1, 5}, {1, 2, 2}},
0, 2, 7,
},
{
"unconnected node",
[][]int{{0, 1, 5}},
0, 2, -1,
},
{
"double paths",
[][]int{{0, 1, 5}, {1, 3, 5}, {0, 2, 5}, {2, 3, 4}},
0, 3, 9,
},
{
"double paths extended",
[][]int{{0, 1, 5}, {1, 3, 5}, {0, 2, 5}, {2, 3, 4}, {3, 4, 1}},
0, 4, 10,
},
}
func TestDijkstra(t *testing.T) {
for _, tc := range tc_dijkstra {
t.Run(tc.name, func(t *testing.T) {
var graph Graph
for _, edge := range tc.edges {
graph.AddWeightedEdge(edge[0], edge[1], edge[2])
}
actual, _ := graph.Dijkstra(tc.node0, tc.node1)
if actual != tc.expected {
t.Errorf("expected %d, got %d, from node %d to %d, with %v",
tc.expected, actual, tc.node0, tc.node1, tc.edges)
}
})
}
}