-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_test.go
86 lines (70 loc) · 2.13 KB
/
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
package go_directed_acyclic_graph
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDFSSorter(t *testing.T) {
graph := NewDirectedGraph()
graph.AddNodes(0, 1, 2, 3, 4, 5, 6, 7)
graph.AddEdge(0, 2)
graph.AddEdge(1, 2)
graph.AddEdge(1, 5)
graph.AddEdge(1, 6)
graph.AddEdge(2, 5)
graph.AddEdge(3, 5)
graph.AddEdge(5, 6)
graph.AddEdge(5, 7)
sorted, err := graph.DFSSort()
assert.NoError(t, err, "graph.DFSSort() error should be nil")
assert.Equal(t, []Node{4, 3, 1, 0, 2, 5, 7, 6}, sorted, "graph.DFSSort() nodes should equal [4, 3, 1, 0, 2, 5, 7, 6]")
}
func TestDFSSorterCyclic(t *testing.T) {
graph := NewDirectedGraph()
graph.AddNodes(0, 1)
graph.AddEdge(0, 1)
graph.AddEdge(1, 0)
sorted, err := graph.DFSSort()
assert.EqualError(t, ErrCyclicGraph, err.Error(), "graph.DFSSort() error should be ErrCyclicGraph")
assert.Nil(t, sorted, "graph.DFSSort() nodes should be nil")
}
func TestCoffmanGrahamSorter(t *testing.T) {
graph := NewDirectedGraph()
graph.AddNodes(0, 1, 2, 3, 4, 5, 6, 7, 8)
graph.AddEdge(0, 2)
graph.AddEdge(0, 5)
graph.AddEdge(1, 2)
graph.AddEdge(2, 3)
graph.AddEdge(2, 4)
graph.AddEdge(3, 6)
graph.AddEdge(4, 6)
graph.AddEdge(5, 7)
graph.AddEdge(6, 7)
graph.AddEdge(6, 8)
sorted, err := graph.CoffmanGrahamSort(2)
assert.NoError(t, err, "graph.CoffmanGrahamSort(2)0 error should be nil")
assert.Equal(t, [][]Node{
[]Node{1, 0},
[]Node{5, 2},
[]Node{4, 3},
[]Node{6},
[]Node{8, 7},
}, sorted, "graph.CoffmanGrahamSort(2) nodes should equal [[1, 0], [5, 2], [4, 3], [6], [8, 7]]")
}
func TestCoffmanGrahamSorterCyclic(t *testing.T) {
graph := NewDirectedGraph()
graph.AddNodes(0, 1, 2, 3, 4, 5, 6, 7, 8)
graph.AddEdge(0, 2)
graph.AddEdge(0, 5)
graph.AddEdge(1, 2)
graph.AddEdge(2, 0) // cyclic edge
graph.AddEdge(2, 3)
graph.AddEdge(2, 4)
graph.AddEdge(3, 6)
graph.AddEdge(4, 6)
graph.AddEdge(5, 7)
graph.AddEdge(6, 7)
graph.AddEdge(6, 8)
sorted, err := graph.CoffmanGrahamSort(2)
assert.EqualError(t, ErrCyclicGraph, err.Error(), "graph.CoffmanGrahamSort(2) error should be ErrCyclicGraph")
assert.Nil(t, sorted, "graph.CoffmanGrahamSort(2) nodes should be nil")
}