Macros | Functions
associated_groups_with_left_test.cc File Reference

Unit test for util::associated_groups_with_left(). More...

#include "lardata/Utilities/ForEachAssociatedGroup.h"
#include "larcorealg/CoreUtils/enumerate.h"
#include "canvas/Persistency/Provenance/ProductID.h"
#include "canvas/Persistency/Common/Ptr.h"
#include "canvas/Persistency/Common/Assns.h"
#include "boost/test/unit_test.hpp"
#include <array>
#include <iostream>
#include "TROOT.h"
#include "TInterpreter.h"
#include "TClassEdit.h"
#include <string>
#include <typeinfo>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )
 

Functions

template<typename T >
TClass * QuickGenerateTClass ()
 
void associated_groups_with_left_test ()
 
 BOOST_AUTO_TEST_CASE (AssociatedGroupsWithLeftTestCase)
 

Detailed Description

Unit test for util::associated_groups_with_left().

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.slac..nosp@m.stan.nosp@m.ford..nosp@m.edu)
Date
January 14, 2021

Definition in file associated_groups_with_left_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )

Definition at line 19 of file associated_groups_with_left_test.cc.

Function Documentation

void associated_groups_with_left_test ( )

Definition at line 61 of file associated_groups_with_left_test.cc.

61  {
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()
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
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
def move(depos, offset)
Definition: depos.py:107
#define A
Definition: memgrp.cpp:38
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( AssociatedGroupsWithLeftTestCase  )

Definition at line 123 of file associated_groups_with_left_test.cc.

123  {
125 } // AssociatedGroupsWithLeftTestCase
void associated_groups_with_left_test()
template<typename T >
TClass* QuickGenerateTClass ( )

Definition at line 38 of file associated_groups_with_left_test.cc.

38  {
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()
std::string string
Definition: nybbler.cc:12
void err(const char *fmt,...)
Definition: message.cpp:226