-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
174 lines (136 loc) · 3.1 KB
/
graph_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// © 2025 Rolf van de Krol <[email protected]>
package graph
import (
"reflect"
"testing"
)
func TestSimpleSort(t *testing.T) {
g := New[string]()
// We construct a graph with the following structure: a -> b -> c.
// The expected topological sort is [a b c]. We explicitly add the nodes in
// another order to ensure that the sort is not just returning the input.
g.Node("c")
g.Node("b")
g.Node("a")
g.Edge("a", "b")
g.Edge("b", "c")
keys, err := g.Sort()
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(keys, []string{"a", "b", "c"}) {
t.Errorf("expected [a b c], got %v", keys)
}
}
func TestCyclicSort(t *testing.T) {
g := New[string]()
// We construct a graph with the following structure: a -> b -> c -> a.
// The graph has a cycle, so the sort should return an error.
g.Node("c")
g.Node("b")
g.Node("a")
g.Edge("a", "b")
g.Edge("b", "c")
g.Edge("c", "a")
_, err := g.Sort()
if err == nil {
t.Error("expected error")
return
}
}
func TestComplexSort(t *testing.T) {
g := New[string]()
// We construct a graph with the following structure: a -> b, c -> b.
// The expected topological sort is [c a b]. We explicitly add the nodes in
// another order to ensure that the sort is not just returning the input.
g.Node("c")
g.Node("b")
g.Node("a")
g.Edge("a", "b")
g.Edge("c", "b")
keys, err := g.Sort()
if err != nil {
t.Error(err)
return
}
if len(keys) != 3 {
t.Errorf("expected 3, got %v", len(keys))
return
}
if !(reflect.DeepEqual(keys, []string{"a", "c", "b"}) || reflect.DeepEqual(keys, []string{"c", "a", "b"})) {
t.Errorf("expected [a c b] or [c a b], got %v", keys)
}
}
// buildGraph creates a graph with a specific structure. It is used to benchmark
// the topological sort algorithm.
// It generates a directed graph with n int nodes, and 2n edges. The graph is
// constructed in such a way that it has a topological sort.
func buildGraph(n int) *Graph[int] {
g := New[int]()
for i := 0; i < n; i++ {
g.Node(i)
x := i % 5
if x == 0 {
continue
}
y := i / x
z := i % y
for j := 0; j < x; j++ {
g.Edge(i, j*y+z)
}
}
return g
}
// TestBuildGraph tests the buildGraph function. It ensures that the generated
// graph has a topological sort.
func TestBuildGraph(t *testing.T) {
g := buildGraph(1000)
_, err := g.Sort()
if err != nil {
t.Error(err)
return
}
}
func BenchmarkSort5(b *testing.B) {
g := buildGraph(5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}
func BenchmarkSort10(b *testing.B) {
g := buildGraph(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}
func BenchmarkSort100(b *testing.B) {
g := buildGraph(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}
func BenchmarkSort1000(b *testing.B) {
g := buildGraph(1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}
func BenchmarkSort10000(b *testing.B) {
g := buildGraph(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}
func BenchmarkSort100000(b *testing.B) {
g := buildGraph(100000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.Sort()
}
}