summaryrefslogtreecommitdiff
path: root/test/unittest_tree.py
blob: 8e7f4bd6616037d15ebb5d9aad5bf8f4fdfa1265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-common.
#
# logilab-common is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option) any
# later version.
#
# logilab-common is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
"""
unit tests for module logilab.common.tree
squeleton generated by /home/syt/bin/py2tests on Jan 20 at 10:43:25
"""
__revision__ = "$Id: unittest_tree.py,v 1.9 2005-09-07 23:44:02 nico Exp $"

from logilab.common.testlib import TestCase, unittest_main
from logilab.common.tree import *

tree = ('root', (
    ('child_1_1', (
    ('child_2_1', ()), ('child_2_2', (
    ('child_3_1', ()),
    )))),
    ('child_1_2', (('child_2_3', ()),))))

def make_tree(tuple):
    n = Node(tuple[0])
    for child in tuple[1]:
        n.append(make_tree(child))
    return n

class Node_ClassTest(TestCase):
    """ a basic tree node, caracterised by an id"""
    def setUp(self):
        """ called before each test from this class """
        self.o = make_tree(tree)


    def test_flatten(self):
        result = [r.id for r in self.o.flatten()]
        expected = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
        self.assertListEqual(result, expected)

    def test_flatten_with_outlist(self):
        resultnodes = []
        self.o.flatten(resultnodes)
        result = [r.id for r in resultnodes]
        expected = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
        self.assertListEqual(result, expected)


    def test_known_values_remove(self):
        """
        remove a child node
        """
        self.o.remove(self.o.get_node_by_id('child_1_1'))
        self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'child_1_1')

    def test_known_values_replace(self):
        """
        replace a child node with another
        """
        self.o.replace(self.o.get_node_by_id('child_1_1'), Node('hoho'))
        self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'child_1_1')
        self.assertEqual(self.o.get_node_by_id('hoho'), self.o.children[0])

    def test_known_values_get_sibling(self):
        """
        return the sibling node that has given id
        """
        self.assertEqual(self.o.children[0].get_sibling('child_1_2'), self.o.children[1], None)

    def test_raise_get_sibling_NodeNotFound(self):
        self.assertRaises(NodeNotFound, self.o.children[0].get_sibling, 'houhou')

    def test_known_values_get_node_by_id(self):
        """
        return node in whole hierarchy that has given id
        """
        self.assertEqual(self.o.get_node_by_id('child_1_1'), self.o.children[0])

    def test_raise_get_node_by_id_NodeNotFound(self):
        self.assertRaises(NodeNotFound, self.o.get_node_by_id, 'houhou')

    def test_known_values_get_child_by_id(self):
        """
        return child of given id
        """
        self.assertEqual(self.o.get_child_by_id('child_2_1', recurse=1), self.o.children[0].children[0])

    def test_raise_get_child_by_id_NodeNotFound(self):
        self.assertRaises(NodeNotFound, self.o.get_child_by_id, nid='child_2_1')
        self.assertRaises(NodeNotFound, self.o.get_child_by_id, 'houhou')

    def test_known_values_get_child_by_path(self):
        """
        return child of given path (path is a list of ids)
        """
        self.assertEqual(self.o.get_child_by_path(['root', 'child_1_1', 'child_2_1']), self.o.children[0].children[0])

    def test_raise_get_child_by_path_NodeNotFound(self):
        self.assertRaises(NodeNotFound, self.o.get_child_by_path, ['child_1_1', 'child_2_11'])

    def test_known_values_depth_down(self):
        """
        return depth of this node in the tree
        """
        self.assertEqual(self.o.depth_down(), 4)
        self.assertEqual(self.o.get_child_by_id('child_2_1', True).depth_down(), 1)

    def test_known_values_depth(self):
        """
        return depth of this node in the tree
        """
        self.assertEqual(self.o.depth(), 0)
        self.assertEqual(self.o.get_child_by_id('child_2_1', True).depth(), 2)

    def test_known_values_width(self):
        """
        return depth of this node in the tree
        """
        self.assertEqual(self.o.width(), 3)
        self.assertEqual(self.o.get_child_by_id('child_2_1', True).width(), 1)

    def test_known_values_root(self):
        """
        return the root node of the tree
        """
        self.assertEqual(self.o.get_child_by_id('child_2_1', True).root(), self.o)

    def test_known_values_leaves(self):
        """
        return a list with all the leaf nodes descendant from this task
        """
        self.assertEqual(self.o.leaves(), [self.o.get_child_by_id('child_2_1', True),
                                          self.o.get_child_by_id('child_3_1', True),
                                          self.o.get_child_by_id('child_2_3', True)])

    def test_known_values_lineage(self):
        c31 = self.o.get_child_by_id('child_3_1', True)
        self.assertEqual(c31.lineage(), [self.o.get_child_by_id('child_3_1', True),
                                         self.o.get_child_by_id('child_2_2', True),
                                         self.o.get_child_by_id('child_1_1', True),
                                         self.o])


