Beispiel #1
0
 def test_add_two_edges(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     test_list = [(g.node_dict, {
         "new": [],
         "node": [],
         "test": []
     }),
                  (g.adjacency_matrix, {
                      "new": {
                          "new": 999999,
                          "node": 999998,
                          "test": 999999
                      },
                      "node": {
                          "new": 999999,
                          "node": 999999,
                          "test": 45
                      },
                      "test": {
                          "new": 999999,
                          "node": 999999,
                          "test": 999999
                      }
                  }), (g.nb_nodes, 3), (g.nb_edges, 2)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)
Beispiel #2
0
 def test_get_dist_matrix(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     mat = g.get_dist_matrix()
     self.assertEqual(
         mat, {
             "new": {
                 "new": 999999,
                 "node": 999998,
                 "test": 999999
             },
             "node": {
                 "new": 999999,
                 "node": 999999,
                 "test": 45
             },
             "test": {
                 "new": 999999,
                 "node": 999999,
                 "test": 999999
             }
         })
Beispiel #3
0
 def test_out_degree(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     deg = g.out_degree("new")
     self.assertEqual(deg, 1)
Beispiel #4
0
 def test_get_out_neighbors(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     neighbors = g.get_out_neighbors("new")
     self.assertEqual(neighbors, ["node"])
Beispiel #5
0
 def test_edge_list(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     edges = g.edge_list()
     self.assertEqual(edges, [("new", "node", 999998),
                              ("node", "test", 45)])
Beispiel #6
0
 def test_out_degree_unknown_key(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     with self.assertRaises(IndexError) as context:
         g.out_degree("blip")
         self.assertTrue(context.exception)
Beispiel #7
0
 def test_get_in_neighbors_unknown_key(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     with self.assertRaises(IndexError) as context:
         g.get_in_neighbors("blip")
         self.assertTrue(context.exception)
Beispiel #8
0
 def test_str_full(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     out = io.StringIO()
     sys.stdout = out
     print(g)
     self.assertEqual(
         out.getvalue(),
         "{'new': {'new': 999999, 'node': 999998, 'test': 999999}, "
         "'node': {'node': 999999, 'new': 999999, 'test': 45}, "
         "'test': {'test': 999999, 'new': 999999, 'node': 999999}}\n")
Beispiel #9
0
 def test_add_the_same_edge_twice(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_edge("new", "node")
     g.add_edge("new", "node")
     test_list = [(g.node_dict, {
         "new": [],
         "node": []
     }),
                  (g.adjacency_matrix, {
                      "new": {
                          "new": 999999,
                          "node": 999998
                      },
                      "node": {
                          "new": 999999,
                          "node": 999999
                      }
                  }), (g.nb_nodes, 2), (g.nb_edges, 1)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)