def createTree(self, reinitialize=0): """ Create a simple tree structure 2 3 \ / \ / 1 | | 0 """ print('>>> creating tree <<<') # create the four nodes node0 = SNode(0) node1 = SNode(1) node2 = SNode(2) node3 = SNode(3) self.nodelist = [node0, node1, node2, node3] # create the tree and set its nodes self.tree = STree() self.tree.setRoot(node0) self.tree.addNodeWithParent(node1, node0) self.tree.addNodeWithParent(node2, node1) self.tree.addNodeWithParent(node3, node1)
def createTree2(self, reinitialize=0): # Create a simple tree structure # 4 5 6 # \ / / # \ / / # 2 3 # \ / # \ / # 1 # | # | # 0 # create the nodes node0 = SNode(0) node1 = SNode(1) node2 = SNode(2) node3 = SNode(3) node4 = SNode(4) node5 = SNode(5) node6 = SNode(6) self.nodelist = [node0, node1, node2, node3, node4, node5, node6] # create the tree and set its nodes self.tree = STree() self.tree.setRoot(node0) self.tree.addNodeWithParent(node1, node0) self.tree.addNodeWithParent(node2, node1) self.tree.addNodeWithParent(node3, node1) self.tree.addNodeWithParent(node4, node2) self.tree.addNodeWithParent(node5, node2) self.tree.addNodeWithParent(node6, node3)
def createTree2(self, reinitialize=0): """ Create a simple tree structure 4 5 6 \ / / \ / / 2 3 \ / \ / 1 | | 0 """ print('>>> creating tree <<<') # create the nodes node0 = SNode(0) node1 = SNode(1) node2 = SNode(2) node3 = SNode(3) node4 = SNode(4) node5 = SNode(5) node6 = SNode(6) self.nodelist = [node0, node1, node2, node3, node4, node5, node6] # create the tree and set its nodes self.tree = STree() self.tree.setRoot(node0) self.tree.addNodeWithParent(node1, node0) self.tree.addNodeWithParent(node2, node1) self.tree.addNodeWithParent(node3, node1) self.tree.addNodeWithParent(node4, node2) self.tree.addNodeWithParent(node5, node2) self.tree.addNodeWithParent(node6, node3)
def createTree(self, reinitialize=0): # Create a simple tree structure # 2 3 # \ / # \ / # 1 # | # | # 0 # create the four nodes node0 = SNode(0) node1 = SNode(1) node2 = SNode(2) node3 = SNode(3) self.nodelist = [node0, node1, node2, node3] # create the tree and set its nodes self.tree = STree() self.tree.setRoot(node0) self.tree.addNodeWithParent(node1, node0) self.tree.addNodeWithParent(node2, node1) self.tree.addNodeWithParent(node3, node1)
def testInsertionRemoval(self): self.createTree() # test node insertion newnode = SNode(15) self.tree.insertNode(newnode, self.nodelist[1], self.nodelist[2:3]) assert newnode in self.nodelist[1].child_nodes assert newnode.parent_node == self.nodelist[1] assert self.nodelist[2] in newnode.child_nodes assert self.nodelist[2].parent_node == newnode assert self.nodelist[3] not in newnode.child_nodes # test rearranging indices self.tree.resetIndices() assert [node.index for node in self.tree] == list(range(5)) # test node removal self.tree.removeSingleNode(newnode) assert set(self.nodelist[1].child_nodes) == set( [self.nodelist[2], self.nodelist[3]]) assert self.nodelist[2].parent_node == self.nodelist[1] assert self.nodelist[3].parent_node == self.nodelist[1] self.tree.resetIndices() assert [node.index for node in self.tree] == list(range(4)) # limit case 1: insert and remove root newroot = SNode(15) self.tree.insertNode(newroot, None) assert self.tree.root == newroot assert newroot.child_nodes == self.nodelist[0:1] assert self.nodelist[0].parent_node == newroot assert newroot.parent_node == None with pytest.raises(ValueError): self.tree.removeSingleNode(newroot) # add a node with a given index self.tree.addNodeWithParentFromIndex(4, self.tree[3]) assert isinstance(self.tree[4], SNode) with pytest.raises(ValueError): self.tree.addNodeWithParentFromIndex(3, self.tree[3]) with pytest.warns(UserWarning): self.tree.addNodeWithParentFromIndex(5, None) # reinitialize original tree self.createTree(reinitialize=1)
def testGetSetLeafs(self): self.createTree() assert self.tree.leafs == [self.nodelist[2], self.nodelist[3]] with pytest.raises(AttributeError): self.tree.leafs = [SNode(15)]
def testGetSetNodes(self): self.createTree() assert self.tree.nodes == self.nodelist with pytest.raises(AttributeError): self.tree.nodes = [SNode(15)]