Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
path/internal/testgraphs: new package common test graphs factored out
Browse files Browse the repository at this point in the history
This allows us to share common shorted path graphs.
  • Loading branch information
kortschak committed Aug 23, 2015
1 parent cf8c953 commit 202374f
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 1,012 deletions.
43 changes: 22 additions & 21 deletions path/a_star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gonum/graph"
"github.com/gonum/graph/concrete"
"github.com/gonum/graph/path/internal"
"github.com/gonum/graph/path/internal/testgraphs"
"github.com/gonum/graph/topo"
)

Expand Down Expand Up @@ -248,9 +249,9 @@ func isMonotonic(g costEdgeListGraph, h Heuristic) (ok bool, at graph.Edge, goal
}

func TestAStarNullHeuristic(t *testing.T) {
for _, test := range shortestPathTests {
g := test.g()
for _, e := range test.edges {
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetEdge(e)
}

Expand All @@ -263,52 +264,52 @@ func TestAStarNullHeuristic(t *testing.T) {
defer func() {
panicked = recover() != nil
}()
pt, _ = AStar(test.query.From(), test.query.To(), g.(graph.Graph), nil)
pt, _ = AStar(test.Query.From(), test.Query.To(), g.(graph.Graph), nil)
}()
if panicked || test.negative {
if !test.negative {
t.Errorf("%q: unexpected panic", test.name)
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q: unexpected panic", test.Name)
}
if !panicked {
t.Errorf("%q: expected panic for negative edge weight", test.name)
t.Errorf("%q: expected panic for negative edge weight", test.Name)
}
continue
}

if pt.From().ID() != test.query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.query.From().ID())
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.Query.From().ID())
}

p, weight := pt.To(test.query.To())
if weight != test.weight {
p, weight := pt.To(test.Query.To())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}
if weight := pt.WeightTo(test.query.To()); weight != test.weight {
if weight := pt.WeightTo(test.Query.To()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}

var got []int
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.want) == 0
for _, sp := range test.want {
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.name, p, test.want)
test.Name, p, test.WantPaths)
}

np, weight := pt.To(test.none.To())
if pt.From().ID() == test.none.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
np, weight := pt.To(test.NoPathFor.To())
if pt.From().ID() == test.NoPathFor.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.name, np, weight)
test.Name, np, weight)
}
}
}
41 changes: 21 additions & 20 deletions path/bellman_ford_moore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,61 @@ import (
"testing"

"github.com/gonum/graph"
"github.com/gonum/graph/path/internal/testgraphs"
)

func TestBellmanFordFrom(t *testing.T) {
for _, test := range shortestPathTests {
g := test.g()
for _, e := range test.edges {
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetEdge(e)
}

pt, ok := BellmanFordFrom(test.query.From(), g.(graph.Graph))
if test.hasNegativeCycle {
pt, ok := BellmanFordFrom(test.Query.From(), g.(graph.Graph))
if test.HasNegativeCycle {
if ok {
t.Errorf("%q: expected negative cycle", test.name)
t.Errorf("%q: expected negative cycle", test.Name)
}
continue
}
if !ok {
t.Fatalf("%q: unexpected negative cycle", test.name)
t.Fatalf("%q: unexpected negative cycle", test.Name)
}

if pt.From().ID() != test.query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.query.From().ID())
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.Query.From().ID())
}

p, weight := pt.To(test.query.To())
if weight != test.weight {
p, weight := pt.To(test.Query.To())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}
if weight := pt.WeightTo(test.query.To()); weight != test.weight {
if weight := pt.WeightTo(test.Query.To()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}

var got []int
for _, n := range p {
got = append(got, n.ID())
}
ok = len(got) == 0 && len(test.want) == 0
for _, sp := range test.want {
ok = len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.name, p, test.want)
test.Name, p, test.WantPaths)
}

np, weight := pt.To(test.none.To())
if pt.From().ID() == test.none.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
np, weight := pt.To(test.NoPathFor.To())
if pt.From().ID() == test.NoPathFor.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.name, np, weight)
test.Name, np, weight)
}
}
}
95 changes: 48 additions & 47 deletions path/dijkstra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (

"github.com/gonum/graph"
"github.com/gonum/graph/internal"
"github.com/gonum/graph/path/internal/testgraphs"
)

