-
-
Notifications
You must be signed in to change notification settings - Fork 654
/
robot_simulator_step3_test.go
267 lines (254 loc) · 6.5 KB
/
robot_simulator_step3_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//go:build step3 || (!step1 && !step2)
package robot
import "testing"
var testOneRobot = []struct {
cmd byte
Step2Robot
nMsg int
}{
// (test2 data with message counts added)
{' ', Step2Robot{N, Pos{1, 1}}, 0},
{'A', Step2Robot{N, Pos{1, 2}}, 0},
{'R', Step2Robot{E, Pos{1, 2}}, 0},
{'A', Step2Robot{E, Pos{2, 2}}, 0},
{'L', Step2Robot{N, Pos{2, 2}}, 0},
{'L', Step2Robot{W, Pos{2, 2}}, 0},
{'L', Step2Robot{S, Pos{2, 2}}, 0},
{'A', Step2Robot{S, Pos{2, 1}}, 0},
{'R', Step2Robot{W, Pos{2, 1}}, 0},
{'A', Step2Robot{W, Pos{1, 1}}, 0},
{'A', Step2Robot{W, Pos{1, 1}}, 1}, // bump W wall
{'L', Step2Robot{S, Pos{1, 1}}, 1},
{'A', Step2Robot{S, Pos{1, 1}}, 2}, // bump S wall
{'L', Step2Robot{E, Pos{1, 1}}, 2},
{'A', Step2Robot{E, Pos{2, 1}}, 2},
{'A', Step2Robot{E, Pos{2, 1}}, 3}, // bump E wall
{'L', Step2Robot{N, Pos{2, 1}}, 3},
{'A', Step2Robot{N, Pos{2, 2}}, 3},
{'A', Step2Robot{N, Pos{2, 2}}, 4}, // bump N wall
}
func logMon(log chan string, nMsg chan int, t *testing.T) {
n := 0
for msg := range log {
t.Log("Sim:", msg)
n++
}
nMsg <- n
}
// Same tests as step 2; single robot, but expect log messages on wall bumps.
func TestOneStep3(t *testing.T) {
for i := 1; i <= len(testOneRobot); i++ {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{2, 2}},
[]Step3Robot{{"Robbie", testOneRobot[0].Step2Robot}},
act, rep, log)
scr := ""
for j := 1; j < i; j++ {
scr += string(testOneRobot[j].cmd)
}
t.Logf("Script %q", scr)
go StartRobot3("Robbie", scr, act, log)
pls := <-rep
lastTest := testOneRobot[i-1]
if len(pls) != 1 {
t.Fatalf("Got report on %d robots, want 1.", len(pls))
}
pl := pls[0]
if pl.Name != "Robbie" {
t.Fatalf(`Got report for robot %q, want report for "Robbie".`,
pl.Name)
}
da := pl.Step2Robot
want := lastTest.Step2Robot
if da.Pos != want.Pos {
t.Fatalf("Script %q, Pos = %v, want %v", scr, da.Pos, want.Pos)
}
if da.Dir != want.Dir {
t.Fatalf("Script %q, Dir = %v, want %v", scr, da.Dir, want.Dir)
}
close(log)
if n := <-nMsg; n != lastTest.nMsg {
t.Errorf("%d sim messages logged, want %d.", n, lastTest.nMsg)
}
}
}
func TestNoName(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{1, 1}},
[]Step3Robot{{"", Step2Robot{N, Pos{1, 1}}}},
act, rep, log)
go StartRobot3("", "", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestSameName(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{2, 1}},
[]Step3Robot{
{"Daryl", Step2Robot{N, Pos{1, 1}}},
{"Daryl", Step2Robot{N, Pos{2, 1}}},
},
act, rep, log)
go StartRobot3("Daryl", "", act, log)
go StartRobot3("Daryl", "", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestSamePosition(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{100, 100}},
[]Step3Robot{
{"Matter", Step2Robot{N, Pos{23, 47}}},
{"Antimatter", Step2Robot{N, Pos{23, 47}}},
},
act, rep, log)
go StartRobot3("Matter", "", act, log)
go StartRobot3("Antimatter", "", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestOutsideRoom(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{1, 1}},
[]Step3Robot{{"Elvis", Step2Robot{N, Pos{2, 3}}}},
act, rep, log)
go StartRobot3("Elvis", "", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestBadCommand(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{0, 0}, Pos{0, 99}},
[]Step3Robot{{"Vgr", Step2Robot{N, Pos{0, 99}}}},
act, rep, log)
go StartRobot3("Vgr", "RET", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestBadRobot(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{0, 0}, Pos{0, 0}},
[]Step3Robot{{"Data", Step2Robot{N, Pos{0, 0}}}},
act, rep, log)
go StartRobot3("Lore", "A", act, log)
<-rep
close(log)
if n := <-nMsg; n != 1 {
t.Fatalf("Got %d messages, want 1.", n)
}
}
func TestThree(t *testing.T) { // no bumping
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
go StartRobot3("clutz", "LAAARALA", act, log)
go StartRobot3("sphero", "RRAAAAALA", act, log)
go StartRobot3("roomba", "LAAARRRALLLL", act, log)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{-10, -10}, Pos{15, 10}},
[]Step3Robot{
{"clutz", Step2Robot{N, Pos{0, 0}}},
{"sphero", Step2Robot{E, Pos{2, -7}}},
{"roomba", Step2Robot{S, Pos{8, 4}}},
},
act, rep, log)
pls := <-rep
if len(pls) != 3 {
t.Fatalf("Got report on %d robots, want 3.", len(pls))
}
exp:
for _, exp := range []Step3Robot{
{"clutz", Step2Robot{W, Pos{-4, 1}}},
{"sphero", Step2Robot{S, Pos{-3, -8}}},
{"roomba", Step2Robot{N, Pos{11, 5}}},
} {
for _, pl := range pls {
if pl.Name != exp.Name {
continue
}
if pl.Step2Robot.Pos != exp.Step2Robot.Pos {
t.Fatalf("%s at %v, want %v",
pl.Name, pl.Step2Robot.Pos, exp.Step2Robot.Pos)
}
if pl.Step2Robot.Dir != exp.Step2Robot.Dir {
t.Fatalf("%s facing %v, want %v",
pl.Name, pl.Step2Robot.Dir, exp.Step2Robot.Dir)
}
continue exp
}
t.Fatalf("Missing %s", exp.Name)
}
}
func TestBattle(t *testing.T) {
log := make(chan string)
nMsg := make(chan int)
go logMon(log, nMsg, t)
act := make(chan Action3)
go StartRobot3("Toro", "AAAAAAA", act, log)
go StartRobot3("Phere", "AAAAAAA", act, log)
rep := make(chan []Step3Robot)
go Room3(
Rect{Pos{1, 1}, Pos{9, 9}},
[]Step3Robot{
{"Toro", Step2Robot{E, Pos{1, 5}}},
{"Phere", Step2Robot{W, Pos{9, 5}}},
},
act, rep, log)
<-rep
close(log)
if n := <-nMsg; n != 7 {
t.Fatalf("Got %d messages, want 7.", n)
}
}