COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-core-tree-node.cpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
3 //
4 // This software is distributed under the terms of the
5 // GNU Lesser General Public License version 3 (LGPLv3).
6 // See doc/lgpl.txt and doc/gpl.txt for the license text.
7 
8 #define BOOST_TEST_DYN_LINK
9 #define BOOST_TEST_MODULE "Test module for the ui TreeNode class"
10 
11 #include "ui/core/NGeneric.hpp"
12 #include "ui/core/NRoot.hpp"
13 #include "ui/core/TreeNode.hpp"
14 
16 
17 using namespace cf3;
18 using namespace cf3::common;
19 using namespace cf3::ui::core;
20 
23 
24 BOOST_AUTO_TEST_SUITE( uiCoreNBrowserSuite )
25 
26 
29 {
30  application();
31 
36 }
37 
39 
40 BOOST_AUTO_TEST_CASE( contructor )
41 {
42  // 1. the node is null
43  BOOST_CHECK_THROW( TreeNode(Handle< CNode >(), nullptr, 0), FailedAssertion );
44 
45  // 2. the row number is below 0
46  boost::shared_ptr< NGeneric > node(new NGeneric("MyNode", "MyType"));
47  BOOST_CHECK_THROW( TreeNode(node->handle<CNode>(), nullptr, -1), FailedAssertion);
48 
49  // 3. everything is OK
50  BOOST_CHECK_NO_THROW( TreeNode(node->handle<CNode>(), nullptr, 0) );
51 }
52 
54 
55 BOOST_AUTO_TEST_CASE( has_parent )
56 {
57  boost::shared_ptr< NGeneric > node(new NGeneric("MyNode", "MyType"));
58  TreeNode parentLess(node->handle<CNode>(), nullptr, 0);
59  TreeNode withParent(node->handle<CNode>(), &parentLess, 0);
60 
61  // 1. does not have a parent
62  BOOST_CHECK( !parentLess.has_parent() );
63 
64  // 2. has a parent
65  BOOST_CHECK( withParent.has_parent() );
66 }
67 
69 
71 {
72  /*
73 
74  The tree we will work with:
75 
76  Root
77  |----> Node1
78  | |----> Node11
79  | |----> Node12
80  |
81  |----> Node2
82  | |----> Node21
83  | |----> Node22
84  | |----> Node23
85  |
86  |----> Node3
87 
88 
89  */
90 
91  boost::shared_ptr< NRoot > node(new NRoot("Root"));
92  boost::shared_ptr< NGeneric > node1(new NGeneric("Node1", "MyType"));
93  boost::shared_ptr< NGeneric > node11(new NGeneric("Node11", "MyType"));
94  boost::shared_ptr< NGeneric > node12(new NGeneric("Node12", "MyType"));
95  boost::shared_ptr< NGeneric > node2(new NGeneric("Node2", "MyType"));
96  boost::shared_ptr< NGeneric > node21(new NGeneric("Node21", "MyType"));
97  boost::shared_ptr< NGeneric > node22(new NGeneric("Node22", "MyType"));
98  boost::shared_ptr< NGeneric > node23(new NGeneric("Node23", "MyType"));
99  boost::shared_ptr< NGeneric > node3(new NGeneric("Node3", "MyType"));
100 
101  TreeNode * treeNode = nullptr;
102  TreeNode * treeNode1 = nullptr;
103  TreeNode * treeNode11 = nullptr;
104  TreeNode * treeNode12 = nullptr;
105  TreeNode * treeNode2 = nullptr;
106  TreeNode * treeNode21 = nullptr;
107  TreeNode * treeNode22 = nullptr;
108  TreeNode * treeNode23 = nullptr;
109  TreeNode * treeNode3 = nullptr;
110 
111  node->add_node(node1);
112  node->add_node(node2);
113  node->add_node(node3);
114 
115  node1->add_node(node11);
116  node1->add_node(node12);
117 
118  node2->add_node(node21);
119  node2->add_node(node22);
120  node2->add_node(node23);
121 
122  treeNode = new TreeNode(node->handle<CNode>(), nullptr, 0);
123 
124  //
125  // 1. check the direct children
126  //
127  treeNode1 = treeNode->child(0);
128  treeNode2 = treeNode->child(1);
129  treeNode3 = treeNode->child(2);
130 
131  // check that pointer are not null
132  BOOST_CHECK( treeNode1 != (TreeNode*) nullptr );
133  BOOST_CHECK( treeNode2 != (TreeNode*) nullptr );
134  BOOST_CHECK( treeNode3 != (TreeNode*) nullptr );
135 
136  // their parent should be the root
137  BOOST_CHECK_EQUAL( treeNode1->parent_node(), treeNode );
138  BOOST_CHECK_EQUAL( treeNode2->parent_node(), treeNode );
139  BOOST_CHECK_EQUAL( treeNode3->parent_node(), treeNode );
140 
141  // check their name
142  BOOST_CHECK_EQUAL( treeNode1->node_name().toStdString(), std::string("Node1") );
143  BOOST_CHECK_EQUAL( treeNode2->node_name().toStdString(), std::string("Node2") );
144  BOOST_CHECK_EQUAL( treeNode3->node_name().toStdString(), std::string("Node3") );
145 
146  //
147  // 2. children of Node1
148  //
149  treeNode11 = treeNode1->child(0);
150  treeNode12 = treeNode1->child(1);
151 
152  // check that pointer are not null
153  BOOST_CHECK( treeNode11 != (TreeNode*) nullptr );
154  BOOST_CHECK( treeNode12 != (TreeNode*) nullptr );
155 
156  // their parent should be the root
157  BOOST_CHECK_EQUAL( treeNode11->parent_node(), treeNode1 );
158  BOOST_CHECK_EQUAL( treeNode12->parent_node(), treeNode1 );
159 
160  // check their name
161  BOOST_CHECK_EQUAL( treeNode11->node_name().toStdString(), std::string("Node11") );
162  BOOST_CHECK_EQUAL( treeNode12->node_name().toStdString(), std::string("Node12") );
163 
164  //
165  // 3. children of Node2
166  //
167  treeNode21 = treeNode2->child(0);
168  treeNode22 = treeNode2->child(1);
169  treeNode23 = treeNode2->child(2);
170 
171  // check that pointer are not null
172  BOOST_CHECK( treeNode21 != (TreeNode*) nullptr );
173  BOOST_CHECK( treeNode22 != (TreeNode*) nullptr );
174  BOOST_CHECK( treeNode23 != (TreeNode*) nullptr );
175 
176  // their parent should be the root
177  BOOST_CHECK_EQUAL( treeNode21->parent_node(), treeNode2 );
178  BOOST_CHECK_EQUAL( treeNode22->parent_node(), treeNode2 );
179  BOOST_CHECK_EQUAL( treeNode23->parent_node(), treeNode2 );
180 
181  // check their name
182  BOOST_CHECK_EQUAL( treeNode21->node_name().toStdString(), std::string("Node21") );
183  BOOST_CHECK_EQUAL( treeNode22->node_name().toStdString(), std::string("Node22") );
184  BOOST_CHECK_EQUAL( treeNode23->node_name().toStdString(), std::string("Node23") );
185 
186  //
187  // 4. no child for Node3 (should return nullptr)
188  //
189  BOOST_CHECK_EQUAL( treeNode3->child(0), (TreeNode*) nullptr );
190 
191  delete treeNode; // deletes the children as well
192 }
193 
195 
196 BOOST_AUTO_TEST_CASE( child_by_name )
197 {
198  // same tree as for test_child()
199 
200  boost::shared_ptr< NRoot > node(new NRoot("Root"));
201  boost::shared_ptr< NGeneric > node1(new NGeneric("Node1", "MyType"));
202  boost::shared_ptr< NGeneric > node11(new NGeneric("Node11", "MyType"));
203  boost::shared_ptr< NGeneric > node12(new NGeneric("Node12", "MyType"));
204  boost::shared_ptr< NGeneric > node2(new NGeneric("Node2", "MyType"));
205  boost::shared_ptr< NGeneric > node21(new NGeneric("Node21", "MyType"));
206  boost::shared_ptr< NGeneric > node22(new NGeneric("Node22", "MyType"));
207  boost::shared_ptr< NGeneric > node23(new NGeneric("Node23", "MyType"));
208  boost::shared_ptr< NGeneric > node3(new NGeneric("Node3", "MyType"));
209 
210  TreeNode * treeNode = nullptr;
211  TreeNode * treeNode1 = nullptr;
212  TreeNode * treeNode11 = nullptr;
213  TreeNode * treeNode12 = nullptr;
214  TreeNode * treeNode2 = nullptr;
215  TreeNode * treeNode21 = nullptr;
216  TreeNode * treeNode22 = nullptr;
217  TreeNode * treeNode23 = nullptr;
218  TreeNode * treeNode3 = nullptr;
219 
220  node->add_node(node1);
221  node->add_node(node2);
222  node->add_node(node3);
223 
224  node1->add_node(node11);
225  node1->add_node(node12);
226 
227  node2->add_node(node21);
228  node2->add_node(node22);
229  node2->add_node(node23);
230 
231  treeNode = new TreeNode(node->handle<CNode>(), nullptr, 0);
232  treeNode1 = treeNode->child(0);
233  treeNode11 = treeNode1->child(0);
234  treeNode12 = treeNode1->child(1);
235  treeNode2 = treeNode->child(1);
236  treeNode21 = treeNode2->child(0);
237  treeNode22 = treeNode2->child(1);
238  treeNode23 = treeNode2->child(2);
239  treeNode3 = treeNode->child(2);
240 
241  //
242  // 1. check the direct children
243  //
244  treeNode1 = treeNode->child_by_name("Node1");
245  treeNode2 = treeNode->child_by_name("Node2");
246  treeNode3 = treeNode->child_by_name("Node3");
247 
248  // check that pointer are not null
249  BOOST_CHECK( treeNode1 != (TreeNode*) nullptr );
250  BOOST_CHECK( treeNode2 != (TreeNode*) nullptr );
251  BOOST_CHECK( treeNode3 != (TreeNode*) nullptr );
252 
253  // their parent should be the root
254  BOOST_CHECK_EQUAL( treeNode1->parent_node(), treeNode );
255  BOOST_CHECK_EQUAL( treeNode2->parent_node(), treeNode );
256  BOOST_CHECK_EQUAL( treeNode3->parent_node(), treeNode );
257 
258  // check their row number
259  BOOST_CHECK_EQUAL( treeNode1->row_number(), 0 );
260  BOOST_CHECK_EQUAL( treeNode2->row_number(), 1 );
261  BOOST_CHECK_EQUAL( treeNode3->row_number(), 2 );
262 
263  //
264  // 2. children of Node1
265  //
266  treeNode11 = treeNode1->child_by_name("Node11");
267  treeNode12 = treeNode1->child_by_name("Node12");
268 
269  // check that pointer are not null
270  BOOST_CHECK( treeNode11 != (TreeNode*) nullptr );
271  BOOST_CHECK( treeNode12 != (TreeNode*) nullptr );
272 
273  // their parent should be the root
274  BOOST_CHECK_EQUAL( treeNode11->parent_node(), treeNode1 );
275  BOOST_CHECK_EQUAL( treeNode12->parent_node(), treeNode1 );
276 
277  // check their row number
278  BOOST_CHECK_EQUAL( treeNode11->row_number(), 0 );
279  BOOST_CHECK_EQUAL( treeNode12->row_number(), 1 );
280 
281  //
282  // 3. wrong name
283  //
284  BOOST_CHECK_EQUAL ( treeNode2->child_by_name("Node"), (TreeNode*) nullptr );
285 
286 
287  delete treeNode; // deletes the children as well
288 }
289 
291 
292 BOOST_AUTO_TEST_CASE( update_child_list )
293 {
294  boost::shared_ptr< NRoot > node(new NRoot("Root"));
295  boost::shared_ptr< NGeneric > node1(new NGeneric("Node1", "MyType"));
296  boost::shared_ptr< NGeneric > node2(new NGeneric("Node2", "MyType"));
297  TreeNode * treeNode;
298  TreeNode * child;
299 
300  node->add_node(node1);
301 
302  treeNode = new TreeNode(node->handle<CNode>(), nullptr, 0);
303 
304  BOOST_CHECK_EQUAL( treeNode->child_count(), 1);
305 
306  node->add_node(node2);
307 
308  BOOST_CHECK_EQUAL( treeNode->child_count(), 2);
309 
310  child = treeNode->child(1);
311  BOOST_CHECK( child != nullptr );
312 
313  BOOST_CHECK_EQUAL( child->node_name().toStdString(), std::string("Node2"));
314 
315  delete treeNode;
316 }
317 
320 
321 BOOST_AUTO_TEST_SUITE_END()
QApplication * application()
Definition: Application.hpp:11
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
bool AssertionThrows
assertions throw exceptions
Definition: Assertions.hpp:67
bool ExceptionOutputs
if exception contructor should output
Definition: Exception.hpp:32
bool has_parent() const
Checks whether the node has a parent or not.
Definition: TreeNode.cpp:45
static AssertionManager & instance()
Gets the instance of the manager.
Definition: Assertions.cpp:33
Basic Classes for client-core library used by coolfluid-client application.
Definition: CNode.cpp:57
bool AssertionDumps
assertions dump backtraces
Definition: Assertions.hpp:65
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
Handles a CNode component in the tree.
Definition: TreeNode.hpp:30
int child_count() const
Gives the child count.
Definition: TreeNode.cpp:111
int row_number() const
Gives the row number.
Definition: TreeNode.cpp:104
TreeNode * child_by_name(const QString &name)
Retrieves a child from its name.
Definition: TreeNode.cpp:121
Base component adapted to fit the client needs.
Definition: CNode.hpp:120
TreeNode * parent_node() const
Gives the parent.
Definition: TreeNode.cpp:97
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
TreeNode * child(int row_number)
Returns the ith child of this node.
Definition: TreeNode.cpp:52
Client root. This class is wrapper for cf3::common::Root class on the client side. A NRoot object may never have any child. Add them to the internal Root componenent instead. It can be obtained by calling root() method.
Definition: NRoot.hpp:34
Client generic component.
Definition: NGeneric.hpp:29
BOOST_AUTO_TEST_CASE(init)
QString node_name() const
Gives the node name.
Definition: TreeNode.hpp:91
Most basic kernel library.
Definition: Action.cpp:19
Send comments to:
COOLFluiD Web Admin