| 5 | ) |
| 6 | |
| 7 | func TestHasCycle(t *testing.T) { |
| 8 | graph := Graph{Directed: true} |
| 9 | edges := [][]int{{0, 1}, {1, 2}, {2, 0}, {4, 0}} |
| 10 | for _, edge := range edges { |
| 11 | graph.AddEdge(edge[0], edge[1]) |
| 12 | } |
| 13 | if !graph.HasCycle() { |
| 14 | t.Error("answer of hasCycle is not correct") |
| 15 | } |
| 16 | |
| 17 | graph = Graph{Directed: true} |
| 18 | edges = [][]int{{0, 1}, {1, 2}, {2, 6}, {4, 0}} |
| 19 | for _, edge := range edges { |
| 20 | graph.AddEdge(edge[0], edge[1]) |
| 21 | } |
| 22 | if graph.HasCycle() { |
| 23 | t.Error("answer of hasCycle is not correct") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestFindAllCycles(t *testing.T) { |
| 28 | graph := Graph{Directed: true} |