ts_BatchGet.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include <icl_core_config/Config.h>
24 
25 #include <iterator>
26 #include <vector>
27 #include <boost/assign/list_of.hpp>
28 #include <boost/test/unit_test.hpp>
29 
30 namespace icc = icl_core::config;
31 namespace icl = icl_core::logging;
32 
34 {
35  ONE,
36  TWO,
38 };
39 
40 std::vector<std::string> enum_value_description =
41  boost::assign::list_of<std::string>("ONE")("TWO")("THREE");
42 
43 char const * enum_value_char_description[] = {"ONE", "TWO", "THREE", NULL};
44 
46 {
47  std::string string_value;
49  struct Foo
50  {
53  struct Bar {
54  double double_value;
56  } bar;
57  } foo;
58 };
59 
60 BOOST_AUTO_TEST_SUITE(ts_BatchGet)
61 
62 #ifdef ICL_CORE_CONFIG_HAS_ENHANCED_CONFIG_MACROS
63 
64 BOOST_AUTO_TEST_CASE(ConfigValue)
65 {
66  // Simulate the following configuration file:
67  //
68  // <?xml version="1.0" encoding="UTF-8"?>
69  //
70  // <ConfigValue>
71  // <StringEntry>string-value</StringEntry>
72  // <UInt32Entry>12345</UInt32Entry>
73  // <DoubleEntry>1.2345</DoubleEntry>
74  // </ConfigValue>
75  icc::setValue("/ConfigValue/StringEntry", "string-value");
76  icc::setValue("/ConfigValue/UInt32Entry", "12345");
77  icc::setValue("/ConfigValue/DoubleEntry", "1.2345");
78 
79  std::string string_value = "";
81  double double_value = 0.;
82  bool read_success =
83  icc::get(CONFIG_VALUES(CONFIG_VALUE("/ConfigValue/StringEntry", string_value)
84  CONFIG_VALUE("/ConfigValue/UInt32Entry", uint32_value)
85  CONFIG_VALUE("/ConfigValue/DoubleEntry", double_value)),
86  icl::Nirwana::instance());
87  BOOST_CHECK(read_success);
88 
89  BOOST_CHECK_EQUAL(string_value, "string-value");
90  BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
91  BOOST_CHECK_EQUAL(double_value, double(1.2345));
92 }
93 
94 BOOST_AUTO_TEST_CASE(ConfigValueDefault)
95 {
96  // Simulate the following configuration file:
97  //
98  // <?xml version="1.0" encoding="UTF-8"?>
99  //
100  // <ConfigValueDefault>
101  // <StringEntry>string-value</StringEntry>
102  // </ConfigValueDefault>
103  icc::setValue("/ConfigValueDefault/StringEntry", "string-value");
104 
105  std::string string_value = "";
107  bool read_success =
109  CONFIG_VALUE_DEFAULT("/ConfigValueDefault/StringEntry", string_value, "other-string-value")
110  CONFIG_VALUE_DEFAULT("/ConfigValue/UInt32Entry", uint32_value, 12345)),
111  icl::Nirwana::instance());
112  BOOST_CHECK(read_success);
113 
114  BOOST_CHECK_EQUAL(string_value, "string-value");
115  BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
116 }
117 
118 BOOST_AUTO_TEST_CASE(ConfigEnum)
119 {
120  // Simulate the following configuration file:
121  //
122  // <?xml version="1.0" encoding="UTF-8"?>
123  //
124  // <ConfigEnum>
125  // <Entry1>ONE</Entry1>
126  // <Entry2>TWO</Entry2>
127  // </ConfigEnum>
128  icc::setValue("/ConfigEnum/Entry1", "ONE");
129  icc::setValue("/ConfigEnum/Entry2", "TWO");
130 
131  EnumValue value1 = THREE;
132  EnumValue value2 = THREE;
133  bool read_success =
134  icc::get(CONFIG_VALUES(CONFIG_ENUM("/ConfigEnum/Entry1", value1, enum_value_char_description)
135  CONFIG_ENUM("/ConfigEnum/Entry2", value2, enum_value_char_description)),
136  icl::Nirwana::instance());
137  BOOST_CHECK(read_success);
138 
139  BOOST_CHECK_EQUAL(value1, ONE);
140  BOOST_CHECK_EQUAL(value2, TWO);
141 }
142 
143 BOOST_AUTO_TEST_CASE(ConfigEnumDefault)
144 {
145  // Simulate the following configuration file:
146  //
147  // <?xml version="1.0" encoding="UTF-8"?>
148  //
149  // <ConfigEnumDefault>
150  // <Entry1>ONE</Entry1>
151  // </ConfigEnumDefault>
152  icc::setValue("/ConfigEnumDefault/Entry1", "ONE");
153 
154  EnumValue value1 = THREE;
155  EnumValue value2 = THREE;
156  bool read_success =
158  CONFIG_ENUM_DEFAULT("/ConfigEnumDefault/Entry1", value1, TWO, enum_value_char_description)
159  CONFIG_ENUM_DEFAULT("/ConfigEnumDefault/Entry2", value2, TWO, enum_value_char_description)),
160  icl::Nirwana::instance());
161  BOOST_CHECK(read_success);
162 
163  BOOST_CHECK_EQUAL(value1, ONE);
164  BOOST_CHECK_EQUAL(value2, TWO);
165 }
166 
167 BOOST_AUTO_TEST_CASE(ConfigPrefix)
168 {
169  // Simulate the following configuration file:
170  //
171  // <?xml version="1.0" encoding="UTF-8"?>
172  //
173  // <ConfigPrefix>
174  // <StringEntry>string-value</StringEntry>
175  // <UInt32Value>12345</UInt32Value>
176  // <DoubleEntry>1.2345</DoubleEntry>
177  // <EnumEntry>TWO</EnumEntry>
178  // </ConfigPrefix>
179  icc::setValue("/ConfigPrefix/StringEntry", "string-value");
180  icc::setValue("/ConfigPrefix/UInt32Entry", "12345");
181  icc::setValue("/ConfigPrefix/DoubleEntry", "1.2345");
182  icc::setValue("/ConfigPrefix/EnumEntry", "TWO");
183 
184  std::string string_value = "";
186  double double_value = 0.;
188  bool read_success =
189  icc::get("/ConfigPrefix",
190  CONFIG_VALUES(CONFIG_VALUE("StringEntry", string_value)
191  CONFIG_VALUE("UInt32Entry", uint32_value)
192  CONFIG_VALUE("DoubleEntry", double_value)
193  CONFIG_ENUM ("EnumEntry", enum_value, enum_value_char_description)),
194  icl::Nirwana::instance());
195  BOOST_CHECK(read_success);
196 
197  BOOST_CHECK_EQUAL(string_value, "string-value");
198  BOOST_CHECK_EQUAL(uint32_value, uint32_t(12345));
199  BOOST_CHECK_EQUAL(double_value, double(1.2345));
200  BOOST_CHECK_EQUAL(enum_value, TWO);
201 }
202 
203 BOOST_AUTO_TEST_CASE(ConfigList)
204 {
205  // Simulate the following configuration file:
206  //
207  // <?xml version="1.0" encoding="UTF-8"?>
208  //
209  // <ConfigList>
210  // <List>
211  // <Entry1>
212  // <StringEntry>string-value-1</StringEntry>
213  // <EnumEntry>ONE</EnumEntry>
214  // <Foo>
215  // <UInt32Value>1</UInt32Value>
216  // <EnumEntry>ONE</EnumEntry>
217  // <Bar>
218  // <DoubleEntry>0.1</DoubleEntry>
219  // <EnumEntry>ONE</EnumEntry>
220  // </Bar>
221  // </Foo>
222  // </Entry1>
223  // <Entry2>
224  // <StringEntry>string-value-2</StringEntry>
225  // <EnumEntry>TWO</EnumEntry>
226  // <Foo>
227  // <UInt32Value>2</UInt32Value>
228  // <EnumEntry>TWO</EnumEntry>
229  // <Bar>
230  // <DoubleEntry>0.2</DoubleEntry>
231  // <EnumEntry>TWO</EnumEntry>
232  // </Bar>
233  // </Foo>
234  // </Entry2>
235  // <Entry3>
236  // <StringEntry>string-value-3</StringEntry>
237  // <EnumEntry>THREE</EnumEntry>
238  // <Foo>
239  // <UInt32Value>3</UInt32Value>
240  // <EnumEntry>TWO</EnumEntry>
241  // <Bar>
242  // <DoubleEntry>0.3</DoubleEntry>
243  // <EnumEntry>TWO</EnumEntry>
244  // </Bar>
245  // </Foo>
246  // </Entry3>
247  // </List>
248  // </ConfigValuePrefix>
249  icc::setValue("/ConfigList/List/Entry1/StringEntry", "string-value-1");
250  icc::setValue("/ConfigList/List/Entry1/EnumEntry", "ONE");
251  icc::setValue("/ConfigList/List/Entry1/Foo/UInt32Entry", "1");
252  icc::setValue("/ConfigList/List/Entry1/Foo/EnumEntry", "ONE");
253  icc::setValue("/ConfigList/List/Entry1/Foo/Bar/DoubleEntry", "0.1");
254  icc::setValue("/ConfigList/List/Entry1/Foo/Bar/EnumEntry", "ONE");
255  icc::setValue("/ConfigList/List/Entry2/StringEntry", "string-value-2");
256  icc::setValue("/ConfigList/List/Entry2/EnumEntry", "TWO");
257  icc::setValue("/ConfigList/List/Entry2/Foo/UInt32Entry", "2");
258  icc::setValue("/ConfigList/List/Entry2/Foo/EnumEntry", "TWO");
259  icc::setValue("/ConfigList/List/Entry2/Foo/Bar/DoubleEntry", "0.2");
260  icc::setValue("/ConfigList/List/Entry2/Foo/Bar/EnumEntry", "TWO");
261  icc::setValue("/ConfigList/List/Entry3/StringEntry", "string-value-3");
262  icc::setValue("/ConfigList/List/Entry3/EnumEntry", "THREE");
263  icc::setValue("/ConfigList/List/Entry3/Foo/UInt32Entry", "3");
264  icc::setValue("/ConfigList/List/Entry3/Foo/EnumEntry", "THREE");
265  icc::setValue("/ConfigList/List/Entry3/Foo/Bar/DoubleEntry", "0.3");
266  icc::setValue("/ConfigList/List/Entry3/Foo/Bar/EnumEntry", "THREE");
267 
268  // Remard: This also works with std::list.
269  // Actually, every container that provides a push_back() method can be used!
270  std::vector<ConfigListEntry> config_list;
271  bool read_successful =
273  CONFIG_LIST(
274  ConfigListEntry, "/ConfigList/List",
278  MEMBER_VALUE_2("Foo/UInt32Entry", ConfigListEntry, foo, uint32_value)
279  MEMBER_VALUE_3("Foo/Bar/DoubleEntry", ConfigListEntry, foo, bar, double_value)
282  MEMBER_ENUM_3 ("Foo/Bar/EnumEntry", ConfigListEntry, foo, bar, enum_value,
283  // Enum members also work with plain C-string arrays.
285  std::back_inserter(config_list))),
286  icl::Nirwana::instance());
287  BOOST_CHECK(read_successful);
288 
289  BOOST_CHECK_EQUAL(config_list.size(), 3u);
290 
291  BOOST_CHECK_EQUAL(config_list[0].string_value, "string-value-1");
292  BOOST_CHECK_EQUAL(config_list[0].foo.uint32_value, 1u);
293  BOOST_CHECK_EQUAL(config_list[0].foo.bar.double_value, 0.1);
294  BOOST_CHECK_EQUAL(config_list[0].enum_value, ONE);
295  BOOST_CHECK_EQUAL(config_list[0].foo.enum_value, ONE);
296  BOOST_CHECK_EQUAL(config_list[0].foo.bar.enum_value, ONE);
297 
298  BOOST_CHECK_EQUAL(config_list[1].string_value, "string-value-2");
299  BOOST_CHECK_EQUAL(config_list[1].foo.uint32_value, 2u);
300  BOOST_CHECK_EQUAL(config_list[1].foo.bar.double_value, 0.2);
301  BOOST_CHECK_EQUAL(config_list[1].enum_value, TWO);
302  BOOST_CHECK_EQUAL(config_list[1].foo.enum_value, TWO);
303  BOOST_CHECK_EQUAL(config_list[1].foo.bar.enum_value, TWO);
304 
305  BOOST_CHECK_EQUAL(config_list[2].string_value, "string-value-3");
306  BOOST_CHECK_EQUAL(config_list[2].foo.uint32_value, 3u);
307  BOOST_CHECK_EQUAL(config_list[2].foo.bar.double_value, 0.3);
308  BOOST_CHECK_EQUAL(config_list[2].enum_value, THREE);
309  BOOST_CHECK_EQUAL(config_list[2].foo.enum_value, THREE);
310  BOOST_CHECK_EQUAL(config_list[2].foo.bar.enum_value, THREE);
311 }
312 
313 #else
314 # if defined(_MSC_VER)
315 # pragma message("The icl_core_config batch convenience macros are only available in Visual Studio 2010 and newer.")
316 # endif
317 #endif
318 
319 BOOST_AUTO_TEST_SUITE_END()
unsigned int uint32_t
Definition: msvc_stdint.h:93
#define CONFIG_ENUM(key, value, descriptions)
Definition: ConfigEnum.h:33
#define MEMBER_MAPPING(cls, arg)
Definition: ConfigList.h:39
bool get(const icl_core::String &key, typename icl_core::ConvertToRef< T >::ToRef value)
Gets the value for the specified key from the configuration.
Definition: Config.h:90
char const * enum_value_char_description[]
Definition: ts_BatchGet.cpp:43
#define CONFIG_VALUES(list)
Definition: ConfigValues.h:39
#define MEMBER_ENUM_1(suffix, cls, member1, descriptions)
Definition: MemberEnum.h:39
#define MEMBER_ENUM_2(suffix, cls, member1, member2, descriptions)
Definition: MemberEnum.h:45
Base header file for the configuration framework.
#define CONFIG_LIST(cls, prefix, members, result)
Definition: ConfigList.h:36
BOOST_AUTO_TEST_CASE(TestStaticParser)
#define MEMBER_VALUE_3(suffix, cls, member1, member2, member3)
Definition: MemberValue.h:53
void setValue(const icl_core::String &key, typename icl_core::ConvertToRef< T >::ToConstRef value)
Definition: Config.h:360
EnumValue enum_value
Definition: ts_BatchGet.cpp:48
#define CONFIG_VALUE(key, value)
Definition: ConfigValue.h:39
#define MEMBER_VALUE_1(suffix, cls, member1)
Definition: MemberValue.h:37
std::vector< std::string > enum_value_description
Definition: ts_BatchGet.cpp:40
std::string string_value
Definition: ts_BatchGet.cpp:47
struct ConfigListEntry::Foo foo
EnumValue
Definition: ts_BatchGet.cpp:33
Framework for processing configuration files.
#define MEMBER_VALUE_2(suffix, cls, member1, member2)
Definition: MemberValue.h:43
#define CONFIG_VALUE_DEFAULT(key, value, default_value)
#define CONFIG_ENUM_DEFAULT(key, value, default_value, descriptions)
struct ConfigListEntry::Foo::Bar bar
Flexible, powerful, configurable logging framework.
Definition: Constants.h:29
#define MEMBER_ENUM_3(suffix, cls, member1, member2, member3, descriptions)
Definition: MemberEnum.h:55


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58