Skip to content

Commit

Permalink
Merge pull request #8 from harisankar95/code-cleanup
Browse files Browse the repository at this point in the history
minor bug fixes, more tests, version bumped for release
  • Loading branch information
harisankar95 authored Jan 21, 2024
2 parents 8687a5a + ac02622 commit eb47906
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.5.1

- Minor bug fixes
- More tests added

## 0.5.0

- New tests added for diagonal movements
Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Version History

## 0.5.1

- Minor bug fixes
- More tests added

## 0.5.0

- New tests added for diagonal movements
Expand Down
3 changes: 2 additions & 1 deletion pathfinding3d/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def expand_path(path: List[Coords]) -> List[Coords]:
return expanded
for i in range(len(path) - 1):
expanded += bresenham(path[i], path[i + 1])
expanded += [path[:-1]]
expanded.pop()
expanded.append(path[-1])
return expanded


Expand Down
2 changes: 0 additions & 2 deletions pathfinding3d/finder/breadth_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def __init__(
time_limit=time_limit,
max_runs=max_runs,
)
if not diagonal_movement:
self.diagonalMovement = DiagonalMovement.never

def check_neighbors(
self,
Expand Down
10 changes: 5 additions & 5 deletions pathfinding3d/finder/ida_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class IDAStarFinder(Finder):
Iterative Deeping A Star (IDA*) path-finder.
Recursion based on:
http://www.apl.jhu.edu/~hall/AI-Programming/IDA-Star.html
http://www.apl.jhu.edu/~hall/AI-Programming/IDA-Star.html
Path retracing based on:
V. Nageshwara Rao, Vipin Kumar and K. Ramesh
"A Parallel Implementation of Iterative-Deeping-A*", January 1987.
ftp://ftp.cs.utexas.edu/.snapshot/hourly.1/pub/AI-Lab/tech-reports/
UT-AI-TR-87-46.pdf
V. Nageshwara Rao, Vipin Kumar and K. Ramesh
"A Parallel Implementation of Iterative-Deeping-A*", January 1987.
ftp://ftp.cs.utexas.edu/.snapshot/hourly.1/pub/AI-Lab/tech-reports/
UT-AI-TR-87-46.pdf
based on the JavaScript implementation by Gerard Meier
(www.gerardmeier.com)
Expand Down
4 changes: 2 additions & 2 deletions pathfinding3d/finder/msp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.heuristic = heuristic.null

def tree(self, grid: Grid, start: GridNode) -> List:
def tree(self, grid: Grid, start: GridNode) -> List[GridNode]:
"""
Returns a list of nodes that are part of the minimum spanning tree
of the grid.
Expand All @@ -36,7 +36,7 @@ def tree(self, grid: Grid, start: GridNode) -> List:
Returns
-------
List
List[GridNode]
"""

return list(self.itertree(grid, start))
Expand Down
2 changes: 1 addition & 1 deletion pathfinding3d/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.0
0.5.1
35 changes: 35 additions & 0 deletions test/test_finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import pytest

from pathfinding3d.core.grid import Grid, GridNode
from pathfinding3d.finder.finder import Finder
from pathfinding3d.finder.msp import MinimumSpanningTree


class DummyFinder(Finder):
def __init__(self):
super().__init__()


def test_check_neighbors_raises_exception():
finder = DummyFinder()
start = GridNode(0, 0, 0)
end = GridNode(1, 1, 1)
grid = Grid(matrix=[[[1]]])
open_list = []

with pytest.raises(NotImplementedError):
finder.check_neighbors(start, end, grid, open_list)


def test_msp():
"""
Test that the minimum spanning tree finder returns all nodes.
"""
matrix = np.array(np.ones((3, 3, 3)))
grid = Grid(matrix=matrix)

start = grid.node(0, 0, 0)

finder = MinimumSpanningTree()
assert finder.tree(grid, start).sort() == [node for row in grid.nodes for col in row for node in col].sort()
47 changes: 47 additions & 0 deletions test/test_heap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
import pytest

from pathfinding3d.core.grid import Grid
from pathfinding3d.core.heap import SimpleHeap


def test_determine_node_retrieval_function():
grid = Grid(width=10, height=10, depth=10)
start = grid.node(0, 0, 0)
heap = SimpleHeap(start, grid)

assert callable(heap._determine_node_retrieval_function())

with pytest.raises(ValueError):
heap.grid = "UnsupportedType"
heap._determine_node_retrieval_function()


def test_determine_node_function():
grid = Grid(width=10, height=10, depth=10)
start = grid.node(0, 0, 0)
heap = SimpleHeap(start, grid)

assert callable(heap._determine_node_function())

with pytest.raises(ValueError):
heap.grid = "UnsupportedType"
heap._determine_node_function()


def test_push_node():
grid = Grid(width=10, height=10, depth=10)
start = grid.node(0, 0, 0)
heap = SimpleHeap(start, grid)

heap.push_node(grid.node(1, 1, 1))
assert len(heap) == 2
assert heap.number_pushed == 1


def test_remove_node():
grid = Grid(width=10, height=10, depth=10)
start = grid.node(0, 0, 0)
heap = SimpleHeap(start, grid)

heap.push_node(grid.node(1, 1, 1))
heap.remove_node(grid.node(1, 1, 1), 0)
assert len(heap) == 2
assert (0.0, 1, 1, 1, 1) in heap.removed_node_tuples


def test_heap():
grid = Grid(width=10, height=10, depth=10)
start = grid.node(0, 0, 0)
Expand Down
25 changes: 24 additions & 1 deletion test/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathfinding3d.core.grid import Grid
from pathfinding3d.core.util import bresenham, raytrace, smoothen_path
from pathfinding3d.core.util import bresenham, expand_path, raytrace, smoothen_path


def test_bresenham():
Expand Down Expand Up @@ -73,3 +73,26 @@ def test_smoothen_path():
[4, 4, 2],
]
assert smoothen_path(grid, path) == smooth_path


def test_expand_path():
"""
test expand_path function
"""
# Test with empty path
assert expand_path([]) == []

# Test with one point path
assert expand_path([[0, 0, 0]]) == []

# Test with two points path
assert expand_path([[0, 0, 0], [1, 1, 1]]) == [[0, 0, 0], [1, 1, 1]]

# Test with multiple points path
assert expand_path([[0, 0, 0], [2, 2, 2], [4, 2, 2]]) == [
[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 2, 2],
[4, 2, 2],
]

0 comments on commit eb47906

Please sign in to comment.