-
Notifications
You must be signed in to change notification settings - Fork 64
/
test_grid.py
85 lines (67 loc) · 1.49 KB
/
test_grid.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
import numpy as np
from pathfinding.core.diagonal_movement import DiagonalMovement
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
BORDERLESS_GRID = """
xxx
xxx
"""
BORDER_GRID = """
+---+
| |
| |
+---+
"""
WALKED_GRID = """
+---+
|s# |
|xe |
+---+
"""
SIMPLE_MATRIX = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
SIMPLE_WALKED = """
+---+
|sx |
| #x|
| e|
+---+
"""
def test_str():
"""
test printing the grid
"""
grid = Grid(height=2, width=3)
assert grid.grid_str(border=False, empty_chr='x') == BORDERLESS_GRID[1:-1]
assert grid.grid_str(border=True) == BORDER_GRID[1:-1]
grid.nodes[0][1].walkable = False
start = grid.nodes[0][0]
end = grid.nodes[1][1]
path = [(0, 1)]
assert grid.grid_str(path, start, end) == WALKED_GRID[1:-1]
def test_empty():
"""
special test for empty values
"""
matrix = ()
grid = Grid(matrix=matrix)
assert grid.grid_str() == '++\n||\n++'
matrix = np.array(matrix)
grid = Grid(matrix=matrix)
assert grid.grid_str() == '++\n||\n++'
def test_numpy():
"""
test grid from numpy array
"""
matrix = np.array(SIMPLE_MATRIX)
grid = Grid(matrix=matrix)
start = grid.node(0, 0)
end = grid.node(2, 2)
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
path, runs = finder.find_path(start, end, grid)
assert grid.grid_str(path, start, end) == SIMPLE_WALKED[1:-1]
if __name__ == '__main__':
test_str()