forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarjan_test.go
86 lines (73 loc) · 1.62 KB
/
tarjan_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 dag
import (
"sort"
"strings"
"testing"
)
func TestGraphStronglyConnected(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 1))
actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g)))
expected := strings.TrimSpace(testGraphStronglyConnectedStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphStronglyConnected_two(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 1))
g.Add(3)
actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g)))
expected := strings.TrimSpace(testGraphStronglyConnectedTwoStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphStronglyConnected_three(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 1))
g.Add(3)
g.Add(4)
g.Add(5)
g.Add(6)
g.Connect(BasicEdge(4, 5))
g.Connect(BasicEdge(5, 6))
g.Connect(BasicEdge(6, 4))
actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g)))
expected := strings.TrimSpace(testGraphStronglyConnectedThreeStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func testSCCStr(list [][]Vertex) string {
var lines []string
for _, vs := range list {
result := make([]string, len(vs))
for i, v := range vs {
result[i] = VertexName(v)
}
sort.Strings(result)
lines = append(lines, strings.Join(result, ","))
}
sort.Strings(lines)
return strings.Join(lines, "\n")
}
const testGraphStronglyConnectedStr = `1,2`
const testGraphStronglyConnectedTwoStr = `
1,2
3
`
const testGraphStronglyConnectedThreeStr = `
1,2
3
4,5,6
`