Package jptests :: Package misc :: Package tree :: Module datastructs
[hide private]
[frames] | no frames]

Source Code for Module jptests.misc.tree.datastructs

  1  """Unit tests for jazzparser.misc.tree datastructures. 
  2   
  3  """ 
  4  """ 
  5  ============================== License ======================================== 
  6   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
  7    
  8   This file is part of The Jazz Parser. 
  9    
 10   The Jazz Parser is free software: you can redistribute it and/or modify 
 11   it under the terms of the GNU General Public License as published by 
 12   the Free Software Foundation, either version 3 of the License, or 
 13   (at your option) any later version. 
 14    
 15   The Jazz Parser is distributed in the hope that it will be useful, 
 16   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18   GNU General Public License for more details. 
 19    
 20   You should have received a copy of the GNU General Public License 
 21   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  ============================ End license ====================================== 
 24   
 25  """ 
 26  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 27   
 28  import unittest 
 29  from jazzparser.misc.tree.datastructs import Node, MutableTree, \ 
 30                      ImmutableTree 
 31   
32 -class TestNode(unittest.TestCase):
33 - def test_leaf(self):
34 """ Create a leaf node. """ 35 node = Node("test") 36 self.assertEqual(len(node.children), 0)
37
38 - def test_internal(self):
39 """ Create an internal node. """ 40 leaf1 = Node("l1") 41 leaf2 = Node("l2") 42 node = Node("int", leaf1, leaf2) 43 44 self.assertEqual(node.label, "int") 45 self.assertEqual(len(node.children), 2)
46
47 -class TestMutableTree(unittest.TestCase):
48 - def test_create(self):
49 tree = MutableTree(Node("root")) 50 self.assertEqual(tree.root.label, "root")
51
52 - def test_create_tree(self):
53 """ Just make sure building a big tree causes no errors. """ 54 tree = MutableTree( 55 Node("root", 56 Node("leaf1"), 57 Node("A", 58 Node("leaf2"), 59 Node("leaf3") 60 ), 61 Node("B", 62 Node("leaf4"), 63 Node("C", 64 Node("leaf5"), 65 Node("leaf6") 66 ) 67 ) 68 ))
69
70 - def test_string(self):
71 """ 72 Tries to get the string representation of a tree. It's not critical 73 that the representation is correct, so we don't worry about that. 74 75 """ 76 tree = MutableTree( 77 Node("root", 78 Node("leaf1"), 79 Node("A", 80 Node("leaf2"), 81 Node("leaf3") 82 ), 83 Node("B", 84 Node("leaf4"), 85 Node("C", 86 Node("leaf5"), 87 Node("leaf6") 88 ) 89 ) 90 )) 91 str(tree)
92
93 - def test_postorder(self):
94 """ Check postorder returns the right list of nodes. """ 95 leaf1 = Node("leaf1") 96 leaf2 = Node("leaf2") 97 leaf4 = Node("leaf4") 98 leaf5 = Node("leaf5") 99 leaf6 = Node("leaf6") 100 101 c_node = Node("C", leaf5, leaf6) 102 b_node = Node("B", leaf4) 103 a_node = Node("A", leaf2, c_node) 104 105 root = Node("root", leaf1, a_node, b_node) 106 107 tree = MutableTree(root) 108 109 # Define the correct postorder manually 110 correct_postorder = [leaf1, leaf2, leaf5, leaf6, c_node, a_node, 111 leaf4, b_node, root] 112 postorder = tree.postorder() 113 114 # Check that these match 115 self.assertEqual([id(n) for n in correct_postorder], \ 116 [id(n) for n in postorder])
117
118 - def test_immutable(self):
119 """ 120 Try converting a mutable tree to an immutable. 121 122 """ 123 tree = MutableTree( 124 Node("root", 125 Node("leaf1"), 126 Node("A", 127 Node("leaf2"), 128 Node("leaf3") 129 ), 130 Node("B", 131 Node("leaf4"), 132 Node("C", 133 Node("leaf5"), 134 Node("leaf6") 135 ) 136 ) 137 )) 138 im = tree.immutable()
139
140 - def test_copy(self):
141 """ Try copying a tree and check the structure is preserved. """ 142 tree = MutableTree( 143 Node("root", 144 Node("leaf1"), 145 Node("A", 146 Node("leaf2"), 147 Node("C", 148 Node("leaf5"), 149 Node("leaf6") 150 ), 151 ), 152 Node("B", 153 Node("leaf4") 154 ) 155 )) 156 # Copy it 157 new_tree = tree.copy() 158 159 # Check none of the nodes has been preserved 160 old_ids = [id(n) for n in tree.postorder()] 161 for new_node in new_tree.postorder(): 162 self.assertNotIn(id(new_node), old_ids) 163 164 # Check the two trees evaluate as equal 165 self.assertEqual(tree, new_tree)
166