-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbellmanford_test.go
50 lines (41 loc) · 970 Bytes
/
bellmanford_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
package graphs
import (
"testing"
)
func TestBellmanFord(t *testing.T) {
graph := NewDigraph[string]()
graph.AddEdge("a", "b", 1)
graph.AddEdge("a", "c", 3)
graph.AddEdge("b", "g", 5)
graph.AddEdge("c", "g", 8)
graph.AddEdge("g", "h", 6)
graph.AddEdge("c", "d", -2)
graph.AddEdge("g", "f", 4)
graph.AddEdge("d", "f", 3)
graph.AddEdge("d", "e", 5)
path := BellmanFord(graph, "a", "e")
if path == nil {
t.Error("no result")
t.FailNow()
}
result := []string{"a", "c", "d", "e"}
if len(path) != len(result) {
t.Error("bad result")
t.FailNow()
}
for i, v := range path {
if v != result[i] {
t.Errorf("bad vertex in path at index %d", i)
}
}
}
func TestBellmanFordNegWeightCycle(t *testing.T) {
graph := NewDigraph[string]()
graph.AddEdge("a", "b", 6)
graph.AddEdge("a", "c", 3)
graph.AddEdge("c", "a", -4)
path := BellmanFord(graph, "a", "b")
if path != nil {
t.Error("should return no result (negative weight cycle)")
}
}