intervals_fhicl_test.cc
Go to the documentation of this file.
1 /**
2  * @file test/Utilities/intervals_fhicl_test.cc
3  * @brief Unit test for `intervals_fhicl.h` header
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 27, 2019
6  * @see lardataalg/Utilities/intervals_fhicl.h
7  *
8  */
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE ( intervals_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>
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 Config validateConfig(std::string const& configStr) {
50  return validateConfig<Config>(fhicl::ParameterSet::make(configStr));
51 } // validateConfig(Config)
52 
53 
54 // -----------------------------------------------------------------------------
55 // --- Interval tests
56 // -----------------------------------------------------------------------------
58 
59  using namespace std::string_view_literals;
60  using namespace util::quantities::time_literals;
62 
63  auto t = util::quantities::makeInterval<microseconds>("-7e1 ms"sv);
64  static_assert(std::is_same_v<decltype(t), microseconds>);
65  BOOST_TEST(t == -70000_us);
66  BOOST_TEST(t == -70_ms);
67 
68  t = util::quantities::makeInterval<microseconds>("7e1ms"sv);
69  BOOST_TEST(t == 70000_us);
70  BOOST_TEST(t == 70_ms);
71 
72  t = util::quantities::makeInterval<microseconds>("7e1"sv, true);
73  BOOST_TEST(t == 70_us);
74 
75  BOOST_CHECK_THROW(
76  util::quantities::makeInterval<microseconds>("7e1"sv),
78  );
79 
80  BOOST_CHECK_THROW(
81  util::quantities::makeInterval<microseconds>("7g ms"sv),
83  );
84 
85  BOOST_CHECK_THROW(
86  util::quantities::makeInterval<microseconds>("g7 ms"sv),
88  );
89 
90  BOOST_CHECK_THROW(
91  util::quantities::makeInterval<microseconds>(""sv),
93  );
94 
95  BOOST_CHECK_THROW(
96  util::quantities::makeInterval<microseconds>(""sv, true),
98  );
99 
100 } // test_makeInterval()
101 
102 
103 // -----------------------------------------------------------------------------
105 
106  using namespace std::string_view_literals;
107  using namespace util::quantities::time_literals;
109 
110  auto t = util::quantities::makePoint<microsecond>("-7e1 ms"sv);
111  static_assert(std::is_same_v<decltype(t), microsecond>);
112  BOOST_TEST(t == -70000_us);
113  BOOST_TEST(t == -70_ms);
114 
115  t = util::quantities::makePoint<microsecond>("7e1ms"sv);
116  BOOST_TEST(t == 70000_us);
117  BOOST_TEST(t == 70_ms);
118 
119  t = util::quantities::makePoint<microsecond>("7e1"sv, true);
120  BOOST_TEST(t == 70_us);
121 
122  BOOST_CHECK_THROW(
123  util::quantities::makePoint<microsecond>("7e1"sv),
125  );
126 
127  BOOST_CHECK_THROW(
128  util::quantities::makePoint<microsecond>("7g ms"sv),
130  );
131 
132  BOOST_CHECK_THROW(
133  util::quantities::makePoint<microsecond>("g7 ms"sv),
135  );
136 
137  BOOST_CHECK_THROW(
138  util::quantities::makePoint<microsecond>(""sv),
140  );
141 
142  BOOST_CHECK_THROW(
143  util::quantities::makePoint<microsecond>(""sv, true),
145  );
146 
147 } // test_makePoint()
148 
149 
150 // -----------------------------------------------------------------------------
151 void test_read() {
152 
153  using namespace util::quantities::time_literals;
154 
155  struct Config {
156 
158  { fhicl::Name("start"), 0_us };
159 
161  { fhicl::Name("end"), 6_ms };
162 
164  { fhicl::Name("duration"), 6_ms };
165 
166  }; // struct Config
167 
168  std::string const configStr { "start: 2ms duration: 16ms" };
169  util::quantities::points::microsecond const expectedStart { 2_ms };
170  util::quantities::points::microsecond const expectedEnd { 6_ms };
171  util::quantities::intervals::microseconds const expectedDuration { 16_ms };
172 
173  auto validatedConfig = validateConfig<Config>(configStr);
174  BOOST_TEST(validatedConfig.start() == expectedStart);
175  BOOST_TEST(validatedConfig.end() == expectedEnd);
176  BOOST_TEST(validatedConfig.duration() == expectedDuration);
177 
178 } // test_read()
179 
180 
181 // -----------------------------------------------------------------------------
182 void test_write() {
183 
184  using namespace util::quantities::time_literals;
185  struct Config {
186 
188  { fhicl::Name("start"), 0_us };
189 
191  { fhicl::Name("end"), 6_ms };
192 
194  { fhicl::Name("duration"), 6_ms };
195 
196  }; // struct Config
197 
198  util::quantities::points::microsecond const expectedStart { 2_ms };
199  util::quantities::points::microsecond const expectedEnd { 6_ms };
200  util::quantities::intervals::microseconds const expectedDuration { 16_ms };
201 
202  fhicl::ParameterSet pset;
203  pset.put("start", expectedStart);
204  pset.put("duration", expectedDuration);
205 
206  auto validatedConfig = validateConfig<Config>(pset);
207  BOOST_TEST(validatedConfig.start() == expectedStart);
208  BOOST_TEST(validatedConfig.end() == expectedEnd);
209  BOOST_TEST(validatedConfig.duration() == expectedDuration);
210 
211 } // test_write()
212 
213 // ----------------------------------------------------------------------------
214 
215 BOOST_AUTO_TEST_SUITE(intervals_fhicl_test)
216 
217 BOOST_AUTO_TEST_CASE(intervals_testcase)
218 {
220 }
221 
222 BOOST_AUTO_TEST_CASE(points_testcase)
223 {
224  test_makePoint();
225 }
226 
227 BOOST_AUTO_TEST_CASE(quantities_fhicl_testcase)
228 {
229  test_read();
230  test_write();
231 }
232 
233 BOOST_AUTO_TEST_SUITE_END()
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
BOOST_AUTO_TEST_CASE(intervals_testcase)
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
microseconds_as<> microseconds
Type of time interval stored in microseconds, in double precision.
Definition: spacetime.h:259
microsecond_as<> microsecond
Type of time stored in microseconds, in double precision.
Definition: spacetime.h:119
ChannelGroupService::Name Name
Config validateConfig(fhicl::ParameterSet const &pset)
microsecond microseconds
Alias for common language habits.
Definition: spacetime.h:122
void test_write()
String representing a quantity has spurious characters after the number.
Definition: quantities.h:1108
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
microsecond_as<> microsecond
Type of time point stored in microseconds, in double precision.
Definition: spacetime.h:328
void test_makePoint()
std::string to_indented_string() const
An interval (duration, length, distance) between two quantity points.
Definition: intervals.h:114
Literal constants for time quantities.
Definition: spacetime.h:175
Utilities to read interval and point quantity FHiCL configuration.
bool is_empty() const
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_read()
void test_makeInterval()
void put(std::string const &key)
QTextStream & endl(QTextStream &s)