-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
cycle_test.go
53 lines (46 loc) · 1.21 KB
/
cycle_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
package graph
import (
"testing"
)
func TestHasCycle(t *testing.T) {
graph := Graph{Directed: true}
edges := [][]int{{0, 1}, {1, 2}, {2, 0}, {4, 0}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
if !graph.HasCycle() {
t.Error("answer of hasCycle is not correct")
}
graph = Graph{Directed: true}
edges = [][]int{{0, 1}, {1, 2}, {2, 6}, {4, 0}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
if graph.HasCycle() {
t.Error("answer of hasCycle is not correct")
}
}
func TestFindAllCycles(t *testing.T) {
graph := Graph{Directed: true}
edges := [][]int{{0, 4}, {1, 3}, {2, 3}, {3, 4}, {4, 7}, {5, 2}, {6, 3}, {7, 3}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
res := graph.FindAllCycles()
if len(res) != 1 {
t.Error("number of cycles is not correct")
}
firstCycle := res[0]
if len(firstCycle.edges) != 3 {
t.Error("number of vertex in cycle is not correct")
}
if _, ok := firstCycle.edges[3][4]; !ok {
t.Error("connection in cycle is not correct")
}
if _, ok := firstCycle.edges[4][7]; !ok {
t.Error("connection in cycle is not correct")
}
if _, ok := firstCycle.edges[7][3]; !ok {
t.Error("connection in cycle is not correct")
}
}