All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
associated_groups_with_left_test.cc
Go to the documentation of this file.
1 /**
2  * @file associated_groups_with_left_test.cc
3  * @brief Unit test for `util::associated_groups_with_left()`.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date January 14, 2021
6  *
7  */
8 
9 // LArSoft libraries
12 
13 // framework libraries
14 #include "canvas/Persistency/Provenance/ProductID.h"
15 #include "canvas/Persistency/Common/Ptr.h"
16 #include "canvas/Persistency/Common/Assns.h"
17 
18 // Boost libraries
19 #define BOOST_TEST_MODULE ( PointIsolationAlg_test )
20 #include "boost/test/unit_test.hpp"
21 
22 // C/C++ standard libraries
23 #include <array>
24 #include <iostream>
25 
26 
27 //------------------------------------------------------------------------------
28 // ROOT libraries
29 #include "TROOT.h" // gROOT
30 #include "TInterpreter.h"
31 #include "TClassEdit.h"
32 
33 // C/C++ standard libraries
34 #include <string>
35 #include <typeinfo>
36 
37 template <typename T>
39 
40  // magic! this interpreter call is needed before GetNormalizedName() is called
41  TInterpreter* interpreter = gROOT->GetInterpreter();
42 
43  // demangle name of type T
44  int err; // we'll ignore errors
45  char* classNameC = TClassEdit::DemangleTypeIdName(typeid(T), err);
46 
47  // "normalise" it
48  std::string normalizedClassName;
49  TClassEdit::GetNormalizedName(normalizedClassName, classNameC);
50 
51  // clean up
52  std::free(classNameC);
53 
54  // generate and register the TClass; load it and be silent.
55  return interpreter->GenerateTClass(normalizedClassName.c_str(), kTRUE, kTRUE);
56 
57 } // QuickGenerateTClass()
58 
59 
60 //------------------------------------------------------------------------------
62 
63  // types used in the association (they actually do not matter)
64  struct TypeA {};
65  struct TypeB {};
66 
67  using MyAssns_t = art::Assns<TypeA, TypeB>;
68 
69  // art::Assns constructor tries to have ROOT initialise its streamer, which
70  // requires a TClass instance which is not present at this time.
71  // This trick creates that TClass.
72  QuickGenerateTClass<MyAssns_t>();
73 
74  using Index_t = art::Ptr<TypeA>::key_type;
75 
76  // association description: B's for each A
77  std::array<std::pair<Index_t, std::vector<Index_t>>, 3U> expected;
78  expected[0] = { 0, { 0, 3, 6 } };
79  expected[1] = { 1, { 2, 4, 6 } };
80  expected[2] = { 3, { 8, 10, 12, 13 } };
81  art::ProductID aPID{ 5 }, bPID{ 12 };
82 
83  // fill the association
84  MyAssns_t assns;
85  for (auto const& pair: expected) {
86  auto const& aIndex = pair.first;
87  for (auto const& bIndex: pair.second) {
88  assns.addSingle({ aPID, aIndex, nullptr }, { bPID, bIndex, nullptr });
89  } // for bIndex
90  } // for pair
91 
92  std::vector<std::pair<Index_t, std::vector<Index_t>>> results;
93  for (auto const& [ A, Bs ]: util::associated_groups_with_left(assns)) {
94  std::cout << "Association group #" << results.size() << ":" << std::endl;
95  std::vector<Index_t> myBs;
96  myBs.reserve(Bs.size());
97  for(art::Ptr<TypeB> const& B: Bs) {
98  std::cout << " " << B << std::endl;
99  myBs.push_back(B.key());
100  }
101  results.emplace_back(A.key(), std::move(myBs));
102  } // for associated groups
103 
104  //strings should be same as vs
105  std::cout << "Starting check..." << std::endl;
106  BOOST_TEST(results.size() == expected.size());
107  for (auto const& [ i, result, expected ]: util::enumerate(results, expected))
108  {
109  auto const& [ A, Bs ] = result;
110  auto const& [ expectedA, expectedBs ] = expected;
111  BOOST_TEST_MESSAGE(" element #" << i << ", A=" << expectedA);
112  BOOST_TEST(A == expectedA);
113  BOOST_CHECK_EQUAL_COLLECTIONS
114  (Bs.cbegin(), Bs.cend(), expectedBs.cbegin(), expectedBs.cend());
115  } // for results
116 
117 } // associated_groups_with_left_test()
118 
119 
120 //------------------------------------------------------------------------------
121 //--- tests
122 //
123 BOOST_AUTO_TEST_CASE(AssociatedGroupsWithLeftTestCase) {
125 } // AssociatedGroupsWithLeftTestCase
BOOST_AUTO_TEST_CASE(AllTests)
void associated_groups_with_left_test()
auto associated_groups_with_left(A const &assns)
Helper functions to access associations in order, also with key.
Definition of util::enumerate().
EResult err(const char *call)
unsigned int Index_t
Type to denote the index of the flag.
Definition: BitMask.h:60
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
Helper functions to access associations in order.
float A
Definition: dedx.py:137
BEGIN_PROLOG could also be cout
TClass * QuickGenerateTClass()