Classes | Macros | Functions
ParameterSet_t.cc File Reference
#include "boost/test/unit_test.hpp"
#include "fhiclcpp/ParameterSet.h"
#include "fhiclcpp/test/boost_test_print_pset.h"
#include <cstddef>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  SampleConfigFixture
 

Macros

#define BOOST_TEST_MODULE   (ParameterSet test)
 

Functions

 BOOST_AUTO_TEST_CASE (Local)
 
 BOOST_AUTO_TEST_CASE (DeepInjection)
 
 BOOST_AUTO_TEST_CASE (DoubleStringMismatchDefaulted)
 
 BOOST_AUTO_TEST_CASE (DoubleStringMismatchNoDefault)
 
 BOOST_AUTO_TEST_CASE (StringSuccess)
 
 BOOST_AUTO_TEST_CASE (NotFound)
 
 BOOST_AUTO_TEST_CASE (DeepVector)
 
 BOOST_AUTO_TEST_CASE (is_key_to)
 
 BOOST_AUTO_TEST_CASE (put)
 
 BOOST_AUTO_TEST_CASE (put_or_replace)
 
 BOOST_AUTO_TEST_CASE (put_or_replace_compatible_nil)
 
 BOOST_AUTO_TEST_CASE (put_or_replace_compatible_atom)
 
 BOOST_AUTO_TEST_CASE (put_or_replace_compatible_sequence)
 
 BOOST_AUTO_TEST_CASE (put_or_replace_compatible_table)
 
unsigned ctox (char c)
 
unsigned hex (std::string const &from)
 
 BOOST_AUTO_TEST_CASE (Custom)
 

Macro Definition Documentation

#define BOOST_TEST_MODULE   (ParameterSet test)

Definition at line 1 of file ParameterSet_t.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( Local  )

Definition at line 28 of file ParameterSet_t.cc.

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 }
std::void_t< T > n
void put(std::string const &key)
BOOST_AUTO_TEST_CASE ( DeepInjection  )

Definition at line 40 of file ParameterSet_t.cc.

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 }
static QStrList * l
Definition: config.cpp:1044
T get(std::string const &key) const
Definition: ParameterSet.h:271
void put(std::string const &key)
BOOST_AUTO_TEST_CASE ( DoubleStringMismatchDefaulted  )

Definition at line 52 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
const double e
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( DoubleStringMismatchNoDefault  )

Definition at line 70 of file ParameterSet_t.cc.

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 }
const double e
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( StringSuccess  )

Definition at line 84 of file ParameterSet_t.cc.

85 {
86  BOOST_TEST("rain" == pset.get<std::string>("e"));
87 }
std::string string
Definition: nybbler.cc:12
BOOST_AUTO_TEST_CASE ( NotFound  )

Definition at line 89 of file ParameterSet_t.cc.

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 }
const double e
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( DeepVector  )

Definition at line 103 of file ParameterSet_t.cc.

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 }
BOOST_AUTO_TEST_CASE ( is_key_to  )

Definition at line 113 of file ParameterSet_t.cc.

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 }
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
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
void put(std::string const &key)
BOOST_AUTO_TEST_CASE ( put  )

Definition at line 139 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
const double e
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( put_or_replace  )

Definition at line 151 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
BOOST_AUTO_TEST_CASE ( put_or_replace_compatible_nil  )

Definition at line 161 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
void put(std::string const &key)
BOOST_AUTO_TEST_CASE ( put_or_replace_compatible_atom  )

Definition at line 189 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
const double e
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( put_or_replace_compatible_sequence  )

Definition at line 222 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
const double e
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( put_or_replace_compatible_table  )

Definition at line 252 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
const double e
void put(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
BOOST_AUTO_TEST_CASE ( Custom  )

Definition at line 331 of file ParameterSet_t.cc.

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 }
unsigned hex(std::string const &from)
std::string string
Definition: nybbler.cc:12
unsigned ctox ( char  c)

Definition at line 284 of file ParameterSet_t.cc.

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 }
std::string string
Definition: nybbler.cc:12
unsigned hex ( std::string const &  from)

Definition at line 318 of file ParameterSet_t.cc.

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 }
static QCString result
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
unsigned ctox(char c)
const double e