-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_test.go
107 lines (102 loc) · 1.84 KB
/
maze_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
package maze
import (
"testing"
)
func TestFindPath(t *testing.T) {
tests := []struct {
matrix [][]int
pathLen int
err error
startcol, startrow int
}{
{
matrix: [][]int{
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 1},
{1, 0, 0, 1, 0, 1, 1},
{1, 0, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 1, 0, 1},
{1, 1, 2, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1},
},
pathLen: 7,
err: nil,
startcol: 3,
startrow: 7,
},
{
matrix: [][]int{
{1, 1, 1, 1},
{1, 1, 2, 1},
{1, 1, 1, 1},
},
pathLen: 0,
err: ErrNoExit,
startcol: 3,
startrow: 2,
},
{
matrix: [][]int{
{1, 1, 1, 1},
{1, 1, 2, 1},
{1, 1, 1, 1},
},
pathLen: 0,
err: ErrInvalidInput,
startcol: 2,
startrow: 1,
},
}
for _, level := range tests {
mz, err := NewMaze(level.matrix, []int{2, 0})
if err != nil {
t.Fatalf("expected error: %v, got: %v", level.err, err)
} else {
pathLen, err := mz.FindLenExit(level.startcol, level.startrow)
//error
if err != nil {
if level.err != err {
t.Fatalf("expected error: %v, got: %v", level.err, err)
}
//success
} else {
if pathLen != level.pathLen {
t.Fatalf("expected pathLen: %d, got: %d", level.pathLen, pathLen)
}
}
}
}
}
func TestNewMaze(t *testing.T) {
tests := []struct {
matrix [][]int
err error
}{
{
matrix: [][]int{
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 1, 0, 1},
},
err: nil,
},
{
matrix: [][]int{
{1, 1, 1, 1},
{1, 1, 0},
{1, 1, 1, 1},
},
err: ErrInvalidMatrix,
},
{
matrix: [][]int{},
err: ErrInvalidMatrix,
},
}
for _, m := range tests {
_, err := NewMaze(m.matrix, []int{2, 0})
if err != m.err {
t.Fatalf("expected error: %v, got: %v", m.err, err)
}
}
}