-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathacyclic_test.go
75 lines (60 loc) · 1.46 KB
/
acyclic_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
package gograph
import (
"reflect"
"testing"
)
func TestTopologySort(t *testing.T) {
// Create a dag with 6 vertices and 6 edges
g := New[int](Acyclic())
if !g.IsDirected() {
t.Error(testErrMsgNotTrue)
}
if !g.IsAcyclic() {
t.Error(testErrMsgNotTrue)
}
v1 := g.AddVertexByLabel(1)
v2 := g.AddVertexByLabel(2)
v3 := g.AddVertexByLabel(3)
v4 := g.AddVertexByLabel(4)
v5 := g.AddVertexByLabel(5)
v6 := g.AddVertexByLabel(6)
_, err := g.AddEdge(v1, v2)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v2, v3)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v2, v4)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v2, v5)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v3, v5)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v4, v6)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
_, err = g.AddEdge(v5, v6)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Perform a topological sort
sortedVertices, err := TopologySort[int](g)
// Check that there was no error
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Check that the sorted order is correct
expectedOrder := []*Vertex[int]{v1, v2, v3, v4, v5, v6}
if !reflect.DeepEqual(sortedVertices, expectedOrder) {
t.Errorf("unexpected sort order. Got %v, expected %v", sortedVertices, expectedOrder)
}
}