Skip to content

Commit

Permalink
First draft of a test_graph.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-poloni committed Feb 17, 2020
1 parent 5196aee commit 6f5473f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 68 deletions.
68 changes: 0 additions & 68 deletions projects/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,71 +75,3 @@ def dfs_recursive(self, starting_vertex):
This should be done using recursion.
"""
pass # TODO

if __name__ == '__main__':
graph = Graph() # Instantiate your graph
# https://github.com/LambdaSchool/Graphs/blob/master/objectives/breadth-first-search/img/bfs-visit-order.png
graph.add_vertex(1)
graph.add_vertex(2)
graph.add_vertex(3)
graph.add_vertex(4)
graph.add_vertex(5)
graph.add_vertex(6)
graph.add_vertex(7)
graph.add_edge(5, 3)
graph.add_edge(6, 3)
graph.add_edge(7, 1)
graph.add_edge(4, 7)
graph.add_edge(1, 2)
graph.add_edge(7, 6)
graph.add_edge(2, 4)
graph.add_edge(3, 5)
graph.add_edge(2, 3)
graph.add_edge(4, 6)

'''
Should print:
{1: {2}, 2: {3, 4}, 3: {5}, 4: {6, 7}, 5: {3}, 6: {3}, 7: {1, 6}}
'''
print(graph.vertices)

'''
Valid BFT paths:
1, 2, 3, 4, 5, 6, 7
1, 2, 3, 4, 5, 7, 6
1, 2, 3, 4, 6, 7, 5
1, 2, 3, 4, 6, 5, 7
1, 2, 3, 4, 7, 6, 5
1, 2, 3, 4, 7, 5, 6
1, 2, 4, 3, 5, 6, 7
1, 2, 4, 3, 5, 7, 6
1, 2, 4, 3, 6, 7, 5
1, 2, 4, 3, 6, 5, 7
1, 2, 4, 3, 7, 6, 5
1, 2, 4, 3, 7, 5, 6
'''
graph.bft(1)

'''
Valid DFT paths:
1, 2, 3, 5, 4, 6, 7
1, 2, 3, 5, 4, 7, 6
1, 2, 4, 7, 6, 3, 5
1, 2, 4, 6, 3, 5, 7
'''
graph.dft(1)
graph.dft_recursive(1)

'''
Valid BFS path:
[1, 2, 4, 6]
'''
print(graph.bfs(1, 6))

'''
Valid DFS paths:
[1, 2, 4, 6]
[1, 2, 4, 7, 6]
'''
print(graph.dfs(1, 6))
print(graph.dfs_recursive(1, 6))
109 changes: 109 additions & 0 deletions projects/graph/test_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import unittest
import sys
import io
from graph import Graph

class Test(unittest.TestCase):
def setUp(self):
self.graph = Graph()
self.graph.add_vertex(1)
self.graph.add_vertex(2)
self.graph.add_vertex(3)
self.graph.add_vertex(4)
self.graph.add_vertex(5)
self.graph.add_vertex(6)
self.graph.add_vertex(7)
self.graph.add_edge(5, 3)
self.graph.add_edge(6, 3)
self.graph.add_edge(7, 1)
self.graph.add_edge(4, 7)
self.graph.add_edge(1, 2)
self.graph.add_edge(7, 6)
self.graph.add_edge(2, 4)
self.graph.add_edge(3, 5)
self.graph.add_edge(2, 3)
self.graph.add_edge(4, 6)

def test_vertices(self):
vertices = {1: {2}, 2: {3, 4}, 3: {5}, 4: {6, 7}, 5: {3}, 6: {3}, 7: {1, 6}}
self.assertDictEqual(self.graph.vertices, vertices)

def test_bft(self):
bft = [
"1\n2\n3\n4\n5\n6\n7\n",
"1\n2\n3\n4\n5\n7\n6\n",
"1\n2\n3\n4\n6\n7\n5\n",
"1\n2\n3\n4\n6\n5\n7\n",
"1\n2\n3\n4\n7\n6\n5\n",
"1\n2\n3\n4\n7\n5\n6\n",
"1\n2\n4\n3\n5\n6\n7\n",
"1\n2\n4\n3\n5\n7\n6\n",
"1\n2\n4\n3\n6\n7\n5\n",
"1\n2\n4\n3\n6\n5\n7\n",
"1\n2\n4\n3\n7\n6\n5\n",
"1\n2\n4\n3\n7\n5\n6\n"
]

stdout_ = sys.stdout
sys.stdout = io.StringIO()
self.graph.bft(1)
output = sys.stdout.getvalue()

self.assertIn(output, bft)

sys.stdout = stdout_ # Restore stdout

def test_dft(self):
dft = [
"1\n2\n3\n5\n4\n6\n7\n",
"1\n2\n3\n5\n4\n7\n6\n",
"1\n2\n4\n7\n6\n3\n5\n",
"1\n2\n4\n6\n3\n5\n7\n"
]

stdout_ = sys.stdout
sys.stdout = io.StringIO()
self.graph.dft(1)
output = sys.stdout.getvalue()

self.assertIn(output, dft)

sys.stdout = stdout_ # Restore stdout

def test_dft_recursive(self):
dft = [
"1\n2\n3\n5\n4\n6\n7\n",
"1\n2\n3\n5\n4\n7\n6\n",
"1\n2\n4\n7\n6\n3\n5\n",
"1\n2\n4\n6\n3\n5\n7\n"
]

stdout_ = sys.stdout
sys.stdout = io.StringIO()
self.graph.dft_recursive(1)
output = sys.stdout.getvalue()

self.assertIn(output, dft)

sys.stdout = stdout_ # Restore stdout

def test_bfs(self):
bfs = [1, 2, 4, 6]
self.assertListEqual(self.graph.bfs(1, 6), bfs)

def test_dfs(self):
dfs = [
[1, 2, 4, 6],
[1, 2, 4, 7, 6]
]
self.assertIn(self.graph.dfs(1,6), dfs)

def test_dfs_recursive(self):
dfs = [
[1, 2, 4, 6],
[1, 2, 4, 7, 6]
]
self.assertIn(self.graph.dfs_recursive(1,6), dfs)

if __name__ == '__main__':
unittest.main()

0 comments on commit 6f5473f

Please sign in to comment.