TsTreeItem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file TsTreeItem.cpp
22 
23  \brief Test suite for the TreeItem.
24  */
25 
26 // Unit-Test TerraLib
27 #include "TsTreeItem.h"
28 
29 // TerraLib
30 #include <terralib/common.h>
31 
33 
35 {
36 }
37 
39 {
40 }
41 
43 {
44  te::common::TreeItem parent;
45  te::common::TreeItem* parentParent = parent.getParent();
46  CPPUNIT_ASSERT(parentParent == 0);
47 }
48 
50 {
52  te::common::TreeItem child(parent);
53  te::common::TreeItem* p = child.getParent();
54  CPPUNIT_ASSERT(parent == p);
55 }
56 
58 {
60  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
61  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
62  CPPUNIT_ASSERT( child1->isSibling(child2) == true);
63  CPPUNIT_ASSERT( child2->isSibling(child1) == true);
64 
65  delete parent;
66 }
67 
69 {
71  CPPUNIT_ASSERT( parent->getChildrenCount() == 0);
72  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
73  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
74  te::common::TreeItem* child3 = new te::common::TreeItem(parent);
75  te::common::TreeItem* child4 = new te::common::TreeItem(parent);
76  CPPUNIT_ASSERT( parent->getChildrenCount() == 4);
77  te::common::TreeItem* child5 = new te::common::TreeItem(parent);
78  CPPUNIT_ASSERT( parent->getChildrenCount() == 5);
79 
80  delete parent;
81 }
82 
84 {
86  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
87  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
88 
89  te::common::TreeItemPtr child1Ref = parent->getChild(0);
90  te::common::TreeItemPtr child2Ref = parent->getChild(1);
91 
92  CPPUNIT_ASSERT( child1Ref->getIndex() == child1->getIndex());
93  CPPUNIT_ASSERT( child2Ref->getIndex() == child2->getIndex());
94 
95  delete parent;
96 }
97 
99 {
101  CPPUNIT_ASSERT( parent->getChildrenCount() == 0);
102 
103  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
104  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
105 
106  CPPUNIT_ASSERT( child1->getChildrenCount() == 0);
107  CPPUNIT_ASSERT( parent->getChildrenCount() == 2);
108 
109  child1->disconnect();
110  CPPUNIT_ASSERT( parent->getChildrenCount() == 1);
111  delete parent;
112 }
113 
115 {
117  te::common::TreeItem* child0 = new te::common::TreeItem(parent);
118  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
119  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
120  te::common::TreeItem* child3 = new te::common::TreeItem(parent);
121 
122  int indexes[4];
123  indexes[0] = child0->getIndex();
124  indexes[1] = child1->getIndex();
125  indexes[2] = child2->getIndex();
126  indexes[3] = child3->getIndex();
127 
128  int idx = 0;
129  for(te::common::TreeItem::const_iterator it = parent->begin(); it != parent->end(); ++it)
130  {
131  te::common::TreeItemPtr item = *it;
132  CPPUNIT_ASSERT(item->getIndex() == indexes[idx]);
133  idx++;
134  }
135  delete parent;
136 }
137 
139 {
141 
142  // parent has 3 children
143  te::common::TreeItem* childA = new te::common::TreeItem(parent);
144  te::common::TreeItem* childB = new te::common::TreeItem(parent);
145  te::common::TreeItem* childC = new te::common::TreeItem(parent);
146 
147  // childA has 2 children
148  te::common::TreeItem* childA1 = new te::common::TreeItem(childA);
149  te::common::TreeItem* childA2 = new te::common::TreeItem(childA);
150 
151  // childC has 1 child
152  te::common::TreeItem* childC1 = new te::common::TreeItem(childC);
153 
154  // childC1 has 5 children
155  te::common::TreeItem* childC1A = new te::common::TreeItem(childC1);
156  te::common::TreeItem* childC1B = new te::common::TreeItem(childC1);
157  te::common::TreeItem* childC1C = new te::common::TreeItem(childC1);
158  te::common::TreeItem* childC1D = new te::common::TreeItem(childC1);
159  te::common::TreeItem* childC1E = new te::common::TreeItem(childC1);
160 
161  std::size_t count = 0;
162  parent->getDescendantsCount(count);
163 
164  // total = 3 + 2 + 1 + 5 = 11 descendants
165  CPPUNIT_ASSERT(count == 11);
166 
167  delete parent;
168 }
169 
171 {
173  te::common::TreeItem* child0 = new te::common::TreeItem(parent);
174  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
175 
176  te::common::TreeItemPtr c0 = (*parent)[0];
177  te::common::TreeItemPtr c1 = (*parent)[1];
178 
179  CPPUNIT_ASSERT(c0->getIndex() == child0->getIndex());
180  CPPUNIT_ASSERT(c1->getIndex() == child1->getIndex());
181 
182  delete parent;
183 }
184 
186 {
188 
191 
192  parent->add(*node0);
193  CPPUNIT_ASSERT(parent->getChildrenCount() == 0);
194 
195  // node1 is now at the first position
196  parent->insert(0, node1);
197  CPPUNIT_ASSERT(parent->getChildrenCount() == 1);
198 
199  te::common::TreeItemPtr c0 = (*parent)[0];
200  te::common::TreeItemPtr c1 = (*parent)[1];
201 
202  CPPUNIT_ASSERT(c0->getIndex() == node1->getIndex());
203  CPPUNIT_ASSERT(c1->getIndex() == (*node0)->getIndex());
204 
205  delete parent;
206 }
207 
209 {
211  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
212  parent->remove(0);
213  CPPUNIT_ASSERT(parent->getChildrenCount() == 0);
214 }
215 
217 {
219  te::common::TreeItem* child0 = new te::common::TreeItem(parent);
220  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
221  te::common::TreeItem* child2 = new te::common::TreeItem(parent);
222  te::common::TreeItem* child3 = new te::common::TreeItem(parent);
223  te::common::TreeItem* child4 = new te::common::TreeItem(parent);
224 
225  parent->remove(0, 4);
226  te::common::TreeItemPtr child = parent->getChild(0);
227 
228  CPPUNIT_ASSERT(child4->getIndex() == child->getIndex());
229 }
230 
232 {
234  te::common::TreeItem* child0 = new te::common::TreeItem(parent);
235  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
236 
237  size_t i0 = child0->getIndex();
238  size_t i1 = child1->getIndex();
239 
240  parent->swap(child0, child1);
241 
242  te::common::TreeItemPtr c0 = (*parent)[0];
243  te::common::TreeItemPtr c1 = (*parent)[1];
244 
245  CPPUNIT_ASSERT(c0->getIndex() == i1);
246  CPPUNIT_ASSERT(c1->getIndex() == i0);
247 
248  delete parent;
249 }
250 
252 {
254  te::common::TreeItem* child0 = new te::common::TreeItem(parent);
255  te::common::TreeItem* child1 = new te::common::TreeItem(parent);
256 
257  size_t i0 = child0->getIndex();
258  size_t i1 = child1->getIndex();
259 
260  child0->swap(child1);
261 
262  te::common::TreeItemPtr c0 = (*parent)[0];
263  te::common::TreeItemPtr c1 = (*parent)[1];
264 
265  CPPUNIT_ASSERT(c0->getIndex() == i1);
266  CPPUNIT_ASSERT(c1->getIndex() == i0);
267 
268  delete parent;
269 }
CPPUNIT_TEST_SUITE_REGISTRATION(TsTreeItem)
const TreeItemPtr & getChild(std::size_t i) const
It returns the n-th child.
std::size_t getChildrenCount() const
It returns the number of children of this node.
void swap(const TreeItemPtr &firstChild, const TreeItemPtr &secondChild)
It swaps the position of the given children.
bool isSibling(const TreeItem *item) const
It checks if the given layer is sibling of this one.
This abstract class describes a basic item to be organized in a tree-oriented way.
void getChildByIndex()
Definition: TsTreeItem.cpp:170
std::size_t getIndex() const
It returns the index of this item in the list of children of its parent item.
void removingItems()
Definition: TsTreeItem.cpp:208
void add(const TreeItemPtr &childItem)
It adds (appends) the item to the end of the children&#39;s list.
void childSwap()
Definition: TsTreeItem.cpp:231
void iterateChildren()
Definition: TsTreeItem.cpp:114
void countItems()
Definition: TsTreeItem.cpp:138
void setUp()
Definition: TsTreeItem.cpp:34
void parentChildConnection()
Definition: TsTreeItem.cpp:49
boost::intrusive_ptr< TreeItem > TreeItemPtr
TreeItemPtr remove(std::size_t i)
It removes the i-th child.
const_iterator begin() const
It returns the constant iterator associated to the first child of this item.
te::gm::Polygon * p
void tearDown()
Definition: TsTreeItem.cpp:38
void simblings()
Definition: TsTreeItem.cpp:57
void removingSequence()
Definition: TsTreeItem.cpp:216
void getChildAndCheckItsIndex()
Definition: TsTreeItem.cpp:83
void childDisconnects()
Definition: TsTreeItem.cpp:98
const_iterator end() const
It returns the constant iterator that refers to one past the end of the children of this item...
Test suite for the TreeItem.
Definition: TsTreeItem.h:40
TreeItem * getParent() const
It returns a pointer to the parent of this node.
void checkNumberOfChildren()
Definition: TsTreeItem.cpp:68
Test suite for the TreeItem.
void noParent()
Definition: TsTreeItem.cpp:42
This file contains include headers for the TerraLib Common Runtime module.
void siblingSwap()
Definition: TsTreeItem.cpp:251
void addingItems()
Definition: TsTreeItem.cpp:185
void getDescendantsCount(std::size_t &count) const
It returns the number of nodes that descends from this node.
std::list< TreeItemPtr >::const_iterator const_iterator
void insert(std::size_t i, const TreeItemPtr &childItem)
It inserts an item in the informed position.