/
PathManager_test.py
321 lines (282 loc) · 9.81 KB
/
PathManager_test.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import unittest
from pathing import PathManager
class SingleSpace:
def __init__(self, value):
self.value = value
def is_passable(self):
return not self.value
class Space:
def __init__(self, dim):
self.a = [[[0
for z in range(dim[2])]
for y in range(dim[1])]
for x in range(dim[0])]
def __getitem__(self, loc):
return SingleSpace(self.a[loc[0]][loc[1]][loc[2]])
def __setitem__(self, loc, value):
self.a[loc[0]][loc[1]][loc[2]] = value
def get_dimensions(self):
return (len(self.a),len(self.a[0]),len(self.a[0][0]))
class PathManagerTestCase(unittest.TestCase):
def test_new(self):
PathManager(Space((4,4,3)))
def test_adjacent_even_x(self):
p = PathManager(Space((4,4,3)))
t = (2,1)
a = set([ (2,0),
(1,0), (3,0),
(1,1), (3,1),
(2,2)])
self.assertEqual(p.adjacent_xy(t), a)
def test_adjacent_odd_x(self):
p = PathManager(Space((4,4,3)))
t = (1,1)
a = set([ (1,0),
(0,1), (2,1),
(0,2), (2,2),
(1,2)])
self.assertEqual(p.adjacent_xy(t), a)
def test_adjacent_edge(self):
p = PathManager(Space((4,4,3)))
t = (0,1)
a = set([(0,0),
(1,0),
(1,1),
(0,2)])
self.assertEqual(p.adjacent_xy(t), a)
def test_adjacent_corner(self):
p = PathManager(Space((4,4,3)))
t = (3,3)
a = set([ (3,2),
(2,3)])
self.assertEqual(p.adjacent_xy(t), a)
def test_open_adjacent(self):
s = Space((4,4,3))
for x in range(4):
for y in range(4):
s[(x,y,0)] = 1
s[(1,0,1)] = s[(1,0,2)] = 1
s[(2,2,1)] = s[(2,2,2)] = 1
p = PathManager(s)
t = (2,1,1)
a = set([ (2,0,1),
(3,0,1),
(1,1,1), (3,1,1)])
self.assertEqual(p.open_adjacent(t), a)
def test_open_adjacent_step_up(self):
s = Space((4,4,3))
for x in range(4):
for y in range(4):
s[(x,y,0)] = 1
s[(1,0,1)] = 1
s[(2,2,1)] = 1
p = PathManager(s)
t = (2,1,1)
a = set([ (2,0,1),
(1,0,2), (3,0,1),
(1,1,1), (3,1,1),
(2,2,2)])
self.assertEqual(p.open_adjacent(t), a)
def test_open_adjacent_step_down(self):
s = Space((4,4,3))
for x in range(4):
for y in range(4):
s[(x,y,0)] = 1
s[(x,y,1)] = 1
s[(1,0,1)] = 0
s[(2,2,1)] = 0
p = PathManager(s)
t = (2,1,2)
a = set([ (2,0,2),
(1,0,1), (3,0,2),
(1,1,2), (3,1,2),
(2,2,1)])
self.assertEqual(p.open_adjacent(t), a)
def test_open_adjacent_step_up_blocked(self):
s = Space((4,4,3))
for x in range(4):
for y in range(4):
s[(x,y,0)] = 1
s[(1,0,1)] = 1
s[(2,2,1)] = 1
s[(2,1,2)] = 1
p = PathManager(s)
t = (2,1,1)
a = set([ (2,0,1),
(3,0,1),
(1,1,1), (3,1,1)])
self.assertEqual(p.open_adjacent(t), a)
def test_open_adjacent_step_down_blocked(self):
s = Space((4,4,3))
for x in range(4):
for y in range(4):
s[(x,y,0)] = 1
s[(x,y,1)] = 1
s[(1,0,1)] = 0
s[(1,0,2)] = 1
s[(2,2,1)] = 0
p = PathManager(s)
t = (2,1,2)
a = set([ (2,0,2),
(3,0,2),
(1,1,2), (3,1,2),
(2,2,1)])
self.assertEqual(p.open_adjacent(t), a)
def test_distance_x(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((0,0),(3,0)), 3)
def test_distance_x_2(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((1,1),(2,2)), 1)
def test_distance_x_3(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((2,1),(3,1)), 1)
def test_distance_y(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((1,0),(1,2)), 2)
def test_distance_x_y(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((0,0),(3,2)), 4)
def test_distance_x_y_2(self):
p = PathManager(Space((4,4,3)))
self.assertEqual(p.distance_xy((0,0),(2,3)), 4)
def test_distance_x_y_3(self):
p = PathManager(Space((8,8,3)))
self.assertEqual(p.distance_xy((5,5),(1,2)), 5)
def test_distance_x_y_4(self):
p = PathManager(Space((8,8,3)))
self.assertEqual(p.distance_xy((1,0),(5,5)), 7)
def test_find_path(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(len(p.find_path((1,2,1),(4,5,1))), 4)
def test_find_path_3d(self):
s = Space((3,2,3))
p = PathManager(s)
for x in range(3):
for y in range(2):
s[(x,y,0)] = 1
s[(1,0,1)] = 1
e = [(1,0,2),(0,0,1)]
self.assertEqual(p.find_path((2,0,1),(0,0,1)), e)
def test_find_path_3d_wall(self):
s = Space((3,3,4))
p = PathManager(s)
for x in range(3):
for y in range(3):
s[(x,y,0)] = 1
s[(1,0,1)] = 1
s[(1,0,2)] = 1
s[(1,1,1)] = 1
e = [(2,1,1),(1,1,2),(0,1,1),(0,0,1)]
self.assertEqual(p.find_path((2,0,1),(0,0,1)), e)
def test_no_path(self):
s = Space((3,2,3))
p = PathManager(s)
for x in range(3):
for y in range(2):
s[(x,y,0)] = 1
s[(1,0,1)] = 1
s[(1,0,2)] = 1
s[(1,1,1)] = 1
s[(1,1,2)] = 1
self.assertEqual(p.find_path((2,0,1),(0,0,1)), None)
def test_find_path_prefer_flat(self):
s = Space((4,2,4))
p = PathManager(s)
for x in range(4):
for y in range(2):
s[(x,y,0)] = 1
# two paths, via 2,0,1 or 2,1,1: find the preferred path for flat
f = p.find_path((1,0,1),(3,0,1))
not_f0 = (2,0,1) if f[0] == (2,1,1) else (2,1,1)
# turn into a hill
s[f[0]] = 1
# should path around it
h = p.find_path((1,0,1),(3,0,1))
self.assertEqual(h[0], not_f0)
# block new path entirely
s[h[0]] = 1
s[(h[0][0],h[0][1],h[0][2]+1)] = 1
# should be back to first, one step up
f[0] = (f[0][0], f[0][1], f[0][2]+1)
self.assertEqual(p.find_path((1,0,1),(3,0,1)), f)
def test_find_path_out_of_limits(self):
s = Space((48,32,8))
p = PathManager(s)
for x in range(48):
for y in range(32):
s[(x,y,6)] = 1
s[(31,27,7)] = 1
try:
p.find_path((31,28,7),(31,27,8))
self.fail('Expected IndexError')
except IndexError:
pass
def test_find_path_inside_block(self):
s = Space((3,2,3))
p = PathManager(s)
for x in range(3):
for y in range(2):
s[(x,y,0)] = 1
s[(1,0,2)] = 1
self.assertEqual(p.find_path((2,0,1),(1,0,2)), None)
def test_get_radius_0(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.within_radius((2,2,0),0), set([(2,2,0)]))
def test_get_radius_1(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.within_radius((2,2,0),1),
set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0),
(1,1,0),(2,2,0)]))
def test_get_radius_2(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.within_radius((2,2,0),2),
set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0),
(1,1,0),(2,2,0),(2,0,0),(3,0,0),(4,1,0),
(4,2,0),(4,3,0),(3,3,0),(2,4,0),(1,3,0),
(0,3,0),(0,2,0),(0,1,0),(1,0,0)]))
def test_get_radius_include_0(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.radius_include((2,2,0),(2,2,0)), set([(2,2,0)]))
def test_get_radius_include_1(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.radius_include((2,2,0),(3,2,0)),
set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0),
(1,1,0),(2,2,0)]))
def test_get_radius_include_2(self):
s = Space((6,6,3))
p = PathManager(s)
for x in range(6):
for y in range(6):
s[(x,y,0)] = 1
self.assertEqual(p.radius_include((2,2,0),(1,0,0)),
set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0),
(1,1,0),(2,2,0),(2,0,0),(3,0,0),(4,1,0),
(4,2,0),(4,3,0),(3,3,0),(2,4,0),(1,3,0),
(0,3,0),(0,2,0),(0,1,0),(1,0,0)]))
if __name__ == '__main__':
unittest.main()