2015-02-04 16:10:32 +01:00
|
|
|
package dag
|
|
|
|
|
|
|
|
import (
|
2015-02-09 02:06:17 +01:00
|
|
|
"fmt"
|
2015-02-04 16:29:03 +01:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
2015-02-04 16:10:32 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAcyclicGraphRoot(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
|
|
|
|
if root, err := g.Root(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
} else if root != 3 {
|
|
|
|
t.Fatalf("bad: %#v", root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAcyclicGraphRoot_cycle(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
g.Connect(BasicEdge(2, 3))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
|
|
|
|
if _, err := g.Root(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAcyclicGraphRoot_multiple(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
|
|
|
|
if _, err := g.Root(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 16:29:03 +01:00
|
|
|
|
2015-02-04 16:36:33 +01:00
|
|
|
func TestAcyclicGraphValidate(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
|
|
|
|
if err := g.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAcyclicGraphValidate_cycle(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
g.Connect(BasicEdge(2, 1))
|
|
|
|
|
|
|
|
if err := g.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 18:52:34 +01:00
|
|
|
func TestAcyclicGraphValidate_cycleSelf(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 1))
|
|
|
|
|
|
|
|
if err := g.Validate(); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 16:29:03 +01:00
|
|
|
func TestAcyclicGraphWalk(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
|
|
|
|
var visits []Vertex
|
|
|
|
var lock sync.Mutex
|
2015-02-09 02:06:17 +01:00
|
|
|
err := g.Walk(func(v Vertex) error {
|
2015-02-04 16:29:03 +01:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
visits = append(visits, v)
|
2015-02-09 02:06:17 +01:00
|
|
|
return nil
|
2015-02-04 16:29:03 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := [][]Vertex{
|
2015-02-05 01:38:38 +01:00
|
|
|
{1, 2, 3},
|
|
|
|
{2, 1, 3},
|
2015-02-04 16:29:03 +01:00
|
|
|
}
|
|
|
|
for _, e := range expected {
|
|
|
|
if reflect.DeepEqual(visits, e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("bad: %#v", visits)
|
|
|
|
}
|
2015-02-09 02:06:17 +01:00
|
|
|
|
|
|
|
func TestAcyclicGraphWalk_error(t *testing.T) {
|
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
g.Connect(BasicEdge(3, 1))
|
|
|
|
|
|
|
|
var visits []Vertex
|
|
|
|
var lock sync.Mutex
|
|
|
|
err := g.Walk(func(v Vertex) error {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
if v == 2 {
|
|
|
|
return fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
visits = append(visits, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := [][]Vertex{
|
|
|
|
{1},
|
|
|
|
}
|
|
|
|
for _, e := range expected {
|
|
|
|
if reflect.DeepEqual(visits, e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("bad: %#v", visits)
|
|
|
|
}
|