86 lines
1.6 KiB
Go
86 lines
1.6 KiB
Go
package dag
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"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 buf bytes.Buffer
|
|
for _, vs := range list {
|
|
result := make([]string, len(vs))
|
|
for i, v := range vs {
|
|
result[i] = vertName(v)
|
|
}
|
|
|
|
buf.WriteString(fmt.Sprintf("%s\n", strings.Join(result, ",")))
|
|
}
|
|
|
|
return buf.String()
|
|
}
|
|
|
|
const testGraphStronglyConnectedStr = `2,1`
|
|
|
|
const testGraphStronglyConnectedTwoStr = `
|
|
2,1
|
|
3
|
|
`
|
|
|
|
const testGraphStronglyConnectedThreeStr = `
|
|
2,1
|
|
3
|
|
6,5,4
|
|
`
|