All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
quantities_fhicl_test.cc
Go to the documentation of this file.
1 /**
2  * @file test/Utilities/quantities_fhicl_test.cc
3  * @brief Unit test for `quantities_fhicl.h` header
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 27, 2019
6  * @see lardataalg/Utilities/quantities_fhicl.h
7  *
8  */
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE ( quantities_fhicl_test )
12 #include <boost/test/unit_test.hpp>
13 
14 // LArSoft libraries
18 
19 // support libraries
20 #include "fhiclcpp/types/Table.h"
21 #include "fhiclcpp/types/Atom.h"
22 #include "fhiclcpp/ParameterSet.h"
23 
24 // C/C++ standard libraries
25 #include <type_traits> // std::is_same_v<>
26 
27 
28 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
29 template <typename Config>
30 fhicl::Table<Config> validateConfig(fhicl::ParameterSet const& pset) {
31  fhicl::Table<Config> validatedConfig { fhicl::Name("validatedConfig") };
32 
33  std::cout << std::string(80, '-') << std::endl;
34  std::cout << "===> FHiCL configuration:";
35  if (pset.is_empty()) std::cout << " <empty>";
36  else std::cout << "\n" << pset.to_indented_string();
37  std::cout << std::endl;
38  validatedConfig.print_allowed_configuration
39  (std::cout << "===> Expected configuration: ");
40  std::cout << std::endl;
41 
42  validatedConfig.validate_ParameterSet(pset);
43  return validatedConfig;
44 } // validateConfig()
45 
46 
47 // -----------------------------------------------------------------------------
48 template <typename Config>
49 fhicl::Table<Config> validateConfig(std::string const& configStr) {
50  auto pset = fhicl::ParameterSet::make(configStr);
51  return validateConfig<Config>(pset);
52 } // validateConfig(Config)
53 
54 
55 // -----------------------------------------------------------------------------
56 // --- Quantity tests
57 // -----------------------------------------------------------------------------
59 
60  using namespace std::string_view_literals;
61  using namespace util::quantities::time_literals;
63 
64  auto t = util::quantities::makeQuantity<microsecond>("-7e1 ms"sv);
65  static_assert(std::is_same_v<decltype(t), microsecond>);
66  BOOST_TEST(t == -70000_us);
67  BOOST_TEST(t == -70_ms);
68 
69  t = util::quantities::makeQuantity<microsecond>("7e1ms"sv);
70  BOOST_TEST(t == 70000_us);
71  BOOST_TEST(t == 70_ms);
72 
73  t = util::quantities::makeQuantity<microsecond>("7e1"sv, true);
74  BOOST_TEST(t == 70_us);
75 
76  BOOST_CHECK_THROW(
77  util::quantities::makeQuantity<microsecond>("7e1"sv),
79  );
80 
81  BOOST_CHECK_THROW(
82  util::quantities::makeQuantity<microsecond>("7g ms"sv),
84  );
85 
86  BOOST_CHECK_THROW(
87  util::quantities::makeQuantity<microsecond>("g7 ms"sv),
89  );
90 
91  BOOST_CHECK_THROW(
92  util::quantities::makeQuantity<microsecond>(""sv),
94  );
95 
96  BOOST_CHECK_THROW(
97  util::quantities::makeQuantity<microsecond>(""sv, true),
99  );
100 
101 } // test_makeQuantity()
102 
103 
104 // -----------------------------------------------------------------------------
106 
107  using namespace util::quantities::time_literals;
109 
110  struct Config {
111 
112  fhicl::Atom<util::quantities::microsecond> start
113  { fhicl::Name("start"), 0_us };
114 
115  fhicl::Atom<util::quantities::microsecond> end
116  { fhicl::Name("end"), 6_ms };
117 
118  }; // struct Config
119 
120  std::string const configStr { "end: 16ms" };
121  util::quantities::microsecond const expectedStart = 0_us;
122  util::quantities::microsecond const expectedEnd = 16_ms;
123 
124  auto validatedConfig = validateConfig<Config>(configStr)();
125  BOOST_TEST(validatedConfig.start() == expectedStart);
126  BOOST_TEST(validatedConfig.end() == expectedEnd);
127 
128 } // test_readQuantity()
129 
130 
131 // -----------------------------------------------------------------------------
133 
134  using namespace util::quantities::time_literals;
136 
137  struct Config {
138 
139  fhicl::Atom<util::quantities::microsecond> start
140  { fhicl::Name("start"), 0_us };
141 
142  fhicl::Atom<util::quantities::microsecond> end
143  { fhicl::Name("end"), 6_ms };
144 
145  }; // struct Config
146 
147  fhicl::ParameterSet pset;
148  pset.put<util::quantities::microsecond>("end", 16_ms);
149  util::quantities::microsecond const expectedStart = 0_us;
150  util::quantities::microsecond const expectedEnd = 16_ms;
151 
152  auto validatedConfig = validateConfig<Config>(pset)();
153  BOOST_TEST(validatedConfig.start() == expectedStart);
154  BOOST_TEST(validatedConfig.end() == expectedEnd);
155 
156 } // test_writeQuantity()
157 
158 
159 // -----------------------------------------------------------------------------
160 // BEGIN Test cases -----------------------------------------------------------
161 // -----------------------------------------------------------------------------
162 BOOST_AUTO_TEST_CASE(quantities_testcase) {
163 
165 
166 } // BOOST_AUTO_TEST_CASE(quantities_testcase)
167 
168 // -----------------------------------------------------------------------------
169 BOOST_AUTO_TEST_CASE(quantities_fhicl_testcase) {
170 
173 
174 } // BOOST_AUTO_TEST_CASE(quantities_fhicl_testcase)
175 
176 // -----------------------------------------------------------------------------
177 // END Test cases -------------------------------------------------------------
178 // -----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(AllTests)
void test_writeQuantity()
microsecond_as<> microsecond
Type of time stored in microseconds, in double precision.
Definition: spacetime.h:119
fhicl::Table< Config > validateConfig(std::string const &configStr)
String representing a quantity has spurious characters after the number.
Definition: quantities.h:1108
Utilities to read and write quantities in FHiCL configuration.
A value measured in the specified unit.
Definition: quantities.h:566
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
BEGIN_PROLOG vertical distance to the surface Name
void test_readQuantity()
String representing a quantity has no unit.
Definition: quantities.h:1092
Dimensioned variables representing space or time quantities.
String representing a quantity has an invalid number.
Definition: quantities.h:1104
void test_makeQuantity()
BEGIN_PROLOG could also be cout