コード例 #1
0
 def test_add_edges_without_nodes(self):
     """Tests adges without nodes"""
     g = WeightedDigraph()
     g.add_edges([('Denver', 'Boston', 1971.8),
                  ('Boston', 'Denver', 1971.8)])
     self.assertEqual(str(g),
                      'Denver->Boston(1971.8)\nBoston->Denver(1971.8)\n')
コード例 #2
0
 def test_are_adjacent(self):
     """Tests adjacency"""
     g = WeightedDigraph()
     g.add_nodes(['Denver', 'Boston'])
     g.add_edges([('Denver', 'Boston', 1971.8),
                  ('Boston', 'Denver', 1971.8)])
     self.assertTrue(g.are_adjacent('Denver', 'Boston'))
コード例 #3
0
 def test_arent_adjacent(self):
     """Tests non-adjacency"""
     g = WeightedDigraph()
     g.add_nodes(['Denver', 'Boston', 'Milano'])
     g.add_edges([('Denver', 'Boston', 1971.8),
                  ('Boston', 'Denver', 1971.8)])
     self.assertFalse(g.are_adjacent('Denver', 'Milano'))
コード例 #4
0
 def test_arent_adjacent_directed(self):
     """Tests non-adjacency directed"""
     g = WeightedDigraph()
     g.add_edges([('Denver', 'Boston', 1971.8)])
     self.assertFalse(g.are_adjacent('Denver', 'Milano'))
     self.assertFalse(g.are_adjacent('Boston', 'Denver'))
     self.assertTrue(g.are_adjacent('Denver', 'Boston'))
コード例 #5
0
 def test_edge(self):
     """Tests digraph edges"""
     g = WeightedDigraph()
     g.add_node(1)
     g.add_node(2)
     g.add_edge(1, 2, 3)
     self.assertEqual(str(g), '1->2(3)\n2\n')
コード例 #6
0
 def test_adding_strings(self):
     """Tests adding strings"""
     g = WeightedDigraph()
     g.add_nodes(['Denver', 'Boston'])
     g.add_edges([('Denver', 'Boston', 1971.8),
                  ('Boston', 'Denver', 1971.8)])
     self.assertEqual(str(g),
                      'Denver->Boston(1971.8)\nBoston->Denver(1971.8)\n')
コード例 #7
0
 def test_infandnone(self):
     """Testing dijkstras"""
     g = WeightedDigraph(False)
     g.add_edges([(1, 2, 2), (1, 3, 1), (2, 3, 1), (2, 4, 1), (2, 5, 2),
                  (3, 5, 5), (4, 5, 3), (4, 6, 6), (5, 6, 1)])
     g.infandnone()
     for node in g:
         self.assertEqual(node.distance, float("inf"))
         self.assertEqual(node.previous, None)
コード例 #8
0
 def test_dijkstra(self):
     """Testing dijkstras"""
     # '''
     #     2---1---4
     #    /|\      |\
     #   2 | \     | 6
     #  /  |  \    |  \
     # 1   1   2   3   6
     #  \  |    \  |  /
     #   1 |     \ | 1
     #    \|      \|/
     #     3---5---5
     # '''
     g = WeightedDigraph(False)
     g.add_edges([(1, 2, 2), (1, 3, 1), (2, 3, 1), (2, 4, 1), (2, 5, 2),
                  (3, 5, 5), (4, 5, 3), (4, 6, 6), (5, 6, 1)])
     if not TRACK_PREVIOUS:
         self.assertEqual(g.dijkstra(1),
                          [[0, 1], [1, 3], [2, 2], [3, 4], [4, 5], [5, 6]])
     else:
         self.assertEqual(g.dijkstra(1),
                          [[0, 1], [1, 3, 1], [2, 2, 1], [3, 4, 2, 1],
                           [4, 5, 2, 1], [5, 6, 5, 2, 1]])
コード例 #9
0
 def empty_graph(self):
     """Testing dijkstras"""
     self.assertRaises(RuntimeError, lambda: WeightedDigraph.dijkstra(0))
コード例 #10
0
 def test_empty(self):
     """Tests if digraph is empty"""
     self.assertEqual(len(WeightedDigraph()), 0)
コード例 #11
0
 def test_adding_ints(self):
     """Tests adding ints"""
     g = WeightedDigraph()
     g.add_nodes([1, 2])
     g.add_edges([(1, 2, 3), (2, 1, 3)])
     self.assertEqual(str(g), '1->2(3)\n2->1(3)\n')
コード例 #12
0
 def test_two(self):
     """Tests if digraph has 2"""
     g = WeightedDigraph()
     g.add_node(1)
     g.add_node(2)
     self.assertEqual(len(g), 2)
コード例 #13
0
 def test_duplicate(self):
     """Tests if digraph has duplicates"""
     g = WeightedDigraph()
     g.add_node(1)
     g.add_node(1)
     self.assertEqual(len(g), 1)
コード例 #14
0
 def test_one(self):
     """Tests if digraph has one"""
     g = WeightedDigraph()
     g.add_node(1)
     self.assertEqual(len(g), 1)
コード例 #15
0
 def unfound_start(self):
     """Testing dijkstras"""
     g = WeightedDigraph(False)
     g.add_nodes(["Denver"])
     self.assertRaises(RuntimeError, lambda: WeightedDigraph.dijkstra(None))