values_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test ParameterSet values
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (values test)
8 
9 #include "boost/test/unit_test.hpp"
10 #include "fhiclcpp/ParameterSet.h"
11 #include <complex>
12 #include <limits>
13 #include <string>
14 #include <vector>
15 
16 using namespace fhicl;
17 using namespace std;
18 
19 using uint = unsigned int;
20 
21 using dbl = double;
22 using ldbl = long double;
23 
24 using cdbl = complex<dbl>;
25 using cldbl = complex<ldbl>;
26 
27 using uvec = vector<uint>;
28 
29 BOOST_AUTO_TEST_SUITE(values_test)
30 
31 BOOST_AUTO_TEST_CASE(bool_values) // test atoms "true" and "false"
32 {
33  {
34  ParameterSet pset;
35  pset.put<string>("b11", "true");
36  BOOST_TEST(pset.is_key_to_atom("b11"));
37  BOOST_TEST(!pset.is_key_to_sequence("b11"));
38  BOOST_TEST(!pset.is_key_to_table("b11"));
39  BOOST_TEST(pset.to_string() == "b11:\"true\"");
40  BOOST_TEST(pset.get<bool>("b11"));
41  pset.put<string>("b13", "\"false\"");
42  BOOST_TEST(pset.to_string() == "b11:\"true\" b13:\"false\"");
43  BOOST_TEST(!pset.get<bool>("b13"));
44  }
45 
46  {
47  ParameterSet pset;
48  pset.put<bool>("b21", true);
49  BOOST_TEST(pset.get<string>("b21") == "true");
50  BOOST_TEST(pset.to_string() == "b21:true");
51  BOOST_TEST(pset.get<bool>("b21"));
52  pset.put<bool>("b24", false);
53  BOOST_TEST(pset.get<string>("b24") == "false");
54  BOOST_TEST(pset.to_string() == "b21:true b24:false");
55  BOOST_TEST(!pset.get<bool>("b24"));
56  }
57 
58  {
59  ParameterSet pset;
60  pset.put<string>("b31", "tru");
61  BOOST_TEST(pset.to_string() == "b31:\"tru\"");
62  BOOST_CHECK_THROW(pset.get<bool>("b31"), fhicl::exception);
63  }
64 }
65 
66 BOOST_AUTO_TEST_CASE(nil_value) // test atom "nil"
67 {
68  using nil_t = std::nullptr_t;
69 
70  ParameterSet pset;
71  BOOST_TEST(pset.is_empty());
72  BOOST_TEST(pset.to_string() == "");
73 
74  pset.put("n11", nil_t{});
75  BOOST_TEST(pset.get<nil_t>("n11") == nil_t{});
76  BOOST_TEST(pset.to_string() == "n11:@nil");
77  BOOST_CHECK_THROW(pset.get<bool>("n11"), fhicl::exception);
78  BOOST_CHECK_THROW(pset.get<uint>("n11"), fhicl::exception);
79  BOOST_CHECK_THROW(pset.get<int>("n11"), fhicl::exception);
80  BOOST_CHECK_THROW(pset.get<double>("n11"), fhicl::exception);
81  BOOST_CHECK_THROW(pset.get<string>("n11"), fhicl::exception);
82  BOOST_CHECK_THROW(pset.get<uvec>("n11"), fhicl::exception);
83  BOOST_CHECK_THROW(pset.get<ParameterSet>("n11"), fhicl::exception);
84 
85  pset.put<string>("n21", "nil");
86  BOOST_CHECK_THROW(pset.get<nil_t>("n21"), fhicl::exception);
87  BOOST_TEST(pset.to_string() == "n11:@nil n21:\"nil\"");
88 
89  pset.put<string>("n31", "NIL");
90  BOOST_TEST(pset.to_string() == "n11:@nil n21:\"nil\" n31:\"NIL\"");
91  BOOST_CHECK_THROW(pset.get<nil_t>("n31"), fhicl::exception);
92 
93  auto const pset2 = ParameterSet::make(pset.to_string());
94  BOOST_TEST(pset.to_string() == pset2.to_string());
95 
96  auto const pset3 = ParameterSet::make("n11:@nil n21:nil n31:\"@nil\"");
97  BOOST_TEST(pset3.to_string() == "n11:@nil n21:\"nil\" n31:\"@nil\"");
98  BOOST_CHECK_THROW(pset3.get<string>("n11"), fhicl::exception);
99  BOOST_TEST(pset3.get<string>("n21") == "nil");
100  BOOST_TEST(pset3.get<string>("n31") == "@nil");
101 }
102 
103 BOOST_AUTO_TEST_CASE(string_values) // test string atoms
104 {
105  ParameterSet pset;
106  BOOST_TEST(pset.is_empty());
107 
108  pset.put<string>("n11", "");
109  BOOST_TEST(pset.get<string>("n11") == "");
110  BOOST_TEST(pset.to_string() == "n11:\"\"");
111  pset.put<string>("n13", "a");
112  BOOST_TEST(pset.get<string>("n13") == "a");
113  BOOST_TEST(pset.to_string() == "n11:\"\" n13:\"a\"");
114  pset.put<string>("n15", "\"a\"");
115  BOOST_TEST(pset.get<string>("n15") == "a");
116  BOOST_TEST(pset.to_string() == "n11:\"\" n13:\"a\" n15:\"a\"");
117  pset.put<string>("n17", "'a b'");
118  BOOST_TEST(pset.get<string>("n17") == "a b");
119  BOOST_TEST(pset.to_string() == "n11:\"\" n13:\"a\" n15:\"a\" n17:\"a b\"");
120 }
121 
122 BOOST_AUTO_TEST_CASE(unsigned_values) // test unsigned integral atoms
123 {
124  {
125  ParameterSet pset;
126  pset.put<uint>("u11", 0u);
127  BOOST_TEST(pset.get<string>("u11") == "0");
128  BOOST_TEST(pset.to_string() == "u11:0");
129  BOOST_TEST(pset.get<uint>("u11") == 0u);
130  }
131 
132  {
133  ParameterSet pset;
134  pset.put<string>("u21", "000");
135  BOOST_TEST(pset.to_string() == "u21:\"000\"");
136  uint u21 = pset.get<uint>("u21");
137  BOOST_TEST(u21 == 0u);
138  pset.put<uint>("u23", u21);
139  BOOST_TEST(pset.get<string>("u23") == "0");
140  BOOST_TEST(pset.to_string() == "u21:\"000\" u23:0");
141  }
142 
143  {
144  ParameterSet pset;
145  pset.put<string>("u31", "1.2e+1");
146  BOOST_TEST(pset.to_string() == "u31:\"1.2e+1\"");
147  BOOST_TEST(pset.get<uint>("u31") == 12u);
148  }
149 
150  {
151  ParameterSet pset;
152  pset.put<unsigned long>("u41", 123456ul);
153  BOOST_TEST(pset.get<string>("u41") == "123456");
154  BOOST_TEST(pset.to_string() == "u41:123456");
155  BOOST_TEST(pset.get<unsigned long>("u41") == 123456ul);
156  pset.put<unsigned long>("u44", 1234567ul);
157  BOOST_TEST(pset.get<string>("u44") == "1.234567e+6");
158  BOOST_TEST(pset.to_string() == "u41:123456 u44:1.234567e+6");
159  BOOST_TEST(pset.get<unsigned long>("u44") == 1234567ul);
160  }
161 }
162 
163 BOOST_AUTO_TEST_CASE(int_values) // test signed integral atoms
164 {
165  {
166  ParameterSet pset;
167  pset.put<int>("i1", 0);
168  BOOST_TEST(pset.get<string>("i1") == "0");
169  BOOST_TEST(pset.to_string() == "i1:0");
170  BOOST_TEST(pset.get<int>("i1") == 0);
171  }
172 
173  {
174  ParameterSet pset;
175  pset.put<string>("i21", "000");
176  BOOST_TEST(pset.to_string() == "i21:\"000\"");
177  int i21 = pset.get<int>("i21");
178  BOOST_TEST(i21 == 0);
179  pset.put<int>("i23", i21);
180  BOOST_TEST(pset.get<string>("i23") == "0");
181  BOOST_TEST(pset.to_string() == "i21:\"000\" i23:0");
182  }
183 
184  {
185  ParameterSet pset;
186  pset.put<string>("i31", "-1.2e+1");
187  BOOST_TEST(pset.to_string() == "i31:\"-1.2e+1\"");
188  BOOST_TEST(pset.get<int>("i31") == -12);
189  }
190 
191  {
192  ParameterSet pset;
193  pset.put<long>("i41", 123456);
194  BOOST_TEST(pset.get<string>("i41") == "123456");
195  BOOST_TEST(pset.to_string() == "i41:123456");
196  BOOST_TEST(pset.get<long>("i41") == 123456);
197  pset.put<long>("i44", 1234567);
198  BOOST_TEST(pset.get<string>("i44") == "1.234567e+6");
199  BOOST_TEST(pset.to_string() == "i41:123456 i44:1.234567e+6");
200  BOOST_TEST(pset.get<long>("i44") == 1234567);
201  }
202 
203  {
204  ParameterSet pset;
205  pset.put<long>("i51", -123456);
206  BOOST_TEST(pset.get<string>("i51") == "-123456");
207  BOOST_TEST(pset.to_string() == "i51:-123456");
208  BOOST_TEST(pset.get<long>("i51") == -123456);
209  pset.put<long>("i54", -1234567);
210  BOOST_TEST(pset.get<string>("i54") == "-1.234567e+6");
211  BOOST_TEST(pset.to_string() == "i51:-123456 i54:-1.234567e+6");
212  BOOST_TEST(pset.get<long>("i54") == -1234567);
213  }
214 }
215 
216 BOOST_AUTO_TEST_CASE(float_values) // test floating-point atoms
217 {
218  {
219  ParameterSet pset;
220  pset.put<dbl>("f11", 0.0);
221  BOOST_TEST(pset.get<string>("f11") == "0");
222  BOOST_TEST(pset.to_string() == "f11:0");
223  BOOST_TEST(pset.get<dbl>("f11") == 0.0);
224  BOOST_TEST(pset.get<int>("f11") == 0);
225  BOOST_TEST(pset.get<uint>("f11") == 0u);
226  pset.put<dbl>("f14", 12.0);
227  BOOST_TEST(pset.get<string>("f14") == "12");
228  BOOST_TEST(pset.to_string() == "f11:0 f14:12");
229  BOOST_TEST(pset.get<dbl>("f14") == 12.0);
230  BOOST_TEST(pset.get<int>("f14") == 12);
231  BOOST_TEST(pset.get<uint>("f14") == 12u);
232  pset.put<dbl>("f17", 12.3e1);
233  BOOST_TEST(pset.get<string>("f17") == "123");
234  BOOST_TEST(pset.to_string() == "f11:0 f14:12 f17:123");
235  BOOST_TEST(pset.get<dbl>("f17") == 123.0);
236  BOOST_TEST(pset.get<int>("f17") == 123);
237  BOOST_TEST(pset.get<uint>("f17") == 123u);
238  }
239 
240  {
241  ParameterSet pset;
242  pset.put<dbl>("f21", 12.3456e4);
243  BOOST_TEST(pset.get<string>("f21") == "123456");
244  BOOST_TEST(pset.to_string() == "f21:123456");
245  BOOST_TEST(pset.get<dbl>("f21") == 123456.0);
246  BOOST_TEST(pset.get<int>("f21") == 123456);
247  BOOST_TEST(pset.get<uint>("f21") == 123456u);
248  pset.put<dbl>("f24", 12.34567e5);
249  BOOST_TEST(pset.get<string>("f24") == "1.234567e+6");
250  BOOST_TEST(pset.to_string() == "f21:123456 f24:1.234567e+6");
251  BOOST_TEST(pset.get<dbl>("f24") == 1234567.0);
252  BOOST_TEST(pset.get<int>("f24") == 1234567L);
253  BOOST_TEST(pset.get<uint>("f24") == 1234567uL);
254  pset.put<dbl>("f27", 3.5);
255  BOOST_TEST(pset.get<string>("f27") == "3.5");
256  BOOST_TEST(pset.to_string() == "f21:123456 f24:1.234567e+6 f27:3.5");
257  BOOST_TEST(pset.get<dbl>("f27") == 3.5);
258  BOOST_CHECK_THROW(pset.get<int>("f27"), fhicl::exception);
259  BOOST_CHECK_THROW(pset.get<uint>("f27"), fhicl::exception);
260  }
261 
262  {
263  ParameterSet pset;
264  pset.put<dbl>("f31", -12.3456e4);
265  BOOST_TEST(pset.get<string>("f31") == "-123456");
266  BOOST_TEST(pset.to_string() == "f31:-123456");
267  BOOST_TEST(pset.get<dbl>("f31") == -123456.0);
268  pset.put<dbl>("f34", -12.34567e5);
269  BOOST_TEST(pset.get<string>("f34") == "-1.234567e+6");
270  BOOST_TEST(pset.to_string() == "f31:-123456 f34:-1.234567e+6");
271  BOOST_TEST(pset.get<dbl>("f34") == -1234567.0);
272  pset.put<dbl>("f37", -12.34567e-5);
273  }
274 
275  {
276  ParameterSet pset;
277  pset.put<dbl>("f41", numeric_limits<dbl>::infinity());
278  BOOST_TEST(pset.get<string>("f41") == "+infinity");
279  BOOST_TEST(pset.to_string() == "f41:+infinity");
280  BOOST_TEST(pset.get<dbl>("f41") == numeric_limits<dbl>::infinity());
281  pset.put<dbl>("f44", -numeric_limits<dbl>::infinity());
282  BOOST_TEST(pset.get<string>("f44") == "-infinity");
283  BOOST_TEST(pset.to_string() == "f41:+infinity f44:-infinity");
284  BOOST_TEST(pset.get<dbl>("f44") == -numeric_limits<dbl>::infinity());
285  }
286 }
287 
288 BOOST_AUTO_TEST_CASE(complex_values) // test complex atoms
289 {
290  {
291  ParameterSet pset;
292  pset.put<cldbl>("f11", cldbl());
293  BOOST_TEST(pset.get<string>("f11") == "(0,0)");
294  BOOST_TEST(pset.to_string() == "f11:(0,0)");
295  BOOST_TEST(pset.get<cldbl>("f11") == cldbl(0.0L, 0.0L));
296  pset.put<cdbl>("f14", cdbl());
297  BOOST_TEST(pset.get<string>("f14") == "(0,0)");
298  BOOST_TEST(pset.to_string() == "f11:(0,0) f14:(0,0)");
299  BOOST_TEST(pset.get<cdbl>("f14") == cdbl(0.0, 0.0));
300  }
301 
302  {
303  ParameterSet pset;
304  pset.put<cldbl>("f21", cldbl(1, 2));
305  BOOST_TEST(pset.to_string() == "f21:(1,2)");
306  BOOST_TEST(pset.get<cldbl>("f21") == cldbl(1.0L, 2.0L));
307  BOOST_TEST(pset.get<cdbl>("f21") == cdbl(1.0, 2.0));
308  pset.put<string>("f24", "(3.25 , 4.75 )");
309  BOOST_TEST(pset.get<cldbl>("f24") == cldbl(3.25L, 4.75L));
310  BOOST_TEST(pset.get<cdbl>("f24") == cdbl(3.25, 4.75));
311  }
312 }
313 
314 BOOST_AUTO_TEST_CASE(sequence_values) // test sequences
315 {
316  ParameterSet pset;
317  uvec uv;
318 
319  pset.put<string>("f11", "[]");
320  BOOST_TEST(pset.get<string>("f11") == "[]");
321  BOOST_TEST(pset.to_string() == "f11:\"[]\"");
322  BOOST_TEST(pset.get<uvec>("f11").empty());
323  BOOST_TEST(pset.get<uvec>("f11") == uvec());
324  BOOST_TEST(pset.get<uvec>("f11") == uv);
325  pset.put<uvec>("f16", uv);
326  // BOOST_TEST( pset.get<string>("f16", "NO") == "NO" );
327  BOOST_TEST(pset.to_string() == "f11:\"[]\" f16:[]");
328  BOOST_TEST(pset.get<uvec>("f16") == uv);
329 
330  uv.push_back(1);
331  pset.put<string>("f21", "[1]");
332  BOOST_TEST(pset.get<string>("f21") == "[1]");
333  BOOST_TEST(pset.to_string() == "f11:\"[]\" f16:[] f21:\"[1]\"");
334  BOOST_TEST(pset.get<uvec>("f21").size() == 1u);
335  BOOST_TEST(pset.get<uvec>("f21") == uv);
336  pset.put<uvec>("f25", uv);
337  // BOOST_TEST( pset.get<string>("f25", "NO") == "NO" );
338  BOOST_TEST(pset.to_string() == "f11:\"[]\" f16:[] f21:\"[1]\" f25:[1]");
339  BOOST_TEST(pset.get<uvec>("f25").size() == 1u);
340  BOOST_TEST(pset.get<uvec>("f25") == uv);
341 
342  uv.push_back(2);
343  pset.put<string>("f31", "[1,2]");
344  BOOST_TEST(pset.get<string>("f31") == "[1,2]");
345  BOOST_TEST(pset.get<uvec>("f31").size() == 2u);
346  BOOST_TEST(pset.get<uvec>("f31") == uv);
347  uv.push_back(3);
348  pset.put<uvec>("f34", uv);
349  BOOST_TEST(pset.to_string() ==
350  "f11:\"[]\" f16:[] f21:\"[1]\" f25:[1] f31:\"[1,2]\" f34:[1,2,3]");
351  // BOOST_TEST( pset.get<string>("f34", "NO") == "NO" );
352  BOOST_TEST(pset.get<uvec>("f34").size() == 3u);
353  BOOST_TEST(pset.get<uvec>("f34") == uv);
354 
355  uv.push_back(4);
356  pset.put<string>("f41", "[1,2,3,4]");
357  BOOST_TEST(pset.get<string>("f41") == "[1,2,3,4]");
358  BOOST_TEST(pset.get<uvec>("f41").size() == 4u);
359  BOOST_TEST(pset.get<uvec>("f41") == uv);
360  uv.push_back(5);
361  pset.put<uvec>("f43", uv);
362  BOOST_TEST(pset.is_key_to_sequence("f43"));
363  BOOST_TEST(!pset.is_key_to_table("f43"));
364  BOOST_TEST(!pset.is_key_to_atom("f43"));
365  // BOOST_TEST( pset.get<string>("f43", "NO") == "NO" );
366  BOOST_TEST(pset.get<uvec>("f43").size() == 5u);
367  BOOST_TEST(pset.get<uvec>("f43") == uv);
368 }
369 
370 BOOST_AUTO_TEST_CASE(table_values) // test tables
371 {
372  ParameterSet p0;
373  BOOST_TEST(p0.to_string() == "");
374  BOOST_CHECK_THROW(p0.get<string>("a"), fhicl::exception);
375 
376  ParameterSet p1;
377  p1.put<string>("b", "B");
378  p1.put<string>("a", "A");
379  BOOST_TEST(p1.get<string>("a") == "A");
380  BOOST_TEST(p1.get<string>("b") == "B");
381  BOOST_TEST(p1.to_string() == "a:\"A\" b:\"B\"");
382 
383  ParameterSet p2;
384  p2.put<string>("y", "Y");
385  p2.put<string>("z", "Z");
386  p2.put<string>("x", "X");
387  BOOST_TEST(p2.get<string>("x") == "X");
388  BOOST_TEST(p2.get<string>("y") == "Y");
389  BOOST_TEST(p2.get<string>("z") == "Z");
390  BOOST_TEST(p2.to_string() == "x:\"X\" y:\"Y\" z:\"Z\"");
391 
392  ParameterSet p3;
393  p3.put<ParameterSet>("p2", p2);
394  p3.put<ParameterSet>("p1", p1);
395  BOOST_TEST(p3.is_key_to_table("p2"));
396  BOOST_TEST(!p3.is_key_to_sequence("p2"));
397  BOOST_TEST(!p3.is_key_to_atom("p2"));
398  BOOST_TEST(p3.get<ParameterSet>("p1").get<string>("a") == "A");
399  BOOST_TEST(p3.get<ParameterSet>("p1").get<string>("b") == "B");
400  BOOST_TEST(p3.get<ParameterSet>("p2").get<string>("x") == "X");
401  BOOST_TEST(p3.get<ParameterSet>("p2").get<string>("y") == "Y");
402  BOOST_TEST(p3.get<ParameterSet>("p2").get<string>("z") == "Z");
403  BOOST_TEST(p3.to_string() ==
404  "p1:{a:\"A\" b:\"B\"} p2:{x:\"X\" y:\"Y\" z:\"Z\"}");
405 }
406 
407 BOOST_AUTO_TEST_SUITE_END()
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
STL namespace.
long double ldbl
Definition: ParameterSet.cc:33
complex< dbl > cdbl
Definition: values_test.cc:24
vector< uint > uvec
Definition: values_test.cc:27
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
T get(std::string const &key) const
Definition: ParameterSet.h:271
complex< ldbl > cldbl
Definition: values_test.cc:25
bool is_empty() const
bool is_key_to_atom(std::string const &key) const
Definition: ParameterSet.h:177
BOOST_AUTO_TEST_CASE(bool_values)
Definition: values_test.cc:31
unsigned uint
Definition: qglobal.h:351
std::numeric_limits< double > dbl
void put(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::string to_string() const
Definition: ParameterSet.h:153