From ac026220bd8c5c1a4ed5e624ab1c5d0524dbdcb9 Mon Sep 17 00:00:00 2001 From: Hari Date: Sun, 21 Jan 2024 18:26:14 +0100 Subject: [PATCH] minor bug fixes, more tests, version bumped for release --- CHANGELOG.md | 5 +++ docs/CHANGELOG.md | 5 +++ pathfinding3d/core/util.py | 3 +- pathfinding3d/finder/breadth_first.py | 2 -- pathfinding3d/finder/ida_star.py | 10 +++--- pathfinding3d/finder/msp.py | 4 +-- pathfinding3d/version.txt | 2 +- test/test_finder.py | 35 ++++++++++++++++++++ test/test_heap.py | 47 +++++++++++++++++++++++++++ test/test_util.py | 25 +++++++++++++- 10 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 test/test_finder.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 73ab3f7..c565f99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.5.1 + +- Minor bug fixes +- More tests added + ## 0.5.0 - New tests added for diagonal movements diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7b89d65..f55cfb0 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,10 @@ # Version History +## 0.5.1 + +- Minor bug fixes +- More tests added + ## 0.5.0 - New tests added for diagonal movements diff --git a/pathfinding3d/core/util.py b/pathfinding3d/core/util.py index 584b975..f34f91e 100644 --- a/pathfinding3d/core/util.py +++ b/pathfinding3d/core/util.py @@ -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 diff --git a/pathfinding3d/finder/breadth_first.py b/pathfinding3d/finder/breadth_first.py index 7209786..7524ff6 100644 --- a/pathfinding3d/finder/breadth_first.py +++ b/pathfinding3d/finder/breadth_first.py @@ -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, diff --git a/pathfinding3d/finder/ida_star.py b/pathfinding3d/finder/ida_star.py index 0ef073f..026d24e 100644 --- a/pathfinding3d/finder/ida_star.py +++ b/pathfinding3d/finder/ida_star.py @@ -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) diff --git a/pathfinding3d/finder/msp.py b/pathfinding3d/finder/msp.py index d565a6a..b2febda 100644 --- a/pathfinding3d/finder/msp.py +++ b/pathfinding3d/finder/msp.py @@ -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. @@ -36,7 +36,7 @@ def tree(self, grid: Grid, start: GridNode) -> List: Returns ------- - List + List[GridNode] """ return list(self.itertree(grid, start)) diff --git a/pathfinding3d/version.txt b/pathfinding3d/version.txt index 79a2734..5d4294b 100644 --- a/pathfinding3d/version.txt +++ b/pathfinding3d/version.txt @@ -1 +1 @@ -0.5.0 \ No newline at end of file +0.5.1 \ No newline at end of file diff --git a/test/test_finder.py b/test/test_finder.py new file mode 100644 index 0000000..8a450a9 --- /dev/null +++ b/test/test_finder.py @@ -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() diff --git a/test/test_heap.py b/test/test_heap.py index 6d821c3..f0bf0b2 100644 --- a/test/test_heap.py +++ b/test/test_heap.py @@ -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) diff --git a/test/test_util.py b/test/test_util.py index c405b34..c5dbd93 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -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(): @@ -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], + ]