-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrid_path_test.go
111 lines (102 loc) · 3.17 KB
/
grid_path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package pathing_test
import (
"math/rand"
"reflect"
"strings"
"testing"
"time"
"github.com/quasilyte/pathing"
)
func testParsePath(s string) pathing.GridPath {
s = s[1 : len(s)-1] // Drop "{}"
if s == "" {
return pathing.GridPath{}
}
var directions []pathing.Direction
for _, part := range strings.Split(s, ",") {
switch part {
case "Right":
directions = append(directions, pathing.DirRight)
case "Down":
directions = append(directions, pathing.DirDown)
case "Left":
directions = append(directions, pathing.DirLeft)
case "Up":
directions = append(directions, pathing.DirUp)
default:
panic("unexpected part: " + part)
}
}
return pathing.MakeGridPath(directions...)
}
func TestGridPathString(t *testing.T) {
tests := []string{
"{}",
"{Left}",
"{Left,Right}",
"{Right,Left}",
"{Down,Down,Down,Up}",
"{Left,Right,Up,Down}",
"{Left,Right,Right,Right,Left}",
"{Up,Up,Down,Down,Left,Left,Right,Right,Down,Down}",
"{Up,Up,Down,Down,Left,Left,Right,Right,Down,Down,Down,Left,Up,Right}",
"{Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left}",
"{Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Right}",
"{Up,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left,Left}",
}
for _, test := range tests {
p := testParsePath(test)
if p.String() != test {
t.Fatalf("results mismatched:\nhave: %q\nwant: %q", p.String(), test)
}
}
}
func TestGridPath(t *testing.T) {
tests := [][]pathing.Direction{
{},
{pathing.DirLeft},
{pathing.DirDown},
{pathing.DirLeft, pathing.DirRight, pathing.DirUp},
{pathing.DirLeft, pathing.DirLeft, pathing.DirLeft},
{pathing.DirDown, pathing.DirDown, pathing.DirDown},
{pathing.DirDown, pathing.DirUp, pathing.DirLeft, pathing.DirRight, pathing.DirLeft, pathing.DirRight},
{pathing.DirDown, pathing.DirLeft, pathing.DirLeft, pathing.DirLeft, pathing.DirLeft, pathing.DirDown},
{pathing.DirRight, pathing.DirRight, pathing.DirRight, pathing.DirRight, pathing.DirRight, pathing.DirRight, pathing.DirRight},
{pathing.DirDown, pathing.DirRight, pathing.DirRight, pathing.DirDown, pathing.DirRight, pathing.DirUp, pathing.DirRight, pathing.DirLeft},
}
for i, directions := range tests {
p := pathing.MakeGridPath(directions...)
reconstructed := []pathing.Direction{}
for p.HasNext() {
reconstructed = append(reconstructed, p.Next())
}
if !reflect.DeepEqual(directions, reconstructed) {
t.Fatalf("test%d paths mismatch", i)
}
}
r := rand.New(rand.NewSource(time.Now().Unix()))
for i := 0; i < 100; i++ {
size := r.Intn(20) + 10
directions := []pathing.Direction{}
for j := 0; j < size; j++ {
d := r.Intn(4)
directions = append(directions, pathing.Direction(d))
}
p := pathing.MakeGridPath(directions...)
reconstructed := []pathing.Direction{}
for p.HasNext() {
reconstructed = append(reconstructed, p.Next())
}
if !reflect.DeepEqual(directions, reconstructed) {
t.Fatalf("test%d paths mismatch", i)
}
p.Rewind()
reconstructed = reconstructed[:0]
for p.HasNext() {
reconstructed = append(reconstructed, p.Next())
}
if !reflect.DeepEqual(directions, reconstructed) {
t.Fatalf("test%d paths mismatch", i)
}
}
}