Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "cppunit/extensions/HelperMacros.h"
0002 #include "DataFormats/Common/interface/RefToBase.h"
0003 #include "DataFormats/Common/interface/Ref.h"
0004 #include "DataFormats/Common/interface/TestHandle.h"
0005 
0006 #include <vector>
0007 
0008 class testRefToBase : public CppUnit::TestFixture {
0009   CPPUNIT_TEST_SUITE(testRefToBase);
0010   CPPUNIT_TEST(check);
0011   CPPUNIT_TEST_SUITE_END();
0012 
0013 public:
0014   void setUp() {}
0015   void tearDown() {}
0016   void check();
0017 };
0018 
0019 CPPUNIT_TEST_SUITE_REGISTRATION(testRefToBase);
0020 namespace testreftobase {
0021   struct Base {
0022     virtual ~Base() {}
0023     virtual int val() const = 0;
0024   };
0025 
0026   struct Inherit1 : public Base {
0027     virtual int val() const { return 1; }
0028   };
0029   struct Inherit2 : public Base {
0030     virtual int val() const { return 2; }
0031   };
0032 }  // namespace testreftobase
0033 
0034 using namespace testreftobase;
0035 
0036 void testRefToBase::check() {
0037   using namespace edm;
0038 
0039   std::vector<Inherit1> v1(2, Inherit1());
0040   std::vector<Inherit2> v2(2, Inherit2());
0041 
0042   TestHandle<std::vector<Inherit1> > h1(&v1, ProductID(1, 1));
0043   Ref<std::vector<Inherit1> > r1(h1, 1);
0044   RefToBase<Base> b1(r1);
0045   CPPUNIT_ASSERT(&(*b1) == static_cast<Base*>(&(v1[1])));
0046   CPPUNIT_ASSERT(b1.operator->() == b1.get());
0047   CPPUNIT_ASSERT(b1.get() == static_cast<Base*>(&(v1[1])));
0048   CPPUNIT_ASSERT(b1.id() == ProductID(1, 1));
0049 
0050   //copy constructor
0051   RefToBase<Base> b2(b1);
0052   CPPUNIT_ASSERT(&(*b2) == static_cast<Base*>(&(v1[1])));
0053   CPPUNIT_ASSERT(b2.id() == b1.id());
0054 
0055   //operator=
0056   RefToBase<Base> b3;
0057   CPPUNIT_ASSERT(b3.isNull());
0058   CPPUNIT_ASSERT(!(b3.isNonnull()));
0059   CPPUNIT_ASSERT(!b3);
0060   b3 = b1;
0061   CPPUNIT_ASSERT(&(*b3) == static_cast<Base*>(&(v1[1])));
0062   CPPUNIT_ASSERT(b3.id() == b1.id());
0063   CPPUNIT_ASSERT(!(b3.isNull()));
0064   CPPUNIT_ASSERT(b3.isNonnull());
0065   CPPUNIT_ASSERT(!(!b3));
0066 
0067   CPPUNIT_ASSERT(b1.castTo<Ref<std::vector<Inherit1> > >() == r1);
0068   bool throwed = false;
0069   try {
0070     b1.castTo<Ref<std::vector<Inherit2> > >();
0071   } catch (edm::Exception const& e) {
0072     throwed = true;
0073   }
0074   CPPUNIT_ASSERT(throwed);
0075 }