def testUnflattenManyTrees(self): if IGNORE_TEST: return tree = NamedTree.createRandomNamedTree(100, 0.5, prob_detach=0.2) tree._checkTreeStructure() tree_list = tree.flatten() new_tree = NamedTree.unflatten(tree_list) self.assertTrue(tree.isEquivalent(new_tree))
def setUp(self): self.root = NamedTree(PARENT) self.root_child = NamedTree(CHILD1) self.root.addChild(self.root_child) self.root.addChild(NamedTree(CHILD2)) self.root.addChild(NamedTree(CHILD3)) self.subparent = NamedTree(SUBPARENT) self.root.addChild(self.subparent) self.subparent_child = NamedTree(CHILD4) self.subparent.addChild(self.subparent_child) self.subparent_child_name = CHILD4 self.children = self.root.getChildren()
def testFlattenRandomTree(self): if IGNORE_TEST: return tree = NamedTree.createRandomNamedTree(100, 0.5, prob_detach=0.2) leaves = tree.getLeaves() detached_nodes = [n for n in tree.getAllNodes() if not n.isAttached()] # Include in the leaves the parents with no attached # children for node in detached_nodes: parent = node.getParent() is_leaf = True for child in parent.getChildren(): if child.isAttached(): is_leaf = False break if is_leaf and (not parent in leaves): leaves.append(parent) leaf_full_names = [l._makeFlattenName() for l in leaves] elements = tree.flatten() names = [] for e in elements: e.tree._checkTreeStructure() for leaf in e.tree.getLeaves(): names.append(leaf._makeFlattenName()) self.assertEqual(set(leaf_full_names), set(names))
def testUnflattenTwoTrees(self): if IGNORE_TEST: return self.subparent.setIsAttached(False) elements = self.root.flatten() new_tree = NamedTree.unflatten(elements) self.assertTrue(self.root.isEquivalent(new_tree, is_exception=True))
def testDetachTreesRandom(self): if IGNORE_TEST: return tree = NamedTree.createRandomNamedTree(300, 0.5, prob_detach=0.2) elements = tree._detachTrees() detached = [c for c in tree.getAllNodes() if not c.isAttached()] expected_num = len(detached) + 1 self.assertEqual(expected_num, len(elements)) for element in elements: root = element.tree parsed_name = root.getName(is_global_name=False).split(FLATTEN_SEPARATOR) self.assertEqual(len(parsed_name), len(set(parsed_name))) for name in parsed_name: b = any([name in node._name for node in tree.getAllNodes()]) self.assertTrue(b)
def _setup(o): """ root(DUMMY): root_child(DUMMY1_CHILD) (DUMMY2_CHILD) (DUMMY3_CHILD) subparent(Subparent) subparent_child(DUMMY4_CHILD) """ o.root = NamedTree(PARENT) o.root_child = NamedTree(CHILD1) o.root.addChild(o.root_child) o.root.addChild(NamedTree(CHILD2)) o.root.addChild(NamedTree(CHILD3)) o.subparent = NamedTree(SUBPARENT) o.root.addChild(o.subparent) o.subparent_child = NamedTree(CHILD4) o.subparent.addChild(o.subparent_child) o.subparent_child_name = CHILD4 o.children = o.root.getChildren()
class TestNamedTree(unittest.TestCase): def setUp(self): self.root = NamedTree(PARENT) self.root_child = NamedTree(CHILD1) self.root.addChild(self.root_child) self.root.addChild(NamedTree(CHILD2)) self.root.addChild(NamedTree(CHILD3)) self.subparent = NamedTree(SUBPARENT) self.root.addChild(self.subparent) self.subparent_child = NamedTree(CHILD4) self.subparent.addChild(self.subparent_child) self.subparent_child_name = CHILD4 self.children = self.root.getChildren() def testCreateGlobalName(self): if IGNORE_TEST: return global_name = self.root.createGlobalName(self.root_child) self.assertEqual(global_name, CHILD1) global_name = self.root.createGlobalName(self.subparent_child) expected_name = ".".join([SUBPARENT, self.subparent_child_name]) self.assertEqual(global_name, expected_name) def testGlobalName(self): if IGNORE_TEST: return global_name = self.root.globalName(SUBPARENT, is_relative=True) self.assertEqual(global_name, SUBPARENT) global_name = self.root.globalName(SUBPARENT, is_relative=False) self.assertEqual(global_name, SUBPARENT) global_name = self.subparent.globalName(self.subparent_child_name, is_relative=True) expected = ".".join([SUBPARENT, self.subparent_child_name]) self.assertEqual(global_name, expected) global_name = self.subparent.globalName(global_name, is_relative=False) self.assertEqual(global_name, expected) def testRelativeNameToGlobalName(self): if IGNORE_TEST: return global_name = \ self.root.globalName(self.subparent_child.getName(), is_relative=True) expected_name = self.subparent_child.getName() self.assertEqual(global_name, expected_name) global_name = self.root.globalName(global_name, is_relative=False) expected_name = self.subparent_child.getName() self.assertEqual(global_name, expected_name) def testChildFromName(self): if IGNORE_TEST: return global_name = self.root.createGlobalName(self.subparent_child) child = self.root.childFromName(global_name, is_relative=False) self.assertEqual(child, self.subparent_child) subparent = self.root.childFromName(SUBPARENT, is_relative=True) self.assertTrue(subparent, self.subparent) child = self.subparent.childFromName(self.subparent_child_name, is_relative=True) self.assertEqual(child, self.subparent_child) def testGetName(self): if IGNORE_TEST: return name = self.root.getName(is_global_name=False) self.assertEqual(name, PARENT) name = self.root.getName(is_global_name=True) self.assertEqual(name, ROOT_NAME) name = self.root_child.getName() self.assertEqual(name, self.root_child._name) name = self.subparent_child.getName(is_global_name=False) self.assertEqual(name, self.subparent_child_name) expected = ".".join([SUBPARENT, self.subparent_child_name]) name = self.subparent_child.getName(is_global_name=True) self.assertEqual(name, expected) def testSetName(self): if IGNORE_TEST: return self.assertIsNone(self.root.setName("newTable")) self.assertIsNotNone(self.root.setName("new Table"))
def testUnflattenOneTree(self): if IGNORE_TEST: return tree_list = self.root.flatten() new_tree = NamedTree.unflatten(tree_list) self.assertTrue(self.root.isEquivalent(new_tree))