예제 #1
0
class DirectedGraphTests(unittest.TestCase):
    def setUp(self):
        self.graph = DirectedGraph()

    def test_add_edge_non_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertEqual(self.graph.nodes, {"Ionko": ["Dingo"], "Dingo": []})

    def test_add_edge_only_one_existing_node(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.graph.add_edge("Dingo", "Penka")
        self.assertEqual(self.graph.nodes, {
            "Ionko": ["Dingo"],
            "Dingo": ["Penka"],
            "Penka": []
        })

    def test_add_edge_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.graph.add_edge("Dingo", "Ionko")
        self.assertEqual(self.graph.nodes, {
            "Ionko": ["Dingo"],
            "Dingo": ["Ionko"]
        })

    def test_get_neighbours_for_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertEqual(self.graph.get_neighbours_for("Ionko"), ["Dingo"])

    def test_get_neighbours_for_non_existing_nodes(self):
        self.assertEqual(self.graph.get_neighbours_for("ASDF"), [])

    def test_path_between_direct_neighbours(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertTrue(self.graph.path_between("Ionko", "Dingo"))
        self.assertFalse(self.graph.path_between("Dingo", "Ionko"))

    def test_path_between_cyclic_neighbours(self):
        self.graph.add_edge("1", "2")
        self.graph.add_edge("2", "3")
        self.graph.add_edge("2", "4")
        self.graph.add_edge("4", "5")
        self.graph.add_edge("4", "6")

        self.graph.add_edge("3", "2")

        self.assertTrue(self.graph.path_between("2", "6"))
        self.assertFalse(self.graph.path_between("6", "2"))

    def test_path_between_indirect_neighbours(self):
        self.graph.add_edge("1", "2")
        self.graph.add_edge("2", "3")
        self.graph.add_edge("2", "4")
        self.graph.add_edge("4", "5")
        self.graph.add_edge("4", "6")

        self.assertTrue(self.graph.path_between("1", "6"))
        self.assertFalse(self.graph.path_between("6", "5"))
예제 #2
0
class DirectedGraphTests(unittest.TestCase):

    def setUp(self):
        self.graph = DirectedGraph()

    def test_add_edge_non_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertEqual(self.graph.nodes, {"Ionko": ["Dingo"], "Dingo": []})

    def test_add_edge_only_one_existing_node(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.graph.add_edge("Dingo", "Penka")
        self.assertEqual(self.graph.nodes,
                         {"Ionko": ["Dingo"], "Dingo": ["Penka"], "Penka": []})

    def test_add_edge_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.graph.add_edge("Dingo", "Ionko")
        self.assertEqual(self.graph.nodes,
                         {"Ionko": ["Dingo"], "Dingo": ["Ionko"]})

    def test_get_neighbours_for_existing_nodes(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertEqual(self.graph.get_neighbours_for("Ionko"),
                         ["Dingo"])

    def test_get_neighbours_for_non_existing_nodes(self):
        self.assertEqual(self.graph.get_neighbours_for("ASDF"), [])

    def test_path_between_direct_neighbours(self):
        self.graph.add_edge("Ionko", "Dingo")
        self.assertTrue(self.graph.path_between("Ionko", "Dingo"))
        self.assertFalse(self.graph.path_between("Dingo", "Ionko"))

    def test_path_between_cyclic_neighbours(self):
        self.graph.add_edge("1", "2")
        self.graph.add_edge("2", "3")
        self.graph.add_edge("2", "4")
        self.graph.add_edge("4", "5")
        self.graph.add_edge("4", "6")

        self.graph.add_edge("3", "2")

        self.assertTrue(self.graph.path_between("2", "6"))
        self.assertFalse(self.graph.path_between("6", "2"))

    def test_path_between_indirect_neighbours(self):
        self.graph.add_edge("1", "2")
        self.graph.add_edge("2", "3")
        self.graph.add_edge("2", "4")
        self.graph.add_edge("4", "5")
        self.graph.add_edge("4", "6")

        self.assertTrue(self.graph.path_between("1", "6"))
        self.assertFalse(self.graph.path_between("6", "5"))
예제 #3
0
class GraphTest (unittest.TestCase):

    def setUp(self):
        self.sample_graph = DirectedGraph()

    def test_graph_init(self):
        self.assertEqual(self.sample_graph.graph, {})

    def test_add_edge(self):
        self.sample_graph.add_edge("A", "B")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("B", self.sample_graph.graph["A"])

        self.sample_graph.add_edge("A", "D")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("D", self.sample_graph.graph["A"])

        self.sample_graph.add_edge("B", "C")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("B", self.sample_graph.graph["A"])
        self.assertIn("B", self.sample_graph.graph)
        self.assertIn("C", self.sample_graph.graph["B"])

    def test_get_neighbours_for(self):
        self.sample_graph.add_edge("A", "B")
        self.sample_graph.add_edge("A", "D")
        self.assertEqual(["B", "D"], self.sample_graph.get_neighbours_for("A"))

    def test_path_between(self):
        self.sample_graph.add_edge("A", "B")
        self.sample_graph.add_edge("B", "C")
        self.assertTrue(self.sample_graph.path_between("A", "C"))
        self.assertFalse(self.sample_graph.path_between("A", "D"))
        self.assertFalse(self.sample_graph.path_between("A", "E"))

    def test_to_str(self):
        self.sample_graph.add_edge("A", "B")
        self.assertEqual(self.sample_graph.to_str(), "A ---> ['B']")
예제 #4
0
class TestGraph(unittest.TestCase):
    def setUp(self):
        self.my_graph = DirectedGraph()

    def test_init(self):
        self.assertEqual(self.my_graph.nodes, {})

    def test_add_edge(self):
        self.my_graph.add_edge("modzozo", "RadoRado")
        self.assertEqual(self.my_graph.nodes, {"modzozo": ["RadoRado"], "RadoRado": []})

    def test_get_neighbours(self):
        self.my_graph.add_edge("modzozo", "RadoRado")
        self.my_graph.add_edge("modzozo", "mod")
        self.assertEqual(self.my_graph.get_neighbours_for("modzozo"), ["RadoRado", "mod"])

    def test_path_between_true(self):
        self.my_graph.add_edge("modzozo", "RadoRado")
        self.my_graph.add_edge("modzozo", "mod")
        self.my_graph.add_edge("RadoRado", "Rado")
        self.my_graph.add_edge("Rado", "zozo")
        self.assertTrue(self.my_graph.path_between("modzozo", "zozo"))

    def test_path_between_false(self):
        self.my_graph.add_edge("modzozo", "RadoRado")
        self.my_graph.add_edge("modzozo", "mod")
        self.my_graph.add_edge("RadoRado", "Rado")
        self.my_graph.add_edge("Rado", "zozo")
        self.assertFalse(self.my_graph.path_between("Rado", "modzozo"))

    def test_path_between_cycle(self):
        self.my_graph.add_edge("modzozo", "RadoRado")
        self.my_graph.add_edge("modzozo", "mod")
        self.my_graph.add_edge("RadoRado", "Rado")
        self.my_graph.add_edge("Rado", "zozo")
        self.my_graph.add_edge("zozo", "modzozo")
        self.assertTrue(self.my_graph.path_between("modzozo", "zozo"))
예제 #5
0
class GraphTest(unittest.TestCase):
    def setUp(self):
        self.sample_graph = DirectedGraph()

    def test_graph_init(self):
        self.assertEqual(self.sample_graph.graph, {})

    def test_add_edge(self):
        self.sample_graph.add_edge("A", "B")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("B", self.sample_graph.graph["A"])

        self.sample_graph.add_edge("A", "D")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("D", self.sample_graph.graph["A"])

        self.sample_graph.add_edge("B", "C")
        self.assertIn("A", self.sample_graph.graph)
        self.assertIn("B", self.sample_graph.graph["A"])
        self.assertIn("B", self.sample_graph.graph)
        self.assertIn("C", self.sample_graph.graph["B"])

    def test_get_neighbours_for(self):
        self.sample_graph.add_edge("A", "B")
        self.sample_graph.add_edge("A", "D")
        self.assertEqual(["B", "D"], self.sample_graph.get_neighbours_for("A"))

    def test_path_between(self):
        self.sample_graph.add_edge("A", "B")
        self.sample_graph.add_edge("B", "C")
        self.assertTrue(self.sample_graph.path_between("A", "C"))
        self.assertFalse(self.sample_graph.path_between("A", "D"))
        self.assertFalse(self.sample_graph.path_between("A", "E"))

    def test_to_str(self):
        self.sample_graph.add_edge("A", "B")
        self.assertEqual(self.sample_graph.to_str(), "A ---> ['B']")
예제 #6
0
 def test_get_neighbours_for_not_empty(self):
     newGraph = DirectedGraph()
     newGraph.add_edge("1", "2")
     newGraph.add_edge("1", "3")
     self.assertEqual(["2", "3"], newGraph.get_neighbours_for("1"))