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
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 
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
void associated_groups_with_left_test()
auto associated_groups_with_left(A const &assns)
Helper functions to access associations in order, also with key.
static QCString result
const char expected[]
Definition: Exception_t.cc:22
Definition of util::enumerate().
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 enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
TClass * QuickGenerateTClass()
def move(depos, offset)
Definition: depos.py:107
void err(const char *fmt,...)
Definition: message.cpp:226
BOOST_AUTO_TEST_CASE(AssociatedGroupsWithLeftTestCase)
Helper functions to access associations in order.
#define A
Definition: memgrp.cpp:38
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)