class TestMyGraph(unittest.TestCase): def setUp(self): self.graph_as_dict = HelperMethods.create_graph_as_dict() self.graph = MyGraph().from_dict(self.graph_as_dict) def test_instantiation(self): graph = MyGraph() self.assertIsNotNone(graph) def test_from_and_to_dict(self): # Create a MyGraph from dict, and then vice versa. Expect the same content. graph = MyGraph.from_dict(self.graph_as_dict) graph_as_dict = graph.to_dict() self.assertEqual(self.graph_as_dict, graph_as_dict) self.assertEqual(self.graph.num_nodes, 5) def test_reset_visited(self): self.graph.nodes[1].visited = True self.graph.nodes[3].visited = True self.assertTrue(self.graph.nodes[1].visited) self.assertTrue(self.graph.nodes[3].visited) self.graph.reset_visited() for node in self.graph.nodes.values(): self.assertFalse(node.visited)
def test_give_2_adjlist(self): mg = MyGraph('i2.txt') expectation = [(1, [2, 3]), (2, [1, 3]), (3, [2, 1]), (4, [6, 7, 8]), (5, []), (6, [4]), (7, [4]), (8, [4])] for i in range(len(expectation)): self.assertEqual(expectation[i][0], mg.adjlist[i][0]) for i in range(len(expectation)): self.assertEqual(expectation[i][0], mg.adjlist[i][0]) es = sorted(expectation[i][1]) ms = sorted(mg.adjlist[i][1]) self.assertListEqual(es, ms)
def test_add_edge(self): graph = MyGraph('graph_testinput1.txt') graph.add_edge("1", "6")
def test_add_vertex(self): graph = MyGraph('graph_testinput2.txt') graph.add_vertex("9") self.assertEqual(graph.get_vertex("9").key, "9") self.assertEqual(graph.get_vertex("9").adjacent_to, [])
def test_get_vertex(self): graph = MyGraph('graph_testinput1.txt') self.assertEqual(graph.get_vertex("1").key, "1") self.assertEqual( graph.get_vertex("1").adjacent_to, ["2", "3", "4", "5"])
def test_given_1_cc(self): mg = MyGraph('i1.txt') self.assertListEqual(mg.conn_components(), [[1, 2, 3, 4, 5], [6, 7, 8, 9]])
from my_graph import MyGraph from itertools import permutations a_graph = MyGraph() with open('data.txt', 'r') as shit: to_input = shit.readlines() for line in to_input: line = line.replace("\n", "") line = line.split(" ") a_graph.add_pair(line[0], line[2], int(line[4])) a_graph.mirror() a_graph.print_contents() all_locs = [ 'Tristram', 'AlphaCentauri', 'Snowdin', 'Tambi', 'Faerun', 'Norrath', 'Straylight', 'Arbre' ] x = permutations(all_locs, 8) possible_routes = [] for subset in x: possible_routes.append(subset) def find_route_total_dist(items, graph): dist_count = 0 pointer = 1 while pointer < len(items): loc1 = items[pointer - 1] loc2 = items[pointer] dist_count += graph.find_distance(loc1, loc2) pointer += 1
def test_instantiation(self): graph = MyGraph() self.assertIsNotNone(graph)
def test_test_2_bic(self): mg = MyGraph('test.txt') self.assertFalse(mg.bicolor())
def test_big7_2_bic(self): mg = MyGraph('big7.txt') self.assertFalse(mg.bicolor())
def test_kearn_1_bic(self): mg = MyGraph('kearn.txt') self.assertTrue(mg.bicolor())
def test_given_1_buildsize(self): mg = MyGraph('i1.txt') self.assertEqual(len(mg.adjlist), 9) for i in range(len(mg.adjlist)): self.assertEqual(mg.adjlist[i][0], i + 1)
def test_given_1_bic(self): mg = MyGraph('i1.txt') self.assertTrue(mg.bicolor())
def test_given_2_cc(self): mg = MyGraph('i2.txt') self.assertListEqual(mg.conn_components(), [[1, 2, 3], [4, 6, 7, 8], [5]])
def test_01(self): graph = MyGraph('graph_testinput1.txt') self.assertEqual(graph.get_conn_components(), [['1', '2', '3', '4', '5'], ['6', '7', '8', '9']]) self.assertTrue(graph.bicolor())
def create_graph(): return MyGraph().from_dict(HelperMethods.create_graph_as_dict())
def test_given_2_bic(self): mg = MyGraph('i2.txt') self.assertFalse(mg.bicolor())
def setUp(self): self.graph_as_dict = HelperMethods.create_graph_as_dict() self.graph = MyGraph().from_dict(self.graph_as_dict)
def test_02(self): graph = MyGraph('graph_testinput2.txt') self.assertEqual(graph.get_conn_components(), [['1', '2', '3'], ['4', '6', '7', '8']]) self.assertFalse(graph.bicolor())
def test_from_and_to_dict(self): # Create a MyGraph from dict, and then vice versa. Expect the same content. graph = MyGraph.from_dict(self.graph_as_dict) graph_as_dict = graph.to_dict() self.assertEqual(self.graph_as_dict, graph_as_dict) self.assertEqual(self.graph.num_nodes, 5)
def test_get_vertices2(self): graph = MyGraph('graph_testinput2.txt') self.assertEqual(graph.get_vertices(), ["1", "2", "3", "4", "6", "7", "8"])
def test_graph_init(self): filename = "graph_testinput1.txt" graph = MyGraph(filename) print(graph)
def test_get_vertex_none(self): graph = MyGraph('graph_testinput1.txt') self.assertEqual(None, graph.get_vertex("10"))