TrajectoryPointFlags_test.cc
Go to the documentation of this file.
1 /**
2  * @file TrajectoryPointFlags_test.cc
3  * @brief Simple test on a recob::TrajectoryPointFlags object
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date January 25, 2016
6  * @version 1.0
7  *
8  * This test simply creates recob::TrajectoryPointFlags objects and verifies
9  * that the values it can access are the right ones.
10  *
11  * See http://www.boost.org/libs/test for the Boost test library home page.
12  *
13  */
14 
15 
16 // Boost libraries
17 /*
18  * Boost: define the name of the module;
19  * and do that before the inclusion of Boost unit test headers
20  * because it will change what they provide.
21  * Among the those, there is a main() function and some wrapping catching
22  * unhandled exceptions and considering them test failures, and probably more.
23  * This also makes fairly complicate to receive parameters from the command line
24  * (for example, a random seed).
25  */
26 #define BOOST_TEST_MODULE ( trajectorypointflags_test )
27 #include "boost/test/unit_test.hpp"
28 
29 // LArSoft libraries
31 
32 // C/C++ standard library
33 #include <set>
34 #include <iostream> // std::cout
35 
36 
37 //------------------------------------------------------------------------------
38 //--- Test code
39 //
41  recob::TrajectoryPointFlags const& flags,
42  std::set<util::flags::Index_t> const& expectedDefined,
43  std::set<util::flags::Index_t> const& expectedSet
44 ) {
45 
46  using trkflag = recob::TrajectoryPointFlags::flag;
47 
48  /*
49  * FlagIndex_t nFlags() const
50  */
51  BOOST_TEST(flags.nFlags() == trkflag::maxFlags());
52 
53  for(
55  i <= trkflag::maxFlags();
56  ++i)
57  {
58  bool const allocated = (i < trkflag::maxFlags());
59  bool const defined = (expectedDefined.count(i) > 0);
60  bool const set = (expectedSet.count(i) > 0);
61 
62  BOOST_TEST_MESSAGE("Testing flag #" << i);
63 
64  /*
65  * constexpr bool isAllocated(FlagIndex_t flag) const
66  */
67  BOOST_TEST(flags.isAllocated(i) == allocated);
68 
69  // for unallocated flag indices, most of the flag interface has
70  // undefined behaviour
71  if (!allocated) {
72  /*
73  * bool test(FlagIndex_t flag) const
74  */
75  BOOST_CHECK_THROW
77 
78  continue;
79  } // if not allocated
80 
81  /*
82  * constexpr bool isFlag(FlagIndex_t flag) const
83  */
84  BOOST_TEST(flags.isFlag(i) == allocated);
85 
86  /*
87  * constexpr bool isDefined(FlagIndex_t flag) const
88  */
89  BOOST_TEST(flags.isDefined(i) == defined);
90 
91  /*
92  * bool test(FlagIndex_t flag) const
93  */
94  if (!defined) {
95  BOOST_CHECK_THROW(
97  );
98  continue;
99  }
100  BOOST_TEST(flags.test(i) == set);
101 
102  /*
103  * get(FlagIndex_t flag) const
104  */
105  BOOST_TEST(flags.get(i) == set);
106 
107  /*
108  * bool isSet(FlagIndex_t flag) const
109  */
110  BOOST_TEST(flags.isSet(i) == set);
111 
112  /*
113  * bool isUnset(FlagIndex_t flag) const
114  */
115  BOOST_TEST(flags.isUnset(i) == !set);
116 
117  } // for
118 
119 } // CheckFlagsByIndex()
120 
121 
122 //------------------------------------------------------------------------------
124 
125  constexpr auto InvalidHitIndex = recob::TrajectoryPointFlags::InvalidHitIndex;
126 
127  /*
128  * constexpr TrajectoryPointFlags()
129  */
130  constexpr recob::TrajectoryPointFlags flags;
131 
132  // we build our expectation based on the following mask:
133  constexpr auto expectedMask = recob::TrajectoryPointFlags::DefaultFlagsMask();
134  std::set<util::flags::Index_t> expectedDefined, expectedSet;
135  for (unsigned int i = 0; i < expectedMask.capacity(); ++i) {
136  if (expectedMask.isUndefined(i)) continue;
137  expectedDefined.insert(i);
138  if (expectedMask.isSet(i)) expectedSet.insert(i);
139  } // for
140 
141  /*
142  * void dump(...) const;
143  */
144  for (unsigned int level = 0; level < 2; ++level) {
145  std::cout << "Default-constructed flags, dump(verbosity=" << level << "):"
146  << std::endl;
147  flags.dump(std::cout, level, " ");
148  std::cout << "\n" << std::endl;
149  } // for
150 
151 
152  BOOST_TEST_MESSAGE("Flag check for default constructed object");
153  CheckFlagsByIndex(flags, expectedDefined, expectedSet);
154 
155  /*
156  * Mask_t mask() const
157  */
158  BOOST_TEST
160 
161  /*
162  * bool isHitIgnored() const
163  */
164  BOOST_TEST(!flags.isHitIgnored());
165 
166  /*
167  * bool isPointValid() const
168  */
169  BOOST_TEST(flags.isPointValid());
170 
171  /*
172  * bool isMerged() const
173  */
174  BOOST_TEST(!flags.isMerged());
175 
176  /*
177  * bool isShared() const
178  */
179  BOOST_TEST(!flags.isShared());
180 
181  /*
182  * bool isDeltaRay() const
183  */
184  BOOST_TEST(!flags.isDeltaRay());
185 
186  /*
187  * bool hasDetectorIssues() const
188  */
189  BOOST_TEST(!flags.hasDetectorIssues());
190 
191  /*
192  * bool isOtherwiseSuspicious() const
193  */
194  BOOST_TEST(!flags.isOtherwiseSuspicious());
195 
196  /*
197  * bool isExclusive() const
198  */
199  BOOST_TEST(flags.isExclusive());
200 
201  /*
202  * bool isExcludedFromFit() const
203  */
204  BOOST_TEST(!flags.isExcludedFromFit());
205 
206  /*
207  * bool belongsToTrack() const
208  */
209  BOOST_TEST(flags.belongsToTrack());
210 
211  /*
212  * bool isHitReinterpreted() const
213  */
214  BOOST_TEST(!flags.isHitReinterpreted());
215 
216  /*
217  * bool isIncludedInFit() const
218  */
219  BOOST_TEST(flags.isIncludedInFit());
220 
221  /*
222  * bool isPointFlawed() const
223  */
224  BOOST_TEST(!flags.isPointFlawed());
225 
226  /*
227  * bool isPointFlawless() const
228  */
229  BOOST_TEST(flags.isPointFlawless());
230 
231  /*
232  * hasOriginalHitIndex() const
233  */
234  BOOST_TEST(!flags.hasOriginalHitIndex());
235 
236  /*
237  * HitIndex_t fromHit() const
238  */
239  BOOST_TEST(flags.fromHit() == InvalidHitIndex);
240 
241 } // TrajectoryPointFlagsTest_DefaultConstructor()
242 
243 
244 //------------------------------------------------------------------------------
246 
247  using trkflag = recob::TrajectoryPointFlags::flag;
248  constexpr auto InvalidHitIndex = recob::TrajectoryPointFlags::InvalidHitIndex;
249 
250  std::set<recob::TrajectoryPointFlags::Flag_t> const expectedBits
251  = { trkflag::NoPoint, trkflag::HitIgnored };
252 
253  constexpr auto flagbitmask = recob::TrajectoryPointFlags::makeMask
254  (trkflag::NoPoint, trkflag::HitIgnored);
255 
256  /*
257  * constexpr TrajectoryPointFlags(HitIndex_t fromHit, Flags... flags)
258  */
259  constexpr recob::TrajectoryPointFlags flags
260  (InvalidHitIndex, trkflag::NoPoint, trkflag::HitIgnored);
261 
262  std::set<util::flags::Index_t> expectedDefined;
263  expectedDefined.insert(trkflag::NoPoint.index());
264  expectedDefined.insert(trkflag::HitIgnored.index());
265  std::set<util::flags::Index_t> expectedSet = expectedDefined;
266 
267  /*
268  * void dump(...) const;
269  */
270  for (unsigned int level = 0; level < 2; ++level) {
271  std::cout << "Flag-constructed flags, dump(verbosity=" << level << "):"
272  << std::endl;
273  flags.dump(std::cout, level, " ");
274  std::cout << "\n" << std::endl;
275  } // for
276 
277  BOOST_TEST_MESSAGE("Flag check for flag-constructed object");
278  CheckFlagsByIndex(flags, expectedDefined, expectedSet);
279 
280 
281  /*
282  * Mask_t mask() const
283  */
284  BOOST_TEST(flags.mask() == flagbitmask);
285 
286  /*
287  * hasOriginalHitIndex() const
288  */
289  BOOST_TEST(!flags.hasOriginalHitIndex());
290 
291  /*
292  * HitIndex_t fromHit() const
293  */
294  BOOST_TEST(flags.fromHit() == InvalidHitIndex);
295 
296 
297 } // TrajectoryPointFlagsTest_FlagsConstructor()
298 
299 
300 //------------------------------------------------------------------------------
302 
303  using trkflag = recob::TrajectoryPointFlags::flag;
304 
306 
307  constexpr auto flagbitmask = recob::TrajectoryPointFlags::makeMask(
308  trkflag::NoPoint, trkflag::Rejected,
309  util::flags::Unset(trkflag::HitIgnored)
310  );
311 
312  std::set<util::flags::Index_t> expectedSet;
313  expectedSet.insert(trkflag::NoPoint.index());
314  expectedSet.insert(trkflag::Rejected.index());
315  std::set<util::flags::Index_t> expectedDefined = expectedSet;
316  expectedDefined.insert(trkflag::HitIgnored.index());
317 
318  std::set<Flag_t> const expectedValues(expectedSet.begin(), expectedSet.end());
319  std::set<Flag_t> const expectedFlags
320  (expectedDefined.begin(), expectedDefined.end());
321 
322  /*
323  * constexpr TrajectoryPointFlags(FromMaskTag_t, HitIndex_t, Mask_t)
324  */
325  constexpr recob::TrajectoryPointFlags flags(12, flagbitmask);
326 
327  BOOST_TEST(flags == recob::TrajectoryPointFlags(12,
328  trkflag::NoPoint + trkflag::Rejected + -trkflag::HitIgnored));
329 /*
330  static_assert(flags == recob::TrajectoryPointFlags(12,
331  trkflag::NoPoint + trkflag::Rejected + -trkflag::HitIgnored),
332  "Constexpr declaration with mask failed.");
333  */
334 
335  /*
336  * void dump(...) const;
337  */
338  for (unsigned int level = 0; level < 2; ++level) {
339  std::cout << "Bitmask-constructed flags, dump(verbosity=" << level << "):"
340  << std::endl;
341  flags.dump(std::cout, level, " ");
342  std::cout << "\n" << std::endl;
343  } // for
344 
345 
346  BOOST_TEST_MESSAGE("Flag check for bitmask-constructed object");
347  CheckFlagsByIndex(flags, expectedDefined, expectedSet);
348 
349 
350  /*
351  * Mask_t mask() const
352  */
353  BOOST_TEST(flags.mask() == flagbitmask);
354 
355  /*
356  * bool isHitIgnored() const
357  */
358  BOOST_TEST
359  (flags.isHitIgnored() == (expectedValues.count(trkflag::HitIgnored) > 0));
360 
361  /*
362  * bool isPointValid() const
363  */
364  BOOST_TEST
365  (flags.isPointValid() == (expectedValues.count(trkflag::NoPoint) == 0));
366 
367  /*
368  * hasOriginalHitIndex() const
369  */
370  BOOST_TEST(flags.hasOriginalHitIndex());
371 
372  /*
373  * HitIndex_t fromHit() const
374  */
375  BOOST_TEST(flags.fromHit() == 12U);
376 
377 
378 } // TrajectoryPointFlagsTest_BitmaskConstructor()
379 
380 
381 //------------------------------------------------------------------------------
383 {
384 
385  /*
386  * constexpr TrajectoryPointFlags(FromMaskTag_t, HitIndex_t, Mask_t)
387  */
388  using trkflag = recob::TrajectoryPointFlags::flag;
389  constexpr recob::TrajectoryPointFlags flags [[gnu::unused]](
390  12,
392  trkflag::NoPoint,
393  trkflag::HitIgnored
394  )
395  );
396 } // TrajectoryPointFlagsDocumentationTest_TrajectoryPointFlags_FromMaskTag_t()
397 
399  ()
400 {
401 
402  /*
403  * constexpr TrajectoryPointFlags(HitIndex_t fromHit, Flags... flags)
404  */
405  using trkflag = recob::TrajectoryPointFlags::flag;
406  constexpr recob::TrajectoryPointFlags flags [[gnu::unused]](
408  trkflag::NoPoint,
409  trkflag::Merged
410  );
411 
412 } // TrajectoryPointFlagsDocumentationTest_TrajectoryPointFlags_HitIndex_t_Flags
413 
415  /*
416  * Mask_t makeMask(Flags... flags)
417  */
418 
419  using trkflag = recob::TrajectoryPointFlags::flag;
420  constexpr auto mask [[gnu::unused]] = recob::TrajectoryPointFlags::makeMask
421  (trkflag::NoPoint, trkflag::Merged);
422 } // TrajectoryPointFlagsDocumentationTest_makeMask()
423 
424 
426  // This test is meant to check that the code in the documentation,
427  // which is copied here, is compilable.
431 
432 } // TrajectoryPointFlagsTest()
433 
434 
435 //------------------------------------------------------------------------------
436 //--- registration of tests
437 //
438 // Boost needs now to know which tests we want to run.
439 // Tests are "automatically" registered, hence the BOOST_AUTO_TEST_CASE()
440 // macro name. The argument is the name of the test; each step may have a
441 // number of checks and it will fail if any of them does.
442 //
443 
444 BOOST_AUTO_TEST_CASE(TrajectoryPointFlagsTestCase) {
445 
449 
450 } // BOOST_AUTO_TEST_CASE(TrajectoryPointFlagsTestCase)
451 
452 
453 BOOST_AUTO_TEST_CASE(TrajectoryPointFlagsDocumentationTestCase) {
454 
456 
457 } // BOOST_AUTO_TEST_CASE(TrajectoryPointFlagsDocumentationTestCase)
constexpr bool isFlag(FlagIndex_t flagIndex) const
void TrajectoryPointFlagsTest_DefaultConstructor()
TrajectoryPointFlagTraits flag
Type of flag traits (indices and meaning of flags).
static constexpr Mask_t makeMask(Flags...flags)
Returns a bit mask with only the specified bit set.
bool isSet(Flag_t flag) const
Returns true if the flag exists and is set.
static constexpr Mask_t DefaultFlagsMask()
Flags used in default construction.
typename Mask_t::OutOfRangeError OutOfRangeError
Out-of-range flag index.
Definition: FlagSet.h:70
void TrajectoryPointFlagsDocumentationTest_makeMask()
bool test(FlagIndex_t index) const
Returns whether the specified flag is set.
void TrajectoryPointFlagsDocumentationTest()
typename Mask_t::FlagNotDefinedError FlagNotDefinedError
Flag not defined.
Definition: FlagSet.h:73
void TrajectoryPointFlagsTest_FlagsConstructor()
bool get(Flag_t flag) const
Returns whether the specified flag is set.
void TrajectoryPointFlagsDocumentationTest_TrajectoryPointFlags_FromMaskTag_t()
static constexpr HitIndex_t InvalidHitIndex
Value marking an invalid hit index.
Set of flags pertaining a point of the track.
Flags_t::Flag_t Flag_t
Type of single flag.
void TrajectoryPointFlagsDocumentationTest_TrajectoryPointFlags_HitIndex_t_Flags()
bool isUnset(Flag_t flag) const
Returns true if the flag exists and is not set.
BOOST_AUTO_TEST_CASE(TrajectoryPointFlagsTestCase)
constexpr bool isAllocated(FlagIndex_t flagIndex) const
Returns whether there is room for a flag with the specified index.
bool isDefined(Flag_t flag) const
Returns true if the flag has been assigned a value.
Flags_t::FlagIndex_t FlagIndex_t
Type of index of single flag.
void CheckFlagsByIndex(recob::TrajectoryPointFlags const &flags, std::set< util::flags::Index_t > const &expectedDefined, std::set< util::flags::Index_t > const &expectedSet)
void TrajectoryPointFlagsTest_BitmaskConstructor()
Set of flags pertaining a point of the track.
constexpr BitMask< Storage > Unset(Flag_t< Storage > flag)
Returns a bit mask which unsets the specified flag.
QTextStream & endl(QTextStream &s)
constexpr FlagIndex_t nFlags() const
Returns the number of defined flags.