ParameterSet_t.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (ParameterSet test)
2 
3 #include "boost/test/unit_test.hpp"
6 
7 #include <cstddef>
8 #include <string>
9 #include <vector>
10 
11 using namespace fhicl;
12 
13 struct SampleConfigFixture {
15 
16  ParameterSet pset;
17 };
18 
20 {
21  cet::filepath_lookup policy("FHICL_FILE_PATH");
22  std::string cfg_in("Sample.cfg");
23  pset = ParameterSet::make(cfg_in, policy);
24 }
25 
26 BOOST_FIXTURE_TEST_SUITE(sampleConfig, SampleConfigFixture)
27 
29 {
31  j.put("y", -1);
32  auto const orig = pset.get<fhicl::ParameterSet>("j");
33  BOOST_TEST(j == orig);
34  BOOST_TEST(orig.get<int>("y") == -1);
35  BOOST_TEST(pset.get<std::vector<int>>("m")[0] == -1);
36  for (auto n : pset.get_names())
37  BOOST_TEST("x" != n);
38 }
39 
40 BOOST_AUTO_TEST_CASE(DeepInjection)
41 {
43  l.put("zz", -2);
45  k.put("l", l);
46  auto const orig = pset.get<fhicl::ParameterSet>("k");
47  BOOST_TEST(k == orig);
48  BOOST_TEST(orig.get<fhicl::ParameterSet>("l").get<int>("zz") == -2);
49  BOOST_TEST(orig.get<int>("l.zz") == -2);
50 }
51 
52 BOOST_AUTO_TEST_CASE(DoubleStringMismatchDefaulted)
53 {
54  std::string s;
55  BOOST_TEST(pset.get_if_present("e", s));
56  BOOST_TEST(s == "rain");
57 
58  try {
59  pset.get<double>("e", 2.0);
60  BOOST_FAIL("Failed to throw an exception as expected");
61  }
62  catch (fhicl::exception& e) {
63  BOOST_TEST(e.categoryCode() == type_mismatch);
64  }
65  catch (...) {
66  BOOST_FAIL("Wrong exception type thrown");
67  }
68 }
69 
70 BOOST_AUTO_TEST_CASE(DoubleStringMismatchNoDefault)
71 {
72  try {
73  pset.get<double>("e");
74  BOOST_FAIL("Failed to throw an exception as expected");
75  }
76  catch (fhicl::exception& e) {
77  BOOST_TEST(e.categoryCode() == type_mismatch);
78  }
79  catch (...) {
80  BOOST_FAIL("Wrong exception type thrown");
81  }
82 }
83 
84 BOOST_AUTO_TEST_CASE(StringSuccess)
85 {
86  BOOST_TEST("rain" == pset.get<std::string>("e"));
87 }
88 
90 {
91  try {
92  pset.get<double>("zz");
93  BOOST_FAIL("Failed to throw an exception as expected");
94  }
95  catch (fhicl::exception& e) {
96  BOOST_TEST(e.categoryCode() == cant_find);
97  }
98  catch (...) {
99  BOOST_FAIL("Wrong exception type thrown");
100  }
101 }
102 
104 {
105  using vv_t = std::vector<std::vector<uint32_t>>;
106  vv_t vv;
107  BOOST_CHECK_NO_THROW(vv = pset.get<vv_t>("vv"));
108  BOOST_TEST(vv.front().back() == 3u);
109  BOOST_TEST(vv.back().front() == 2u);
110  BOOST_TEST(vv.back().back() == 4u);
111 }
112 
114 {
115  // p1: {
116  // p2: {
117  // i: 37
118  // js: [3,2,67]
119  // t:{}
120  // }
121  // }
122 
123  int const i{37};
124  std::vector<int> const js{3, 2, 67};
125  fhicl::ParameterSet const t{};
127  p.put("i", i);
128  p.put("js", js);
129  p.put("t", t);
131  ps.put("p", p);
132 
133  BOOST_TEST(ps.is_key_to_atom("p.i"));
134  BOOST_TEST(ps.is_key_to_sequence("p.js"));
135  BOOST_TEST(ps.is_key_to_atom("p.js[2]"));
136  BOOST_TEST(ps.is_key_to_table("p.t"));
137 }
138 
140 {
141  std::string const sval = "friendly";
142  std::string const sval2 = "unfriendly";
143  BOOST_CHECK_NO_THROW(pset.put("putTest", sval));
144  BOOST_TEST(pset.get<std::string>("putTest") == sval);
145  BOOST_CHECK_EXCEPTION(
146  pset.put("putTest", sval2), fhicl::exception, [](auto const& e) {
147  return e.categoryCode() == fhicl::error::cant_insert;
148  });
149 }
150 
151 BOOST_AUTO_TEST_CASE(put_or_replace)
152 {
153  std::string const sval = "friendly";
154  std::string const sval2 = "superfriendly";
155  BOOST_CHECK_NO_THROW(pset.put_or_replace("putOrReplaceTest", sval));
156  BOOST_TEST(pset.get<std::string>("putOrReplaceTest") == sval);
157  BOOST_CHECK_NO_THROW(pset.put_or_replace("putOrReplaceTest", sval2));
158  BOOST_TEST(pset.get<std::string>("putOrReplaceTest") == sval2);
159 }
160 
161 BOOST_AUTO_TEST_CASE(put_or_replace_compatible_nil)
162 {
163  std::string const sval = "friendly";
164  std::vector<int> vval{1, 2, 3, 5, 7, 11, 13};
165  fhicl::ParameterSet psval;
166  psval.put("junk", 3);
167  BOOST_CHECK_NO_THROW(
168  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
169  BOOST_CHECK_NO_THROW(
170  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", sval));
171  BOOST_TEST(pset.get<std::string>("putOrReplaceCompatibleTest") == sval);
172  BOOST_CHECK_NO_THROW(
173  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
174  BOOST_CHECK_NO_THROW(
175  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval.front()));
176  BOOST_TEST(pset.get<int>("putOrReplaceCompatibleTest") == vval.front());
177  BOOST_CHECK_NO_THROW(
178  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
179  BOOST_CHECK_NO_THROW(
180  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval));
181  BOOST_TEST(pset.get<decltype(vval)>("putOrReplaceCompatibleTest") == vval);
182  BOOST_CHECK_NO_THROW(
183  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
184  BOOST_CHECK_NO_THROW(
185  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", psval));
186  BOOST_TEST(pset.get<decltype(psval)>("putOrReplaceCompatibleTest") == psval);
187 }
188 
189 BOOST_AUTO_TEST_CASE(put_or_replace_compatible_atom)
190 {
191  std::string const sval = "friendly";
192  std::string const sval2 = "superfriendly";
193  std::vector<int> vval{1, 2, 3, 5, 7, 11, 13};
194  fhicl::ParameterSet psval;
195  BOOST_CHECK_NO_THROW(
196  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
197  BOOST_CHECK_NO_THROW(
198  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", sval));
199  BOOST_TEST(pset.get<std::string>("putOrReplaceCompatibleTest") == sval);
200  BOOST_CHECK_NO_THROW(
201  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", sval2));
202  BOOST_TEST(pset.get<std::string>("putOrReplaceCompatibleTest") == sval2);
203  BOOST_CHECK_EXCEPTION(
204  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval),
206  [](auto const& e) {
207  return e.categoryCode() == fhicl::error::cant_insert;
208  });
209  BOOST_CHECK_EXCEPTION(
210  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", psval),
212  [](auto const& e) {
213  return e.categoryCode() == fhicl::error::cant_insert;
214  });
215  BOOST_CHECK_NO_THROW(
216  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval.front()));
217  BOOST_TEST(pset.get<int>("putOrReplaceCompatibleTest") == vval.front());
218  BOOST_CHECK_NO_THROW(
219  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
220 }
221 
222 BOOST_AUTO_TEST_CASE(put_or_replace_compatible_sequence)
223 {
224  std::string const sval = "friendly";
225  std::vector<int> vval{1, 2, 3, 5, 7, 11, 13};
226  std::vector<int> vval2{4, 6, 8, 9, 10, 12, 14};
227  fhicl::ParameterSet psval;
228  BOOST_CHECK_NO_THROW(
229  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
230  BOOST_CHECK_NO_THROW(
231  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval));
232  BOOST_TEST(pset.get<decltype(vval)>("putOrReplaceCompatibleTest") == vval);
233  BOOST_CHECK_NO_THROW(
234  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval2));
235  BOOST_TEST(pset.get<decltype(vval)>("putOrReplaceCompatibleTest") == vval2);
236  BOOST_CHECK_EXCEPTION(
237  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", sval),
239  [](auto const& e) {
240  return e.categoryCode() == fhicl::error::cant_insert;
241  });
242  BOOST_CHECK_EXCEPTION(
243  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", psval),
245  [](auto const& e) {
246  return e.categoryCode() == fhicl::error::cant_insert;
247  });
248  BOOST_CHECK_NO_THROW(
249  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
250 }
251 
252 BOOST_AUTO_TEST_CASE(put_or_replace_compatible_table)
253 {
254  std::string const sval = "friendly";
255  std::vector<int> vval{1, 2, 3, 5, 7, 11, 13};
256  fhicl::ParameterSet psval, psval2;
257  psval.put("junk", 3);
258  psval.put("ethel", 3);
259  BOOST_CHECK_NO_THROW(
260  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
261  BOOST_CHECK_NO_THROW(
262  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", psval));
263  BOOST_TEST(pset.get<decltype(psval)>("putOrReplaceCompatibleTest") == psval);
264  BOOST_CHECK_NO_THROW(
265  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", psval2));
266  BOOST_TEST(pset.get<decltype(psval)>("putOrReplaceCompatibleTest") == psval2);
267  BOOST_CHECK_EXCEPTION(
268  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", sval),
270  [](auto const& e) {
271  return e.categoryCode() == fhicl::error::cant_insert;
272  });
273  BOOST_CHECK_EXCEPTION(
274  pset.put_or_replace_compatible("putOrReplaceCompatibleTest", vval),
276  [](auto const& e) {
277  return e.categoryCode() == fhicl::error::cant_insert;
278  });
279  BOOST_CHECK_NO_THROW(
280  pset.put_or_replace("putOrReplaceCompatibleTest")); // Nil.
281 }
282 
283 unsigned
284 ctox(char c)
285 {
286  switch (c) {
287  case '0':
288  case '1':
289  case '2':
290  case '3':
291  case '4':
292  case '5':
293  case '6':
294  case '7':
295  case '8':
296  case '9':
297  return c - '0';
298  case 'a':
299  case 'b':
300  case 'c':
301  case 'd':
302  case 'e':
303  case 'f':
304  return 10 + c - 'a';
305  case 'A':
306  case 'B':
307  case 'C':
308  case 'D':
309  case 'E':
310  case 'F':
311  return 10 + c - 'A';
312  default:
313  throw std::string("ctox(): invalid hex character");
314  }
315 }
316 
317 unsigned
318 hex(std::string const& from)
319 {
320  if (from.size() < 3 || from[0] != '0' || (from[1] != 'x' && from[1] != 'X'))
321  throw std::string("hex(): invalid argument");
322 
323  unsigned result = 0u;
324  for (std::string::const_iterator it = from.begin() + 2, e = from.end();
325  it != e;
326  ++it)
327  result = 16u * result + ctox(*it);
328  return result;
329 }
330 
332 {
333  BOOST_TEST(pset.get<std::string>("n") == "0x123");
334  unsigned u;
335  BOOST_TEST(pset.get_if_present("n", u, hex));
336  BOOST_TEST(pset.get<unsigned>("n", hex) == u);
337  BOOST_TEST(pset.get<unsigned>("n", hex), (((1u) * 16u + 2u) * 16u + 3u));
338  BOOST_CHECK_THROW(pset.get_if_present("e", u, hex), std::string);
339 }
340 
341 BOOST_AUTO_TEST_SUITE_END()
unsigned hex(std::string const &from)
static QCString result
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
intermediate_table::const_iterator const_iterator
static QStrList * l
Definition: config.cpp:1044
unsigned ctox(char c)
BOOST_AUTO_TEST_CASE(Local)
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:171
bool is_key_to_table(std::string const &key) const
Definition: ParameterSet.h:165
const double e
std::void_t< T > n
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
static constexpr double ps
Definition: Units.h:99
bool is_key_to_atom(std::string const &key) const
Definition: ParameterSet.h:177
static QCString * s
Definition: config.cpp:1042
void put(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33