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
34 """ Create a leaf node. """
35 node = Node("test")
36 self.assertEqual(len(node.children), 0)
37
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
51
69
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
110 correct_postorder = [leaf1, leaf2, leaf5, leaf6, c_node, a_node,
111 leaf4, b_node, root]
112 postorder = tree.postorder()
113
114
115 self.assertEqual([id(n) for n in correct_postorder], \
116 [id(n) for n in postorder])
117
139
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
157 new_tree = tree.copy()
158
159
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
165 self.assertEqual(tree, new_tree)
166