예제 #1
0
    def test_get_edges_vertex(self):

        # (1) test a-->b and a-->c
        vertex = Vertex('a')
        solution = {('b', 1), ('c', 2)}
        vertex.adj['b'] = 1
        vertex.adj['c'] = 2
        subject = vertex.get_edges()
        self.assertEqual(subject, solution)

        # (2) test empty case
        vertex = Vertex('a')
        solution = set()
        subject = vertex.get_edges()
        self.assertEqual(subject, solution)

        # (3) test a-->letter for all letters in alphabet
        for i, char in enumerate(string.ascii_lowercase):
            vertex.adj[char] = i
            solution.add((char, i))
        subject = vertex.get_edges()
        self.assertEqual(subject, solution)
예제 #2
0
    def test_01_vertex_methods(self):

        ### degree ###

        vertex = Vertex('a')
        vertex.adj['b'] = 1
        assert vertex.degree() == 1
        vertex.adj['c'] = 3
        assert vertex.degree() == 2

        ### visit/reset ###

        assert not vertex.visited
        vertex.visit()
        assert vertex.visited
        vertex.reset()
        assert not vertex.visited

        ### get_edges ###

        vertex = Vertex('a')
        solution = [('b', 1), ('c', 2)]

        vertex.adj['b'] = 1
        vertex.adj['c'] = 2

        subject = vertex.get_edges()
        assert subject == solution

        ### euclidean_distance ###

        vertex_a = Vertex('a')
        vertex_b = Vertex('b', 3, 4)

        subject = vertex_a.euclidean_distance(vertex_b)
        assert subject == 5
        subject = vertex_b.euclidean_distance(vertex_a)
        assert subject == 5
예제 #3
0
    def test_06_get_edges(self):

        ### get_edge ###

        graph = Graph()

        # Neither vertex exists
        subject = graph.get_edge('a', 'b')
        assert subject is None

        # One vertex exists
        graph.vertices['a'] = Vertex('a')
        subject = graph.get_edge('a', 'b')
        assert subject is None

        # Both vertices exist, but no edge
        graph.vertices['a'] = Vertex('a')
        graph.vertices['b'] = Vertex('b')
        subject = graph.get_edge('a', 'b')
        assert subject is None

        # a -> b exists but b -> a does not
        graph.vertices.get('a').adj['b'] = 331
        subject = graph.get_edge('a', 'b')
        assert subject == ('a', 'b', 331)
        subject = graph.get_edge('b', 'a')
        assert subject is None

        ### get_edges ###

        graph = Graph()

        # Test empty graph
        subject = graph.get_edges()
        assert subject == []

        # Test graph with vertices but no edges
        graph.vertices['a'] = Vertex('a')
        graph.vertices['b'] = Vertex('b')
        subject = graph.get_edges()
        assert subject == []

        # Test graph with one edge
        graph.vertices.get('a').adj['b'] = 331
        subject = graph.get_edges()
        assert subject == [('a', 'b', 331)]

        # Test graph with two edges (compare setwise since dict does not guarantee ordering)
        graph = Graph()
        graph.vertices['a'] = Vertex('a')
        graph.vertices['b'] = Vertex('b')
        graph.vertices.get('a').adj['b'] = 331
        graph.vertices.get('b').adj['a'] = 1855
        subject = graph.get_edges()
        subject_set = set(subject)
        solution_set = set([('a', 'b', 331), ('b', 'a', 1855)])
        assert subject_set == solution_set

        # Test on two edges
        vertex = Vertex('a')
        vertex.adj['b'] = 331
        vertex.adj['c'] = 100
        vertex.adj['d'] = 662

        subject = vertex.get_edges()
        print(subject)