-
Notifications
You must be signed in to change notification settings - Fork 95
/
sort_test.go
102 lines (83 loc) · 1.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package hero
import (
"reflect"
"testing"
)
func TestAddVertices(t *testing.T) {
sort := newSort()
vertices := []string{"a", "b", "c", "d", "e"}
for _, vertex := range vertices {
sort.addVertices(vertex)
}
if len(sort.vertices) != len(vertices) {
t.Fail()
}
for _, vertex := range vertices {
if _, ok := sort.vertices[vertex]; !ok {
t.Fail()
}
}
}
func TestAddEdge(t *testing.T) {
sort := newSort()
edges := map[string][]string{
"a": {"1"},
"b": {"2", "3"},
"c": {"4", "5", "6"},
}
// init graph.
for from, tos := range edges {
for _, to := range tos {
sort.addEdge(from, to)
}
}
if len(sort.graph) != len(edges) {
t.Fail()
}
for from, tos := range edges {
if v, ok := sort.graph[from]; !ok || len(v) != len(tos) {
t.Fail()
}
for _, to := range tos {
if _, ok := sort.graph[from][to]; !ok {
t.Fail()
}
}
}
}
func TestCollect(t *testing.T) {
sort := &sort{
v: map[string]int{
"a": 0,
"b": 1,
"c": 2,
},
}
var queue []string
sort.collect(&queue)
if !reflect.DeepEqual(queue, []string{"a"}) ||
!reflect.DeepEqual(sort.v, map[string]int{"b": 1, "c": 2}) {
t.Fail()
}
}
func TestSort(t *testing.T) {
sort := newSort()
vertices := []string{"a", "b", "c", "d", "e"}
for _, vertex := range vertices {
sort.addVertices(vertex)
}
edges := map[string][]string{
"a": {"b"},
"b": {"c"},
"c": {"d"},
"d": {"e"},
}
for from, tos := range edges {
for _, to := range tos {
sort.addEdge(from, to)
}
}
if !reflect.DeepEqual(sort.sort(), vertices) {
t.Fail()
}
}