7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for XML maps manipulation"
10 #include <boost/assign/list_of.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include "rapidxml/rapidxml.hpp"
45 BOOST_FIXTURE_TEST_SUITE( XmlMap_TestSuite,
XmlFixture )
57 rapidxml::xml_attribute<>* key_attr =
nullptr;
61 BOOST_CHECK_THROW ( map.set_value(
"", common::class_name<bool>(),
"true" ),
BadValue );
68 value_node = map.set_value(
"TheUltimateAnswer", common::class_name<int>(),
"12" );
69 BOOST_CHECK ( value_node.is_valid() );
77 BOOST_CHECK_EQUAL ( std::strcmp(key_attr->value(),
"TheUltimateAnswer"), 0 );
80 type_node =
XmlNode( value_node.content->first_node( common::class_name<int>().c_str() ) );
81 BOOST_CHECK ( type_node.
is_valid() );
82 BOOST_CHECK_EQUAL ( std::strcmp(type_node.
content->value(),
"12"), 0 );
89 BOOST_CHECK_THROW ( map.set_value(
"TheUltimateAnswer", common::class_name<std::string>(), std::string()),
XmlError );
93 BOOST_CHECK_THROW ( map.set_value(
"hello", common::class_name<std::string>(),std::string()),
XmlError );
97 BOOST_CHECK_THROW ( map.set_value(
"world", common::class_name<std::string>(), std::string()),
XmlError );
103 mod_value_node = map.set_value(
"TheUltimateAnswer", common::class_name<int>(),
"42" );
104 BOOST_CHECK ( mod_value_node.is_valid() );
107 BOOST_CHECK_EQUAL ( mod_value_node.content, value_node.content );
110 type_node =
XmlNode( mod_value_node.content->first_node( common::class_name<int>().c_str() ) );
111 BOOST_CHECK ( type_node.
is_valid() );
112 BOOST_CHECK_EQUAL ( std::strcmp(type_node.
content->value(),
"42"), 0 );
115 delete node.
content->document();
127 std::vector<int> vect_first = list_of<int>(12)(5)(546)(2135)(12164)(3464)(1);
128 std::vector<int> vect_second = list_of<int>(7987)(346)(101);
129 cf3::Uint size_first = vect_first.size();
130 cf3::Uint size_second = vect_second.size();
133 std::string str_first(
"12 ; 5 ; 546 ; 2135 ; 12164 ; 3464 ; 1");
134 std::string str_second(
"7987_|_346_|_101");
136 rapidxml::xml_attribute<>* tmp_attr =
nullptr;
154 value_node = map.set_array(
"TheArray", common::class_name<int>(),
option_vector_to_str(vect_first,
" ; "),
" ; " );
155 BOOST_CHECK ( value_node.is_valid() );
163 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(
"TheArray") );
168 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(common::class_name<int>() ));
173 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(
to_str(size_first) ));
178 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(
" ; ") );
181 BOOST_CHECK (
is_not_null(value_node.content->value()) );
182 BOOST_CHECK_EQUAL ( std::string(value_node.content->value()), str_first );
189 std::vector<std::string> vect_wrong;
205 BOOST_CHECK ( mod_value_node.is_valid() );
208 BOOST_CHECK_EQUAL ( mod_value_node.content, value_node.content );
213 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(
to_str(size_second) ));
218 BOOST_CHECK_EQUAL ( std::string(tmp_attr->value()), std::string(
"_|_") );
221 BOOST_CHECK (
is_not_null(mod_value_node.content->value()) );
222 BOOST_CHECK_EQUAL ( std::string(mod_value_node.content->value()), str_second );
225 delete node.
content->document();
237 rapidxml::xml_attribute<>* key_attr =
nullptr;
246 found_node = map.find_value();
248 BOOST_CHECK ( found_node.is_valid() );
249 BOOST_CHECK_EQUAL ( found_node.content, first_node.content );
255 found_node = map.find_value(
"SecondNode" );
257 BOOST_CHECK ( found_node.is_valid() );
258 BOOST_CHECK_EQUAL ( found_node.content, second_node.content );
264 found_node = map.find_value(
"MapThatDoesNotExist" );
266 BOOST_CHECK ( !found_node.is_valid() );
269 delete node.
content->document();
293 delete node.
content->document();
317 delete node.
content->document();
326 std::vector<int> vect;
328 XmlNode value( map.set_value(
"MyString", class_name<std::string>(), std::string(
"TheString")) );
329 XmlNode array( map.set_array(
"MyVector", class_name<int>(),
"",
" ; ") );
340 BOOST_CHECK_EQUAL ( std::strcmp(
Map::get_value_type(value), common::class_name<std::string>().c_str() ), 0);
341 BOOST_CHECK_EQUAL ( std::strcmp(
Map::get_value_type(array), common::class_name<int>().c_str() ), 0);
349 delete node.
content->document();
359 std::vector<int> int_vals = list_of<int>(1213)(5464)(5554)(5654)(273)(554)(354);
360 std::string str(
"Hello, World!");
362 map.set_value(
"AString", class_name<std::string>(), str );
363 map.set_value(
"Zero", class_name<int>(),
"0" );
367 BOOST_CHECK_THROW ( map.get_value<
int>(
"AString"),
XmlError );
370 BOOST_CHECK_THROW ( map.get_value<
int>(
"SomeInts"),
XmlError );
373 BOOST_CHECK_EQUAL ( map.get_value<std::string>(
"AString"), str );
376 delete node.
content->document();
386 std::vector<int> int_vals = list_of<int>(1213)(5464)(5554)(5654)(273)(554)(354);
387 std::vector<int> int_read;
388 std::vector<int>::iterator it_vals = int_vals.begin();
389 std::vector<int>::iterator it_read;
390 std::string str(
"Hello, World!");
392 map.set_value(
"AString", class_name<std::string>(), str );
393 map.set_value(
"Zero", class_name<int>(),
"0" );
397 BOOST_CHECK_THROW ( map.get_array<Real>(
"SomeInts"),
XmlError );
400 BOOST_CHECK_THROW ( map.get_array<
int>(
"Zero"),
XmlError );
403 BOOST_CHECK_NO_THROW ( int_read = map.get_array<
int>(
"SomeInts") );
405 BOOST_CHECK_EQUAL ( int_read.size(), int_vals.size() );
408 for( it_read = int_read.begin() ; it_read != int_read.end() ; ++it_read, ++it_vals )
409 BOOST_CHECK_EQUAL ( *it_read, *it_vals);
412 delete node.
content->document();
422 std::vector<std::string> str_vals = list_of<std::string>(
"hello")(
"hello with some white spaces");
423 std::vector<std::string> str_read;
424 std::vector<std::string>::iterator it_vals = str_vals.begin();
425 std::vector<std::string>::iterator it_read;
427 map.set_array(
"SomeStrings", class_name<std::string>(),
option_vector_to_str(str_vals,
" ; "),
" ; " );
430 BOOST_CHECK_NO_THROW ( str_read = map.get_array<std::string>(
"SomeStrings") );
433 BOOST_CHECK_EQUAL ( str_read.size(), str_vals.size() );
436 for( it_read = str_read.begin() ; it_read != str_read.end() ; ++it_read, ++it_vals )
437 BOOST_CHECK_EQUAL ( *it_read, *it_vals);
440 delete node.
content->document();
445 BOOST_AUTO_TEST_SUITE_END()
447 XmlNode add_node(const std::string &name, const std::string &value=std::string()) const
static const char * get_value_type(const XmlNode &node)
Classes that implement the XML protocol for use in COOLFluiD.
BOOST_AUTO_TEST_CASE(set_value)
rapidxml::xml_node< char > * content
Pointer to the underlying XML implementation.
Conversions from and to std::string.
Common_API std::string to_str(const T &v)
Converts to std::string.
void set_attribute(const std::string &name, const std::string &value)
Top-level namespace for coolfluid.
std::string option_vector_to_str(const std::vector< T > &vec, const std::string &delim)
Helper function to convert a vector to string, skipping empty entries.
static bool is_array_value(const XmlNode &node)
static bool is_single_value(const XmlNode &node)
common::Environment & environment() const
unsigned int Uint
typedef for unsigned int
coolfluid3 header, included almost everywhere
void set(const std::string &pname, const boost::any &val)
void map(Field &field, Eigen::Map< Derived > &v, const Uint row_idx, const Uint var_idx)
Most basic kernel library.
bool is_not_null(T ptr)
predicate for comparison to nullptr