Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Add test suite for dfs.Search
Browse files Browse the repository at this point in the history
also make travis run recursively with gocheck
  • Loading branch information
sdboyer committed Mar 21, 2014
1 parent 1027a5c commit 2525278
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ go:
install:
- go get -t launchpad.net/gocheck
- go get github.com/fatih/set
script: go test -v ./... -gocheck.v
17 changes: 10 additions & 7 deletions dfs/dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func Search(g gogl.Graph, target gogl.Vertex, start gogl.Vertex) (path []gogl.Ve
w := walker{
vis: visitor,
g: g,
// TODO is there ANY way to do this more efficiently without mutating/coloring the vertex objects directly? this is horribly slow
// TODO is there ANY way to do this more efficiently without mutating/coloring the vertex objects directly? this means lots of hashtable lookups
colors: make(map[gogl.Vertex]uint),
target: target,
}

w.dfsearch(start)

return path, nil
return visitor.getPath(), nil
}

// Performs a depth-first search on the provided graph for the given vertex, using the vertices
Expand Down Expand Up @@ -220,9 +220,12 @@ func (sv *searchVisitor) OnFinishVertex(vertex gogl.Vertex) {
sv.stack.pop()
}

func (sv *searchVisitor) getPath()[] gogl.Vertex {
func (sv *searchVisitor) getPath() []gogl.Vertex {
path := make([]gogl.Vertex, 0, sv.stack.length())
for vertex := sv.stack.pop; vertex != nil; vertex = sv.stack.pop {

stacklen := sv.stack.length()
for i := 0; i < stacklen; i++ {
vertex := sv.stack.pop()
path = append(path, vertex)
}

Expand Down Expand Up @@ -327,14 +330,14 @@ func (w *walker) dfsearch(v gogl.Vertex) {
if color == grey {
w.vis.OnBackEdge(v)
} else if color == white {
w.visiting[v] = struct{}{}
w.colors[v] = grey
w.vis.OnStartVertex(v)

w.g.EachAdjacent(v, func(to gogl.Vertex) {
// no more new visits if complete
if !w.complete {
w.vis.OnExamineEdge(gogl.BaseEdge{v, to})
w.dfsearch(v)
w.dfsearch(to)
}
})
// escape hatch
Expand All @@ -343,7 +346,7 @@ func (w *walker) dfsearch(v gogl.Vertex) {
}

w.vis.OnFinishVertex(v)
w.visited[v] = struct{}{}
w.colors[v] = black
}
}

Expand Down
20 changes: 20 additions & 0 deletions dfs/dfs_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package dfs

import (
"fmt"
"testing"

"github.com/sdboyer/gogl"
. "launchpad.net/gocheck"
)

// Hook gocheck into the go test runner
func Test(t *testing.T) { TestingT(t) }

var dfEdgeSet = []gogl.Edge{
&gogl.BaseEdge{"foo", "bar"},
&gogl.BaseEdge{"bar", "baz"},
&gogl.BaseEdge{"baz", "qux"},
}

type DepthFirstSearchSuite struct {
}

var _ = Suite(&DepthFirstSearchSuite{})

// Basic test of outermost search functionality.
func (s *DepthFirstSearchSuite) TestSearch(c *C) {
g := gogl.NewDirected()
g.AddEdges(dfEdgeSet...)

path, err := Search(g, "qux", "bar")
c.Assert(fmt.Sprint(path), Equals, fmt.Sprint([]gogl.Vertex{"qux", "baz", "bar"}))
c.Assert(err, IsNil)
}

func sliceEquals(a, b []gogl.Vertex) bool {
if len(a) != len(b) {
return false
Expand Down

0 comments on commit 2525278

Please sign in to comment.