associated_groups_test.cc
Go to the documentation of this file.
1 /**
2  * @file associated_groups_test.cc
3  * @brief Unit test for `util::associated_groups()`.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date February 7th, 2017
6  *
7  */
8 
9 // LArSoft libraries
11 
12 // framework libraries
16 
17 // Boost libraries
18 #define BOOST_TEST_MODULE ( PointIsolationAlg_test )
19 #include "boost/test/unit_test.hpp"
20 
21 // C/C++ standard libraries
22 #include <array>
23 #include <iostream>
24 
25 
26 //------------------------------------------------------------------------------
27 // ROOT libraries
28 #include "TROOT.h" // gROOT
29 #include "TInterpreter.h"
30 #include "TClassEdit.h"
31 
32 // C/C++ standard libraries
33 #include <string>
34 #include <typeinfo>
35 
36 template <typename T>
38 
39  // magic! this interpreter call is needed before GetNormalizedName() is called
40  TInterpreter* interpreter = gROOT->GetInterpreter();
41 
42  // demangle name of type T
43  int err; // we'll ignore errors
44  char* classNameC = TClassEdit::DemangleTypeIdName(typeid(T), err);
45 
46  // "normalise" it
47  std::string normalizedClassName;
48  TClassEdit::GetNormalizedName(normalizedClassName, classNameC);
49 
50  // clean up
51  std::free(classNameC);
52 
53  // generate and register the TClass; load it and be silent.
54  return interpreter->GenerateTClass(normalizedClassName.c_str(), kTRUE, kTRUE);
55 
56 } // QuickGenerateTClass()
57 
58 
59 //------------------------------------------------------------------------------
61 
62  // types used in the association (they actually do not matter)
63  struct TypeA {};
64  struct TypeB {};
65 
66  using MyAssns_t = art::Assns<TypeA, TypeB>;
67 
68  // art::Assns constructor tries to have ROOT initialise its streamer, which
69  // requires a TClass instance which is not present at this time.
70  // This trick creates that TClass.
71  QuickGenerateTClass<MyAssns_t>();
72 
74 
75  // association description: B's for each A
76  std::array<std::pair<Index_t, std::vector<Index_t>>, 3U> expected;
77  expected[0] = { 0, { 0, 3, 6 } };
78  expected[1] = { 1, { 2, 4, 6 } };
79  expected[2] = { 3, { 8, 10, 12, 13 } };
80  art::ProductID aPID{ 5 }, bPID{ 12 };
81 
82  // fill the association
83  MyAssns_t assns;
84  for (auto const& pair: expected) {
85  auto const& aIndex = pair.first;
86  for (auto const& bIndex: pair.second) {
87  assns.addSingle({ aPID, aIndex, nullptr }, { bPID, bIndex, nullptr });
88  } // for bIndex
89  } // for pair
90 
91  std::vector<std::vector<Index_t>> results;
92  for (auto Bs: util::associated_groups(assns)) {
93  std::cout << "Association group #" << results.size() << ":" << std::endl;
94  results.emplace_back();
95  std::vector<Index_t>& myBs = results.back();
96  for(art::Ptr<TypeB> const& B: Bs) {
97  std::cout << " " << B << std::endl;
98  myBs.push_back(B.key());
99  }
100  } // for associated groups
101 
102  //strings should be same as vs
103  std::cout << "Starting check..." << std::endl;
104  BOOST_TEST(results.size() == expected.size());
105  for (size_t i = 0; i < results.size(); ++i) {
106  auto const& Bs = results[i];
107  auto const& expectedBs = expected[i].second;
108  BOOST_TEST_MESSAGE(" element #" << i << ", A=" << expected[i].first);
109  BOOST_TEST(Bs.size() == expectedBs.size());
110  for (size_t j = 0; j < expectedBs.size(); ++j) {
111  BOOST_TEST_MESSAGE(" assn #" << j);
112  BOOST_TEST(Bs[j] == expectedBs[j]);
113  } // for j
114  } // for results
115 
116 } // associated_groups_test()
117 
118 
119 //------------------------------------------------------------------------------
120 //--- tests
121 //
122 BOOST_AUTO_TEST_CASE(AssociatedGroupsTestCase) {
124 } // AssociatedGroupsTestCase
BOOST_AUTO_TEST_CASE(AssociatedGroupsTestCase)
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
std::size_t key_type
Definition: Ptr.h:79
unsigned int Index_t
Type to denote the index of the flag.
Definition: BitMask.h:60
auto associated_groups(A const &assns)
Helper functions to access associations in order.
void AssociatedGroupsTest()
void err(const char *fmt,...)
Definition: message.cpp:226
Helper functions to access associations in order.
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)
TClass * QuickGenerateTClass()