def test_equality(self): a = GNode("a") b = GNode("b", a) self.assertEqual(a, a) self.assertEqual(b, b) self.assertNotEqual(a, b) pass
def test_add_remove_parent(self): add_method = lambda n, c: n.add_parent_s(c) remove_method_by_name = lambda n, c: n.remove_parent_s_by_name(c) remove_method = lambda n, c: n.remove_parent_s(c) get_method = lambda n: n.parent_s get_inverse_method = lambda n: n.child_s constructor_method = lambda x: GNode(x) node = GNode("node") self.add_remove_item_test(node, add_method, remove_method_by_name, get_method, get_inverse_method, constructor_method, True) self.add_remove_item_test(node, add_method, remove_method, get_method, get_inverse_method, constructor_method, False)
def test_node_to_strings(self): start = Start() a = GNode("a") c = End() d = Key("key") e = Lock("e") start.add_child_s(a) start.add_child_s(d) d.add_lock_s(e) self.assertTrue( self.is_list_permutation_in_string( str(start), "Start(\'Start\', children={})", ["a", "key"])) c.add_parent_s([e, d]) self.assertTrue( self.is_list_permutation_in_string(str(c), "End(\'End\', parents={})", ["e", "key"])) self.assertTrue( self.is_list_permutation_in_string( str(d), "Key(\'key\', parents=[\'Start\'], children={}, locks=[\'e\'])", ["e", "End"])) self.assertEqual( str(e), "Lock(\'e\', parents=[\'key\'], children=[\'End\'], keys=[\'key\'])" ) self.assertEqual(str(a), "GNode(\'a\', parents=[\'Start\'], children=[])")
def test_node_create_params(self): n0 = GNode("n0") n2 = GNode("n2") n1 = GNode("n1", n0, n2) n3 = GNode("n3", [n0], [n2]) start = Start(n0) end = End(n2) key1 = Key("key1", n0) lock1 = Lock("lock1", n2, end, key1) lock2 = Lock("lock1", n2, end) key2 = Key("key1", n0, lock2) self.assertEqual(start.parent_s, set()) self.assertEqual(start.child_s, {n0}) self.assertEqual(n0.parent_s, {start}) self.assertEqual(n0.child_s, {n1, n3, key1, key2}) self.assertEqual(n1.parent_s, {n0}) self.assertEqual(n1.child_s, {n2}) self.assertEqual(n2.parent_s, {n1, n3}) self.assertEqual(n2.child_s, {end, lock1, lock2}) self.assertEqual(n3.parent_s, {n0}) self.assertEqual(n3.child_s, {n2}) self.assertEqual(key1.parent_s, {n0}) self.assertEqual(key1.child_s, {lock1}) self.assertEqual(key1.lock_s, {lock1}) self.assertEqual(lock1.parent_s, {n2, key1}) self.assertEqual(lock1.child_s, {end}) self.assertEqual(lock1.key_s, {key1}) self.assertEqual(key2.parent_s, {n0}) self.assertEqual(key2.child_s, {lock2}) self.assertEqual(key2.lock_s, {lock2}) self.assertEqual(lock2.parent_s, {n2, key2}) self.assertEqual(lock2.child_s, {end}) self.assertEqual(lock2.key_s, {key2}) self.assertEqual(end.parent_s, {n2, lock1, lock2}) self.assertEqual(end.child_s, set())
def test_layout(self): n = [GNode("n{}".format(i)) for i in range(13)] connections = { 0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8], 4: [9], 5: [9, 10], 6: [11, 12] } for parent, children in connections.items(): n[parent].add_child_s([n[child] for child in children]) layout = GraphVisualizer.get_node_layout(n[0]) self.assert_no_overlapping_layout(layout)
def test_remove_by_name(self): n1 = GNode("1") n2 = GNode("2") n3 = GNode("3") n4 = GNode("4") n5 = GNode("5") n0 = GNode("0") n0.child_s = {n1, n2, n3, n4, n5} n1.parent_s.add(n0) n2.parent_s.add(n0) n2.parent_s.add(n0) n3.parent_s.add(n0) n4.parent_s.add(n0) n5.parent_s.add(n0) Node._remove_by_name(n0, n2.name, lambda x: x.child_s, lambda x: x.parent_s) self.assertEqual(n0.child_s, {n1, n3, n4, n5}) self.assertEqual(n1.parent_s, {n0}) self.assertEqual(n2.parent_s, set()) self.assertEqual(n3.parent_s, {n0}) self.assertEqual(n4.parent_s, {n0}) self.assertEqual(n5.parent_s, {n0})
def test_add(self): n0 = GNode("0") n1 = GNode("1") n2 = GNode("2") n3 = GNode("3") n4 = GNode("4") n5 = GNode("5") Node._add(n0, [n1], lambda x: x.child_s, lambda x: x.parent_s) self.assertEqual(n0.child_s, {n1}) self.assertEqual(n1.parent_s, {n0}) Node._add(n0, [n2, n3, n4, n5], lambda x: x.child_s, lambda x: x.parent_s) self.assertEqual(n0.child_s, {n1, n2, n3, n4, n5}) self.assertEqual(n1.parent_s, {n0}) self.assertEqual(n2.parent_s, {n0}) self.assertEqual(n3.parent_s, {n0}) self.assertEqual(n4.parent_s, {n0}) self.assertEqual(n5.parent_s, {n0})
def generate_mission_graph(): a = Start() b = GNode([], [], "b") c = Key(name="c") d = GNode([], [], "d") e = End() f = Lock(name="f") g = GNode([], [], "g") a.add_child_s(b) a.add_child_s(c) a.add_child_s(d) b.add_child_s(e) c.add_child_s(f) c.add_child_s(g) f.add_key_s(c) c.add_lock_s(f) return a, {a, b, c, d, e, f, g}
def get_graph_b(): n1 = GNode("a") n2 = GNode("b") n3 = GNode("c") n4 = GNode("d") n5 = GNode("e") n6 = GNode("f") n7 = GNode("g") n8 = GNode("h") n9 = GNode("i") n10 = GNode("j") n1.add_child_s([n2, n3]) n2.add_child_s(n4) n3.add_child_s([n4, n5]) n5.add_child_s(n6) n6.add_child_s(n7) n7.add_child_s([n8, n9]) n8.add_child_s(n10) n9.add_child_s(n10) return n1, {n1, n2, n3, n4, n5, n6, n7, n8, n9, n10}
def get_graph_a(): n1 = GNode("a") n2 = GNode("b") n3 = GNode("c") n4 = GNode("d") n5 = GNode("e") n1.add_child_s([n3, n4, n5]) n2.add_child_s([n3, n4, n5]) n3.add_child_s([n1, n2, n4, n5]) n4.add_child_s([n1, n2, n3, n5]) n5.add_child_s([n1, n2, n3, n4]) return n1, {n1, n2, n3, n4, n5}
def get_triangle_graph(): a = GNode("a") b = GNode("b") c = GNode("c") d = GNode("d") a.add_child_s(b) b.add_child_s(c) c.add_child_s(a) a.add_child_s(d) b.add_child_s(d) c.add_child_s(d) return a, {a, b, c, d}
def get_house_graph(): a = GNode("a") b = GNode("b") c = GNode("c") d = GNode("d") e = GNode("e") a.add_child_s(b) b.add_child_s(c) c.add_child_s(d) d.add_child_s(a) c.add_child_s(e) e.add_child_s(a) e.add_child_s(d) return a, {a, b, c, d, e}
def get_man_graph(): a = Start() b = GNode("b") c = GNode("c") d = GNode("d") e = GNode("e") f = End() g = GNode("g") h = GNode("h") i = GNode("i") a.add_child_s([b, c, d]) b.add_child_s(e) c.add_child_s([f, g, h, i]) h.add_child_s(i) return a, {a, b, c, d, e, f, g, h, i}
def test_mutable_default_argument_bug(self): gnode1 = GNode("") gnode2 = GNode("") bad_node0 = GNode("BadNode0") gnode1.add_child_s(bad_node0) self.assertEqual(len(gnode2.child_s), 0) start1 = Start() start2 = Start() bad_node1 = GNode("BadNode1") start1.add_child_s(bad_node1) self.assertEqual(len(start2.child_s), 0) end1 = End() end2 = End() bad_node2 = GNode("BadNode2") end1.add_parent_s(bad_node2) self.assertEqual(len(end2.parent_s), 0) key1 = Key() key2 = Key() bad_node3 = GNode("BadNode3") key1.add_child_s(bad_node3) bad_node4 = GNode("BadNode4") key1.add_parent_s(bad_node4) self.assertEqual(len(key2.child_s), 0) self.assertEqual(len(key2.parent_s), 0) lock1 = Lock() lock2 = Lock() bad_node5 = GNode("BadNode5") lock1.add_child_s(bad_node5) bad_node6 = GNode("BadNode6") lock1.add_parent_s(bad_node6) self.assertEqual(len(lock2.child_s), 0) self.assertEqual(len(lock2.parent_s), 0)
def test_find_all_nodes_topological_sort(self): top_sort = "topological-sort" n0 = GNode("0") n1 = GNode("1") n2 = GNode("2") n3 = GNode("3") n4 = GNode("4") n5 = GNode("5") n5.add_child_s([n0, n2, n4]) n2.add_child_s(n3) n3.add_child_s(n1) n4.add_child_s([n0, n1]) nodes = Node.find_all_nodes(n5, method=top_sort) self.assertTrue( self.assert_ordered(nodes, [(n5, [n4, n3, n2, n1, n0]), (n4, [n0, n1]), (n2, [n3, n1]), (n3, [n1]), (n0, []), (n1, [])])) n0 = GNode("0") n1 = GNode("1") n2 = GNode("2") n3 = GNode("3") n4 = GNode("4") n5 = GNode("5") n0.add_child_s([n1, n2, n3]) n1.add_child_s(n4) n2.add_child_s(n5) n3.add_child_s(n1) n4.add_child_s(n2) nodes = Node.find_all_nodes(n0, method=top_sort) self.assertTrue( self.assert_ordered(nodes, [(n0, [n1, n2, n3, n4, n5]), (n1, [n4, n2, n5]), (n2, [n5]), (n3, [n1, n4, n2, n5]), (n4, [n2, n5]), (n5, [])]))