-
Notifications
You must be signed in to change notification settings - Fork 2
/
gorder_test.go
53 lines (46 loc) · 1.17 KB
/
gorder_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 gorder
import "testing"
func TestTopologicalSort(t *testing.T) {
digraph := map[interface{}][]interface{}{
1: []interface{}{2, 4},
2: []interface{}{3, 5},
3: []interface{}{4, 5},
}
want := []int{1, 2, 3, 5, 4}
_, err := TopologicalSort(digraph, "ultrasuperfast")
if err == nil {
t.Fatal("TopologicalSort: should have returned an error")
}
o, err := TopologicalSort(digraph, "kahn")
if err != nil {
t.Fatalf("Kahn: %v", err)
}
for i, v := range o {
if v != want[i] {
t.Fatal("Kahn: output order does not match the desired order")
}
}
o, err = TopologicalSort(digraph, "dfsbased")
if err != nil {
t.Fatalf("DFS-based: %v", err)
}
for i, v := range o {
if v != want[i] {
t.Fatal("DFS-based: output order does not match the desired order")
}
}
graphWithCycles := map[interface{}][]interface{}{
1: []interface{}{2, 4},
2: []interface{}{3, 5},
3: []interface{}{4, 5},
4: []interface{}{2},
}
_, err = TopologicalSort(graphWithCycles, "kahn")
if err == nil {
t.Fatal("Kahn: should have returned an error")
}
_, err = TopologicalSort(graphWithCycles, "dfsbased")
if err == nil {
t.Fatal("DFS-based: should have returned an error")
}
}