Macros | Functions
associated_groups_test.cc File Reference

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

#include "lardata/Utilities/ForEachAssociatedGroup.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 AssociatedGroupsTest ()
 
 BOOST_AUTO_TEST_CASE (AssociatedGroupsTestCase)
 

Detailed Description

Unit test for util::associated_groups().

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
February 7th, 2017

Definition in file associated_groups_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )

Definition at line 18 of file associated_groups_test.cc.

Function Documentation

void AssociatedGroupsTest ( )

Definition at line 60 of file associated_groups_test.cc.

60  {
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()
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 associated_groups(A const &assns)
Helper functions to access associations in order.
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( AssociatedGroupsTestCase  )

Definition at line 122 of file associated_groups_test.cc.

122  {
124 } // AssociatedGroupsTestCase
void AssociatedGroupsTest()
template<typename T >
TClass* QuickGenerateTClass ( )

Definition at line 37 of file associated_groups_test.cc.

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