COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-handle.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::common::handle"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include "common/Link.hpp"
13 #include "common/Group.hpp"
14 #include "common/Handle.hpp"
15 
16 using namespace cf3;
17 using namespace cf3::common;
18 
20 
21 BOOST_AUTO_TEST_SUITE(HandleSuite)
22 
23 
25 BOOST_AUTO_TEST_CASE( NullHandle )
26 {
27  Handle<Group> a, b;
28 
29  BOOST_CHECK(!a);
30  BOOST_CHECK(a == b);
31  BOOST_CHECK(is_null(a));
32  BOOST_CHECK(a == nullptr);
33  BOOST_CHECK(nullptr == a);
34  BOOST_CHECK(!is_not_null(a));
35  BOOST_CHECK(!(nullptr != a));
36  BOOST_CHECK(!(a != nullptr));
37 }
38 
39 BOOST_AUTO_TEST_CASE( ValidHandle )
40 {
41  boost::shared_ptr<Group> g = allocate_component<Group>("group");
42  Handle<Group> a(g);
43 
44  BOOST_CHECK(is_not_null(a));
45  BOOST_CHECK_EQUAL(a->name(), "group");
46  BOOST_CHECK_EQUAL((*a).name(), "group");
47  BOOST_CHECK_EQUAL(a.get()->name(), "group");
48 
49  g.reset();
50  BOOST_CHECK(is_null(a));
51  BOOST_CHECK(is_null(a.get()));
52 }
53 
54 BOOST_AUTO_TEST_CASE( Conversion )
55 {
56  boost::shared_ptr<Group> group = allocate_component<Group>("group");
57  Handle<Group> group_handle(group);
58 
59  Handle<Component> a(group);
60  BOOST_CHECK(is_not_null(a));
61  BOOST_CHECK_EQUAL(a->name(), "group");
62 
63  Handle<Component> b(group_handle);
64  BOOST_CHECK(is_not_null(b));
65  BOOST_CHECK_EQUAL(b->name(), "group");
66 
67  // Successful dynamic cast
68  Handle<Group> c(b);
69  BOOST_CHECK(is_not_null(c));
70  BOOST_CHECK_EQUAL(c->name(), "group");
71 
72  // Bad dynamic cast
73  Handle<Link> d(b);
74  BOOST_CHECK(is_null(d));
75 
76  // Bad dynamic cast
77  Handle<Link> e(c);
78  BOOST_CHECK(is_null(e));
79 
80  // Successful dynamic cast
81  Handle<Group> f = b->handle<Group>();
82  BOOST_CHECK(is_not_null(f));
83  BOOST_CHECK_EQUAL(f->name(), "group");
84 
85  // Bad dynamic cast
86  Handle<Link> g = b->handle<Link>();
87  BOOST_CHECK(is_null(g));
88 
89  // Construction from const
90  boost::shared_ptr<Group const> const_shared(group);
91  Handle<Group const> const_handle(const_shared);
92  BOOST_CHECK_EQUAL(const_handle->name(), "group");
93  //Handle<Group> bad(const_shared); // these shouldn't compile
94  //Handle<Group> bad2(const_handle);
95 
96  // Check releasing
97  group.reset();
98  const_shared.reset();
99  BOOST_CHECK(is_null(a));
100  BOOST_CHECK(is_null(b));
101  BOOST_CHECK(is_null(c));
102  BOOST_CHECK(is_null(d));
103  BOOST_CHECK(is_null(e));
104 
105 }
106 
108 {
109  boost::shared_ptr<Group> group = allocate_component<Group>("group");
110 
111  Handle<Group> group_handle(group);
112  BOOST_CHECK_EQUAL("group", group_handle->name());
113 
114  group = allocate_component<Group>("group2");
115  BOOST_CHECK(!group_handle); // should have expired
116  group_handle = Handle<Group>(group);
117  BOOST_CHECK_EQUAL("group2", group_handle->name());
118 }
119 
120 BOOST_AUTO_TEST_CASE( AssignToConst )
121 {
122  boost::shared_ptr<Group> group = allocate_component<Group>("group");
123 
124  Handle<Group> group_handle(group);
125  Handle<Group const> const_group_handle = group_handle;
126 
127  BOOST_CHECK_EQUAL("group", const_group_handle->name());
128 }
129 
130 BOOST_AUTO_TEST_CASE( AssignToBase )
131 {
132  boost::shared_ptr<Group> group = allocate_component<Group>("group");
133 
134  Handle<Group> group_handle(group);
135  Handle<Component> comp_handle = group_handle;
136  Handle<Component const> const_comp_handle = group_handle;
137 
138  //Handle<Group> try_dynamic_cast = comp_handle; // Won't compile (and shouldn't!)
139  //Handle<Component> try_const_cast = const_comp_handle; // Won't compile (and shouldn't!)
140 
141  BOOST_CHECK_EQUAL("group", comp_handle->name());
142  BOOST_CHECK_EQUAL("group", const_comp_handle->name());
143 }
144 
146 
147 BOOST_AUTO_TEST_SUITE_END()
148 
149 
bool is_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:151
BOOST_AUTO_TEST_CASE(NullHandle)
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
const std::string & name() const
Access the name of the component.
Definition: Component.hpp:146
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Component for grouping other components.
Definition: Group.hpp:25
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static boost::proto::terminal< ExpressionGroupTag >::type group
Use group(expr1, expr2, ..., exprN) to evaluate a group of expressions.
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin