- Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtest_basic.py
More file actions
Latest commit
58 lines (48 loc) · 1.72 KB
/
Copy pathtest_basic.py
File metadata and controls
58 lines (48 loc) · 1.72 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
importunittest
fromastarimportAStar
classBasicAStar(AStar):
def__init__(self, nodes):
self.nodes=nodes
defneighbors(self, n):
forn1, dinself.nodes[n]:
yieldn1
defdistance_between(self, n1, n2):
forn, dinself.nodes[n1]:
ifn==n2:
returnd
defheuristic_cost_estimate(self, current, goal):
return1
defis_goal_reached(self, current, goal):
returncurrent==goal
classBasicTests(unittest.TestCase):
deftest_bestpath(self):
"""ensure that we take the shortest path, and not the path with less elements.
the path with less elements is A -> B with a distance of 100
the shortest path is A -> C -> D -> B with a distance of 60
"""
nodes= {'A': [('B', 100), ('C', 20)],
'C': [('D', 20)],
'D': [('B', 20)]}
path=BasicAStar(nodes).astar('A', 'B')
self.assertIsNotNone(path)
ifpath:
path=list(path)
self.assertEqual(4, len(path))
fori, ninenumerate('ACDB'):
self.assertEqual(n, path[i])
deftest_issue_15(self):
"""This test case reproduces https://github.com/jrialland/python-astar/issues/15.
B has no neighbors, therefore the computation should return None and not raise an exception.
"""
node= {
'A': [('B', 200000)],
'C': [('D', 200000)],
'D': [('E', 200000)],
'E': [('F', 200000)],
'B': [],
'F': []
}
path=BasicAStar(node).astar('A', 'D')
self.assertIsNone(path)
if__name__=='__main__':
unittest.main()