All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
intervals_test.cc
Go to the documentation of this file.
1 /**
2  * @file test/Utilities/intervals_test.cc
3  * @brief Unit test for `intervals.h` header
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date May 29, 2019
6  * @see lardataalg/Utilities/intervals.h
7  *
8  */
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE ( intervals_test )
12 #include <boost/test/unit_test.hpp>
13 
14 // LArSoft libraries
17 #include "larcorealg/CoreUtils/DebugUtils.h" // lar::debug::static_assert_on<>()
19 
20 
21 // C/C++ standard libraries
22 #include <type_traits>
23 
24 namespace {
25  template <typename T>
26  struct static_assert_type {
27  template <typename U>
28  explicit static_assert_type(U)
29  {
30  static_assert(std::is_same_v<T, U>);
31  }
32  };
33 
34  template <typename T>
35  struct static_assert_type<T&> {
36  template <typename U>
37  explicit static_assert_type(U&)
38  {
39  static_assert(std::is_same_v<T&, U&>);
40  }
41  };
42 }
43 
44 // -----------------------------------------------------------------------------
45 // --- implementation detail tests
46 // -----------------------------------------------------------------------------
49  using category_t = void;
50 };
51 
52 static_assert
53  (!util::quantities::concepts::details::has_category_v<ObjectWithoutCategory>);
54 static_assert
55  ( util::quantities::concepts::details::has_category_v<ObjectWithCategory>);
58  );
61  );
62 
63 
64 // -----------------------------------------------------------------------------
67  );
70  );
73  );
74 
77  );
80  );
83  );
84 
87  );
90  );
93  );
94 
97  );
100  );
103  );
104 
107  );
110  );
113  );
114 
117  );
120  );
123  );
124 
125 
126 // -----------------------------------------------------------------------------
127 // --- is_point, is_interval etc.
128 // -----------------------------------------------------------------------------
129 static_assert(!util::quantities::concepts::is_interval_v<double>);
130 static_assert
131  (!util::quantities::concepts::is_interval_v<ObjectWithoutCategory>);
132 static_assert(!util::quantities::concepts::is_interval_v<ObjectWithCategory>);
133 static_assert(
136  );
137 static_assert(
140  );
141 
142 static_assert(!util::quantities::concepts::is_point_v<double>);
143 static_assert
144  (!util::quantities::concepts::is_point_v<ObjectWithoutCategory>);
145 static_assert(!util::quantities::concepts::is_point_v<ObjectWithCategory>);
146 static_assert(
149  );
150 static_assert(
153  );
154 
155 static_assert(!util::quantities::concepts::is_interval_or_point_v<double>);
156 static_assert
157  (!util::quantities::concepts::is_interval_or_point_v<ObjectWithoutCategory>);
158 static_assert(!util::quantities::concepts::is_interval_or_point_v<ObjectWithCategory>);
159 static_assert(
162  );
163 static_assert(
166  );
167 
168 
169 //------------------------------------------------------------------------------
170 //--- Interval tests
171 //------------------------------------------------------------------------------
173 
174  using namespace util::quantities::time_literals;
175 
178 
179  microseconds ct0; // can't be constant since it's uninitialized
180  (void) ct0; // don't let this fall unused
181 
182  constexpr microseconds ct1 { 2.0 };
183  static_assert(ct1.value() == 2.0);
184 
185  constexpr microseconds ct2 { +6.0_us };
186  static_assert(ct2.value() == 6.0);
187 
188  constexpr microseconds ct3 { -4000.0_ns };
189  static_assert(ct3.value() == -4.0);
190 
191 } // void test_interval_construction()
192 
193 
194 // -----------------------------------------------------------------------------
196 
197  using namespace util::quantities::time_literals;
200 
201  constexpr microseconds t1 { +6.0_us };
202  constexpr nanoseconds t2 { -4'000'000.0_ps };
203 
204  constexpr microseconds t1s = t1;
205 
206  static_assert( t1 == 6_us );
207  static_assert( t1 == 6000_ns );
208  static_assert( 6_us == t1 );
209  static_assert( 6000_ns == t1 );
210  static_assert( t1 == t1s );
211  static_assert(!(t1 == 5_us ));
212  static_assert(!(t1 == 5_ns ));
213  static_assert(!(5_us == t1 ));
214  static_assert(!(5_ns == t1 ));
215  static_assert(!(t1 == t2 ));
216 
217  static_assert(!(t1 != 6_us ));
218  static_assert(!(t1 != 6000_ns));
219  static_assert(!(6_us != t1 ));
220  static_assert(!(6000_ns != t1 ));
221  static_assert(!(t1 != t1s ));
222  static_assert( t1 != 5_us );
223  static_assert( t1 != 5_ns );
224  static_assert( 5_us != t1 );
225  static_assert( 5_ns != t1 );
226  static_assert( t1 != t2 );
227 
228  static_assert( t1 >= 6_us );
229  static_assert( t1 >= 6000_ns );
230  static_assert( 6_us >= t1 );
231  static_assert( 6000_ns >= t1 );
232  static_assert( t1 >= t1s );
233  static_assert( t1 >= 5_us );
234  static_assert( t1 >= 5000_ns );
235  static_assert(!(5_us >= t1 ));
236  static_assert(!(5000_ns >= t1 ));
237  static_assert(!(t1 >= 8_us ));
238  static_assert(!(t1 >= 8000_ns));
239  static_assert( 8_us >= t1 );
240  static_assert( 8000_ns >= t1 );
241  static_assert( t1 >= t2 );
242 
243  static_assert(!(t1 > 6_us ));
244  static_assert(!(t1 > 6000_ns));
245  static_assert(!(6_us > t1 ));
246  static_assert(!(6000_ns > t1 ));
247  static_assert(!(t1 > t1s ));
248  static_assert( t1 > 5_us );
249  static_assert( t1 > 5000_ns );
250  static_assert(!(5_us > t1 ));
251  static_assert(!(5000_ns > t1 ));
252  static_assert(!(t1 > 8_us ));
253  static_assert(!(t1 > 8000_ns));
254  static_assert( 8_us > t1 );
255  static_assert( 8000_ns > t1 );
256  static_assert( t1 > t2 );
257 
258  static_assert( t1 <= 6_us );
259  static_assert( t1 <= 6000_ns );
260  static_assert( 6000_ns <= t1 );
261  static_assert( t1 <= t1s );
262  static_assert(!(t1 <= 5_us ));
263  static_assert(!(t1 <= 5000_ns));
264  static_assert( 5_us <= t1 );
265  static_assert( 5000_ns <= t1 );
266  static_assert( t1 <= 8_us );
267  static_assert( t1 <= 8000_ns );
268  static_assert(!(8_us <= t1 ));
269  static_assert(!(8000_ns <= t1 ));
270  static_assert(!(t1 <= t2 ));
271 
272  static_assert(!(t1 < 6_us ));
273  static_assert(!(t1 < 6000_ns));
274  static_assert(!(6_us < t1 ));
275  static_assert(!(6000_ns < t1 ));
276  static_assert(!(t1 < t1s ));
277  static_assert(!(t1 < 5_us ));
278  static_assert(!(t1 < 5000_ns));
279  static_assert( 5_us < t1 );
280  static_assert( 5000_ns < t1 );
281  static_assert( t1 < 8_us );
282  static_assert( t1 < 8000_ns );
283  static_assert(!(8_us < t1 ));
284  static_assert(!(8000_ns < t1 ));
285  static_assert(!(t1 < t2 ));
286 
287 } // test_interval_comparisons()
288 
289 
290 // -----------------------------------------------------------------------------
292 
293  using namespace util::quantities::time_literals;
294 
297 
298  constexpr microseconds t1 { +6.0_us };
299  constexpr nanoseconds t2 { -4'000'000.0_ps };
300 
301  static_assert(std::is_same_v<microseconds::quantity_t, util::quantities::microsecond>);
302  static_assert(std::is_same_v<microseconds::unit_t, util::quantities::microsecond::unit_t>);
303 
304  static_assert(t1.quantity() == 6_us );
305  static_assert(t2.quantity() == -4_us );
306  static_assert(t1.value() == 6.0 );
307  static_assert(t2.value() == -4000.0 );
308 
309 } // test_interval_queries()
310 
311 // -----------------------------------------------------------------------------
313 
314  using namespace util::quantities::time_literals;
315 
318 
319  constexpr microseconds ct1 { +6.0_us };
320  constexpr microseconds ct2 { -4000.0_ns };
321 
322  static_assert_type<microseconds>{ct1 + -4000.0_ns};
323  static_assert(ct1 + -4000.0_ns == 2_us);
324 
325  static_assert_type<microseconds>{ct1 + ct2};
326  static_assert(ct1 + ct2 == 2_us);
327 
328  static_assert_type<microseconds>{ct1 - -4000.0_ns};
329  static_assert(ct1 - -4000.0_ns == 10_us);
330 
331  static_assert_type<microseconds>{ct1 - ct2};
332  static_assert(ct1 - ct2 == 10_us);
333 
334  static_assert_type<microseconds>{+ct1};
335  static_assert(+ct1 == 6_us);
336 
337  static_assert_type<microseconds>{-ct1};
338  static_assert(-ct1 == -6_us);
339 
340  static_assert_type<microseconds>{ct1.abs()};
341  static_assert(ct1.abs() == 6_us);
342  static_assert(ct2.abs() == 4_us);
343 
344  static_assert_type<microseconds>{ct1 * 3.0};
345  static_assert(ct1 * 3.0 == 6_us * 3.0);
346 
347  static_assert_type<microseconds>{3.0 * ct1};
348  static_assert(3.0 * ct1 == 6_us * 3.0);
349 
350  static_assert_type<microseconds>{ct1 / 2.0};
351  static_assert(ct1 / 2.0 == 6_us / 2.0);
352 
353  microseconds t1 { +6.0_us };
354  nanoseconds const t2 { -4000.0_ns };
355 
356  decltype(auto) incr = (t1 += 2000_ns);
357  static_assert_type<microseconds&>{incr};
358  BOOST_TEST(t1 == 8_us);
359  BOOST_TEST(&incr == &t1);
360 
361  decltype(auto) incr2 = (t1 += t2);
362  static_assert_type<microseconds&>{incr2};
363  BOOST_TEST(t1 == 4_us);
364  BOOST_TEST(t2 == -4_us);
365  BOOST_TEST(&incr2 == &t1);
366 
367  decltype(auto) decr = (t1 -= 2000_ns);
368  static_assert_type<microseconds&>{decr};
369  BOOST_TEST(t1 == 2_us);
370  BOOST_TEST(&decr == &t1);
371 
372  decltype(auto) decr2 = (t1 -= t2);
373  static_assert_type<microseconds&>{decr2};
374  BOOST_TEST(t1 == 6_us);
375  BOOST_TEST(t2 == -4_us);
376  BOOST_TEST(&decr2 == &t1);
377 
378  decltype(auto) expand = (t1 *= 2);
379  static_assert_type<microseconds&>{expand};
380  BOOST_TEST(t1 == 12_us);
381  BOOST_TEST(&expand == &t1);
382 
383  decltype(auto) shrink = (t1 /= 2);
384  static_assert_type<microseconds&>{shrink};
385  BOOST_TEST(t1 == 6_us);
386  BOOST_TEST(&shrink == &t1);
387 
388 } // test_interval_operations()
389 
390 
391 // -----------------------------------------------------------------------------
392 // --- Point tests
393 // -----------------------------------------------------------------------------
395 
396  using namespace util::quantities::time_literals;
397 
400 
401  microsecond ct0;
402  (void) ct0; // don't let this fall unused
403 
404  constexpr microsecond ct1 { 2.0 };
405  static_assert(ct1.value() == 2.0);
406 
407  constexpr microsecond ct2 { +6.0_us };
408  static_assert(ct2.value() == 6.0);
409 
410  constexpr microsecond ct3 { -4000.0_ns };
411  static_assert(ct3.value() == -4.0);
412 
413 } // void test_point_construction()
414 
415 
416 // -----------------------------------------------------------------------------
418  using namespace util::quantities::time_literals;
421 
422  constexpr microsecond const t1 { +6.0_us };
423  constexpr nanosecond const t2 { -4'000'000.0_ps };
424 
425  constexpr microsecond const t1s = t1;
426 
427  static_assert( t1 == 6_us );
428  static_assert( t1 == 6000_ns );
429  static_assert( 6_us == t1 );
430  static_assert( 6000_ns == t1 );
431  static_assert( t1 == t1s );
432  static_assert(!(t1 == 5_us ));
433  static_assert(!(t1 == 5_ns ));
434  static_assert(!(5_us == t1 ));
435  static_assert(!(5_ns == t1 ));
436  static_assert(!(t1 == t2 ));
437 
438  static_assert(!(t1 != 6_us ));
439  static_assert(!(t1 != 6000_ns));
440  static_assert(!(6_us != t1 ));
441  static_assert(!(6000_ns != t1 ));
442  static_assert(!(t1 != t1s ));
443  static_assert( t1 != 5_us );
444  static_assert( t1 != 5_ns );
445  static_assert( 5_us != t1 );
446  static_assert( 5_ns != t1 );
447  static_assert( t1 != t2 );
448 
449  static_assert( t1 >= 6_us );
450  static_assert( t1 >= 6000_ns );
451  static_assert( 6_us >= t1 );
452  static_assert( 6000_ns >= t1 );
453  static_assert( t1 >= t1s );
454  static_assert( t1 >= 5_us );
455  static_assert( t1 >= 5000_ns );
456  static_assert(!(5_us >= t1 ));
457  static_assert(!(5000_ns >= t1 ));
458  static_assert(!(t1 >= 8_us ));
459  static_assert(!(t1 >= 8000_ns));
460  static_assert( 8_us >= t1 );
461  static_assert( 8000_ns >= t1 );
462  static_assert( t1 >= t2 );
463 
464  static_assert(!(t1 > 6_us ));
465  static_assert(!(t1 > 6000_ns));
466  static_assert(!(6_us > t1 ));
467  static_assert(!(6000_ns > t1 ));
468  static_assert(!(t1 > t1s ));
469  static_assert( t1 > 5_us );
470  static_assert( t1 > 5000_ns );
471  static_assert(!(5_us > t1 ));
472  static_assert(!(5000_ns > t1 ));
473  static_assert(!(t1 > 8_us ));
474  static_assert(!(t1 > 8000_ns));
475  static_assert( 8_us > t1 );
476  static_assert( 8000_ns > t1 );
477  static_assert( t1 > t2 );
478 
479  static_assert( t1 <= 6_us );
480  static_assert( t1 <= 6000_ns );
481  static_assert( 6_us <= t1 );
482  static_assert( 6000_ns <= t1 );
483  static_assert( t1 <= t1s );
484  static_assert(!(t1 <= 5_us ));
485  static_assert(!(t1 <= 5000_ns));
486  static_assert( 5_us <= t1 );
487  static_assert( 5000_ns <= t1 );
488  static_assert( t1 <= 8_us );
489  static_assert( t1 <= 8000_ns );
490  static_assert(!(8_us <= t1 ));
491  static_assert(!(8000_ns <= t1 ));
492  static_assert(!(t1 <= t2 ));
493 
494  static_assert(!(t1 < 6_us ));
495  static_assert(!(t1 < 6000_ns));
496  static_assert(!(6_us < t1 ));
497  static_assert(!(6000_ns < t1 ));
498  static_assert(!(t1 < t1s ));
499  static_assert(!(t1 < 5_us ));
500  static_assert(!(t1 < 5000_ns));
501  static_assert( 5_us < t1 );
502  static_assert( 5000_ns < t1 );
503  static_assert( t1 < 8_us );
504  static_assert( t1 < 8000_ns );
505  static_assert(!(8_us < t1 ));
506  static_assert(!(8000_ns < t1 ));
507  static_assert(!(t1 < t2 ));
508 
509 } // test_point_comparisons()
510 
511 
512 // -----------------------------------------------------------------------------
514 
515  using namespace util::quantities::time_literals;
516 
519 
520  constexpr microsecond t1 { +6.0_us };
521  constexpr nanosecond t2 { -4'000'000.0_ps };
522 
523  static_assert(std::is_same_v<microsecond::quantity_t, util::quantities::microsecond>);
524  static_assert(std::is_same_v<microsecond::unit_t, util::quantities::microsecond::unit_t>);
525 
526  static_assert(t1.quantity() == 6_us );
527  static_assert(t2.quantity() == -4_us );
528  static_assert(t1.value() == 6.0 );
529  static_assert(t2.value() == -4000.0 );
530 
531 } // test_points_queries()
532 
533 
534 // -----------------------------------------------------------------------------
536 
537  using namespace util::quantities::time_literals;
538 
541 
542  constexpr microsecond cp1 { +6.0_us };
543  constexpr util::quantities::intervals::microseconds ct { 3.0_us };
544 
545  static_assert_type<microsecond>{cp1 + 3000_ns};
546  static_assert(cp1 + 3000_ns == 9_us);
547 
548  static_assert_type<microsecond>{cp1 + ct};
549  static_assert(cp1 + ct == 9_us);
550 
551  static_assert_type<microsecond>{cp1 - 3000_ns};
552  static_assert(cp1 - 3000_ns == 3_us);
553 
554  static_assert_type<microsecond>{cp1 - ct};
555  static_assert(cp1 - ct == 3_us);
556 
557  static_assert_type<microsecond>{+cp1};
558  static_assert(+cp1 == 6_us);
559 
560  static_assert_type<microsecond>{-cp1};
561  static_assert(-cp1 == -6_us);
562 
563  microsecond p1 { +6.0_us };
564  microsecond p2 { -4000.0_ns };
566 
567  decltype(auto) incr = (p1 += 2000_ns);
568  static_assert_type<microsecond&>{incr};
569  BOOST_TEST(p1 == 8_us);
570  BOOST_TEST(&incr == &p1);
571 
572  decltype(auto) incr2 = (p1 += t);
573  static_assert_type<microsecond&>{incr2};
574  BOOST_TEST(p1 == 11_us);
575  BOOST_TEST(t == 3_us);
576  BOOST_TEST(&incr2 == &p1);
577 
578  decltype(auto) decr = (p1 -= 2000_ns);
579  static_assert_type<microsecond&>{decr};
580  BOOST_TEST(p1 == 9_us);
581  BOOST_TEST(&decr == &p1);
582 
583  decltype(auto) decr2 = (p1 -= t);
584  static_assert_type<microsecond&>{decr2};
585  BOOST_TEST(p1 == 6_us);
586  BOOST_TEST(t == 3_us);
587  BOOST_TEST(&decr2 == &p1);
588 
589  decltype(auto) diff = (p1 - p2);
590  static_assert_type<util::quantities::intervals::microseconds>{diff};
591  BOOST_TEST(p1 == 6_us);
592  BOOST_TEST(p2 == -4_us);
593  BOOST_TEST(diff == 10_us);
594 
595 } // test_point_operations()
596 
597 
598 // -----------------------------------------------------------------------------
599 // BEGIN Test cases -----------------------------------------------------------
600 // -----------------------------------------------------------------------------
601 BOOST_AUTO_TEST_CASE(intervals_testcase) {
602 
607 
608 } // BOOST_AUTO_TEST_CASE(intervals_testcase)
609 
610 // -----------------------------------------------------------------------------
611 BOOST_AUTO_TEST_CASE(points_testcase) {
612 
617 
618 } // BOOST_AUTO_TEST_CASE(points_testcase)
619 
620 // -----------------------------------------------------------------------------
621 // END Test cases -------------------------------------------------------------
622 // -----------------------------------------------------------------------------
void test_point_comparisons()
BOOST_AUTO_TEST_CASE(AllTests)
void test_interval_construction()
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
microsecond microseconds
Alias for common language habits.
Definition: spacetime.h:122
constexpr bool is_quantity_v
Trait: true if Q is a Quantity specialization.
Definition: quantities.h:268
constexpr bool is_interval_v
Trait: true if IV is a Interval specialization.
Definition: intervals.h:67
Defines point and interval variables based on quantities.
microsecond_as<> microsecond
Type of time point stored in microseconds, in double precision.
Definition: spacetime.h:328
void test_point_operations()
j template void())
Definition: json.hpp:3108
constexpr bool has_unit_v
Trait: true if U is a ScaledUnit-based object.
Definition: quantities.h:260
An interval (duration, length, distance) between two quantity points.
Definition: intervals.h:114
void test_interval_operations()
constexpr bool has_quantity_v
Trait: true if Q is a Quantity-based object.
Definition: quantities.h:276
nanoseconds_as<> nanoseconds
Type of time interval stored in nanoseconds, in double precision.
Definition: spacetime.h:270
Functions to help debugging by instrumenting code.
void test_point_construction()
Dimensioned variables representing space or time quantities.
nanosecond_as<> nanosecond
Type of time stored in nanoseconds, in double precision.
Definition: spacetime.h:136
nanosecond_as<> nanosecond
Type of time point stored in nanoseconds, in double precision.
Definition: spacetime.h:339
void test_point_queries()
constexpr bool is_interval_or_point_v
Trait: true if PT is a specialization of Interval or Point.
Definition: intervals.h:87
void test_interval_queries()
constexpr bool is_point_v
Trait: true if PT is a Point specialization.
Definition: intervals.h:77
physics associatedGroupsWithLeft p1
nanosecond nanoseconds
Alias for common language habits.
Definition: spacetime.h:139
void test_interval_comparisons()