Repository navigation
Expand file tree
/
Copy pathtest_util.py
More file actions
77 lines (65 loc) · 2.03 KB
/
Copy pathtest_util.py
File metadata and controls
77 lines (65 loc) · 2.03 KB
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
import pytest
from pathfinding.core.grid import Grid
from pathfinding.core.util import bresenham, raytrace, smoothen_path
def test_bresenham():
"""
test bresenham path interpolation
"""
assert bresenham([0, 0], [2, 5]) == [
[0, 0], [0, 1],
[1, 2], [1, 3],
[2, 4], [2, 5]
]
assert bresenham([0, 1], [0, 4]) == [
[0, 1], [0, 2], [0, 3], [0, 4]
]
def test_raytrace():
"""
test raytrace path interpolation
"""
assert raytrace([0, 0], [2, 5]) == [
[0, 0], [0, 1],
[1, 1], [1, 2], [1, 3], [1, 4],
[2, 4], [2, 5]
]
assert raytrace([0, 1], [0, 4]) == [
[0, 1], [0, 2], [0, 3], [0, 4]
]
@pytest.mark.parametrize('sx', [-1, 1])
@pytest.mark.parametrize('sy', [-1, 1])
@pytest.mark.parametrize('swap_axes', [False, True])
def test_raytrace_all_octants(sx, sy, swap_axes):
# Reflect a known line about its starting cell, then swap the axes.
expected = [
[0, 0], [0, 1], [1, 1], [1, 2], [1, 3], [1, 4], [2, 4], [2, 5]
]
expected = [[sx * x, sy * y] for x, y in expected]
if swap_axes:
expected = [[y, x] for x, y in expected]
expected = [[x + 5, y + 5] for x, y in expected]
assert raytrace(expected[0], expected[-1]) == expected
@pytest.mark.parametrize('end, expected', [
([1, 3], [[3, 3], [2, 3], [1, 3]]),
([5, 3], [[3, 3], [4, 3], [5, 3]]),
([3, 1], [[3, 3], [3, 2], [3, 1]]),
([3, 5], [[3, 3], [3, 4], [3, 5]]),
([3, 3], [[3, 3]]),
])
def test_raytrace_axis_aligned_and_stationary(end, expected):
assert raytrace([3, 3], end) == expected
def test_smoothen_path():
matrix = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
grid = Grid(matrix=matrix)
path = [
[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [3, 3], [3, 4], [4, 4]
]
smooth_path = [
[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [3, 3], [4, 4]
]
assert smoothen_path(grid, path) == smooth_path