-
Notifications
You must be signed in to change notification settings - Fork 10
/
topological_sort_test.go
113 lines (103 loc) · 2.78 KB
/
topological_sort_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package graphs
import (
"testing"
)
func TestTopologicalSort_SimpleGraph(t *testing.T) {
graph := NewDigraph[int]()
graph.AddEdge(1, 3, 0)
graph.AddEdge(1, 2, 0)
topOrder, topClasses, noDAGError := TopologicalSort(graph)
if noDAGError != nil {
t.Errorf("Error was thrown: %v", noDAGError)
}
if topOrder.Len() != 3 {
t.Log("Topological order should have 3 items")
t.Error("Topological order should have 3 items")
}
// check if the topological class assigned to each node is correct
if len(topClasses) != 3 {
t.Log("We don't have information about each node.")
t.Error("We don't have information about each node.")
}
if topClasses[1] != 0 {
t.Error("Node 1 has wrong class")
}
if topClasses[2] != 1 {
t.Error("Node 2 has wrong class")
}
if topClasses[3] != 1 {
t.Error("Node 3 has wrong class")
}
// check if the topological ordering is correct e := topOrder.Front()
e := topOrder.Front()
if e.Value.(int) != 1 {
t.Error("First node shuld be: 1")
}
e = e.Next()
if e.Value.(int) != 2 && e.Value.(int) != 3 {
t.Error("Second node shuld be: 2 or 3")
}
e = e.Next()
if e.Value.(int) != 2 && e.Value.(int) != 3 {
t.Error("Third node shuld be: 2 or 3")
}
}
func TestTopologicalSort_Case2(t *testing.T) {
graph := NewDigraph[int]()
graph.AddEdge(1, 2, 0)
graph.AddEdge(1, 3, 0)
graph.AddEdge(2, 4, 0)
graph.AddEdge(3, 4, 0)
topOrder, topClasses, noDAGError := TopologicalSort(graph)
if noDAGError != nil {
t.Errorf("Error was thrown: %v", noDAGError)
}
if topOrder.Len() != 4 {
t.Log("Topological order should have 4 items")
t.Error("Topological order should have 4 items")
}
// check if the topological class assigned to each node is correct
if len(topClasses) != 4 {
t.Log("We don't have information about each node.")
t.Error("We don't have information about each node.")
}
if topClasses[1] != 0 {
t.Error("Node 1 has wrong class")
}
if topClasses[2] != 1 {
t.Error("Node 2 has wrong class")
}
if topClasses[3] != 1 {
t.Error("Node 3 has wrong class")
}
if topClasses[4] != 2 {
t.Error("Node 4 has wrong class")
}
// check if the topological ordering is correct
e := topOrder.Front()
if e.Value.(int) != 1 {
t.Error("First node should be: 1")
}
e = e.Next()
if e.Value.(int) != 2 && e.Value.(int) != 3 {
t.Error("Second node should be: 2 or 3")
}
e = e.Next()
if e.Value.(int) != 2 && e.Value.(int) != 3 {
t.Error("Third node should be: 2 or 3")
}
e = e.Next()
if e.Value.(int) != 4 {
t.Error("Fourth node should be: 4")
}
}
func TestTopologicalSort_NotDAG(t *testing.T) {
graph := NewDigraph[int]()
graph.AddEdge(1, 2, 0)
graph.AddEdge(1, 3, 0)
graph.AddEdge(2, 1, 0)
_, _, noDagError := TopologicalSort(graph)
if noDagError != ErrNoDAG {
t.Error("This graph is not a DAG. TopologicalSort should have returned an Error.")
}
}