COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-component-iterator.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 iteration over components"
9 
10 #include <iostream>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/timer.hpp>
13 
14 #include "common/Log.hpp"
15 #include "common/Component.hpp"
17 #include "common/Group.hpp"
18 #include "common/Link.hpp"
19 
21 
22 using namespace cf3;
23 using namespace cf3::common;
24 
26 
28 {
31  {
35 
36  m_root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
37  Handle<Component> comp1 = m_root->create_component<Component>("comp1");
38  top_component_names.push_back(comp1->name());
39  component_names.push_back(comp1->name());
40  Handle<Component> comp1_1 = comp1->create_component<Component>("comp1_1");
41  component_names.push_back(comp1_1->name());
42  Handle<Component> comp1_2 = comp1->create_component<Component>("comp1_2");
43  component_names.push_back(comp1_2->name());
44  Handle<Component> comp2 = m_root->create_component<Component>("comp2");
45  top_component_names.push_back(comp2->name());
46  component_names.push_back(comp2->name());
47  Handle<Component> comp2_1 = comp2->create_component<Component>("comp2_1");
48  component_names.push_back(comp2_1->name());
49  Handle<Component> comp2_2 = comp2->create_component<Component>("comp2_2");
50  component_names.push_back(comp2_2->name());
51  Handle<Group> group1 = m_root->create_component<Group>("group1");
52  top_component_names.push_back(group1->name());
53  component_names.push_back(group1->name());
54  top_group_names.push_back(group1->name());
55  group_names.push_back(group1->name());
56  Handle<Component> comp3 = group1->create_component<Component>("comp3");
57  comp3->add_tag("special");
58  component_names.push_back(comp3->name());
59  special_component_names.push_back(comp3->name());
60  group1_component_names.push_back(comp3->name());
61  Handle<Group> group1_1 = group1->create_component<Group>("group1_1");
62  component_names.push_back(group1_1->name());
63  group_names.push_back(group1_1->name());
64  group1_group_names.push_back(group1_1->name());
65  group1_component_names.push_back(group1_1->name());
66  Handle<Group> group1_2 = group1->create_component<Group>("group1_2");
67  group1_2->add_tag("special");
68  component_names.push_back(group1_2->name());
69  group_names.push_back(group1_2->name());
70  group1_group_names.push_back(group1_2->name());
71  group1_component_names.push_back(group1_2->name());
72  special_component_names.push_back(group1_2->name());
73  Handle<Group> group2 = m_root->create_component<Group>("group2");
74  top_component_names.push_back(group2->name());
75  component_names.push_back(group2->name());
76  top_group_names.push_back(group2->name());
77  group_names.push_back(group2->name());
78  Handle<Group> group2_1 = group2->create_component<Group>("group2_1");
79  component_names.push_back(group2_1->name());
80  group_names.push_back(group2_1->name());
81  Handle<Group> group2_1_1 = group2_1->create_component<Group>("group2_1_1");
82  component_names.push_back(group2_1_1->name());
83  group_names.push_back(group2_1_1->name());
84  group2_1_1->add_tag("very_special");
85  Handle<Link> link2 = group2->create_component<Link>("link2");
86  component_names.push_back(link2->name());
87  Handle<Group> group3 = m_root->create_component<Group>("group3");
88  top_component_names.push_back(group3->name());
89  component_names.push_back(group3->name());
90  top_group_names.push_back(group3->name());
91  group_names.push_back(group3->name());
92  Handle<Group> group3_1 = group3->create_component<Group>("group3_1");
93  component_names.push_back(group3_1->name());
94  group_names.push_back(group3_1->name());
95  Handle<Link> link1 = m_root->create_component<Link>("link1");
96  component_names.push_back(link1->name());
97  top_component_names.push_back(link1->name());
98 
99  //
100  //comp1
101  //comp1/comp1_1
102  //comp1/comp1_2
103  //comp2
104  //comp2/comp2_1
105  //comp2/comp2_2
106  //group1
107  //group1/comp3 tag: special
108  //group1/group1_1
109  //group1/group1_2 tag: special
110  //group2
111  //group2/group2_1
112  //group2/group2_1/group2_1_1 tag: very_special
113  //group2/link2
114  //group3
115  //group3/group3_1
116  //link1
117 
118 
119  }
120 
123  {
124  }
125 
127  Component& root() { return *m_root; }
128 
130  const Component& const_root() { return *m_root; }
131 
132  Group& group1() { return *Handle<Group>(root().get_child("group1")); }
133  const Group& const_group1() { return *Handle<Group const>(const_root().get_child("group1")); }
134  Group& group2() { return *Handle<Group>(root().get_child("group2")); }
135  const Group& const_group2() { return *Handle<Group const>(const_root().get_child("group2")); }
136  Group& group3() { return *Handle<Group>(root().get_child("group3")); }
137  const Group& const_group3() { return *Handle<Group const>(const_root().get_child("group3")); }
138 
139  Group& group2_1() { return *Handle<Group>(group2().get_child("group2_1")); }
140  const Group& const_group2_1() { return *Handle<Group const>(const_group2().get_child("group2_1")); }
141 
143  std::vector<std::string> top_component_names;
145  std::vector<std::string> component_names;
147  std::vector<std::string> top_group_names;
149  std::vector<std::string> group1_group_names;
151  std::vector<std::string> group1_component_names;
153  std::vector<std::string> group_names;
155  std::vector<std::string> special_component_names;
156 private:
157  boost::shared_ptr<Component> m_root;
158 
159 };
160 
163 
164 BOOST_FIXTURE_TEST_SUITE( ComponentIteration, ComponentIterationFixture )
165 
166 
169 BOOST_AUTO_TEST_CASE( test_find_parent )
170 {
171  const Group& group2 = find_parent_component<Group>(const_group2_1());
172  BOOST_CHECK_EQUAL(group2.uri().string() , "cpath:/group2");
173 
174  Component& root = *group2_1().root();
175  BOOST_CHECK_EQUAL(root.uri().string() , "cpath:/");
176 
177  Component& group22 = find_parent_component_with_filter(group2_1(),IsComponentName("group2"));
178  BOOST_CHECK_EQUAL(group22.uri().string() , "cpath:/group2");
179 
180  Component& root2 = *group2_1().root();
181  BOOST_CHECK_EQUAL(root2.uri().string() , "cpath:/");
182 
183 }
185 
186 // Iterator tests:
187 // ---------------
188 // Several ways are available to iterate using a filter or predicate.
189 // They are listed from most difficult to use (showing how to do it yourself -> BOILER-PLATE-CODE)
190 // to most easy to use (wrappers that make users life easy)
191 
192 
194 // Non-recursive tests
196 
199 {
200  Uint counter = 0;
201  BOOST_FOREACH(Component& comp, root())
202  BOOST_CHECK_EQUAL(comp.name(), top_component_names[counter++]);
203 
204 }
205 
206 BOOST_AUTO_TEST_CASE( IteratorConst )
207 {
208  Uint counter = 0;
209  BOOST_FOREACH(const Component& comp, const_root())
210  BOOST_CHECK_EQUAL(comp.name(), top_component_names[counter++]);
211 }
212 
214 BOOST_AUTO_TEST_CASE( RangeTyped )
215 {
216  Uint counter = 0;
217  BOOST_FOREACH(const Component& comp, find_components<Group>(const_root()))
218  BOOST_CHECK_EQUAL(comp.name(), top_group_names[counter++]);
219 }
220 
222 BOOST_AUTO_TEST_CASE( RangeFilteredTyped )
223 {
224  BOOST_FOREACH(const Component& comp, find_components_with_name<Group>(const_root(), "group1"))
225  BOOST_CHECK_EQUAL(comp.name(), "group1");
226 }
227 
230 {
231  BOOST_CHECK_THROW(find_component_with_name(root(), "blah"), ValueNotFound);
232  BOOST_CHECK_THROW(find_component_with_name(root(), "group1_1"), ValueNotFound);
233  BOOST_CHECK_THROW(find_component_with_name<Group>(root(), "comp1"), ValueNotFound);
234 
235  BOOST_CHECK_EQUAL(find_component_with_name<Group>(root(), "group1").name(), "group1");
236  BOOST_CHECK_EQUAL(find_component_ptr_with_name<Group>(root(), "comp1"), Handle<Group>());
237  BOOST_CHECK_EQUAL(find_component_ptr_with_name<Group>(root(), "group1")->name(), "group1");
238  BOOST_CHECK_EQUAL(find_component_ptr_with_name(root(), "group1")->name(), "group1");
239  BOOST_CHECK_EQUAL(find_component_ptr_with_name(root(), "group1"), root().get_child("group1"));
240 }
241 
243 // Recursive tests
245 
247 BOOST_AUTO_TEST_CASE( RecursiveIterator )
248 {
249  Uint counter = 0;
250  for(Component::iterator it = root().recursive_begin(); it != root().recursive_end(); ++it)
251  BOOST_CHECK_EQUAL(it->name(), component_names[counter++]);
252 }
253 
255 BOOST_AUTO_TEST_CASE( ConstRecursiveIterator )
256 {
257  Uint counter = 0;
258  for(Component::const_iterator it = const_root().recursive_begin(); it != const_root().recursive_end(); ++it)
259  BOOST_CHECK_EQUAL(it->name(), component_names[counter++]);
260 }
261 
263 //BOOST_AUTO_TEST_CASE( RecursiveIteratorTyped )
264 //{
265 // Uint counter = 0;
266 // for(ComponentIterator<Group> it = root().recursive_begin<Group>(); it != root().recursive_end<Group>(); ++it)
267 // BOOST_CHECK_EQUAL(it->name(), group_names[counter++]);
268 //}
269 
271 //BOOST_AUTO_TEST_CASE( RecursiveIteratorTypedConst )
272 //{
273 // Uint counter = 0;
274 // for(ComponentIterator<Group const> it = const_root().recursive_begin<Group>(); it != const_root().recursive_end<Group>(); ++it) {
275 // BOOST_CHECK_EQUAL(it->name(), group_names[counter++]);
276 // }
277 //}
278 
280 BOOST_AUTO_TEST_CASE( RecursiveIteratorFiltered )
281 {
282  typedef boost::filter_iterator< IsComponentName , Component::const_iterator > FilterIterator;
283  FilterIterator filterIterator(IsComponentName("group1"), const_root().recursive_begin(), const_root().recursive_end());
284  FilterIterator last_filterIterator(IsComponentName("group1"), const_root().recursive_end(), const_root().recursive_end());
285  for (; filterIterator != last_filterIterator; ++filterIterator)
286  BOOST_CHECK_EQUAL(filterIterator->name(),"group1");
287  FilterIterator empty_begin(IsComponentName("nonExistingGroup"), const_root().recursive_begin(), const_root().recursive_end());
288  FilterIterator empty_end(IsComponentName("nonExistingGroup"), const_root().recursive_end(), const_root().recursive_end());
289  BOOST_CHECK_EQUAL(empty_begin == empty_end, true);
290 }
291 
293 BOOST_AUTO_TEST_CASE( RecursiveRange )
294 {
295  Uint counter = 0;
296  BOOST_FOREACH(Component& comp, find_components_recursively(root()))
297  BOOST_CHECK_EQUAL(comp.name(), component_names[counter++]);
298 }
299 
301 BOOST_AUTO_TEST_CASE( RecursiveRangeTyped )
302 {
303  Uint counter = 0;
304  BOOST_FOREACH(const Component& comp, find_components_recursively<Group>(const_root()))
305  BOOST_CHECK_EQUAL(comp.name(), group_names[counter++]);
306 }
307 
309 BOOST_AUTO_TEST_CASE( RecursiveRangeFilteredIterators )
310 {
311  BOOST_FOREACH(const Component& comp, make_filtered_range(root().recursive_begin(), root().recursive_end(), IsComponentName("group1")))
312  BOOST_CHECK_EQUAL(comp.name(), "group1");
313 }
314 
316 BOOST_AUTO_TEST_CASE( RecursiveRangeFiltered )
317 {
318  BOOST_FOREACH(const Component& comp, find_components_recursively_with_name(const_root(), "group1"))
319  BOOST_CHECK_EQUAL(comp.name(), "group1");
320 }
321 
323 BOOST_AUTO_TEST_CASE( RecursiveRangeFilteredTyped )
324 {
325  BOOST_FOREACH(const Component& comp, find_components_recursively_with_name<Group>(const_root(), "group1"))
326  BOOST_CHECK_EQUAL(comp.name(), "group1");
327 }
328 
330 BOOST_AUTO_TEST_CASE( RecursiveRangeEmpty )
331 {
332  // Use the empty method of boost::range
333  BOOST_CHECK_EQUAL(find_components_recursively_with_name<Group>(const_root(), "comp1").empty(), true);
334  // Explicitely construct a range:
335  ComponentIteratorRangeSelector<Component const, Group, IsComponentName>::type range = find_components_recursively_with_name<Group>(const_root(), "comp1");
336  BOOST_CHECK_EQUAL(range.begin() == range.end(), true);
337 }
338 
340 BOOST_AUTO_TEST_CASE( GetByNameRecursive )
341 {
342  BOOST_CHECK_THROW(find_component_recursively_with_name(root(), "blah"), ValueNotFound);
343  BOOST_CHECK_EQUAL(find_component_recursively_with_name(root(), "group1_1").name(), "group1_1");
344  BOOST_CHECK_THROW(find_component_recursively_with_name<Group>(root(), "comp1"), ValueNotFound);
345  BOOST_CHECK_EQUAL(find_component_recursively_with_name<Group>(root(), "group1").name(), "group1");
346  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_name<Group>(root(), "comp1"), Handle<Group>());
347  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_name<Group>(root(), "group1")->name(), "group1");
348  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_name(root(), "group1")->name(), "group1");
349 }
350 
351 BOOST_AUTO_TEST_CASE( test_find_components )
352 {
353  Uint counter;
354 
355  counter = 0;
356  BOOST_FOREACH(Group& group, find_components<Group>(group1()))
357  BOOST_CHECK_EQUAL(group.name(),group1_group_names[counter++]);
358  BOOST_CHECK_EQUAL(counter, group1_group_names.size());
359 
360  counter = 0;
361  BOOST_FOREACH(const Group& group, find_components<Group>(const_group1()))
362  BOOST_CHECK_EQUAL(group.name(),group1_group_names[counter++]);
363  BOOST_CHECK_EQUAL(counter, group1_group_names.size());
364 
365  counter = 0;
366  BOOST_FOREACH(Component& group, find_components(group1()))
367  BOOST_CHECK_EQUAL(group.name(),group1_component_names[counter++]);
368  BOOST_CHECK_EQUAL(counter, group1_component_names.size());
369 
370  counter = 0;
371  BOOST_FOREACH(const Component& group, find_components(const_group1()))
372  BOOST_CHECK_EQUAL(group.name(),group1_component_names[counter++]);
373  BOOST_CHECK_EQUAL(counter, group1_component_names.size());
374 }
375 
376 BOOST_AUTO_TEST_CASE( test_find_components_with_filter )
377 {
378  Uint counter;
379 
380  BOOST_CHECK_EQUAL(find_components_with_filter<Group>(group1(),IsComponentTag("special")).empty() , false);
381  BOOST_CHECK_EQUAL(find_components_with_filter<Group>(const_group1(),IsComponentTag("special")).empty() , false);
382  BOOST_CHECK_EQUAL(find_components_with_filter(group1(),IsComponentTag("special")).empty() , false);
383  BOOST_CHECK_EQUAL(find_components_with_filter(const_group1(),IsComponentTag("special")).empty() , false);
384 
385  BOOST_FOREACH(Group& group, find_components_with_filter<Group>(group1(),IsComponentTag("special")))
386  BOOST_CHECK_EQUAL(group.name(),"group1_2");
387 
388  counter = 0;
389  BOOST_FOREACH(const Group& group, find_components_with_filter<Group>(const_group1(),IsComponentTag("special")))
390  BOOST_CHECK_EQUAL(group.name(),"group1_2");
391 
392  counter = 0;
393  BOOST_FOREACH(Component& group, find_components_with_filter(group1(),IsComponentTag("special")))
394  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
395 
396  counter = 0;
397  BOOST_FOREACH(const Component& group, find_components_with_filter(const_group1(),IsComponentTag("special")))
398  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
399 }
400 
401 BOOST_AUTO_TEST_CASE( test_find_components_with_tag )
402 {
403  Uint counter;
404 
405  BOOST_CHECK_EQUAL(find_components_with_tag<Group>(group2(),"special").empty() , true);
406  BOOST_CHECK_EQUAL(find_components_with_tag<Group>(const_group2(),"special").empty() , true);
407  BOOST_CHECK_EQUAL(find_components_with_tag(group2(),"special").empty() , true);
408  BOOST_CHECK_EQUAL(find_components_with_tag(const_group2(),"special").empty() , true);
409 
410  BOOST_CHECK_EQUAL(find_components_with_tag<Group>(group1(),"special").empty() , false);
411  BOOST_CHECK_EQUAL(find_components_with_tag<Group>(const_group1(),"special").empty() , false);
412  BOOST_CHECK_EQUAL(find_components_with_tag(group1(),"special").empty() , false);
413  BOOST_CHECK_EQUAL(find_components_with_tag(const_group1(),"special").empty() , false);
414 
415  BOOST_FOREACH(Group& group, find_components_with_tag<Group>(group1(),"special"))
416  BOOST_CHECK_EQUAL(group.name(),"group1_2");
417 
418  BOOST_FOREACH(const Group& group, find_components_with_tag<Group>(group1(),"special"))
419  BOOST_CHECK_EQUAL(group.name(),"group1_2");
420 
421  counter = 0;
422  BOOST_FOREACH(Component& group, find_components_with_tag(group1(),"special"))
423  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
424  BOOST_CHECK_EQUAL(counter,special_component_names.size());
425 
426  counter = 0;
427  BOOST_FOREACH(const Component& group, find_components_with_tag(const_group1(),"special"))
428  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
429  BOOST_CHECK_EQUAL(counter,special_component_names.size());
430 }
431 
432 BOOST_AUTO_TEST_CASE( test_find_components_with_name )
433 {
434  BOOST_CHECK_EQUAL(find_components_with_name<Group>(group1(),"group1_2").empty() , false);
435  BOOST_CHECK_EQUAL(find_components_with_name<Group>(const_group1(),"group1_2").empty() , false);
436  BOOST_CHECK_EQUAL(find_components_with_name(group1(),"group1_2").empty() , false);
437  BOOST_CHECK_EQUAL(find_components_with_name(const_group1(),"group1_2").empty() , false);
438 
439  BOOST_FOREACH(Group& group, find_components_with_name<Group>(group1(),"group1_2"))
440  BOOST_CHECK_EQUAL(group.name(),"group1_2");
441 
442  BOOST_FOREACH(const Group& group, find_components_with_name<Group>(const_group1(),"group1_2"))
443  BOOST_CHECK_EQUAL(group.name(),"group1_2");
444 
445  BOOST_FOREACH(Component& group, find_components_with_name(group1(),"group1_2"))
446  BOOST_CHECK_EQUAL(group.name(),"group1_2");
447 
448  BOOST_FOREACH(const Component& group, find_components_with_name(const_group1(),"group1_2"))
449  BOOST_CHECK_EQUAL(group.name(),"group1_2");
450 }
451 
452 BOOST_AUTO_TEST_CASE( test_find_components_recursively )
453 {
454  Uint counter;
455 
456  counter = 0;
457  BOOST_FOREACH(Group& group, find_components_recursively<Group>(group1()))
458  BOOST_CHECK_EQUAL(group.name(),group1_group_names[counter++]);
459  BOOST_CHECK_EQUAL(counter, group1_group_names.size());
460 
461  counter = 0;
462  BOOST_FOREACH(const Group& group, find_components_recursively<Group>(group1()))
463  BOOST_CHECK_EQUAL(group.name(),group1_group_names[counter++]);
464  BOOST_CHECK_EQUAL(counter, group1_group_names.size());
465 
466  counter = 0;
467  BOOST_FOREACH(Component& group, find_components_recursively(group1()))
468  BOOST_CHECK_EQUAL(group.name(),group1_component_names[counter++]);
469  BOOST_CHECK_EQUAL(counter, group1_component_names.size());
470 
471  counter = 0;
472  BOOST_FOREACH(const Component& group, find_components_recursively(const_group1()))
473  BOOST_CHECK_EQUAL(group.name(),group1_component_names[counter++]);
474  BOOST_CHECK_EQUAL(counter, group1_component_names.size());
475 }
476 
477 BOOST_AUTO_TEST_CASE( test_find_components_recursively_with_filter )
478 {
479  Uint counter;
480 
481  BOOST_CHECK_EQUAL(find_components_recursively_with_filter<Group>(group1(),IsComponentTag("special")).empty() , false);
482  BOOST_CHECK_EQUAL(find_components_recursively_with_filter<Group>(const_group1(),IsComponentTag("special")).empty() , false);
483  BOOST_CHECK_EQUAL(find_components_recursively_with_filter(group1(),IsComponentTag("special")).empty() , false);
484  BOOST_CHECK_EQUAL(find_components_recursively_with_filter(const_group1(),IsComponentTag("special")).empty() , false);
485 
486  BOOST_FOREACH(Group& group, find_components_recursively_with_filter<Group>(group1(),IsComponentTag("special")))
487  BOOST_CHECK_EQUAL(group.name(),"group1_2");
488 
489  BOOST_FOREACH(const Group& group, find_components_recursively_with_filter<Group>(const_group1(),IsComponentTag("special")))
490  BOOST_CHECK_EQUAL(group.name(),"group1_2");
491 
492  counter = 0;
493  BOOST_FOREACH(Component& group, find_components_recursively_with_filter(group1(),IsComponentTag("special")))
494  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
495  BOOST_CHECK_EQUAL(counter, special_component_names.size());
496 
497  counter = 0;
498  BOOST_FOREACH(const Component& group, find_components_recursively_with_filter(const_group1(),IsComponentTag("special")))
499  BOOST_CHECK_EQUAL(group.name(),special_component_names[counter++]);
500  BOOST_CHECK_EQUAL(counter, special_component_names.size());
501 }
502 
503 BOOST_AUTO_TEST_CASE( test_find_components_recursively_with_tag )
504 {
505 
506  BOOST_CHECK_EQUAL(find_components_recursively_with_tag<Group>(group2(),"very_special").empty() , false);
507  BOOST_CHECK_EQUAL(find_components_recursively_with_tag<Group>(const_group2(),"very_special").empty() , false);
508  BOOST_CHECK_EQUAL(find_components_recursively_with_tag(group2(),"very_special").empty() , false);
509  BOOST_CHECK_EQUAL(find_components_recursively_with_tag(const_group2(),"very_special").empty() , false);
510 
511  BOOST_FOREACH(Group& group, find_components_recursively_with_tag<Group>(group2(),"very_special"))
512  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
513 
514  BOOST_FOREACH(const Group& group, find_components_recursively_with_tag<Group>(const_group2(),"very_special"))
515  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
516 
517  BOOST_FOREACH(Component& group, find_components_recursively_with_tag(group2(),"very_special"))
518  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
519 
520  BOOST_FOREACH(const Component& group, find_components_recursively_with_tag(const_group2(),"very_special"))
521  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
522 }
523 
524 BOOST_AUTO_TEST_CASE( test_find_components_recursively_with_name )
525 {
526  BOOST_CHECK_EQUAL(find_components_recursively_with_name<Group>(group2(),"group2_1_1").empty() , false);
527  BOOST_CHECK_EQUAL(find_components_recursively_with_name<Group>(const_group2(),"group2_1_1").empty() , false);
528  BOOST_CHECK_EQUAL(find_components_recursively_with_name(group2(),"group2_1_1").empty() , false);
529  BOOST_CHECK_EQUAL(find_components_recursively_with_name(const_group2(),"group2_1_1").empty() , false);
530 
531  BOOST_FOREACH(Group& group, find_components_recursively_with_name<Group>(group2(),"group2_1_1"))
532  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
533 
534  BOOST_FOREACH(const Group& group, find_components_recursively_with_name<Group>(const_group2(),"group2_1_1"))
535  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
536 
537  BOOST_FOREACH(Component& group, find_components_recursively_with_name(group2(),"group2_1_1"))
538  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
539 
540  BOOST_FOREACH(const Component& group, find_components_recursively_with_name(const_group2(),"group2_1_1"))
541  BOOST_CHECK_EQUAL(group.name(),"group2_1_1");
542 }
543 
544 BOOST_AUTO_TEST_CASE( test_find_component )
545 {
546  BOOST_CHECK_EQUAL(find_component(group2_1()).name() , "group2_1_1" );
547  BOOST_CHECK(find_component_ptr(group2_1()) != nullptr );
548  BOOST_CHECK_EQUAL(find_component(const_group2_1()).name() , "group2_1_1" );
549  BOOST_CHECK(find_component_ptr(const_group2_1()) != nullptr );
550 
551  BOOST_CHECK_EQUAL(find_component<Group>(group2()).name() , "group2_1" );
552  BOOST_CHECK(find_component_ptr<Group>(group2()) != nullptr );
553  BOOST_CHECK_EQUAL(find_component_ptr<Group>(group2())->name() , "group2_1" );
554  BOOST_CHECK_EQUAL(find_component<Group>(const_group2()).name() , "group2_1" );
555  BOOST_CHECK(find_component_ptr<Group>(const_group2()) != nullptr );
556  BOOST_CHECK_EQUAL(find_component_ptr<Group>(const_group2())->name() , "group2_1" );
557 }
558 
559 BOOST_AUTO_TEST_CASE( test_find_component_with_filter )
560 {
561  BOOST_CHECK_EQUAL(find_component_with_filter(group2(),IsComponentName("group2_1")).name() , "group2_1" );
562  BOOST_CHECK(find_component_ptr_with_filter(group2(),IsComponentName("group2_1")) != nullptr );
563  BOOST_CHECK_EQUAL(find_component_with_filter(const_group2(),IsComponentName("group2_1")).name() , "group2_1" );
564  BOOST_CHECK(find_component_ptr_with_filter(const_group2(),IsComponentName("group2_1")) != nullptr );
565 
566  BOOST_CHECK_EQUAL(find_component_with_filter<Group>(group2(),IsComponentName("group2_1")).name() , "group2_1" );
567  BOOST_CHECK(find_component_ptr_with_filter<Group>(group2(),IsComponentName("group2_1")) != nullptr );
568  BOOST_CHECK_EQUAL(find_component_ptr_with_filter<Group>(group2(),IsComponentName("group2_1"))->name() , "group2_1" );
569  BOOST_CHECK_EQUAL(find_component_with_filter<Group>(const_group2(),IsComponentName("group2_1")).name() , "group2_1" );
570  BOOST_CHECK(find_component_ptr_with_filter<Group>(const_group2(),IsComponentName("group2_1")) != nullptr );
571  BOOST_CHECK_EQUAL(find_component_ptr_with_filter<Group>(const_group2(),IsComponentName("group2_1"))->name() , "group2_1" );
572 }
573 
574 BOOST_AUTO_TEST_CASE( test_find_component_with_name )
575 {
576  BOOST_CHECK_EQUAL(find_component_with_name(group2(),"group2_1").name() , "group2_1" );
577  BOOST_CHECK(find_component_ptr_with_name(group2(),"group2_1") != nullptr );
578  BOOST_CHECK_EQUAL(find_component_with_name(const_group2(),"group2_1").name() , "group2_1" );
579  BOOST_CHECK(find_component_ptr_with_name(const_group2(),"group2_1") != nullptr );
580 
581  BOOST_CHECK_EQUAL(find_component_with_name<Group>(group2(),"group2_1").name() , "group2_1" );
582  BOOST_CHECK(find_component_ptr_with_name<Group>(group2(),"group2_1") != nullptr );
583  BOOST_CHECK_EQUAL(find_component_ptr_with_name<Group>(group2(),"group2_1")->name() , "group2_1" );
584  BOOST_CHECK_EQUAL(find_component_with_name<Group>(const_group2(),"group2_1").name() , "group2_1" );
585  BOOST_CHECK(find_component_ptr_with_name<Group>(const_group2(),"group2_1") != nullptr );
586  BOOST_CHECK_EQUAL(find_component_ptr_with_name<Group>(const_group2(),"group2_1")->name() , "group2_1" );
587 }
588 
589 BOOST_AUTO_TEST_CASE( test_find_component_with_tag )
590 {
591  BOOST_CHECK_EQUAL(find_component_with_tag(group2_1(),"very_special").name() , "group2_1_1" );
592  BOOST_CHECK(find_component_ptr_with_tag(group2_1(),"very_special") != nullptr );
593  BOOST_CHECK_EQUAL(find_component_with_tag(const_group2_1(),"very_special").name() , "group2_1_1" );
594  BOOST_CHECK(find_component_ptr_with_tag(const_group2_1(),"very_special") != nullptr );
595 
596  BOOST_CHECK_EQUAL(find_component_with_tag<Group>(group1(),"special").name() , "group1_2" );
597  BOOST_CHECK(find_component_ptr_with_tag<Group>(group1(),"special") != nullptr );
598  BOOST_CHECK_EQUAL(find_component_ptr_with_tag<Group>(group1(),"special")->name() , "group1_2" );
599  BOOST_CHECK_EQUAL(find_component_with_tag<Group>(const_group1(),"special").name() , "group1_2" );
600  BOOST_CHECK(find_component_ptr_with_tag<Group>(const_group1(),"special") != nullptr );
601  BOOST_CHECK_EQUAL(find_component_ptr_with_tag<Group>(const_group1(),"special")->name() , "group1_2" );
602 }
603 
604 BOOST_AUTO_TEST_CASE( test_find_component_recursively )
605 {
606  BOOST_CHECK_EQUAL(find_component_recursively(group2_1()).name() , "group2_1_1" );
607  BOOST_CHECK(find_component_ptr_recursively(group2_1()) != nullptr );
608  BOOST_CHECK_EQUAL(find_component_recursively(const_group2_1()).name() , "group2_1_1" );
609  BOOST_CHECK(find_component_ptr_recursively(const_group2_1()) != nullptr );
610 
611  BOOST_CHECK_EQUAL(find_component_recursively<Group>(group2_1()).name() , "group2_1_1" );
612  BOOST_CHECK(find_component_ptr_recursively<Group>(group2_1()) != nullptr );
613  BOOST_CHECK_EQUAL(find_component_ptr_recursively<Group>(group2_1())->name() , "group2_1_1" );
614  BOOST_CHECK_EQUAL(find_component_recursively<Group>(const_group2_1()).name() , "group2_1_1" );
615  BOOST_CHECK(find_component_ptr_recursively<Group>(const_group2_1()) != nullptr );
616  BOOST_CHECK_EQUAL(find_component_ptr_recursively<Group>(const_group2_1())->name() , "group2_1_1" );
617 }
618 
619 BOOST_AUTO_TEST_CASE( test_find_component_recursively_with_filter )
620 {
621  BOOST_CHECK_EQUAL(find_component_recursively_with_filter(group2(),IsComponentTag("very_special")).name() , "group2_1_1" );
622  BOOST_CHECK(find_component_ptr_recursively_with_filter(group2(),IsComponentTag("very_special")) != nullptr );
623  BOOST_CHECK_EQUAL(find_component_recursively_with_filter(const_group2(),IsComponentTag("very_special")).name() , "group2_1_1" );
624  BOOST_CHECK(find_component_ptr_recursively_with_filter(const_group2(),IsComponentTag("very_special")) != nullptr );
625 
626  BOOST_CHECK_EQUAL(find_component_recursively_with_filter<Group>(group2(),IsComponentTag("very_special")).name() , "group2_1_1" );
627  BOOST_CHECK(find_component_ptr_recursively_with_filter<Group>(group2(),IsComponentTag("very_special")) != nullptr );
628  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_filter<Group>(group2(),IsComponentTag("very_special"))->name() , "group2_1_1" );
629  BOOST_CHECK_EQUAL(find_component_recursively_with_filter<Group>(const_group2(),IsComponentTag("very_special")).name() , "group2_1_1" );
630  BOOST_CHECK(find_component_ptr_recursively_with_filter<Group>(const_group2(),IsComponentTag("very_special")) != nullptr );
631  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_filter<Group>(const_group2(),IsComponentTag("very_special"))->name() , "group2_1_1" );
632 }
633 
634 BOOST_AUTO_TEST_CASE( test_find_component_recursively_with_name )
635 {
636  BOOST_CHECK_EQUAL(find_component_recursively_with_name(group2(),"group2_1_1").name() , "group2_1_1" );
637  BOOST_CHECK(find_component_ptr_recursively_with_name(group2(),"group2_1_1") != nullptr );
638  BOOST_CHECK_EQUAL(find_component_recursively_with_name(const_group2(),"group2_1_1").name() , "group2_1_1" );
639  BOOST_CHECK(find_component_ptr_recursively_with_name(const_group2(),"group2_1_1") != nullptr );
640 
641  BOOST_CHECK_EQUAL(find_component_recursively_with_name<Group>(group2(),"group2_1_1").name() , "group2_1_1" );
642  BOOST_CHECK(find_component_ptr_recursively_with_name<Group>(group2(),"group2_1_1") != nullptr );
643  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_name<Group>(group2(),"group2_1_1")->name() , "group2_1_1" );
644  BOOST_CHECK_EQUAL(find_component_recursively_with_name<Group>(const_group2(),"group2_1_1").name() , "group2_1_1" );
645  BOOST_CHECK(find_component_ptr_recursively_with_name<Group>(const_group2(),"group2_1_1") != nullptr );
646  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_name<Group>(const_group2(),"group2_1_1")->name() , "group2_1_1" );
647 }
648 
649 BOOST_AUTO_TEST_CASE( test_find_component_recursively_with_tag )
650 {
651  BOOST_CHECK_EQUAL(find_component_recursively_with_tag(group2(),"very_special").name() , "group2_1_1" );
652  BOOST_CHECK(find_component_ptr_recursively_with_tag(group2(),"very_special") != nullptr );
653  BOOST_CHECK_EQUAL(find_component_recursively_with_tag(const_group2(),"very_special").name() , "group2_1_1" );
654  BOOST_CHECK(find_component_ptr_recursively_with_tag(const_group2(),"very_special") != nullptr );
655 
656  BOOST_CHECK_EQUAL(find_component_recursively_with_tag<Group>(group2(),"very_special").name() , "group2_1_1" );
657  BOOST_CHECK(find_component_ptr_recursively_with_tag<Group>(group2(),"very_special") != nullptr );
658  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_tag<Group>(group2(),"very_special")->name() , "group2_1_1" );
659  BOOST_CHECK_EQUAL(find_component_recursively_with_tag<Group>(const_group2(),"very_special").name() , "group2_1_1" );
660  BOOST_CHECK(find_component_ptr_recursively_with_tag<Group>(const_group2(),"very_special") != nullptr );
661  BOOST_CHECK_EQUAL(find_component_ptr_recursively_with_tag<Group>(const_group2(),"very_special")->name() , "group2_1_1" );
662 }
663 
665 
666 BOOST_AUTO_TEST_CASE( speed_find_tag )
667 {
668  Handle<Group> mg = root().create_component<Group>("ManyGroup2");
669 
670  // allocate 5000 components
671  for ( Uint i = 0; i < 250 ; ++i)
672  {
673  mg->create_component<Group>( std::string("ggg") + to_str(i) )->add_tag("Group");
674  }
675 
676  boost::timer timer;
677  const Uint counter = count(find_components_recursively_with_tag(*mg, "Group" ));
678  std::cout << "iterate by [tag] over " << counter << " components in " << timer.elapsed() << " seconds" << std::endl;
679 }
680 
682 
683 BOOST_AUTO_TEST_CASE( speed_find_type )
684 {
685  Handle<Group> mg = root().create_component<Group>("ManyGroup1");
686 
687  // allocate 5000 components
688  for ( Uint i = 0; i < 250 ; ++i)
689  {
690  mg->create_component<Group>( std::string("ggg") + to_str(i) );
691  }
692 
693  boost::timer timer;
694  const Uint counter = count(find_components_recursively<Group>(*mg) );
695  std::cout << "iterate by [type] over " << counter << " components in " << timer.elapsed() << " seconds" << std::endl;
696 }
697 
699 
700 BOOST_AUTO_TEST_CASE( test_range_to_vector )
701 {
702  Handle<Group> mg = root().create_component<Group>("ManyGroup1");
703 
704  // allocate 10 components
705  for ( Uint i = 0; i < 10 ; ++i)
706  {
707  mg->create_component<Group>( std::string("ggg") + to_str(i) );
708  }
709 
710  std::vector< Handle<Group> > vector = range_to_vector(find_components_recursively<Group>(*mg));
711  BOOST_CHECK_EQUAL(vector.size() , 10u);
712 
713  Handle<Group const> const_mg(mg);
714  std::vector< Handle<Group const> > const_vector = range_to_vector(find_components_recursively<Group>(*const_mg));
715  BOOST_CHECK_EQUAL(const_vector.size(), 10u);
716 
717  const_vector = range_to_const_vector(find_components_recursively<Group>(*mg));
718  BOOST_CHECK_EQUAL(const_vector.size(), 10u);
719 }
720 
722 
723 BOOST_AUTO_TEST_CASE( test_new_range )
724 {
725  ComponentIteratorRange<Component> new_range ( root().begin(),root().end() );
726  BOOST_FOREACH( Component& comp, new_range )
727  CFLogVar(comp.name());
728 
730  ComponentIteratorRange<Group> new_range2 ( component_begin<Group>(root()), component_end<Group>(root()) );
731  BOOST_FOREACH( Group& comp, new_range2 )
732  CFLogVar(comp.name());
733 
735  ComponentIteratorRange<const Group,IsComponentName> new_range5 ( component_begin<Group const>(const_root()), component_end<Group const>(const_root()), IsComponentName("group1") );
736  BOOST_FOREACH( const Group& comp, new_range5 )
737  CFLogVar(comp.name());
738 
740  typedef ComponentIteratorRange<Group,IsComponentName> filtered_range;
741  typedef ComponentIteratorRange<Group> group_range;
742 
743  BOOST_FOREACH( Group& comp, filtered_range( component_begin<Group>(root()), component_end<Group>(root()) , IsComponentName("group1") ) )
744  CFLogVar(comp.name());
745 
746  CFLogVar(group_range(component_begin<Group>(root()),component_end<Group>(root())).as_vector().size());
747  CFLogVar(group_range(component_begin<Group>(root()),component_end<Group>(root())).as_const_vector().size());
748  CFLogVar(group_range(component_begin<Group>(root()),component_end<Group>(root())).size());
749 
751  BOOST_FOREACH( Group& comp, make_new_range(component_begin<Group>(root()),component_end<Group>(root()),IsComponentName("group1")) )
752  CFLogVar(comp.name());
753 
754  BOOST_FOREACH( const Group& comp, make_new_range(component_begin<Group>(const_root()),component_end<Group>(const_root()),IsComponentName("group1")) )
755  CFLogVar(comp.name());
756 
758  ConstComponentIteratorRange<Group> new_range3 ( component_begin<Group>(const_root()), component_end<Group>(const_root()) );
759  BOOST_FOREACH( const Group& comp, new_range3 )
760  CFLogVar(comp.name());
761 
763  ConstComponentIteratorRange<Group> new_range6 ( component_begin<Group const>(root()), component_end<Group const>(root()) );
764  BOOST_FOREACH( const Group& comp, new_range6 )
765  CFLogVar(comp.name());
766  CFLogVar(new_range6.size());
767 
768 }
769 
771 
772 BOOST_AUTO_TEST_SUITE_END()
773 
774 
ComponentReference< Component >::type find_component_recursively_with_filter(Component &parent, const Predicate &pred)
std::string name(ComponentWrapper &self)
boost::python::object get_child(ComponentWrapper &self, const std::string &name)
ComponentIteratorRangeSelector< Component, Component, Predicate >::type find_components_with_filter(Component &parent, const Predicate &pred)
ParentT & find_parent_component_with_filter(ComponentT &comp, const Predicate &pred)
std::vector< std::string > component_names
list of all component names
ComponentIteratorRangeSelector< Component, Component, Predicate >::type find_components_recursively_with_filter(Component &parent, const Predicate &pred)
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
bool ExceptionOutputs
if exception contructor should output
Definition: Exception.hpp:32
std::vector< std::string > top_group_names
list of all group names at the top level
~ComponentIterationFixture()
common tear-down for each test case
ComponentReference< Component >::type find_component_with_tag(Component &parent, StringConverter tag)
ComponentIteratorRange< T, Predicate >::type make_filtered_range(const ComponentIterator< T > &from, const ComponentIterator< T > &to, const Predicate &pred)
URI uri() const
Construct the full path.
Definition: Component.cpp:248
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
const std::string & name() const
Access the name of the component.
Definition: Component.hpp:146
tuple root
Definition: coolfluid.py:24
Conversions from and to std::string.
ComponentIteratorRangeSelector< Component, Component, IsComponentName >::type find_components_with_name(Component &parent, StringConverter name)
ComponentIteratorRangeSelector< Component, Component, IsComponentTag >::type find_components_with_tag(Component &parent, StringConverter tag)
std::vector< std::string > group_names
list of all group names
#define CF3_DEBUG_POINT
Definition of a macro for placing a debug point in the code.
Definition: Log.hpp:125
ComponentIteratorRangeSelector< Component, Component, IsComponentName >::type find_components_recursively_with_name(Component &parent, StringConverter name)
Common_API std::string to_str(const T &v)
Converts to std::string.
Handle< Component const > root() const
Definition: Component.cpp:266
Holds the Component class, as well as the ComponentIterator class plus some functions related to comp...
ComponentHandle< Component >::type find_component_ptr_recursively_with_name(Component &parent, StringConverter name)
ComponentIterationFixture()
common setup for each test case
ComponentReference< Component >::type find_component_recursively_with_name(Component &parent, StringConverter name)
ComponentHandle< Component >::type find_component_ptr_recursively(Component &parent)
ComponentHandle< Component >::type find_component_ptr_recursively_with_filter(Component &parent, const Predicate &pred)
ComponentHandle< Component >::type find_component_ptr_recursively_with_tag(Component &parent, StringConverter tag)
BOOST_AUTO_TEST_CASE(test_find_parent)
std::vector< std::string > group1_group_names
list of all group names at in group1
ComponentReference< Component >::type find_component_with_name(Component &parent, StringConverter name)
std::vector< std::string > group1_component_names
list of all group names at in group1
const Component & const_root()
const root
std::vector< std::string > special_component_names
list of all components with a tag "special"
ComponentHandle< Component >::type find_component_ptr_with_name(Component &parent, StringConverter name)
Component for grouping other components.
Definition: Group.hpp:25
ComponentIteratorRangeSelector< Component, Component >::type find_components(Component &parent)
std::vector< std::string > top_component_names
list of all component names on the first level
ComponentIterator class, can linearize a complete tree of components
Definition: Component.hpp:39
ComponentHandle< Component >::type find_component_ptr_with_tag(Component &parent, StringConverter tag)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
std::vector< Handle< const T > > range_to_const_vector(boost::iterator_range< ComponentIterator< T > > range)
ComponentReference< Component >::type find_component_with_filter(Component &parent, const Predicate &pred)
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
impl< typename boost::is_abstract< Predicate >::type >::type type
ComponentHandle< Component >::type find_component_ptr(Component &parent)
void add_tag(const std::string &tag)
ComponentReference< Component >::type find_component(Component &parent)
ComponentIteratorRange< T, Predicate > make_new_range(ComponentIterator< T > from, ComponentIterator< T > to, const Predicate &pred=IsComponentTrue())
std::vector< Handle< T > > range_to_vector(boost::iterator_range< ComponentIterator< T > > range)
Create a vector of handles of components, given an iterator_range.
#define CFLogVar(x)
log the value of a variable
Definition: Log.hpp:123
ComponentReference< Component >::type find_component_recursively_with_tag(Component &parent, StringConverter tag)
bool ExceptionAborts
if exception contructor should abort execution immedietly
Definition: Exception.hpp:36
static boost::proto::terminal< ExpressionGroupTag >::type group
Use group(expr1, expr2, ..., exprN) to evaluate a group of expressions.
boost::shared_ptr< Component > m_root
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
ComponentIteratorRangeSelector< Component, Component >::type find_components_recursively(Component &parent)
ComponentHandle< Component >::type find_component_ptr_with_filter(Component &parent, const Predicate &pred)
ComponentReference< Component >::type find_component_recursively(Component &parent)
Component & root()
mutable root
Base class for defining CF components.
Definition: Component.hpp:82
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
Uint count(const RangeT &range)
Count the elements in a range.
ComponentIteratorRangeSelector< Component, Component, IsComponentTag >::type find_components_recursively_with_tag(Component &parent, StringConverter tag)
Send comments to:
COOLFluiD Web Admin