Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:06

0001 
0002 #include "DataFormats/Common/interface/PtrVector.h"
0003 #include "DataFormats/Common/interface/TestHandle.h"
0004 #include "DataFormats/Common/interface/Wrapper.h"
0005 #include "DataFormats/Common/interface/IntValues.h"
0006 
0007 #include "cppunit/extensions/HelperMacros.h"
0008 
0009 #include <algorithm>
0010 #include <iostream>
0011 #include <vector>
0012 
0013 class testPtrVector : public CppUnit::TestFixture {
0014   CPPUNIT_TEST_SUITE(testPtrVector);
0015   CPPUNIT_TEST(check);
0016   CPPUNIT_TEST(get);
0017   CPPUNIT_TEST_SUITE_END();
0018 
0019 public:
0020   void setUp() {}
0021   void tearDown() {}
0022   void check();
0023   void get();
0024 };
0025 
0026 CPPUNIT_TEST_SUITE_REGISTRATION(testPtrVector);
0027 
0028 namespace testPtr {
0029   struct Base {
0030     virtual ~Base() {}
0031     virtual int val() const = 0;
0032     bool operator==(Base const& other) const { return val() == other.val(); }
0033   };
0034 
0035   struct Inherit1 : public Base {
0036     virtual int val() const { return 1; }
0037   };
0038   struct Inherit2 : public Base {
0039     virtual int val() const { return 2; }
0040   };
0041 
0042   struct TestGetter : public edm::EDProductGetter {
0043     edm::WrapperBase const* hold_;
0044     edm::WrapperBase const* getIt(edm::ProductID const&) const override { return hold_; }
0045     std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(edm::ProductID const&,
0046                                                                                        unsigned int) const override {
0047       return std::nullopt;
0048     }
0049 
0050     void getThinnedProducts(edm::ProductID const& pid,
0051                             std::vector<edm::WrapperBase const*>& wrappers,
0052                             std::vector<unsigned int>& keys) const override {}
0053 
0054     edm::OptionalThinnedKey getThinnedKeyFrom(edm::ProductID const&,
0055                                               unsigned int,
0056                                               edm::ProductID const&) const override {
0057       return std::monostate{};
0058     }
0059 
0060     unsigned int transitionIndex_() const override { return 0U; }
0061 
0062     TestGetter() : hold_() {}
0063   };
0064 }  // namespace testPtr
0065 
0066 using namespace testPtr;
0067 
0068 void do_some_tests(edm::PtrVector<Base> const& x) {
0069   edm::PtrVector<Base> copy(x);
0070 
0071   CPPUNIT_ASSERT(x.empty() == copy.empty());
0072   CPPUNIT_ASSERT(x.size() == copy.size());
0073   edm::PtrVector<Base>::const_iterator b = x.begin(), e = x.end(), cb = copy.begin(), ce = copy.end();
0074   CPPUNIT_ASSERT(e - b == ce - cb);
0075   CPPUNIT_ASSERT(std::distance(b, e) == std::distance(cb, ce));
0076 }
0077 
0078 void testPtrVector::check() {
0079   using namespace edm;
0080 
0081   std::vector<Inherit1> v1(2, Inherit1());
0082   std::vector<Inherit2> v2(2, Inherit2());
0083 
0084   TestHandle<std::vector<Inherit1>> h1(&v1, ProductID(1, 1));
0085   PtrVector<Inherit1> rv1;
0086   rv1.push_back(Ptr<Inherit1>(h1, 0));
0087   rv1.push_back(Ptr<Inherit1>(h1, 1));
0088   TestHandle<std::vector<Inherit2>> h2(&v2, ProductID(1, 2));
0089   PtrVector<Inherit2> rv2;
0090   rv2.push_back(Ptr<Inherit2>(h2, 0));
0091   rv2.push_back(Ptr<Inherit2>(h2, 1));
0092 
0093   PtrVector<Base> empty;
0094   PtrVector<Base> copy_of_empty(empty);
0095 
0096   CPPUNIT_ASSERT(empty == copy_of_empty);
0097 
0098   PtrVector<Base> bv1(rv1);
0099   Ptr<Base> r1_0 = bv1[0];
0100   Ptr<Base> r1_1 = bv1[1];
0101   PtrVector<Base> bv2(rv2);
0102   Ptr<Base> r2_0 = bv2[0];
0103   Ptr<Base> r2_1 = bv2[1];
0104 
0105   CPPUNIT_ASSERT(bv1.empty() == false);
0106   CPPUNIT_ASSERT(bv1.size() == 2);
0107   CPPUNIT_ASSERT(bv2.size() == 2);
0108   CPPUNIT_ASSERT(r1_0->val() == 1);
0109   CPPUNIT_ASSERT(r1_1->val() == 1);
0110   CPPUNIT_ASSERT(r2_0->val() == 2);
0111   CPPUNIT_ASSERT(r2_1->val() == 2);
0112 
0113   PtrVector<Base>::const_iterator b = bv1.begin(), e = bv1.end();
0114   PtrVector<Base>::const_iterator i = b;
0115   CPPUNIT_ASSERT((*i)->val() == 1);
0116   CPPUNIT_ASSERT(i != e);
0117   CPPUNIT_ASSERT(i - b == 0);
0118   ++i;
0119   CPPUNIT_ASSERT((*i)->val() == 1);
0120   CPPUNIT_ASSERT(i != e);
0121   CPPUNIT_ASSERT(i - b == 1);
0122   ++i;
0123   CPPUNIT_ASSERT(i == e);
0124 
0125   PtrVector<Base> assigned_from_bv1;
0126   do_some_tests(assigned_from_bv1);
0127   CPPUNIT_ASSERT(assigned_from_bv1.empty());
0128   assigned_from_bv1 = bv1;
0129   CPPUNIT_ASSERT(assigned_from_bv1.size() == bv1.size());
0130   CPPUNIT_ASSERT(std::equal(bv1.begin(), bv1.end(), assigned_from_bv1.begin()));
0131   CPPUNIT_ASSERT(assigned_from_bv1 == bv1);
0132 
0133   do_some_tests(assigned_from_bv1);
0134 
0135   /// creation of empty vector adding with push_back
0136   PtrVector<Base> bv3;
0137   bv3.push_back(r1_0);
0138   CPPUNIT_ASSERT(bv3.size() == 1);
0139   CPPUNIT_ASSERT(&(*r1_0) == &(*bv3[0]));
0140   bv3.push_back(r1_1);
0141   CPPUNIT_ASSERT(bv3.size() == 2);
0142   CPPUNIT_ASSERT(&(*r1_1) == &(*bv3[1]));
0143 
0144   /// clearing, then pushing in Ptr with other product ID
0145   bv3.clear();
0146   CPPUNIT_ASSERT(bv3.size() == 0);
0147   bv3.push_back(r2_0);
0148   CPPUNIT_ASSERT(bv3.size() == 1);
0149 }
0150 
0151 void testPtrVector::get() {
0152   using namespace test_with_dictionaries;
0153   typedef std::vector<IntValue> IntCollection;
0154   auto ptr = std::make_unique<IntCollection>();
0155 
0156   ptr->push_back(0);
0157   ptr->push_back(1);
0158   ptr->push_back(2);
0159   ptr->push_back(3);
0160 
0161   edm::Wrapper<IntCollection> wrapper(std::move(ptr));
0162   TestGetter tester;
0163   tester.hold_ = &wrapper;
0164 
0165   edm::ProductID const pid(1, 1);
0166 
0167   IntCollection const* wptr = dynamic_cast<IntCollection const*>(wrapper.product());
0168 
0169   edm::PtrVector<IntValue> iVec;
0170   std::cerr << "pre push_backs" << std::endl;
0171   iVec.push_back(edm::Ptr<IntValue>(pid, 0, &tester));
0172   iVec.push_back(edm::Ptr<IntValue>(pid, 2, &tester));
0173   std::cerr << "post push_backs" << std::endl;
0174 
0175   iVec.setProductGetter(&tester);
0176 
0177   std::cerr << "pre size" << std::endl;
0178   CPPUNIT_ASSERT(iVec.size() == 2);
0179   std::cerr << "pre iterator diff" << std::endl;
0180   CPPUNIT_ASSERT(static_cast<size_t>(iVec.end() - iVec.begin()) == iVec.size());
0181   std::cerr << "address compare" << std::endl;
0182   CPPUNIT_ASSERT(&(*(*(iVec.begin()))) == &(*(wptr->begin())));
0183   std::cerr << "pre value deref" << std::endl;
0184   CPPUNIT_ASSERT((*(*(iVec.begin()))).value_ == 0);
0185   std::cerr << "pre value ->" << std::endl;
0186   CPPUNIT_ASSERT((*(iVec.begin()))->value_ == 0);
0187   std::cerr << "post everything" << std::endl;
0188 
0189   /*
0190   typedef std::vector<Inherit1> I1Collection;
0191   auto ptr = std::make_unique<I1Collection>();
0192 
0193   ptr->push_back(0);
0194   ptr->push_back(1);
0195 
0196   edm::Wrapper<I1Collection> wrapper(ptr);
0197   TestGetter tester;
0198   tester.hold_ = &wrapper;
0199   */
0200 }