func TestDijkstraFrom(t *testing.T) {
for _, test := range shortestPathTests {
g := test.g()
for _, e := range test.edges {
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetEdge(e)
}

Expand All @@ -30,60 +31,60 @@ func TestDijkstraFrom(t *testing.T) {
defer func() {
panicked = recover() != nil
}()
pt = DijkstraFrom(test.query.From(), g.(graph.Graph))
pt = DijkstraFrom(test.Query.From(), g.(graph.Graph))
}()
if panicked || test.negative {
if !test.negative {
t.Errorf("%q: unexpected panic", test.name)
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q: unexpected panic", test.Name)
}
if !panicked {
t.Errorf("%q: expected panic for negative edge weight", test.name)
t.Errorf("%q: expected panic for negative edge weight", test.Name)
}
continue
}

if pt.From().ID() != test.query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.query.From().ID())
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", pt.From().ID(), test.Query.From().ID())
}

p, weight := pt.To(test.query.To())
if weight != test.weight {
p, weight := pt.To(test.Query.To())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}
if weight := pt.WeightTo(test.query.To()); weight != test.weight {
if weight := pt.WeightTo(test.Query.To()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}

var got []int
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.want) == 0
for _, sp := range test.want {
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.name, p, test.want)
test.Name, p, test.WantPaths)
}

np, weight := pt.To(test.none.To())
if pt.From().ID() == test.none.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
np, weight := pt.To(test.NoPathFor.To())
if pt.From().ID() == test.NoPathFor.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.name, np, weight)
test.Name, np, weight)
}
}
}

func TestDijkstraAllPaths(t *testing.T) {
for _, test := range shortestPathTests {
g := test.g()
for _, e := range test.edges {
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetEdge(e)
}

Expand All @@ -98,59 +99,59 @@ func TestDijkstraAllPaths(t *testing.T) {
}()
pt = DijkstraAllPaths(g.(graph.Graph))
}()
if panicked || test.negative {
if !test.negative {
t.Errorf("%q: unexpected panic", test.name)
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q: unexpected panic", test.Name)
}
if !panicked {
t.Errorf("%q: expected panic for negative edge weight", test.name)
t.Errorf("%q: expected panic for negative edge weight", test.Name)
}
continue
}

// Check all random paths returned are OK.
for i := 0; i < 10; i++ {
p, weight, unique := pt.Between(test.query.From(), test.query.To())
if weight != test.weight {
p, weight, unique := pt.Between(test.Query.From(), test.Query.To())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}
if weight := pt.Weight(test.query.From(), test.query.To()); weight != test.weight {
if weight := pt.Weight(test.Query.From(), test.Query.To()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}
if unique != test.unique {
if unique != test.HasUniquePath {
t.Errorf("%q: unexpected number of paths: got: unique=%t want: unique=%t",
test.name, unique, test.unique)
test.Name, unique, test.HasUniquePath)
}

var got []int
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.want) == 0
for _, sp := range test.want {
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.name, p, test.want)
test.Name, p, test.WantPaths)
}
}

np, weight, unique := pt.Between(test.none.From(), test.none.To())
np, weight, unique := pt.Between(test.NoPathFor.From(), test.NoPathFor.To())
if np != nil || !math.IsInf(weight, 1) || unique != false {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f unique=%t\nwant:path=<nil> weight=+Inf unique=false",
test.name, np, weight, unique)
test.Name, np, weight, unique)
}

paths, weight := pt.AllBetween(test.query.From(), test.query.To())
if weight != test.weight {
paths, weight := pt.AllBetween(test.Query.From(), test.Query.To())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.name, weight, test.weight)
test.Name, weight, test.Weight)
}

var got [][]int
Expand All @@ -163,15 +164,15 @@ func TestDijkstraAllPaths(t *testing.T) {
}
}
sort.Sort(internal.BySliceValues(got))
if !reflect.DeepEqual(got, test.want) {
if !reflect.DeepEqual(got, test.WantPaths) {
t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
test.name, got, test.want)
test.Name, got, test.WantPaths)
}

nps, weight := pt.AllBetween(test.none.From(), test.none.To())
nps, weight := pt.AllBetween(test.NoPathFor.From(), test.NoPathFor.To())
if nps != nil || !math.IsInf(weight, 1) {
t.Errorf("%q: unexpected path:\ngot: paths=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.name, nps, weight)
test.Name, nps, weight)
}
}
}
Loading

0 comments on commit 202374f

Please sign in to comment.