Skip to content

Commit edfe2d3

Browse files
committed
improve tests
1 parent 0d1f993 commit edfe2d3

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (g graph) isFreeAt(p image.Point) bool {
103103
}
104104

105105
func (g graph) isInBounds(p image.Point) bool {
106-
return p.Y >= 0 && p.X >= 0 && p.Y < len(g) && p.X < len(g[p.Y])
106+
return (0 <= p.X && p.X < len(g[p.Y])) && (0 <= p.Y && p.Y < len(g))
107107
}
108108

109109
func (g graph) put(p image.Point, c rune) {

astar_test.go

+43-36
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,62 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package astar
5+
package astar_test
66

77
import (
88
"image"
9-
"math"
9+
"reflect"
1010
"testing"
11-
)
12-
13-
type graph[Node comparable] map[Node][]Node
14-
15-
func newGraph[Node comparable]() graph[Node] {
16-
return make(map[Node][]Node)
17-
}
18-
19-
func (g graph[Node]) Link(a, b Node) graph[Node] {
20-
g[a] = append(g[a], b)
21-
g[b] = append(g[b], a)
22-
return g
23-
}
2411

25-
func (g graph[Node]) Neighbours(n Node) []Node {
26-
return g[n]
27-
}
28-
29-
func nodeDist(p, q image.Point) float64 {
30-
d := q.Sub(p)
31-
return math.Sqrt(float64(d.X*d.X + d.Y*d.Y))
32-
}
12+
"github.com/fzipp/astar"
13+
)
3314

3415
func TestFindPath(t *testing.T) {
3516
a := image.Pt(2, 3)
3617
b := image.Pt(1, 7)
3718
c := image.Pt(1, 6)
3819
d := image.Pt(5, 6)
39-
g := newGraph[image.Point]().Link(a, b).Link(a, c).Link(b, d).Link(c, d)
4020

41-
want := Path[image.Point]{
42-
image.Pt(2, 3),
43-
image.Pt(1, 6),
44-
image.Pt(5, 6),
21+
tests := []struct {
22+
name string
23+
graph graph[image.Point]
24+
start image.Point
25+
dest image.Point
26+
want astar.Path[image.Point]
27+
}{
28+
{
29+
name: "find simple path",
30+
graph: newGraph[image.Point]().
31+
link(a, b).link(a, c).
32+
link(b, d).
33+
link(c, d),
34+
start: a,
35+
dest: d,
36+
want: astar.Path[image.Point]{
37+
image.Pt(2, 3),
38+
image.Pt(1, 6),
39+
image.Pt(5, 6),
40+
},
41+
},
42+
{
43+
name: "find no path",
44+
graph: newGraph[image.Point]().
45+
link(a, b).link(a, c).
46+
link(b, d).
47+
link(c, d),
48+
start: a,
49+
dest: image.Pt(1, 1),
50+
want: nil,
51+
},
4552
}
4653

47-
p := FindPath[image.Point](g, a, d, nodeDist, nodeDist)
48-
if len(p) != len(want) {
49-
t.Errorf("Returned path has %d nodes, want %d nodes.", len(p), len(want))
50-
}
51-
for i, n := range p {
52-
if n != want[i] {
53-
t.Errorf("Node %d of path is %v, want %v.", i, n, want[i])
54-
}
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
got := astar.FindPath[image.Point](tt.graph, tt.start, tt.dest, nodeDist, nodeDist)
57+
if !reflect.DeepEqual(got, tt.want) {
58+
t.Errorf("\ngraph = %v\nFindPath(graph, %v, %v, nodeDist, nodeDist) = %v, want %v",
59+
tt.graph, tt.start, tt.dest, got, tt.want)
60+
}
61+
})
5562
}
5663
}

example2_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func distance(p, q image.Point) float64 {
6868

6969
type floorPlan []string
7070

71-
// Neighbours implements the astar.Graph interface
71+
// Neighbours implements the astar.Graph[Node] interface (with Node = image.Point).
7272
func (f floorPlan) Neighbours(p image.Point) []image.Point {
7373
offsets := []image.Point{
7474
image.Pt(0, -1), // North
@@ -91,7 +91,7 @@ func (f floorPlan) isFreeAt(p image.Point) bool {
9191
}
9292

9393
func (f floorPlan) isInBounds(p image.Point) bool {
94-
return p.Y >= 0 && p.X >= 0 && p.Y < len(f) && p.X < len(f[p.Y])
94+
return (0 <= p.X && p.X < len(f[p.Y])) && (0 <= p.Y && p.Y < len(f))
9595
}
9696

9797
func (f floorPlan) put(p image.Point, c rune) {

0 commit comments

Comments
 (0)