2015-01-24 01:42:06 +01:00
|
|
|
package dag
|
|
|
|
|
|
|
|
import (
|
2015-01-30 21:56:03 +01:00
|
|
|
"sort"
|
2015-01-24 01:42:06 +01:00
|
|
|
"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 {
|
2015-01-30 21:56:03 +01:00
|
|
|
var lines []string
|
2015-01-24 01:42:06 +01:00
|
|
|
for _, vs := range list {
|
|
|
|
result := make([]string, len(vs))
|
|
|
|
for i, v := range vs {
|
2015-01-28 06:48:46 +01:00
|
|
|
result[i] = VertexName(v)
|
2015-01-24 01:42:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
sort.Strings(result)
|
|
|
|
lines = append(lines, strings.Join(result, ","))
|
2015-01-24 01:42:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
sort.Strings(lines)
|
|
|
|
return strings.Join(lines, "\n")
|
2015-01-24 01:42:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
const testGraphStronglyConnectedStr = `1,2`
|
2015-01-24 01:42:06 +01:00
|
|
|
|
|
|
|
const testGraphStronglyConnectedTwoStr = `
|
2015-01-30 21:56:03 +01:00
|
|
|
1,2
|
2015-01-24 01:42:06 +01:00
|
|
|
3
|
|
|
|
`
|
|
|
|
|
|
|
|
const testGraphStronglyConnectedThreeStr = `
|
2015-01-30 21:56:03 +01:00
|
|
|
1,2
|
2015-01-24 01:42:06 +01:00
|
|
|
3
|
2015-01-30 21:56:03 +01:00
|
|
|
4,5,6
|
2015-01-24 01:42:06 +01:00
|
|
|
`
|