def test_make_graph(self): expected = Graph(directed=False) # Top-Down expected.add_edge((0, 0), (1, 0)) expected.add_edge((1, 0), (2, 0)) expected.add_edge((0, 1), (1, 1)) expected.add_edge((1, 1), (2, 1)) # Left-Right expected.add_edge((0, 0), (0, 1)) expected.add_edge((1, 0), (1, 1)) expected.add_edge((1, 1), (1, 2)) expected.add_edge((2, 0), (2, 1)) # Diagonal expected.add_edge((0, 0), (1, 1)) expected.add_edge((0, 1), (1, 2)) expected.add_edge((2, 0), (1, 1)) expected.add_edge((2, 1), (1, 2)) actual = self.simple_game.make_graph() self.assertSetEqual(set(expected.vertices()), set(actual.vertices())) self.assertSetEqual(set(frozenset(edge) for edge in expected.edges()), set(frozenset(edge) for edge in actual.edges()))
ok(g.has_vertex(3)) ok(g.has_vertex(4)) ok(not g.has_vertex(5)) ok(not g.has_vertex(0)) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 3)) ok(not g.has_edge(2, 1)) ok(not g.has_edge(1, 3)) ok(not g.has_edge(4, 1)) eq(len(g.vertices()), 4) eq(len(g.successors(1)), 1) eq(len(g.predecessors(1)), 0) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2) eq(len(g.edges()), 4) eq(len(g.unique_edges()), 3) eq(len(g.edges_from(1)), 2) eq(len(g.edges_to(1)), 0) eq(len(g.edges_at(1)), 2) eq(len(g.edges_from(2)), 1) eq(len(g.edges_to(2)), 2) eq(len(g.edges_at(2)), 3) eq(g.degree(1), 2)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) g = g.create_graph('tree', 10) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10 - 1) g = Graph(directed=True, multiedged=True) g = g.create_graph('tree', 100) ok(g) eq(len(g.vertices()), 100) eq(len(g.edges()), 100 - 1) g = Graph(directed=False, multiedged=True) g = g.create_graph('tree', 10) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10 - 1) ok(g.is_connected())
class SolverTest(unittest.TestCase): def setUp(self): self.test_solver = solver.Solver() self.field1 = TriangleField([[1], [1, 0, 2], [3, 0, 3, 0, 2]]) self.field2 = TriangleField([[1], [1, 0, 2], [1, 0, 3, 0, 2], [3, 0, 0, 0, 4, 0, 4]]) self.field3 = TriangleField([[1], [1, 0, 2]]) self.graph = Graph(directed=False) self.graph.add_edge('a', 'b') self.graph.add_edge('a', 'c') self.graph.add_edge('c', 'b') self.graph.add_edge('b', 'd') def test_update_neighbors(self): self.method_tester( expected={ 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, parent_node=Node(self.graph.edges()[0], {v: v for v in self.graph.vertices()}, 1), main_path=list('abcd')) self.method_tester(expected={ 'b': 'c', 'c': 'b', 'd': 'd' }, parent_node=Node(self.graph.edges()[1], { 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bcd')) self.method_tester(expected={ 'b': 0, 'd': 'd' }, parent_node=Node(self.graph.edges()[2], { 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bd')) def method_tester(self, expected, parent_node, main_path): actual = self.test_solver.update_main_path( self.test_solver.update_neighbors(parent_node), main_path) self.assertDictEqual(expected, actual) def test_solve(self): expected = [[(0, 0), (1, 1)], [(1, 0), (1, 1)], [(1, 2), (2, 3)], [(2, 0), (2, 1)], [(2, 1), (2, 2)], [(2, 3), (2, 4)]] actual = list(self.test_solver.solve(self.field1)) assert len(actual) == 64 self.assertListEqual(expected, actual[0]) expected = [] actual = list(self.test_solver.solve(self.field2)) self.assertListEqual(expected, actual) actual = list(self.test_solver.solve(self.field3)) self.assertListEqual(expected, actual)
#!/usr/bin/env python3 import re from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) buf = """digraph sample { 1; 2; 1 -> 2; } """.splitlines() g.import_graph('dot', buf) eq(len(g.vertices()), 2) eq(len(g.edges()), 1) ok(g.is_directed()) ok(g.has_edge(1, 2)) g = Graph(directed=True, multiedged=True) buf = """// comment here digraph sample { 1; /* another comment here */ 2; 4; 1 -> 2; 1 -> 4; } """.splitlines()
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) g.create_configuration_graph([3, 2, 2, 1, 1, 1, 0]) ok(g.degree(1) == 3) ok(g.degree(2) == 2) ok(g.degree(3) == 2) ok(g.degree(4) == 1) ok(g.degree(5) == 1) ok(g.degree(6) == 1) ok(g.degree(7) == 0) g = Graph(directed=False) g.create_configuration_graph([3, 3, 3, 3, 3, 3]) for v in g.vertices(): ok(g.degree(v) == 3) g = Graph(directed=False) g.create_random_regular_graph(10, 3) for v in g.vertices(): ok(g.degree(v) == 3)
class SolverTest(unittest.TestCase): def setUp(self): self.instance_one_solution = HexLink([ [1, 0], # 1 0 [0, 2, 1], # 0 2 1 [0, 2] # 0 2 ]) self.instance_many_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [1, 2], # 1 2 ]) self.instance_no_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [2, 1] # 2 1 ]) self.graph = Graph(directed=False) self.graph.add_edge("p", "q") # e_1 self.graph.add_edge("p", "r") # e_2 self.graph.add_edge("r", "q") # e_3 self.graph.add_edge("q", "s") # e_4 def test_update_mate(self): self.update_mate_tester( expected={ "p": "q", "q": "p", "r": "r", "s": "s" }, parent_node=Node(self.graph.edges()[0], {v: v for v in self.graph.vertices()}, 1), domain=list("pqrs")) self.update_mate_tester(expected={ "q": "r", "r": "q", "s": "s" }, parent_node=Node(self.graph.edges()[1], { "p": "q", "q": "p", "r": "r", "s": "s" }, 1), domain=list("qrs")) self.update_mate_tester(expected={ "q": 0, "s": "s" }, parent_node=Node(self.graph.edges()[2], { "q": "p", "r": "r", "s": "s" }, 1), domain=list("qs")) def update_mate_tester(self, expected, parent_node, domain): actual = update_domain(update_mate(parent_node), domain) self.assertDictEqual(expected, actual) def test_solve_one_solution(self): expected = [[(0, 0), (0, 1)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 0), (2, 0)], [(2, 0), (2, 1)]] actual = list(solve(self.instance_one_solution)) self.assertTrue(len(actual) == 1) self.assertListEqual(expected, actual[0]) def test_solve_many_solutions(self): expected = [[[(0, 0), (1, 0)], [(0, 1), (1, 1)], [(1, 0), (2, 0)], [(1, 1), (1, 2)], [(1, 2), (2, 1)]], [[(0, 0), (1, 0)], [(0, 1), (1, 2)], [(1, 0), (2, 0)], [(1, 1), (1, 2)], [(1, 1), (2, 1)]], [[(0, 0), (1, 0)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 1), (2, 0)], [(1, 2), (2, 1)]], [[(0, 0), (1, 1)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 0), (2, 0)], [(1, 2), (2, 1)]]] actual = list(solve(self.instance_many_solutions)) self.assertListEqual(expected, actual) def test_solve_no_solutions(self): expected = [] actual = list(solve(self.instance_no_solutions)) self.assertListEqual(expected, actual)