def setUp(self): node_ids = [ 1, 2, 5, 10, 20, 41, 21, 11, 22, 44, 89, 45, 91, 182, 364, 729, 1459 ] self.node_ids = node_ids init = [(node_id, "{0}".format(node_id)) for node_id in node_ids] self.tree = NAryTree(init)
class NAryTreeTestCase(TestCase): def setUp(self): node_ids = [ 1, 2, 5, 10, 20, 41, 21, 11, 22, 44, 89, 45, 91, 182, 364, 729, 1459 ] self.node_ids = node_ids init = [(node_id, "{0}".format(node_id)) for node_id in node_ids] self.tree = NAryTree(init) # end def setUp def test_get_children_ids(self): ae = self.assertEqual ar = self.assertRaises results = { 1: [2, 5, 11], 2: [], 5: [10, 21], 10: [20, 41], 20: [], 41: [], 21: [], 11: [22, 45, 91], 22: [44, 89], 44: [], 89: [], 45: [], 91: [182], 182: [364, 729, 1459], 364: [], 729: [], 1459: [] } for key, value in results.items(): ae(self.tree.get_children_ids(key), value) # end for ar(TypeError, self.tree.get_children_ids, "1") # end def test_get_children_ids def test_get_sibling_ids(self): ae = self.assertEqual ar = self.assertRaises results = { 1: [], 2: [5, 11], 5: [11], 10: [21], 20: [41], 41: [], 21: [], 11: [], 22: [45, 91], 44: [89], 89: [], 45: [91], 91: [], 182: [], 364: [729, 1459], 729: [1459], 1459: [] } for key, value in results.items(): ae(self.tree.get_sibling_ids(key), value) # end for ar(TypeError, self.tree.get_sibling_ids, "1") # end def test_get_sibling_ids: def test_walk_postorder(self): ae = self.assertEqual results = [ 2, 20, 41, 10, 21, 5, 44, 89, 22, 45, 364, 729, 1459, 182, 91, 11, 1 ] for index, node_id in enumerate(self.tree.walk_postorder()): ae(node_id, results[index]) # end for # end def walk_postorder def test_get_leaf_ids(self): ae = self.assertEqual results = [2, 20, 41, 21, 44, 89, 45, 364, 729, 1459] for index, node_id in enumerate(self.tree.get_leaf_ids()): ae(node_id, results[index]) # end for # end def test_get_leaf_ids def test_get_parent_id(self): fue = self.failUnlessEqual results = [ 1, 1, 5, 10, 10, 5, 1, 11, 22, 22, 11, 11, 91, 182, 182, 182 ] for (index, node_id) in enumerate(self.node_ids[1:]): fue(self.tree.get_parent_id(node_id), results[index])