Beispiel #1
0
    def test_nodes(self):
        graph = Graph()
        self.assertEqual(graph.nodes(), set(), "Wrong nodes at initialization")
        ints = list(range(250, 275))
        for i in ints:
            graph.add_node(i)
        floats = [k / 10 for k in range(25, 50)]
        for j in floats:
            graph.add_node(j)

        original_list = ints + floats
        original_list.sort()
        nodes = list(graph.nodes())
        nodes.sort()
        self.assertEqual(nodes, original_list, "Wrong nodes implementation")

        nodes.append(10)
        self.assertNotEqual(nodes, original_list, "Wrong nodes implementation")
        nodes = list(graph.nodes())
        nodes.sort()
        self.assertEqual(nodes, original_list, "Wrong nodes implementation")

        graph.add_node(10000)
        original_list.append(10000)
        nodes = list(graph.nodes())
        nodes.sort()
        self.assertEqual(nodes, original_list, "Wrong nodes implementation")
Beispiel #2
0
    def test_add_node(self):
        with self.assertRaises(GraphTypeError):
            Graph(5)

        graph = Graph()
        with self.assertRaises(GraphElementError):
            graph.add_node(None)
        self.assertTrue(graph.size == 0)
        graph.add_node(1)
        graph.add_node(5.5)
        graph.add_node("string")
        self.assertEqual(len(graph), 3, "Wrong add_node implementation")
        self.assertEqual(graph.nodes(), {1, 5.5, "string"},
                         "Wrong add_node implementation")

        graph = Graph(int)
        with self.assertRaises(GraphTypeError):
            graph.add_node(5.0)
        self.assertEqual(len(graph.edges()), 5,
                         "Wrong number of edges at initialization")
        for i in range(100):
            graph.add_node(i * 2)
        self.assertEqual(len(graph.edges()), 160,
                         "Wrong add_node implementation")
        graph.add_node(2)
        self.assertEqual(len(graph), 100, "Wrong add_node implementation")
        graph.add_node(1)
        self.assertEqual(len(graph), 101, "Wrong add_node implementation")
        graph.remove_node(1)