|
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -package astar |
| 5 | +package astar_test |
6 | 6 |
|
7 | 7 | import (
|
8 | 8 | "image"
|
9 |
| - "math" |
| 9 | + "reflect" |
10 | 10 | "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 |
| -} |
24 | 11 |
|
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 | +) |
33 | 14 |
|
34 | 15 | func TestFindPath(t *testing.T) {
|
35 | 16 | a := image.Pt(2, 3)
|
36 | 17 | b := image.Pt(1, 7)
|
37 | 18 | c := image.Pt(1, 6)
|
38 | 19 | d := image.Pt(5, 6)
|
39 |
| - g := newGraph[image.Point]().Link(a, b).Link(a, c).Link(b, d).Link(c, d) |
40 | 20 |
|
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 | + }, |
45 | 52 | }
|
46 | 53 |
|
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 | + }) |
55 | 62 | }
|
56 | 63 | }
|
0 commit comments