COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-config.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 CF log level filter"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/function.hpp>
13 #include <boost/foreach.hpp>
14 #include <boost/assign/std/vector.hpp>
15 #include <boost/date_time/gregorian/gregorian.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 
18 #include "common/OptionArray.hpp"
20 #include "common/Group.hpp"
21 #include "common/Log.hpp"
22 #include "common/URI.hpp"
24 #include "common/OptionURI.hpp"
25 #include "common/OptionT.hpp"
26 
27 #include "common/XML/Protocol.hpp"
29 #include "common/XML/XmlDoc.hpp"
31 
33 
34 using namespace std;
35 using namespace boost;
36 using namespace boost::assign;
37 
38 using namespace cf3;
39 using namespace cf3::common;
40 
42 
43 class MyC : public Component {
44 public:
45  static string type_name() { return "MyC"; }
46 
47  typedef boost::shared_ptr<MyC> Ptr;
48  typedef boost::shared_ptr<MyC const> CosntPtr;
49 
50  private: // data
51 
52  std::string m_str;
53  int m_i;
57 
58  public: // functions
59 
60  const Handle<CConcrete1>& comp() { return m_component_lnk; }
61 
62  MyC ( const std::string& name ) : Component(name)
63  {
64  // POD's (plain old data)
65  options().add ( "OptBool", false ).description("bool option");
66  options().add ( "OptInt", -5 ).description("int option");
67  options().add ( "OptUInt", 10u ).description("Uint option");
68  options().add ( "OptReal", 0.0 ).description("real option");
69  options().add ( "OptStr", std::string("LOLO") ).description("string option");
70  options().add ( "OptURI", URI("cpath:/lolo") ).description( "URI option");
71 
72  // vector of POD's
73  std::vector<int> def;
74  def += 1,2,3,4,5,6,7,8,9; /* uses boost::assign */
75  options().add( "VecInt", def ).description("vector ints option");
76 
77  // vector of POD's
78  std::vector< std::string > defs;
79  defs += "lolo","koko"; /* uses boost::assign */
80  options().add( "VecStr", defs ).description("vector strs option");;
81 
82 // option("OptInt").set_value(10);
83 
84  options()["OptInt"].link_to( &m_i );
85 
86  options().link_to_parameter ( "OptStr", &m_str );
87 
88  options()["OptBool"].attach_trigger( boost::bind ( &MyC::config_bool, this ) );
89  options()["OptInt"].attach_trigger ( boost::bind ( &MyC::config_int, this ) );
90  options()["OptStr"].attach_trigger ( boost::bind ( &MyC::config_str, this ) );
91  options()["VecInt"].attach_trigger ( boost::bind ( &MyC::config_vecint,this ) );
92  options()["OptURI"].attach_trigger ( boost::bind ( &MyC::config_uri, this ) );
93 
94  std::vector<int> vi = options().value< std::vector<int> >("VecInt");
95 // for (Uint i = 0; i < vi.size(); ++i)
96 // CFinfo << "vi[" << i << "] : " << vi[i] << "\n" << CFendl;
97 
98  options().add( "OptC", Handle<CConcrete1>() )
99  .description("component option");
100  options().link_to_parameter ( "OptC", &m_component_lnk );
101  boost::shared_ptr<Option> opt2 (new OptionComponent<CConcrete1>("OptC2", Handle<CConcrete1>()));
102  opt2->description("component option");
103  options().add(opt2).link_to( &m_component_lnk2 ).mark_basic();
104 
105  Option& opt3 = options().add("OptC3",m_component);
106  opt3.description("component option");
107  CFinfo << opt3.value_str() << CFendl;
108  };
109 
110 
111  void config_bool ()
112  {
113  BOOST_CHECK(options().value<bool>("OptBool") == boost::any_cast<bool>(options().option("OptBool").value()));
114  }
115 
116  void config_int ()
117  {
118  BOOST_CHECK(options().value<int>("OptInt") == boost::any_cast<int>(options().option("OptInt").value()));
119  }
120 
121  void config_str ()
122  {
123  BOOST_CHECK(options().value<std::string>("OptStr") == boost::any_cast<std::string>(options().option("OptStr").value()));
124  }
125 
127  {
128  BOOST_CHECK(options().value< std::vector<int> >("VecInt") == boost::any_cast< std::vector<int> >(options().option("VecInt").value()));
129  }
130 
131  void config_uri ()
132  {
133  BOOST_CHECK(options().value<URI>("OptURI") == boost::any_cast<URI>(options().option("OptURI").value()));
134  }
135 };
136 
138 
139 BOOST_AUTO_TEST_SUITE( Config_TestSuite )
140 
141 
143 BOOST_AUTO_TEST_CASE( add_options_to )
144 {
145  boost::gregorian::date today = boost::gregorian::day_clock::local_day();
146  boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
147 
148 // CFinfo << "starting [" << today << "] [" << now << "]\n" << CFendl;
149 
150  boost::shared_ptr<MyC> pm ( allocate_component<MyC>("LOLO") );
151 
152 // CFinfo << "ending\n" << CFendl;
153 }
154 
156 
158 {
159  using namespace rapidxml;
160 
161  boost::gregorian::date today = boost::gregorian::day_clock::local_day();
162  boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
163 
164  CFinfo << "starting [" << today << "] [" << now << "]" << CFendl;
165 
166 
167  boost::shared_ptr<MyC> pm ( allocate_component<MyC>("LOLO") );
168 
169  std::string text = (
170  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
171  "<cfxml version=\"1.0\">"
172  "<frame>"
173  " <map>"
174  " <value key=\"options\">"
175  " <map>"
176  ""
177  "<value key=\"OptBool\"> <bool> 1 </bool> </value>"
178  "<value key=\"OptInt\" > <integer> -156 </integer> </value>"
179  "<value key=\"OptUInt\" > <unsigned> 134 </unsigned> </value>"
180  "<value key=\"OptReal\" > <real> 6.4564E+5 </real> </value>"
181  "<value key=\"OptStr\" > <string> lolo </string> </value>"
182  ""
183  "<ignore> popo </ignore>"
184  ""
185  "<value key=\"OptPath\"> <path> /opt/path </path> </value>"
186  ""
187  "<value key=\"OptDate\"> <date> 2010-05-24 </date> </value>"
188  ""
189  "<value key=\"OptData\" > <data> PEKBpYGlmYFCPA== </data> </value>"
190  ""
191  "<map key=\"OptList\" desc=\"sub set\" mode=\"advanced\" >"
192  " <value key=\"mi\"> <integer> 2 </integer> </value>"
193  " <value key=\"mr\"> <real> 8 </real> </value>"
194  " <value key=\"mb\"> <bool> 1 </bool> </value>"
195  "</map>"
196  ""
197  "<array key=\"VecInt\" type=\"integer\" size=\"3\" delimiter=\" ; \" > 2 ; 8 ; 9</array>"
198  ""
199  "<array key=\"VecStr\" type=\"string\" size=\"2\" delimiter=\" ; \">aabbcc ; ddeeff</array>"
200  ""
201  " </map>"
202  " </value>"
203  " </map>"
204  "</frame>"
205  "</cfxml>"
206  );
207 
208  boost::shared_ptr<XmlDoc> xml = XML::parse_string(text);
209 
210  XmlNode doc = Protocol::goto_doc_node(*xml.get());
211  SignalFrame frame( Protocol::first_frame_node( doc ) );
212 
213 // CFinfo << "FRAME [" << frame.name() << "]" << CFendl;
214 
215  pm->signal_configure( frame );
216 
217  BOOST_CHECK ( pm->options().value<bool>("OptBool") );
218  BOOST_CHECK_EQUAL ( pm->options().option("OptBool").value_str() , "true" );
219 
220  BOOST_CHECK_EQUAL ( pm->options().value<int>("OptInt"), -156 );
221  BOOST_CHECK_EQUAL ( pm->options().option("OptInt").value_str() , "-156" );
222 
223  BOOST_CHECK_EQUAL ( pm->options().value<Uint>("OptUInt"), (Uint) 134 );
224  BOOST_CHECK_EQUAL ( pm->options().option("OptUInt").value_str() , "134" );
225 
226  BOOST_CHECK_EQUAL ( pm->options().value<Real>("OptReal"), 6.4564E+5 );
227 // BOOST_CHECK_EQUAL ( pm->options().option("OptReal").value_str() , "6.4564E+5" );
228 
229  BOOST_CHECK_EQUAL ( pm->options().value<std::string>("OptStr"), "lolo" );
230  BOOST_CHECK_EQUAL ( pm->options().option("OptStr").value_str(), "lolo" );
231 
232  std::vector<int> vecint(3);
233  vecint[0]=2;
234  vecint[1]=8;
235  vecint[2]=9;
236  BOOST_CHECK ( pm->options().value<std::vector<int> >("VecInt") == vecint);
237 
238  std::vector<std::string> vecstr(2);
239  vecstr[0]="aabbcc";
240  vecstr[1]="ddeeff";
241  BOOST_CHECK ( pm->options().value<std::vector<std::string> >("VecStr") == vecstr);
242 
243  CFinfo << "ending" << CFendl;
244 
245 }
246 
248 
249 
250 BOOST_AUTO_TEST_CASE( configure_component_path )
251 {
252  // Setup a little data-structure
253  boost::shared_ptr<Group> root = allocate_component<Group>("root");
254  Handle<CConcrete1> component1 = root->create_component<CConcrete1>("component1");
255  Handle<CConcrete1> component2 = root->create_component<CConcrete1>("component2");
256 
257  // Configure component 1 without XML (It could also be done with xml)
258  component1->options().set("MyRelativeFriend",URI("cpath:../component2"));
259  component1->options().set("MyAbsoluteFriend",URI("cpath:/component2"));
260 
261  // Check if everything worked OK.
262  URI absolute_friend_path = component1->options().value<URI>("MyAbsoluteFriend");
263  Handle<CConcrete1> absolute_friend(component1->access_component(absolute_friend_path));
264  BOOST_CHECK_EQUAL(absolute_friend->name(),"component2");
265 
266  URI relative_friend_path = component1->options().value<URI>("MyRelativeFriend");
267  Handle<CConcrete1> relative_friend(component1->access_component(relative_friend_path));
268  BOOST_CHECK_EQUAL(relative_friend->name(),"component2");
269 }
270 
272 
273 BOOST_AUTO_TEST_CASE( optionComponent )
274 {
275  // Setup a little data-structure
276  Component& root = Core::instance().root();
277  Handle<MyC> component1 = root.create_component<MyC>("component1");
278  Handle<CConcrete1> component2 = root.create_component<CConcrete1>("component2");
279 
280  // Configure component 1 without XML (It could also be done with xml)
281  component1->options().set("OptC",component2);
282 
283  BOOST_CHECK( component1->comp() == component2 );
284  BOOST_CHECK(component1->options().value< Handle<CConcrete1> >("OptC") == component2);
285  // // Check if everything worked OK.
286  // URI absolute_friend_path = component1->option("MyAbsoluteFriend").value<URI>();
287  // Handle<CConcrete1> absolute_friend = component1->access_component(absolute_friend_path)->as_ptr<CConcrete1>();
288  // BOOST_CHECK_EQUAL(absolute_friend->name(),"component2");
289  //
290  // URI relative_friend_path = component1->option("MyRelativeFriend").value<URI>();
291  // Handle<CConcrete1> relative_friend = component1->access_component(relative_friend_path)->as_ptr<CConcrete1>();
292  // BOOST_CHECK_EQUAL(relative_friend->name(),"component2");
293 }
294 
296 
297 
298 BOOST_AUTO_TEST_SUITE_END()
299 
300 
Adds fonctionnalities to Property class.
Definition: Option.hpp:76
Handle< CConcrete1 > m_component_lnk
#define CFinfo
these are always defined
Definition: Log.hpp:104
std::string name(ComponentWrapper &self)
const Handle< CConcrete1 > & comp()
external boost library namespace
MyC(const std::string &name)
std::string m_str
boost::shared_ptr< XmlDoc > parse_string(const std::string &str)
STL namespace.
static string type_name()
tuple root
Definition: coolfluid.py:24
#define CFendl
Definition: Log.hpp:109
boost::shared_ptr< MyC > Ptr
void config_uri()
Manages a set of maps.
Definition: SignalFrame.hpp:31
int m_i
Uniform Resource Identifier (see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier) ...
void config_int()
Top-level namespace for coolfluid.
Definition: Action.cpp:18
const TYPE value(const std::string &opt_name) const
Get the value of the option with given name.
Definition: OptionList.hpp:104
Handle< CConcrete1 > m_component
void config_bool()
virtual std::string value_str() const =0
BOOST_AUTO_TEST_CASE(add_options_to)
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
void config_vecint()
Handle< CConcrete1 > m_component_lnk2
Option & description(const std::string &description)
Sets the option description.
Definition: Option.cpp:148
external library used for XML parsing
Definition: XmlDoc.hpp:16
OptionList & options()
Definition: Component.cpp:856
Base class for defining CF components.
Definition: Component.hpp:82
boost::shared_ptr< MyC const > CosntPtr
void set(const std::string &pname, const boost::any &val)
Definition: OptionList.cpp:132
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
void config_str()
Send comments to:
COOLFluiD Web Admin