ts_BatchGet.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include <icl_core_config/Config.h>
00024 
00025 #include <iterator>
00026 #include <vector>
00027 #include <boost/assign/list_of.hpp>
00028 #include <boost/test/unit_test.hpp>
00029 
00030 namespace icc = icl_core::config;
00031 namespace icl = icl_core::logging;
00032 
00033 enum EnumValue
00034 {
00035   ONE,
00036   TWO,
00037   THREE
00038 };
00039 
00040 std::vector<std::string> enum_value_description =
00041   boost::assign::list_of<std::string>("ONE")("TWO")("THREE");
00042 
00043 char const * enum_value_char_description[] = {"ONE", "TWO", "THREE", NULL};
00044 
00045 struct ConfigListEntry
00046 {
00047   std::string string_value;
00048   EnumValue enum_value;
00049   struct Foo
00050   {
00051     uint32_t uint32_value;
00052     EnumValue enum_value;
00053     struct Bar {
00054       double double_value;
00055       EnumValue enum_value;
00056     } bar;
00057   } foo;
00058 };
00059 
00060 BOOST_AUTO_TEST_SUITE(ts_BatchGet)
00061 
00062 #ifdef ICL_CORE_CONFIG_HAS_ENHANCED_CONFIG_MACROS
00063 
00064 BOOST_AUTO_TEST_CASE(ConfigValue)
00065 {
00066   // Simulate the following configuration file:
00067   //
00068   // <?xml version="1.0" encoding="UTF-8"?>
00069   //
00070   // <ConfigValue>
00071   //   <StringEntry>string-value</StringEntry>
00072   //   <UInt32Entry>12345</UInt32Entry>
00073   //   <DoubleEntry>1.2345</DoubleEntry>
00074   // </ConfigValue>
00075   icc::setValue("/ConfigValue/StringEntry", "string-value");
00076   icc::setValue("/ConfigValue/UInt32Entry", "12345");
00077   icc::setValue("/ConfigValue/DoubleEntry", "1.2345");
00078 
00079   std::string string_value = "";
00080   uint32_t uint32_value = 0;
00081   double double_value = 0.;
00082   bool read_success =
00083     icc::get(CONFIG_VALUES(CONFIG_VALUE("/ConfigValue/StringEntry", string_value)
00084                            CONFIG_VALUE("/ConfigValue/UInt32Entry", uint32_value)
00085                            CONFIG_VALUE("/ConfigValue/DoubleEntry", double_value)),
00086              icl::Nirwana::instance());
00087   BOOST_CHECK(read_success);
00088 
00089   BOOST_CHECK_EQUAL(string_value, "string-value");
00090   BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
00091   BOOST_CHECK_EQUAL(double_value, double(1.2345));
00092 }
00093 
00094 BOOST_AUTO_TEST_CASE(ConfigValueDefault)
00095 {
00096   // Simulate the following configuration file:
00097   //
00098   // <?xml version="1.0" encoding="UTF-8"?>
00099   //
00100   // <ConfigValueDefault>
00101   //   <StringEntry>string-value</StringEntry>
00102   // </ConfigValueDefault>
00103   icc::setValue("/ConfigValueDefault/StringEntry", "string-value");
00104 
00105   std::string string_value = "";
00106   uint32_t uint32_value = 0;
00107   bool read_success =
00108     icc::get(CONFIG_VALUES(
00109                CONFIG_VALUE_DEFAULT("/ConfigValueDefault/StringEntry", string_value, "other-string-value")
00110                CONFIG_VALUE_DEFAULT("/ConfigValue/UInt32Entry", uint32_value, 12345)),
00111              icl::Nirwana::instance());
00112   BOOST_CHECK(read_success);
00113 
00114   BOOST_CHECK_EQUAL(string_value, "string-value");
00115   BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
00116 }
00117 
00118 BOOST_AUTO_TEST_CASE(ConfigEnum)
00119 {
00120   // Simulate the following configuration file:
00121   //
00122   // <?xml version="1.0" encoding="UTF-8"?>
00123   //
00124   // <ConfigEnum>
00125   //   <Entry1>ONE</Entry1>
00126   //   <Entry2>TWO</Entry2>
00127   // </ConfigEnum>
00128   icc::setValue("/ConfigEnum/Entry1", "ONE");
00129   icc::setValue("/ConfigEnum/Entry2", "TWO");
00130 
00131   EnumValue value1 = THREE;
00132   EnumValue value2 = THREE;
00133   bool read_success =
00134     icc::get(CONFIG_VALUES(CONFIG_ENUM("/ConfigEnum/Entry1", value1, enum_value_char_description)
00135                            CONFIG_ENUM("/ConfigEnum/Entry2", value2, enum_value_char_description)),
00136              icl::Nirwana::instance());
00137   BOOST_CHECK(read_success);
00138 
00139   BOOST_CHECK_EQUAL(value1, ONE);
00140   BOOST_CHECK_EQUAL(value2, TWO);
00141 }
00142 
00143 BOOST_AUTO_TEST_CASE(ConfigEnumDefault)
00144 {
00145   // Simulate the following configuration file:
00146   //
00147   // <?xml version="1.0" encoding="UTF-8"?>
00148   //
00149   // <ConfigEnumDefault>
00150   //   <Entry1>ONE</Entry1>
00151   // </ConfigEnumDefault>
00152   icc::setValue("/ConfigEnumDefault/Entry1", "ONE");
00153 
00154   EnumValue value1 = THREE;
00155   EnumValue value2 = THREE;
00156   bool read_success =
00157     icc::get(CONFIG_VALUES(
00158                CONFIG_ENUM_DEFAULT("/ConfigEnumDefault/Entry1", value1, TWO, enum_value_char_description)
00159                CONFIG_ENUM_DEFAULT("/ConfigEnumDefault/Entry2", value2, TWO, enum_value_char_description)),
00160              icl::Nirwana::instance());
00161   BOOST_CHECK(read_success);
00162 
00163   BOOST_CHECK_EQUAL(value1, ONE);
00164   BOOST_CHECK_EQUAL(value2, TWO);
00165 }
00166 
00167 BOOST_AUTO_TEST_CASE(ConfigPrefix)
00168 {
00169   // Simulate the following configuration file:
00170   //
00171   // <?xml version="1.0" encoding="UTF-8"?>
00172   //
00173   // <ConfigPrefix>
00174   //   <StringEntry>string-value</StringEntry>
00175   //   <UInt32Value>12345</UInt32Value>
00176   //   <DoubleEntry>1.2345</DoubleEntry>
00177   //   <EnumEntry>TWO</EnumEntry>
00178   // </ConfigPrefix>
00179   icc::setValue("/ConfigPrefix/StringEntry", "string-value");
00180   icc::setValue("/ConfigPrefix/UInt32Entry", "12345");
00181   icc::setValue("/ConfigPrefix/DoubleEntry", "1.2345");
00182   icc::setValue("/ConfigPrefix/EnumEntry",   "TWO");
00183 
00184   std::string string_value = "";
00185   uint32_t uint32_value = 0;
00186   double double_value = 0.;
00187   EnumValue enum_value = ONE;
00188   bool read_success =
00189     icc::get("/ConfigPrefix",
00190              CONFIG_VALUES(CONFIG_VALUE("StringEntry", string_value)
00191                            CONFIG_VALUE("UInt32Entry", uint32_value)
00192                            CONFIG_VALUE("DoubleEntry", double_value)
00193                            CONFIG_ENUM ("EnumEntry",   enum_value, enum_value_char_description)),
00194              icl::Nirwana::instance());
00195   BOOST_CHECK(read_success);
00196 
00197   BOOST_CHECK_EQUAL(string_value, "string-value");
00198   BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
00199   BOOST_CHECK_EQUAL(double_value, double(1.2345));
00200   BOOST_CHECK_EQUAL(enum_value,   TWO);
00201 }
00202 
00203 BOOST_AUTO_TEST_CASE(ConfigList)
00204 {
00205   // Simulate the following configuration file:
00206   //
00207   // <?xml version="1.0" encoding="UTF-8"?>
00208   //
00209   // <ConfigList>
00210   //   <List>
00211   //     <Entry1>
00212   //       <StringEntry>string-value-1</StringEntry>
00213   //       <EnumEntry>ONE</EnumEntry>
00214   //       <Foo>
00215   //         <UInt32Value>1</UInt32Value>
00216   //         <EnumEntry>ONE</EnumEntry>
00217   //         <Bar>
00218   //           <DoubleEntry>0.1</DoubleEntry>
00219   //           <EnumEntry>ONE</EnumEntry>
00220   //         </Bar>
00221   //       </Foo>
00222   //     </Entry1>
00223   //     <Entry2>
00224   //       <StringEntry>string-value-2</StringEntry>
00225   //       <EnumEntry>TWO</EnumEntry>
00226   //       <Foo>
00227   //         <UInt32Value>2</UInt32Value>
00228   //         <EnumEntry>TWO</EnumEntry>
00229   //         <Bar>
00230   //           <DoubleEntry>0.2</DoubleEntry>
00231   //           <EnumEntry>TWO</EnumEntry>
00232   //         </Bar>
00233   //       </Foo>
00234   //     </Entry2>
00235   //     <Entry3>
00236   //       <StringEntry>string-value-3</StringEntry>
00237   //       <EnumEntry>THREE</EnumEntry>
00238   //       <Foo>
00239   //         <UInt32Value>3</UInt32Value>
00240   //         <EnumEntry>TWO</EnumEntry>
00241   //         <Bar>
00242   //           <DoubleEntry>0.3</DoubleEntry>
00243   //           <EnumEntry>TWO</EnumEntry>
00244   //         </Bar>
00245   //       </Foo>
00246   //     </Entry3>
00247   //   </List>
00248   // </ConfigValuePrefix>
00249   icc::setValue("/ConfigList/List/Entry1/StringEntry",         "string-value-1");
00250   icc::setValue("/ConfigList/List/Entry1/EnumEntry",           "ONE");
00251   icc::setValue("/ConfigList/List/Entry1/Foo/UInt32Entry",     "1");
00252   icc::setValue("/ConfigList/List/Entry1/Foo/EnumEntry",       "ONE");
00253   icc::setValue("/ConfigList/List/Entry1/Foo/Bar/DoubleEntry", "0.1");
00254   icc::setValue("/ConfigList/List/Entry1/Foo/Bar/EnumEntry",   "ONE");
00255   icc::setValue("/ConfigList/List/Entry2/StringEntry",         "string-value-2");
00256   icc::setValue("/ConfigList/List/Entry2/EnumEntry",           "TWO");
00257   icc::setValue("/ConfigList/List/Entry2/Foo/UInt32Entry",     "2");
00258   icc::setValue("/ConfigList/List/Entry2/Foo/EnumEntry",       "TWO");
00259   icc::setValue("/ConfigList/List/Entry2/Foo/Bar/DoubleEntry", "0.2");
00260   icc::setValue("/ConfigList/List/Entry2/Foo/Bar/EnumEntry",   "TWO");
00261   icc::setValue("/ConfigList/List/Entry3/StringEntry",         "string-value-3");
00262   icc::setValue("/ConfigList/List/Entry3/EnumEntry",           "THREE");
00263   icc::setValue("/ConfigList/List/Entry3/Foo/UInt32Entry",     "3");
00264   icc::setValue("/ConfigList/List/Entry3/Foo/EnumEntry",       "THREE");
00265   icc::setValue("/ConfigList/List/Entry3/Foo/Bar/DoubleEntry", "0.3");
00266   icc::setValue("/ConfigList/List/Entry3/Foo/Bar/EnumEntry",   "THREE");
00267 
00268   // Remard: This also works with std::list.
00269   // Actually, every container that provides a push_back() method can be used!
00270   std::vector<ConfigListEntry> config_list;
00271   bool read_successful =
00272     icc::get(CONFIG_VALUES(
00273                CONFIG_LIST(
00274                  ConfigListEntry, "/ConfigList/List",
00275                  MEMBER_MAPPING(
00276                    ConfigListEntry,
00277                    MEMBER_VALUE_1("StringEntry", ConfigListEntry, string_value)
00278                    MEMBER_VALUE_2("Foo/UInt32Entry", ConfigListEntry, foo, uint32_value)
00279                    MEMBER_VALUE_3("Foo/Bar/DoubleEntry", ConfigListEntry, foo, bar, double_value)
00280                    MEMBER_ENUM_1 ("EnumEntry", ConfigListEntry, enum_value, enum_value_description)
00281                    MEMBER_ENUM_2 ("Foo/EnumEntry", ConfigListEntry, foo, enum_value, enum_value_description)
00282                    MEMBER_ENUM_3 ("Foo/Bar/EnumEntry",   ConfigListEntry, foo, bar, enum_value,
00283                                   // Enum members also work with plain C-string arrays.
00284                                   enum_value_char_description)),
00285                  std::back_inserter(config_list))),
00286              icl::Nirwana::instance());
00287   BOOST_CHECK(read_successful);
00288 
00289   BOOST_CHECK_EQUAL(config_list.size(), 3u);
00290 
00291   BOOST_CHECK_EQUAL(config_list[0].string_value, "string-value-1");
00292   BOOST_CHECK_EQUAL(config_list[0].foo.uint32_value, 1u);
00293   BOOST_CHECK_EQUAL(config_list[0].foo.bar.double_value, 0.1);
00294   BOOST_CHECK_EQUAL(config_list[0].enum_value,   ONE);
00295   BOOST_CHECK_EQUAL(config_list[0].foo.enum_value,   ONE);
00296   BOOST_CHECK_EQUAL(config_list[0].foo.bar.enum_value,   ONE);
00297 
00298   BOOST_CHECK_EQUAL(config_list[1].string_value, "string-value-2");
00299   BOOST_CHECK_EQUAL(config_list[1].foo.uint32_value, 2u);
00300   BOOST_CHECK_EQUAL(config_list[1].foo.bar.double_value, 0.2);
00301   BOOST_CHECK_EQUAL(config_list[1].enum_value,   TWO);
00302   BOOST_CHECK_EQUAL(config_list[1].foo.enum_value,   TWO);
00303   BOOST_CHECK_EQUAL(config_list[1].foo.bar.enum_value,   TWO);
00304 
00305   BOOST_CHECK_EQUAL(config_list[2].string_value, "string-value-3");
00306   BOOST_CHECK_EQUAL(config_list[2].foo.uint32_value, 3u);
00307   BOOST_CHECK_EQUAL(config_list[2].foo.bar.double_value, 0.3);
00308   BOOST_CHECK_EQUAL(config_list[2].enum_value,   THREE);
00309   BOOST_CHECK_EQUAL(config_list[2].foo.enum_value,   THREE);
00310   BOOST_CHECK_EQUAL(config_list[2].foo.bar.enum_value,   THREE);
00311 }
00312 
00313 #else
00314 # if defined(_MSC_VER)
00315 #  pragma message("The icl_core_config batch convenience macros are only available in Visual Studio 2010 and newer.")
00316 # endif
00317 #endif
00318 
00319 BOOST_AUTO_TEST_SUITE_END()


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24