class post_order_list_FunctionTest(TestCase):
    """"""
    def setUp(self):
        """ called before each test from this class """
        self.o = make_tree(tree)

    def test_known_values_post_order_list(self):
        """
        create a list with tree nodes for which the <filter> function returned true
        in a post order foashion
        """
        L = ['child_2_1', 'child_3_1', 'child_2_2', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
        l = [n.id for n in post_order_list(self.o)]
        self.assertEqual(l, L, l)

    def test_known_values_post_order_list2(self):
        """
        create a list with tree nodes for which the <filter> function returned true
        in a post order foashion
        """
        def filter(node):
            if node.id == 'child_2_2':
                return 0
            return 1
        L = ['child_2_1', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
        l = [n.id for n in post_order_list(self.o, filter)]
        self.assertEqual(l, L, l)


class PostfixedDepthFirstIterator_ClassTest(TestCase):
    """"""
    def setUp(self):
        """ called before each test from this class """
        self.o = make_tree(tree)

    def test_known_values_next(self):
        L = ['child_2_1', 'child_3_1', 'child_2_2', 'child_1_1', 'child_2_3', 'child_1_2', 'root']
        iter = PostfixedDepthFirstIterator(self.o)
        o = iter.next()
        i = 0
        while o:
            self.assertEqual(o.id, L[i])
            o = iter.next()
            i += 1


class pre_order_list_FunctionTest(TestCase):
    """"""
    def setUp(self):
        """ called before each test from this class """
        self.o = make_tree(tree)

    def test_known_values_pre_order_list(self):
        """
        create a list with tree nodes for which the <filter> function returned true
        in a pre order fashion
        """
        L = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
        l = [n.id for n in pre_order_list(self.o)]
        self.assertEqual(l, L, l)

    def test_known_values_pre_order_list2(self):
        """
        create a list with tree nodes for which the <filter> function returned true
        in a pre order fashion
        """
        def filter(node):
            if node.id == 'child_2_2':
                return 0
            return 1
        L = ['root', 'child_1_1', 'child_2_1', 'child_1_2', 'child_2_3']
        l = [n.id for n in pre_order_list(self.o, filter)]
        self.assertEqual(l, L, l)


class PrefixedDepthFirstIterator_ClassTest(TestCase):
    """"""
    def setUp(self):
        """ called before each test from this class """
        self.o = make_tree(tree)

    def test_known_values_next(self):
        L = ['root', 'child_1_1', 'child_2_1', 'child_2_2', 'child_3_1', 'child_1_2', 'child_2_3']
        iter = PrefixedDepthFirstIterator(self.o)
        o = iter.next()
        i = 0
        while o:
            self.assertEqual(o.id, L[i])
            o = iter.next()
            i += 1


if __name__ == '__main__':
    unittest_main()