COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-component.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for cf3::Component"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <boost/foreach.hpp>
13 #include <boost/iterator.hpp>
14 
15 #include "common/Log.hpp"
16 #include "common/Component.hpp"
18 #include "common/Group.hpp"
19 #include "common/Link.hpp"
20 #include "common/OptionList.hpp"
21 #include "common/PropertyList.hpp"
22 
23 #include "common/XML/Protocol.hpp"
25 
26 using namespace std;
27 using namespace boost;
28 using namespace cf3;
29 using namespace cf3::common;
30 using namespace cf3::common::XML;
31 
33 
34 BOOST_AUTO_TEST_SUITE( Component_TestSuite )
35 
36 
38 BOOST_AUTO_TEST_CASE( constructors )
39 {
40  // constructor with passed path
41  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
42 
43  BOOST_CHECK_EQUAL ( root->name() , "root" );
44  BOOST_CHECK_EQUAL ( root->uri().base_path().string() , "cpath:/" );
45  BOOST_CHECK_EQUAL ( root->uri().string() , "cpath:/" );
46 
47  BOOST_CHECK_EQUAL ( root->properties().check("brief") , true );
48  BOOST_CHECK_EQUAL ( root->properties().check("description") , true );
49 
50  // constructor with empty path
51  boost::shared_ptr<Group> dir1 = allocate_component<Group>( "dir1" );
52 
53  BOOST_CHECK_EQUAL ( dir1->name() , "dir1" );
54  BOOST_CHECK_EQUAL ( dir1->uri().base_path().string() , "cpath:/" );
55  BOOST_CHECK_EQUAL ( dir1->uri().string() , "cpath:/" );
56 
57  // constructor with passed path
58  boost::shared_ptr<Link> lnk = allocate_component<Link>( "lnk" );
59 
60  BOOST_CHECK_EQUAL ( lnk->name() , "lnk" );
61  BOOST_CHECK_EQUAL ( lnk->uri().base_path().string() , "cpath:/" );
62  BOOST_CHECK_EQUAL ( lnk->uri().string() , "cpath:/" );
63 }
64 
66 
67 BOOST_AUTO_TEST_CASE( add_component )
68 {
69  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
70 
71  boost::shared_ptr<Component> dir1 = allocate_component<Group>( "dir1" );
72  boost::shared_ptr<Component> dir2 = allocate_component<Group>( "dir2" );
73 
74  root->add_component( dir1 );
75  dir1->add_component( dir2 );
76 
77  BOOST_CHECK_EQUAL ( root->uri().string() , "cpath:/" );
78  BOOST_CHECK_EQUAL ( dir1->uri().string() , "cpath:/dir1" );
79  BOOST_CHECK_EQUAL ( dir2->uri().string() , "cpath:/dir1/dir2" );
80 }
81 
83 
85 {
86  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
87 
88  boost::shared_ptr<Component> dir1 = allocate_component<Group>( "dir1" );
89  boost::shared_ptr<Component> lnk1 = allocate_component<Link>( "lnk1" );
90 
91  // add child components to root
92  root->add_component( dir1 );
93  root->add_component( lnk1 );
94 
95  // point link to the dir1
96  boost::shared_ptr<Link> p_lnk1 = boost::dynamic_pointer_cast<Link>(lnk1);
97  p_lnk1->link_to(*dir1);
98 
99  // check that the root returns himself
100  BOOST_CHECK_EQUAL ( follow_link(*root)->name(), "root" );
101  BOOST_CHECK_EQUAL ( follow_link(*root)->uri().string(), "cpath:/" );
102 
103  // check that the link is sane
104  BOOST_CHECK_EQUAL ( lnk1->name(), "lnk1" );
105  BOOST_CHECK_EQUAL ( lnk1->uri().string(), "cpath:/lnk1" );
106 
107  // check that the link returns the dir1
108  BOOST_CHECK_EQUAL ( follow_link(*lnk1)->name(), "dir1" );
109  BOOST_CHECK_EQUAL ( follow_link(*lnk1)->uri().string(), "cpath:/dir1" );
110 
111 }
112 
114 
115 BOOST_AUTO_TEST_CASE( complete_path )
116 {
117  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
118 
119  boost::shared_ptr<Component> dir1 = allocate_component<Group>( "dir1" );
120  boost::shared_ptr<Component> dir2 = allocate_component<Group>( "dir2" );
121  boost::shared_ptr<Component> dir3 = allocate_component<Group>( "dir3" );
122 
123  // add child components to root
124  root->add_component( dir1 );
125  dir1->add_component( dir2 );
126  dir2->add_component( dir3 );
127 
128  // test absolute & complete path
129  URI p0 ( "cpath:/dir1" );
130  dir2->complete_path( p0 );
131  BOOST_CHECK_EQUAL ( p0.string(), "cpath:/dir1" );
132 
133  // test relative
134  URI p10 ( "cpath:.." );
135  dir2->complete_path( p10 );
136  BOOST_CHECK_EQUAL ( p10.string(), "cpath:/dir1" );
137 
138  // test relative
139  URI p11 ( "cpath:./" );
140  dir2->complete_path( p11 );
141  BOOST_CHECK_EQUAL ( p11.string(), "cpath:/dir1/dir2" );
142 
143  // test relative & complete path
144  URI p12 ( "cpath:../../dir2" );
145  dir3->complete_path( p12 );
146  BOOST_CHECK_EQUAL ( p12.string(), "cpath:/dir1/dir2" );
147 
148  // test absolute & incomplete path
149  URI p2 ( "cpath:/dir1/dir2/../dir2" );
150  dir2->complete_path( p2 );
151  BOOST_CHECK_EQUAL ( p2.string(), "cpath:/dir1/dir2" );
152 
153  // test absolute & multiple incomplete path
154  URI p3 ( "cpath:/dir1/../dir1/../dir1/dir2/../../dir1/dir2" );
155  dir2->complete_path( p3 );
156  BOOST_CHECK_EQUAL ( p3.string(), "cpath:/dir1/dir2" );
157 
158  // test absolute & multiple incomplete path at end
159  URI p4 ( "cpath:/dir1/dir2/dir3/../../" );
160  dir2->complete_path( p4 );
161  BOOST_CHECK_EQUAL ( p4.string(), "cpath:/dir1" );
162 
163 }
164 
166 
167 BOOST_AUTO_TEST_CASE( access_component )
168 {
169  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
170 
171  boost::shared_ptr<Component> dir1 = allocate_component<Group>( "dir1" );
172  boost::shared_ptr<Component> dir2 = allocate_component<Group>( "dir2" );
173  boost::shared_ptr<Component> dir21 = allocate_component<Group>( "dir21" );
174  boost::shared_ptr<Component> dir22 = allocate_component<Group>( "dir22" );
175 
176  // add child components to root
177  root->add_component( dir1 );
178  dir1->add_component( dir2 );
179  dir2->add_component( dir21 );
180  dir2->add_component( dir22 );
181 
182  // test relative & complete path
183  URI p0 ( "cpath:../dir21" );
184  Handle<Component> cp0 = dir22->access_component( p0 );
185  BOOST_CHECK_EQUAL ( cp0->uri().string(), "cpath:/dir1/dir2/dir21" );
186 
187  // test relative & complete path
188  URI p1 ( "cpath:/dir1" );
189  Handle<Component> cp1 = dir22->access_component( p1 );
190  BOOST_CHECK_EQUAL ( cp1->uri().string(), "cpath:/dir1" );
191 }
192 
194 
196 {
197  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
198 
199  boost::shared_ptr<Component> dir1 = allocate_component<Group>( "dir1" );
200  boost::shared_ptr<Component> dir2 = allocate_component<Group>( "dir2" );
201 
202  // add child components to root
203  root->add_component( dir1 );
204  dir1->add_component( dir2 );
205 
206  BOOST_CHECK_EQUAL ( dir2->uri().string(), "cpath:/dir1/dir2" );
207 
208  dir2->move_to( *root );
209 
210  BOOST_CHECK_EQUAL ( dir2->uri().string(), "cpath:/dir2" );
211 
212 }
213 
215 
217 {
218  boost::shared_ptr<Component> root = allocate_component<Group> ( "Simulator" );
219 
220  Handle<Component> proot = root->access_component("cpath:/");
221 
222  BOOST_CHECK_EQUAL ( proot->uri().string(), "cpath:/" );
223 
224 }
225 
227 
228 BOOST_AUTO_TEST_CASE( create_subcomponents )
229 {
230  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
231  Handle<Component> comp1 = root->create_component<Component>("comp1");
232  comp1->create_component<Component>("comp1_1");
233  comp1->create_component<Component>("comp1_2");
234 
235  BOOST_CHECK_EQUAL(find_component_with_name(*root, "comp1").name(),"comp1");
236  BOOST_CHECK_EQUAL(find_component_recursively_with_name(*root, "comp1_1").name(),"comp1_1");
237 }
238 
240 
241 BOOST_AUTO_TEST_CASE( create_component_signal )
242 {
243  boost::shared_ptr<Component> root = allocate_component<Group> ( "croot" );
244 
245  SignalFrame sf("Signal", "/", "/");
246 
247  sf.set_option( "name", common::class_name<std::string>(), "MyMesh" );
248  sf.set_option( "atype", common::class_name<std::string>(), "MeshReader" );
249  sf.set_option( "ctype", common::class_name<std::string>(), "CGNS" );
250 
251 // XmlOps::print_xml_node( *doc.get() );
252 // XmlOps::write_xml_node( *doc.get(), "test.xml" );
253 
254 }
255 
257 
259 {
260  boost::shared_ptr<Component> root = allocate_component<Group> ( "Simulator" );
261 
262  Handle<Component> c1 = root->create_component<Component>("c1");
263 
264  BOOST_CHECK_EQUAL ( c1->name(), "c1" );
265 
266  Handle<Component> c2 = root->create_component<Component>("c1");
267 
268  BOOST_CHECK_EQUAL ( c2->name(), "c1_1" );
269 
270  Handle<Component> c3 = root->create_component<Component>("c1");
271 
272  BOOST_CHECK_EQUAL ( c3->name(), "c1_2" );
273 
274  // turn off exception dumping
275  ExceptionManager::instance().ExceptionDumps = false;
276 
277  BOOST_CHECK_NO_THROW (c2->rename("c1_3"));
278  BOOST_CHECK(is_not_null(root->get_child("c1_3")));
279 }
280 
282 
284 {
285  boost::shared_ptr<Component> root = allocate_component<Group> ( "Simulator" );
286  Handle<Component> c1 = root->create_component<Component>("c1");
287  Handle<Component> c2 = root->create_component<Component>("c2");
288 
289  BOOST_CHECK(is_not_null(c1));
290  BOOST_CHECK(is_not_null(c2));
291 
292  root->clear();
293 
294  BOOST_CHECK(is_null(c1));
295  BOOST_CHECK(is_null(c2));
296 }
297 
299 
300 BOOST_AUTO_TEST_SUITE_END()
301 
302 
std::string name(ComponentWrapper &self)
BOOST_AUTO_TEST_CASE(constructors)
bool is_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:151
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
external boost library namespace
Classes that implement the XML protocol for use in COOLFluiD.
Definition: Component.hpp:43
URI uri() const
Construct the full path.
Definition: Component.cpp:248
STL namespace.
tuple root
Definition: coolfluid.py:24
Manages a set of maps.
Definition: SignalFrame.hpp:31
Holds the Component class, as well as the ComponentIterator class plus some functions related to comp...
ComponentReference< Component >::type find_component_recursively_with_name(Component &parent, StringConverter name)
ComponentReference< Component >::type find_component_with_name(Component &parent, StringConverter name)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Base class for defining CF components.
Definition: Component.hpp:82
Handle< Component > follow_link(const Handle< Component > &link_or_comp)
Follow links or return the component itself if it's not a link.
Definition: Link.cpp:84
Handle< Component > create_component(const std::string &name, const std::string &builder)
Build a (sub)component of this component using the extended type_name of the component.
Definition: Component.cpp:568
Most basic kernel library.
Definition: Action.cpp:19
std::string string() const
Definition: URI.cpp:258
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin