예제 #1
0
 def test_connected_componenets(self):
     print('List all connected components ...')
     self.my_test_graph = my_graph({"A": {"B"}, "B": {"A"}})
     self.my_test_graph2 = my_graph({
         "A": {"B"},
         "B": {"A"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertTrue(
         self.my_test_graph.connected_components() == [{'A', 'B'}]
         or self.my_test_graph.list_edges() == [{'B', 'A'}])
     self.assertTrue(
         self.my_test_graph2.connected_components() == [{'A', 'B'},
                                                        {'E', 'D'}]
         or self.my_test_graph2.list_edges() == [{'A', 'B'}, {'D', 'E'}]
         or self.my_test_graph2.list_edges() == [{'B', 'A'}, {'D', 'E'}]
         or self.my_test_graph2.list_edges() == [{'B', 'A'}, {'E', 'D'}])
예제 #2
0
 def test_list_edges(self):
     print('If check to make sure that all edges are listed ...')
     self.my_test_graph = my_graph({"A": {"B"}, "B": {"A"}})
     self.my_test_graph2 = my_graph({
         "A": {"C"},
         "B": {"C"},
         "C": {"A", "B"}
     })
     print(self.my_test_graph)
     self.assertTrue(self.my_test_graph.list_edges() == {('A', 'B')}
                     or self.my_test_graph.list_edges() == {('B', 'A')})
     self.assertTrue(self.my_test_graph2.list_edges() == {('A', 'C'),
                                                          ('B', 'C')}
                     or self.my_test_graph2.list_edges() == {('C', 'A'),
                                                             ('C', 'B')}
                     or self.my_test_graph2.list_edges() == {('A', 'C'),
                                                             ('C', 'B')}
                     or self.my_test_graph2.list_edges() == {('C', 'A'),
                                                             ('B', 'C')})
예제 #3
0
 def test_is_edge_throws_error_if_both_vertices_are_same(self):
     print('If both vertixes are same, function should raise an error ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "A", "A")
예제 #4
0
 def test_is_edge_throws_error_if_enough_arguments_not_provided(self):
     print(
         'If gewers than arguments are provided should raise an error ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(TypeError, self.my_test_graph.is_edge, "2")
예제 #5
0
 def test_is_edge_on_conncted_vertices_returns_false(self):
     print(
         'check if the given two vertices are conncted via an edge. This test should return false...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertEqual(self.my_test_graph.is_edge("A", "E"), False)
예제 #6
0
 def test_add_vertex_that_exists_should_throw_error(self):
     print(
         'if you try to add a vertex that exists then there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.add_vertex, "A")
예제 #7
0
 def test_list_nieghbors(self):
     print('If check to make sure that all edges are listed ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertEqual(self.my_test_graph.list_neighbors("A"), {"B", "C"})
     self.assertEqual(self.my_test_graph.list_neighbors("B"), {"A", "C"})
     self.assertEqual(self.my_test_graph.list_neighbors("D"), {"E"})
예제 #8
0
 def test_is_edge_throws_error_if_asking_for_non_existent_vertex(self):
     print('If vertex does not exist, function should raise an error ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "A", "2")
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "2", "A")
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "2", "1")
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "", "B")
예제 #9
0
 def test_add_vertex_for_bad_values_vertex_should_throw_error(self):
     print(
         'if you try to add an vertex with bad value there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.add_vertex, 1)
     self.assertRaises(ValueError, self.my_test_graph.add_vertex, [1])
예제 #10
0
 def test_add_edge_for_an_edge_that_exists_should_throw_error(self):
     print(
         'if you try to add an edge that exists already there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "A", "B")
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "B", "A")
예제 #11
0
 def test_add_edge_for_bad_values_vertex_should_throw_error(self):
     print(
         'if you try to add an edge for a non string vertex then there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.add_edge, 1, "A")
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "A", 1)
     self.assertRaises(ValueError, self.my_test_graph.add_edge, 1, 1)
예제 #12
0
 def test_if_path_exists_between_vertices_return_false(self):
     print('check to see if path exists. Should return false ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertEqual(self.my_test_graph.path("A", "E"), False)
     self.assertEqual(self.my_test_graph.path("B", "D"), False)
     self.assertEqual(self.my_test_graph.path("D", "A"), False)
     self.assertEqual(self.my_test_graph.path("D", "B"), False)
     self.assertEqual(self.my_test_graph.path("D", "C"), False)
     self.assertEqual(self.my_test_graph.path("C", "D"), False)
예제 #13
0
 def test_path_wrong_vertices_should_return_error(self):
     print('check path with wrong vertices. Should return error ...')
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.path, [1], "A")
     self.assertRaises(ValueError, self.my_test_graph.path, "U", "A")
     self.assertRaises(ValueError, self.my_test_graph.path, "A", "Q")
     self.assertRaises(ValueError, self.my_test_graph.path, "A", "")
     self.assertRaises(ValueError, self.my_test_graph.path, "A", 1)
     self.assertRaises(TypeError, self.my_test_graph.path, "A")
예제 #14
0
 def test_is_edge_throws_error_if_type_of_vertex_arguemnt_is_not_string(
         self):
     print(
         'If you provide a non-string argument to is edge function should raise an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.is_edge, "A", 2)
     self.assertRaises(ValueError, self.my_test_graph.is_edge, 2, "A")
     self.assertRaises(ValueError, self.my_test_graph.is_edge, 1, 2)
예제 #15
0
 def test_add_edge_between_vertices_that_were_disconnected(self):
     print(
         'You should be able to add an edge between vertices that were previously disconnected ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.my_test_graph.add_edge("C", "D")
     self.assertEqual(self.my_test_graph.list_neighbors("C"),
                      {'A', 'B', 'D'})
     self.assertEqual(self.my_test_graph.list_neighbors("D"), {'E', 'C'})
예제 #16
0
 def test_add_edge_for_non_existent_vertex_should_throw_error(self):
     print(
         'if you try to add an edge for a non-existent vertex then there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "A", "A")
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "A", "Z")
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "Z", "A")
     self.assertRaises(ValueError, self.my_test_graph.add_edge, "Z", "Q")
예제 #17
0
 def test_add_vertex(self):
     print(
         'if you try to add an edge for a non-existent vertex then there should be an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.my_test_graph.add_vertex("H")
     self.my_test_graph.add_edge("H", "A")
     self.assertEqual(self.my_test_graph.list_neighbors("H"), {'A'})
     self.assertEqual(self.my_test_graph.list_neighbors("A"),
                      {'B', 'C', 'H'})
예제 #18
0
 def test_if_list_neighbors_if_type_of_vertex_arguemnt_is_not_string(self):
     print(
         'If you list neighbor argument to is edge function should raise an error ...'
     )
     self.my_test_graph = my_graph({
         "A": {"B", "C"},
         "B": {"A", "C"},
         "C": {"A", "B"},
         "D": {"E"},
         "E": {"D"}
     })
     print(self.my_test_graph)
     self.assertRaises(TypeError, self.my_test_graph.is_edge, "H")
     self.assertRaises(TypeError, self.my_test_graph.is_edge, "2")
     self.assertRaises(TypeError, self.my_test_graph.is_edge, 1)
     self.assertRaises(TypeError, self.my_test_graph.is_edge, [1])
     self.assertRaises(TypeError, self.my_test_graph.is_edge)