-
Notifications
You must be signed in to change notification settings - Fork 11
/
toposort_test.go
83 lines (65 loc) · 1.37 KB
/
toposort_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package toposort
import "testing"
func index(s []string, v string) int {
for i, s := range s {
if s == v {
return i
}
}
return -1
}
type Edge struct {
From string
To string
}
func TestDuplicatedNode(t *testing.T) {
graph := NewGraph(2)
graph.AddNode("a")
if graph.AddNode("a") {
t.Errorf("not raising duplicated node error")
}
}
func TestRemoveNotExistEdge(t *testing.T) {
graph := NewGraph(0)
if graph.RemoveEdge("a", "b") {
t.Errorf("not raising not exist edge error")
}
}
func TestWikipedia(t *testing.T) {
graph := NewGraph(8)
graph.AddNodes("2", "3", "5", "7", "8", "9", "10", "11")
edges := []Edge{
{"7", "8"},
{"7", "11"},
{"5", "11"},
{"3", "8"},
{"3", "10"},
{"11", "2"},
{"11", "9"},
{"11", "10"},
{"8", "9"},
}
for _, e := range edges {
graph.AddEdge(e.From, e.To)
}
result, ok := graph.Toposort()
if !ok {
t.Errorf("closed path detected in no closed pathed graph")
}
for _, e := range edges {
if i, j := index(result, e.From), index(result, e.To); i > j {
t.Errorf("dependency failed: not satisfy %v(%v) > %v(%v)", e.From, i, e.To, j)
}
}
}
func TestCycle(t *testing.T) {
graph := NewGraph(3)
graph.AddNodes("1", "2", "3")
graph.AddEdge("1", "2")
graph.AddEdge("2", "3")
graph.AddEdge("3", "1")
_, ok := graph.Toposort()
if ok {
t.Errorf("closed path not detected in closed pathed graph")
}
}