Skip to content

Commit 35335fa

Browse files
committed
Refactored eveything and added Unit testing
1 parent ee4c325 commit 35335fa

11 files changed

+138
-9
lines changed

.travis.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Travis configuration file
2+
# http://docs.travis-ci.com/user/languages/python/
3+
language: python
4+
python:
5+
- "3.5_with_system_site_packages"
6+
7+
# Install numpy and scipy to avoid the painful compilation
8+
before_install:
9+
- sudo apt-get update -qq
10+
- sudo apt-get install -qq python-numpy python-scipy
11+
12+
# Install fits_reduce...
13+
install:
14+
- pip install .
15+
- pip install coveralls
16+
# Run tests
17+
script:
18+
- nosetests --with-coverage --cover-package=fits_reduce --cover-inclusive
19+
after_success:
20+
- coveralls

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ class DijkstraHeap(list):
178178

179179
while self and self[0].point in self.visited:
180180
heapq.heappop(self)
181-
182-
next_elem = heapq.heappop(self)
183-
self.visited[next_elem.point] = next_elem.came_from
184-
self.costs[next_elem.point] = next_elem.cost
185-
return next_elem
181+
182+
if self:
183+
next_elem = heapq.heappop(self)
184+
self.visited[next_elem.point] = next_elem.came_from
185+
self.costs[next_elem.point] = next_elem.cost
186+
return next_elem
186187
```
187188

188189
# So WHAT is this deep logic you talk about?

a_star/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__author__ = 'Pablo Galindo Salgado'
2+
3+
from a_star import *
201 Bytes
Binary file not shown.
3.19 KB
Binary file not shown.
5.63 KB
Binary file not shown.

a_star.py renamed to a_star/a_star.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ def pop(self):
5656
while self and self[0].point in self.visited:
5757
heapq.heappop(self)
5858

59-
next_elem = heapq.heappop(self)
60-
self.visited[next_elem.point] = next_elem.came_from
61-
self.costs[next_elem.point] = next_elem.cost
62-
return next_elem
59+
if self:
60+
next_elem = heapq.heappop(self)
61+
self.visited[next_elem.point] = next_elem.came_from
62+
self.costs[next_elem.point] = next_elem.cost
63+
return next_elem
6364

6465

6566

File renamed without changes.

setup.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from distutils.core import setup
2+
3+
setup(
4+
name='a_star',
5+
version='0.1',
6+
packages=['a_star'],
7+
url='https://github.com/pablogsal/a_star_algorithm/',
8+
license='BSD',
9+
author='Pablo Galindo Salgado',
10+
author_email='[email protected]',
11+
description='A pythonic implementation of the A* algorithm',
12+
)
3.14 KB
Binary file not shown.

tests/test_suite.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
import unittest
3+
import a_star.a_star as a_star
4+
5+
6+
7+
8+
class DijkstraHeapTests(unittest.TestCase):
9+
10+
def test_sorted_unique(self):
11+
12+
test_data = [a_star.Node(x,x,None) for x in (5,7,3,2,5,3,5,7,2,1,3,1)]
13+
sorted_unique = sorted(set(test_data))
14+
15+
# Prepare and insert elements in DijkstraHeap
16+
17+
frontier = a_star.DijkstraHeap()
18+
for elem in test_data:
19+
frontier.insert(elem)
20+
21+
# get out elements from the DijkstraHeap
22+
23+
result = []
24+
while frontier:
25+
elem = frontier.pop()
26+
if elem:
27+
result.append(elem)
28+
29+
self.assertTrue( result == sorted_unique )
30+
31+
def test_delete_repeated(self):
32+
33+
test_data = [a_star.Node(x,x,None) for x in (1,1,1,1,1,1,1,1,1)]
34+
35+
# Prepare and insert elements in DijkstraHeap
36+
37+
frontier = a_star.DijkstraHeap()
38+
for elem in test_data:
39+
frontier.insert(elem)
40+
41+
# get out elements from the DijkstraHeap
42+
43+
result = []
44+
while frontier:
45+
elem = frontier.pop()
46+
if elem:
47+
result.append(elem)
48+
self.assertTrue( result == [a_star.Node(1,1,None)] )
49+
50+
51+
def test_came_from(self):
52+
53+
test_data = [a_star.Node(x,x,x+1) for x in range(10)]
54+
55+
# Prepare and insert elements in DijkstraHeap
56+
57+
frontier = a_star.DijkstraHeap()
58+
for elem in test_data:
59+
frontier.insert(elem)
60+
61+
# Drain the DijkstraHeap
62+
63+
while frontier:
64+
frontier.pop()
65+
66+
came_from_dic = { x:x+1 for x in range(10) }
67+
68+
69+
self.assertTrue( came_from_dic == frontier.visited )
70+
71+
def test_came_from_unique(self):
72+
73+
test_data = [a_star.Node(x,x,x+1) for x in [0,1,2,3,2,4,1]]
74+
75+
# Prepare and insert elements in DijkstraHeap
76+
77+
frontier = a_star.DijkstraHeap()
78+
for elem in test_data:
79+
frontier.insert(elem)
80+
81+
# Drain the DijkstraHeap
82+
83+
while frontier:
84+
frontier.pop()
85+
86+
came_from_dic = { x:x+1 for x in range(5) }
87+
88+
89+
self.assertTrue( came_from_dic == frontier.visited )
90+
91+
if __name__ == "__main__":
92+
unittest.main()

0 commit comments

Comments
 (0)