COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-options.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 the options facility"
9 
10 #include <boost/assign/std/vector.hpp>
11 #include <boost/mpl/if.hpp>
12 #include <boost/test/unit_test.hpp>
13 #include <boost/type_traits/is_base_of.hpp>
14 
16 #include "common/Group.hpp"
17 #include "common/Core.hpp"
18 #include "common/OptionArray.hpp"
20 #include "common/OptionList.hpp"
21 #include "common/OptionT.hpp"
22 
23 using namespace std;
24 using namespace boost::assign;
25 
26 using namespace cf3;
27 using namespace cf3::common;
28 
30 
31 BOOST_AUTO_TEST_SUITE( OptionsSuite )
32 
33 
36 
37 BOOST_AUTO_TEST_CASE( StringOption )
38 {
39  Component& root = Core::instance().root();
40 
41  root.options().add( "test_option", std::string("test01"));
42  BOOST_CHECK_EQUAL(root.options().option("test_option").value_str(), "test01");
43 
44  root.options().option("test_option").change_value(std::string("test02"));
45  BOOST_CHECK_EQUAL(root.options().option("test_option").value_str(), "test02");
46 }
47 
48 BOOST_AUTO_TEST_CASE( ComponentOption )
49 {
50  ExceptionManager::instance().ExceptionDumps = false;
51  ExceptionManager::instance().ExceptionAborts = false;
52  ExceptionManager::instance().ExceptionOutputs = false;
53 
54  Component& root = Core::instance().root();
55  const Handle<Component> referred = root.create_component<Component>("ReferredComponent");
56 
57  OptionComponent<Component>& opt = root.options().add("test_component_option", root.handle<Component>());
58  BOOST_CHECK(root.uri() == root.options().value< Handle<Component> >("test_component_option")->uri());
59  BOOST_CHECK(root.name() == opt.value< Handle<Component> >()->name());
60 
61  root.options().option("test_component_option").change_value(referred);
62  BOOST_CHECK(referred->uri() == root.options().value< Handle<Component> >("test_component_option")->uri());
63  BOOST_CHECK(referred->name() == opt.value< Handle<Component> >()->name());
64 
65  const Handle<Group> group = root.create_component<Group>("TestGroup");
66  OptionComponent<Group>& group_opt = root.options().add("test_group_option", Handle<Group>());
67  BOOST_CHECK_THROW(root.options().option("test_group_option").change_value(referred), CastingFailed);
68 
69  root.options().option("test_group_option").change_value(group);
70  BOOST_CHECK(group == group_opt.value< Handle<Group> >());
71 
72  Handle<Group const> const_group(group);
73  BOOST_CHECK_THROW(root.options().option("test_group_option").change_value(const_group), CastingFailed);
74 
75  OptionComponent<Group const>& group_opt_const = root.options().add("test_const_group_option", Handle<Group const>());
76  root.options().option("test_const_group_option").change_value(group);
77  BOOST_CHECK(group == group_opt_const.value< Handle<Group const> >());
78 
79  root.options().option("test_const_group_option").change_value(Handle<Component>());
80  BOOST_CHECK(is_null(group_opt_const.value< Handle<Group const> >()));
81 }
82 
83 BOOST_AUTO_TEST_CASE( TestOptionArray )
84 {
85  Component& root = Core::instance().root();
86 
87  std::vector<int> def;
88  def += 1,2,3,4,5,6,7,8,9;
89  BOOST_CHECK(root.options().add("test_array_option", def).value< std::vector<int> >() == def);
90 
91  BOOST_CHECK(def == root.options().value< std::vector<int> >("test_array_option"));
92 }
93 
94 BOOST_AUTO_TEST_CASE( TestOptionURI )
95 {
96  Component& root = Core::instance().root();
97 
98  // Since the result is properly typed, we can immediately call supported_protocol
99  root.options().add("test_uri_option", root.uri()).supported_protocol(cf3::common::URI::Scheme::CPATH);
100 
101  BOOST_CHECK(root.uri() == root.options().value< URI >("test_uri_option"));
102 }
103 
104 BOOST_AUTO_TEST_CASE( ResetOptions )
105 {
106  Component& root = Core::instance().root();
107 
108  root.options().add( "test_reset", std::string("test01"));
109  root.options().option("test_reset").change_value(std::string("test02"));
110  BOOST_CHECK_EQUAL(root.options().option("test_reset").value_str(), "test02");
111 
112  root.reset_options();
113  BOOST_CHECK_EQUAL(root.options().option("test_reset").value_str(), "test01");
114 }
115 
116 
118 
119 BOOST_AUTO_TEST_SUITE_END()
120 
121 
std::string name(ComponentWrapper &self)
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
URI uri() const
Construct the full path.
Definition: Component.cpp:248
STL namespace.
common::URI uri(ComponentWrapper &self)
const std::string & name() const
Access the name of the component.
Definition: Component.hpp:146
tuple root
Definition: coolfluid.py:24
virtual void change_value(const boost::any &value)
change the value of this option
Definition: Option.cpp:89
Component for grouping other components.
Definition: Group.hpp:25
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
virtual std::string value_str() const =0
static boost::proto::terminal< ExpressionGroupTag >::type group
Use group(expr1, expr2, ..., exprN) to evaluate a group of expressions.
void reset_options()
Reset all options to their default value.
Definition: Component.cpp:870
Handle< Component > handle()
Get a handle to the component.
Definition: Component.hpp:179
OptionList & options()
Definition: Component.cpp:856
BOOST_AUTO_TEST_CASE(StringOption)
SelectOptionType< T >::type & add(const std::string &name, const T &default_value=T())
Definition: OptionList.hpp:45
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
const Option & option(const std::string &pname) const
get a constant option from the list
Definition: OptionList.cpp:52
Most basic kernel library.
Definition: Action.cpp:19
Send comments to:
COOLFluiD Web Admin