Skip to content

Commit e1809ca

Browse files
committed
Added testing for python and ruby
1 parent 45a7a19 commit e1809ca

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

dijkstras.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def shortest_path(self, start, finish):
1919
distances[vertex] = 0
2020
heapq.heappush(nodes, [0, vertex])
2121
else:
22-
distances[vertex] = sys.maxint
23-
heapq.heappush(nodes, [sys.maxint, vertex])
22+
distances[vertex] = sys.maxsize
23+
heapq.heappush(nodes, [sys.maxsize, vertex])
2424
previous[vertex] = None
2525

2626
while nodes:
@@ -31,7 +31,7 @@ def shortest_path(self, start, finish):
3131
path.append(smallest)
3232
smallest = previous[smallest]
3333
return path
34-
if distances[smallest] == sys.maxint: # All remaining vertices are inaccessible from source
34+
if distances[smallest] == sys.maxsize: # All remaining vertices are inaccessible from source
3535
break
3636

3737
for neighbor in self.vertices[smallest]: # Look at all the nodes that this vertex is attached to
@@ -48,14 +48,15 @@ def shortest_path(self, start, finish):
4848

4949
def __str__(self):
5050
return str(self.vertices)
51-
52-
g = Graph()
53-
g.add_vertex('A', {'B': 7, 'C': 8})
54-
g.add_vertex('B', {'A': 7, 'F': 2})
55-
g.add_vertex('C', {'A': 8, 'F': 6, 'G': 4})
56-
g.add_vertex('D', {'F': 8})
57-
g.add_vertex('E', {'H': 1})
58-
g.add_vertex('F', {'B': 2, 'C': 6, 'D': 8, 'G': 9, 'H': 3})
59-
g.add_vertex('G', {'C': 4, 'F': 9})
60-
g.add_vertex('H', {'E': 1, 'F': 3})
61-
print g.shortest_path('A', 'H')
51+
52+
if __name__ == '__main__':
53+
g = Graph()
54+
g.add_vertex('A', {'B': 7, 'C': 8})
55+
g.add_vertex('B', {'A': 7, 'F': 2})
56+
g.add_vertex('C', {'A': 8, 'F': 6, 'G': 4})
57+
g.add_vertex('D', {'F': 8})
58+
g.add_vertex('E', {'H': 1})
59+
g.add_vertex('F', {'B': 2, 'C': 6, 'D': 8, 'G': 9, 'H': 3})
60+
g.add_vertex('G', {'C': 4, 'F': 9})
61+
g.add_vertex('H', {'E': 1, 'F': 3})
62+
print(g.shortest_path('A', 'H'))

dijkstras.rb

+13-11
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ def shortest_path(start, finish)
5151
end
5252
end
5353
end
54-
return distances.inspect
54+
return distances
5555
end
5656

5757
def to_s
5858
return @vertices.inspect
5959
end
6060
end
6161

62-
g = Graph.new
63-
g.add_vertex('A', {'B' => 7, 'C' => 8})
64-
g.add_vertex('B', {'A' => 7, 'F' => 2})
65-
g.add_vertex('C', {'A' => 8, 'F' => 6, 'G' => 4})
66-
g.add_vertex('D', {'F' => 8})
67-
g.add_vertex('E', {'H' => 1})
68-
g.add_vertex('F', {'B' => 2, 'C' => 6, 'D' => 8, 'G' => 9, 'H' => 3})
69-
g.add_vertex('G', {'C' => 4, 'F' => 9})
70-
g.add_vertex('H', {'E' => 1, 'F' => 3})
71-
puts g.shortest_path('A', 'H')
62+
if __FILE__ == $0
63+
g = Graph.new
64+
g.add_vertex('A', {'B' => 7, 'C' => 8})
65+
g.add_vertex('B', {'A' => 7, 'F' => 2})
66+
g.add_vertex('C', {'A' => 8, 'F' => 6, 'G' => 4})
67+
g.add_vertex('D', {'F' => 8})
68+
g.add_vertex('E', {'H' => 1})
69+
g.add_vertex('F', {'B' => 2, 'C' => 6, 'D' => 8, 'G' => 9, 'H' => 3})
70+
g.add_vertex('G', {'C' => 4, 'F' => 9})
71+
g.add_vertex('H', {'E' => 1, 'F' => 3})
72+
puts g.shortest_path('A', 'H')
73+
end

dijkstras_test.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from dijkstras import Graph
3+
4+
class Graph_Test(unittest.TestCase):
5+
6+
#Runs before each test
7+
def setUp(self):
8+
self.graph = Graph()
9+
10+
def test_add_vertex(self):
11+
self.graph.add_vertex('A', {'B': 7, 'C': 8})
12+
self.assertEqual(self.graph.vertices, {'A': {'C': 8, 'B': 7}})
13+
14+
def test_shortest_path(self, ):
15+
self.graph.add_vertex('A', {'B': 7, 'C': 8})
16+
self.graph.add_vertex('B', {'A': 7, 'F': 2})
17+
self.graph.add_vertex('C', {'A': 8, 'F': 6, 'G': 4})
18+
self.graph.add_vertex('D', {'F': 8})
19+
self.graph.add_vertex('E', {'H': 1})
20+
self.graph.add_vertex('F', {'B': 2, 'C': 6, 'D': 8, 'G': 9, 'H': 3})
21+
self.graph.add_vertex('G', {'C': 4, 'F': 9})
22+
self.graph.add_vertex('H', {'E': 1, 'F': 3})
23+
24+
self.assertEqual(self.graph.shortest_path('A', 'H'), ['H', 'F', 'B'])
25+
self.assertEqual(self.graph.shortest_path('H', 'I'), {'A': 12, 'B': 5, 'C': 9, 'D': 11, 'E': 1, 'F': 3, 'G': 12, 'H': 0})
26+
27+
unittest.main()

dijkstras_test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'test/unit'
2+
require_relative 'dijkstras'
3+
4+
class Graph_Test < Test::Unit::TestCase
5+
6+
#Runs before each test
7+
def setup
8+
@graph = Graph.new
9+
end
10+
11+
def test_add_vertex
12+
@graph.add_vertex('A', {'B' => 7, 'C' => 8})
13+
assert_equal({"A"=>{"B"=>7, "C"=>8}}, @graph.instance_variable_get(:@vertices), "Failed to add vertex")
14+
end
15+
16+
def test_shortest_path
17+
@graph.add_vertex('A', {'B' => 7, 'C' => 8})
18+
@graph.add_vertex('B', {'A' => 7, 'F' => 2})
19+
@graph.add_vertex('C', {'A' => 8, 'F' => 6, 'G' => 4})
20+
@graph.add_vertex('D', {'F' => 8})
21+
@graph.add_vertex('E', {'H' => 1})
22+
@graph.add_vertex('F', {'B' => 2, 'C' => 6, 'D' => 8, 'G' => 9, 'H' => 3})
23+
@graph.add_vertex('G', {'C' => 4, 'F' => 9})
24+
@graph.add_vertex('H', {'E' => 1, 'F' => 3})
25+
26+
assert_equal(['H', 'F', 'B'], @graph.shortest_path('A', 'H'), 'Failed to find shortest path')
27+
assert_equal({'A' => 12, 'B' => 5, 'C' => 9, 'D' => 11, 'E' => 1, 'F' => 3, 'G' => 12, 'H' => 0}, @graph.shortest_path('H', 'I'), 'Failed to find shortest paths from root node')
28+
end
29+
end

0 commit comments

Comments
 (0)