Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  *  ptr_t.cppunit.cc
0003  *  CMSSW
0004  *
0005  *  Created by Chris Jones on 02/11/05.
0006  *
0007  */
0008 
0009 #include "cppunit/extensions/HelperMacros.h"
0010 #include <vector>
0011 #include <set>
0012 #include <list>
0013 #include <deque>
0014 #include "DataFormats/Common/interface/Ptr.h"
0015 
0016 #include "DataFormats/Common/interface/EDProductGetter.h"
0017 #include "DataFormats/Common/interface/OrphanHandle.h"
0018 #include "DataFormats/Common/interface/Wrapper.h"
0019 
0020 #include <iostream>
0021 
0022 #include "DataFormats/Common/interface/IntValues.h"
0023 #include <typeinfo>
0024 
0025 using namespace edm;
0026 using namespace test_with_dictionaries;
0027 
0028 class testPtr : public CppUnit::TestFixture {
0029   CPPUNIT_TEST_SUITE(testPtr);
0030 
0031   CPPUNIT_TEST(constructTest);
0032   CPPUNIT_TEST(comparisonTest);
0033   CPPUNIT_TEST(getTest);
0034 
0035   CPPUNIT_TEST_SUITE_END();
0036 
0037 public:
0038   void setUp() {}
0039   void tearDown() {}
0040 
0041   void constructTest();
0042   void comparisonTest();
0043   void getTest();
0044 };
0045 
0046 ///registration of the test so that the runner can find it
0047 CPPUNIT_TEST_SUITE_REGISTRATION(testPtr);
0048 
0049 namespace {
0050   struct Dummy {
0051     Dummy() {}
0052     virtual ~Dummy() {}
0053     bool operator==(Dummy const& iRHS) const { return this == &iRHS; }
0054     bool operator<(Dummy const& iRHS) const { return this->address() < iRHS.address(); }
0055     void const* address() const { return this; }
0056   };
0057 
0058   typedef std::vector<Dummy> DummyCollection;
0059 
0060   typedef std::set<Dummy> DummySet;
0061   typedef std::list<Dummy> DummyList;
0062   typedef std::deque<Dummy> DummyDeque;
0063 
0064   struct Dummy2 : public Dummy {
0065     Dummy2() {}
0066     virtual ~Dummy2() {}
0067   };
0068 
0069   typedef std::vector<Dummy2> DummyCollection2;
0070 }  // namespace
0071 
0072 void testPtr::constructTest() {
0073   Ptr<Dummy> nulled;
0074   CPPUNIT_ASSERT(!nulled);
0075   CPPUNIT_ASSERT(nulled.isNull());
0076   CPPUNIT_ASSERT(!nulled.isNonnull());
0077   CPPUNIT_ASSERT(!nulled.isAvailable());
0078 
0079   ProductID const pid(1, 1);
0080 
0081   {
0082     unsigned int const key = 2;
0083     Dummy const dummy;
0084     DummyCollection dummyContainer;
0085     dummyContainer.push_back(dummy);
0086     dummyContainer.push_back(dummy);
0087     dummyContainer.push_back(dummy);
0088     OrphanHandle<DummyCollection> handle(&dummyContainer, pid);
0089     Ptr<Dummy> dummyPtr(handle, key);
0090 
0091     CPPUNIT_ASSERT(dummyPtr.isAvailable());
0092     CPPUNIT_ASSERT(dummyPtr.id() == pid);
0093     CPPUNIT_ASSERT(dummyPtr.key() == key);
0094     CPPUNIT_ASSERT(dummyPtr.get() == &dummyContainer[key]);
0095     CPPUNIT_ASSERT(&(*dummyPtr) == &dummyContainer[key]);
0096     CPPUNIT_ASSERT((dummyPtr.operator->()) == &dummyContainer[key]);
0097     CPPUNIT_ASSERT(dummyPtr->address() == dummyContainer[key].address());
0098 
0099     Ptr<Dummy> anotherPtr(dummyPtr.id(), dummyPtr.get(), dummyPtr.key(), dummyPtr.isTransient());
0100     CPPUNIT_ASSERT(dummyPtr.get() == anotherPtr.get() && dummyPtr.key() == anotherPtr.key() &&
0101                    dummyPtr.isTransient() == anotherPtr.isTransient() && dummyPtr.id() == anotherPtr.id());
0102 
0103     Ptr<Dummy> ptrTrans(&dummyContainer, key);
0104     Ptr<Dummy> ptrTrans2(&dummyContainer[key], key);
0105 
0106     CPPUNIT_ASSERT(ptrTrans.get() == &dummyContainer[key] && ptrTrans2.get() == &dummyContainer[key] &&
0107                    ptrTrans.id() == ProductID() && ptrTrans.id() == ptrTrans2.id() && ptrTrans.isTransient() &&
0108                    ptrTrans2.isTransient() && ptrTrans.key() == key && ptrTrans2.key() == key);
0109 
0110     Ptr<Dummy> ptrTrans3(nullptr, key);
0111     CPPUNIT_ASSERT(ptrTrans3.isNull());
0112   }
0113 
0114   {
0115     unsigned int const key = 2;
0116     Dummy const dummy;
0117     DummySet dummyContainer;
0118     dummyContainer.insert(dummy);
0119     dummyContainer.insert(dummy);
0120     dummyContainer.insert(dummy);
0121     OrphanHandle<DummySet> handle(&dummyContainer, pid);
0122     Ptr<Dummy> dummyPtr(handle, key);
0123 
0124     CPPUNIT_ASSERT(dummyPtr.isAvailable());
0125     CPPUNIT_ASSERT(dummyPtr.id() == pid);
0126     CPPUNIT_ASSERT(dummyPtr.key() == key);
0127     DummySet::const_iterator it = dummyContainer.begin();
0128     std::advance(it, key);
0129     CPPUNIT_ASSERT(dummyPtr.get() == it->address());
0130     CPPUNIT_ASSERT(&(*dummyPtr) == it->address());
0131     CPPUNIT_ASSERT((dummyPtr.operator->()) == it->address());
0132     CPPUNIT_ASSERT(dummyPtr->address() == it->address());
0133   }
0134 
0135   {
0136     unsigned int const key = 2;
0137     Dummy const dummy;
0138     DummyList dummyContainer;
0139     dummyContainer.push_back(dummy);
0140     dummyContainer.push_back(dummy);
0141     dummyContainer.push_back(dummy);
0142     OrphanHandle<DummyList> handle(&dummyContainer, pid);
0143     Ptr<Dummy> dummyPtr(handle, key);
0144 
0145     CPPUNIT_ASSERT(dummyPtr.isAvailable());
0146     CPPUNIT_ASSERT(dummyPtr.id() == pid);
0147     CPPUNIT_ASSERT(dummyPtr.key() == key);
0148     DummyList::const_iterator it = dummyContainer.begin();
0149     std::advance(it, key);
0150     CPPUNIT_ASSERT(dummyPtr.get() == it->address());
0151     CPPUNIT_ASSERT(&(*dummyPtr) == it->address());
0152     CPPUNIT_ASSERT((dummyPtr.operator->()) == it->address());
0153     CPPUNIT_ASSERT(dummyPtr->address() == it->address());
0154   }
0155 
0156   {
0157     unsigned int const key = 2;
0158     Dummy const dummy;
0159     DummyDeque dummyContainer;
0160     dummyContainer.push_back(dummy);
0161     dummyContainer.push_back(dummy);
0162     dummyContainer.push_back(dummy);
0163     OrphanHandle<DummyDeque> handle(&dummyContainer, pid);
0164     Ptr<Dummy> dummyPtr(handle, key);
0165 
0166     CPPUNIT_ASSERT(dummyPtr.isAvailable());
0167     CPPUNIT_ASSERT(dummyPtr.id() == pid);
0168     //CPPUNIT_ASSERT(dummyPtrProd.id() == pid);
0169     CPPUNIT_ASSERT(dummyPtr.key() == key);
0170     DummyDeque::const_iterator it = dummyContainer.begin();
0171     std::advance(it, key);
0172     CPPUNIT_ASSERT(dummyPtr.get() == it->address());
0173     CPPUNIT_ASSERT(&(*dummyPtr) == it->address());
0174     CPPUNIT_ASSERT((dummyPtr.operator->()) == it->address());
0175     CPPUNIT_ASSERT(dummyPtr->address() == it->address());
0176   }
0177 
0178   {
0179     unsigned int const key = 2;
0180     Dummy2 const dummy;
0181     DummyCollection2 dummyContainer;
0182     dummyContainer.push_back(dummy);
0183     dummyContainer.push_back(dummy);
0184     dummyContainer.push_back(dummy);
0185     OrphanHandle<DummyCollection2> handle(&dummyContainer, pid);
0186     Ptr<Dummy> dummyPtr(handle, key);
0187     Ptr<Dummy2> dummyPtr2(dummyPtr);
0188 
0189     CPPUNIT_ASSERT(dummyPtr.isAvailable());
0190     CPPUNIT_ASSERT(dummyPtr.id() == pid);
0191     CPPUNIT_ASSERT(dummyPtr.key() == key);
0192     CPPUNIT_ASSERT(dummyPtr.get() == &dummyContainer[key]);
0193     CPPUNIT_ASSERT(&(*dummyPtr) == &dummyContainer[key]);
0194     CPPUNIT_ASSERT((dummyPtr.operator->()) == &dummyContainer[key]);
0195     CPPUNIT_ASSERT(dummyPtr->address() == dummyContainer[key].address());
0196 
0197     CPPUNIT_ASSERT(dummyPtr2.isAvailable());
0198     CPPUNIT_ASSERT(dummyPtr2.id() == pid);
0199     CPPUNIT_ASSERT(dummyPtr2.key() == key);
0200     CPPUNIT_ASSERT(dummyPtr2.get() == &dummyContainer[key]);
0201     CPPUNIT_ASSERT(&(*dummyPtr2) == &dummyContainer[key]);
0202     CPPUNIT_ASSERT((dummyPtr2.operator->()) == &dummyContainer[key]);
0203     CPPUNIT_ASSERT(dummyPtr2->address() == dummyContainer[key].address());
0204 
0205     Ptr<Dummy2> dummy2Ptr(handle, key);
0206     Ptr<Dummy> copyPtr(dummy2Ptr);
0207     CPPUNIT_ASSERT(dummy2Ptr.key() == copyPtr.key());
0208     CPPUNIT_ASSERT(dummy2Ptr.get() == static_cast<const Dummy*>(dummy2Ptr.get()));
0209   }
0210 }
0211 
0212 void testPtr::comparisonTest() {
0213   {
0214     ProductID const pid(1, 1);
0215 
0216     unsigned int const key = 2;
0217     Dummy const dummy;
0218     DummyCollection dummyContainer;
0219     dummyContainer.push_back(dummy);
0220     OrphanHandle<DummyCollection> handle(&dummyContainer, pid);
0221     OrphanHandle<DummyCollection> handle2(&dummyContainer, pid);
0222     Ptr<Dummy> dummyPtr1(handle, key);
0223     Ptr<Dummy> dummyPtr2(handle2, key);
0224     //PtrProd<DummyCollection> dummyPtrProd1(handle);
0225     //PtrProd<DummyCollection> dummyPtrProd2(handle2);
0226 
0227     CPPUNIT_ASSERT(dummyPtr1 == dummyPtr2);
0228     CPPUNIT_ASSERT(!(dummyPtr1 != dummyPtr2));
0229     CPPUNIT_ASSERT(!(dummyPtr1 < dummyPtr2));
0230     CPPUNIT_ASSERT(!(dummyPtr2 < dummyPtr1));
0231     CPPUNIT_ASSERT(!(dummyPtr1 < dummyPtr1));
0232     CPPUNIT_ASSERT(!(dummyPtr2 < dummyPtr2));
0233 
0234     /*   CPPUNIT_ASSERT(dummyPtrProd1 == dummyPtrProd2);
0235    CPPUNIT_ASSERT(!(dummyPtrProd1 != dummyPtrProd2));
0236    CPPUNIT_ASSERT(!(dummyPtrProd1 < dummyPtrProd2));
0237    CPPUNIT_ASSERT(!(dummyPtrProd2 < dummyPtrProd1));
0238    CPPUNIT_ASSERT(!(dummyPtrProd1 < dummyPtrProd1));
0239    CPPUNIT_ASSERT(!(dummyPtrProd2 < dummyPtrProd2)); */
0240 
0241     Ptr<Dummy> dummyPtrNewKey(handle, key + 1);
0242     CPPUNIT_ASSERT(!(dummyPtr1 == dummyPtrNewKey));
0243     CPPUNIT_ASSERT(dummyPtr1 != dummyPtrNewKey);
0244     CPPUNIT_ASSERT(dummyPtr1 < dummyPtrNewKey);
0245     CPPUNIT_ASSERT(!(dummyPtrNewKey < dummyPtr1));
0246 
0247     ProductID const pidOther(1, 4);
0248     OrphanHandle<DummyCollection> handleNewPID(&dummyContainer, pidOther);
0249     Ptr<Dummy> dummyPtrNewPID(handleNewPID, key);
0250     //PtrProd<DummyCollection> dummyPtrProdNewPID(handleNewPID);
0251     CPPUNIT_ASSERT(!(dummyPtr1 == dummyPtrNewPID));
0252     CPPUNIT_ASSERT(dummyPtr1 != dummyPtrNewPID);
0253     /*CPPUNIT_ASSERT(!(dummyPtrProd1 == dummyPtrProdNewPID));
0254    CPPUNIT_ASSERT(dummyPtrProd1 != dummyPtrProdNewPID);
0255    CPPUNIT_ASSERT(dummyPtrProd1 < dummyPtrProdNewPID);
0256    CPPUNIT_ASSERT(!(dummyPtrProdNewPID < dummyPtrProd1)); */
0257   }
0258   /*
0259    
0260 {
0261    typedef std::map<int, double> DummyCollection2;
0262    ProductID const pid2(1, 2);
0263    DummyCollection2 dummyContainer2;
0264    dummyContainer2.insert(std::make_pair(1, 1.0));
0265    dummyContainer2.insert(std::make_pair(2, 2.0));
0266    OrphanHandle<DummyCollection2> handle2(&dummyContainer2, pid2);
0267    Ptr<DummyCollection2> dummyPtr21(handle2, 1);
0268    Ptr<DummyCollection2> dummyPtr22(handle2, 2);
0269    CPPUNIT_ASSERT(dummyPtr21 != dummyPtr22);
0270    CPPUNIT_ASSERT(dummyPtr21 < dummyPtr22);
0271    CPPUNIT_ASSERT(!(dummyPtr22 < dummyPtr21));
0272 
0273    typedef std::map<int, double, std::greater<int> > DummyCollection3;
0274    ProductID const pid3(1, 3);
0275    DummyCollection3 dummyContainer3;
0276    dummyContainer3.insert(std::make_pair(1, 1.0));
0277    dummyContainer3.insert(std::make_pair(2, 2.0));
0278    OrphanHandle<DummyCollection3> handle3(&dummyContainer3, pid3);
0279    Ptr<DummyCollection3> dummyPtr31(handle3, 1);
0280    Ptr<DummyCollection3> dummyPtr32(handle3, 2);
0281    CPPUNIT_ASSERT(dummyPtr31 != dummyPtr32);
0282    CPPUNIT_ASSERT(!(dummyPtr31 < dummyPtr32));
0283    CPPUNIT_ASSERT(dummyPtr32 < dummyPtr31);
0284  }
0285 */
0286 }
0287 
0288 namespace {
0289   struct TestGetter : public edm::EDProductGetter {
0290     WrapperBase const* hold_;
0291     WrapperBase const* getIt(ProductID const&) const override { return hold_; }
0292     std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(ProductID const&,
0293                                                                                        unsigned int) const override {
0294       return std::nullopt;
0295     }
0296 
0297     void getThinnedProducts(ProductID const& pid,
0298                             std::vector<WrapperBase const*>& wrappers,
0299                             std::vector<unsigned int>& keys) const override {}
0300 
0301     edm::OptionalThinnedKey getThinnedKeyFrom(ProductID const&, unsigned int, ProductID const&) const override {
0302       return std::monostate{};
0303     }
0304 
0305     unsigned int transitionIndex_() const override { return 0U; }
0306 
0307     TestGetter() : hold_() {}
0308   };
0309 }  // namespace
0310 
0311 void testPtr::getTest() {
0312   {
0313     typedef std::vector<IntValue> IntCollection;
0314     auto ptr = std::make_unique<IntCollection>();
0315 
0316     ptr->push_back(0);
0317     ptr->push_back(1);
0318 
0319     edm::Wrapper<IntCollection> wrapper(std::move(ptr));
0320     TestGetter tester;
0321     tester.hold_ = &wrapper;
0322 
0323     ProductID const pid(1, 1);
0324 
0325     IntCollection const* wptr = dynamic_cast<IntCollection const*>(wrapper.product());
0326 
0327     OrphanHandle<IntCollection> handle(wptr, pid);
0328 
0329     Ptr<IntValue> ref0(pid, 0, &tester);
0330     CPPUNIT_ASSERT(!ref0.hasProductCache());
0331 
0332     Ptr<IntValue> ref1(pid, 1, &tester);
0333 
0334     Ptr<IntValue> ref2(pid, 1, &tester);
0335 
0336     CPPUNIT_ASSERT(ref0.isAvailable());
0337     CPPUNIT_ASSERT(0 == ref0->value_);
0338     CPPUNIT_ASSERT(ref0.hasProductCache());
0339     CPPUNIT_ASSERT(1 == ref1->value_);
0340     CPPUNIT_ASSERT(1 == ref2->value_);
0341     CPPUNIT_ASSERT(1 == (*ref1).value_);
0342   }
0343   {
0344     typedef std::vector<IntValue2> SDCollection;
0345     auto ptr = std::make_unique<SDCollection>();
0346 
0347     ptr->push_back(IntValue2(0));
0348     ptr->back().value_ = 0;
0349     ptr->push_back(IntValue2(1));
0350     ptr->back().value_ = 1;
0351 
0352     edm::Wrapper<SDCollection> wrapper(std::move(ptr));
0353     TestGetter tester;
0354     tester.hold_ = &wrapper;
0355 
0356     ProductID const pid(1, 1);
0357 
0358     SDCollection const* wptr = dynamic_cast<SDCollection const*>(wrapper.product());
0359 
0360     OrphanHandle<SDCollection> handle(wptr, pid);
0361 
0362     Ptr<IntValue> ref0(pid, 0, &tester);
0363     CPPUNIT_ASSERT(!ref0.hasProductCache());
0364 
0365     Ptr<IntValue> ref1(pid, 1, &tester);
0366 
0367     Ptr<IntValue> ref2(pid, 1, &tester);
0368 
0369     CPPUNIT_ASSERT(0 == ref0->value_);
0370     CPPUNIT_ASSERT(ref0.hasProductCache());
0371     CPPUNIT_ASSERT(1 == ref1->value_);
0372     CPPUNIT_ASSERT(1 == ref2->value_);
0373     CPPUNIT_ASSERT(1 == (*ref1).value_);
0374   }
0375 
0376   {
0377     TestGetter tester;
0378     tester.hold_ = nullptr;
0379     ProductID const pid(1, 1);
0380 
0381     Ptr<IntValue> ref0(pid, 0, &tester);
0382     CPPUNIT_ASSERT_THROW((*ref0), cms::Exception);
0383     CPPUNIT_ASSERT_THROW((ref0.operator->()), cms::Exception);
0384   }
0385 }