Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "cppunit/extensions/HelperMacros.h"
0002 #include "DataFormats/Provenance/interface/Provenance.h"
0003 #include "DataFormats/Common/interface/Handle.h"
0004 #include "DataFormats/Common/interface/ValidHandle.h"
0005 #include "DataFormats/Common/interface/FunctorHandleExceptionFactory.h"
0006 #include "FWCore/Utilities/interface/Exception.h"
0007 
0008 class testHandle : public CppUnit::TestFixture {
0009   CPPUNIT_TEST_SUITE(testHandle);
0010   CPPUNIT_TEST(check);
0011   CPPUNIT_TEST(checkValidHandle);
0012   CPPUNIT_TEST_SUITE_END();
0013 
0014 public:
0015   void setUp() {}
0016   void tearDown() {}
0017   void check();
0018   void checkValidHandle();
0019 };
0020 
0021 CPPUNIT_TEST_SUITE_REGISTRATION(testHandle);
0022 
0023 void testHandle::check() {
0024   using namespace edm;
0025 
0026   {
0027     Handle<int> hDefault;
0028     CPPUNIT_ASSERT(not hDefault.isValid());
0029     CPPUNIT_ASSERT(not hDefault.failedToGet());
0030 
0031     //the following leads to a seg fault :(
0032     //CPPUNIT_ASSERT(hDefault.id() == ProductID{});
0033     CPPUNIT_ASSERT(not hDefault.whyFailed());
0034 
0035     //This doesn't throw
0036     //CPPUNIT_ASSERT_THROW([&hDefault](){*hDefault;},
0037     //                     cms::Exception);
0038   }
0039 
0040   {
0041     Provenance provDummy;
0042     int value = 3;
0043 
0044     Handle<int> h(&value, &provDummy);
0045     CPPUNIT_ASSERT(h.isValid());
0046     CPPUNIT_ASSERT(not h.failedToGet());
0047     CPPUNIT_ASSERT(not h.whyFailed());
0048     CPPUNIT_ASSERT(3 == *h);
0049 
0050     Handle<int> hCopy(h);
0051     CPPUNIT_ASSERT(hCopy.isValid());
0052     CPPUNIT_ASSERT(not hCopy.failedToGet());
0053     CPPUNIT_ASSERT(not hCopy.whyFailed());
0054     CPPUNIT_ASSERT(3 == *hCopy);
0055 
0056     Handle<int> hOpEq;
0057     hOpEq = h;
0058     CPPUNIT_ASSERT(hOpEq.isValid());
0059     CPPUNIT_ASSERT(not hOpEq.failedToGet());
0060     CPPUNIT_ASSERT(not hOpEq.whyFailed());
0061     CPPUNIT_ASSERT(3 == *hOpEq);
0062 
0063     Handle<int> hOpEqMove;
0064     hOpEqMove = std::move(hCopy);
0065     CPPUNIT_ASSERT(hOpEqMove.isValid());
0066     CPPUNIT_ASSERT(not hOpEqMove.failedToGet());
0067     CPPUNIT_ASSERT(not hOpEqMove.whyFailed());
0068     CPPUNIT_ASSERT(3 == *hOpEqMove);
0069   }
0070 
0071   {
0072     Handle<int> hFail(makeHandleExceptionFactory(
0073         []() -> std::shared_ptr<cms::Exception> { return std::make_shared<cms::Exception>("DUMMY"); }));
0074 
0075     CPPUNIT_ASSERT(not hFail.isValid());
0076     CPPUNIT_ASSERT(hFail.failedToGet());
0077 
0078     CPPUNIT_ASSERT(hFail.whyFailed());
0079 
0080     CPPUNIT_ASSERT_THROW(*hFail, cms::Exception);
0081   }
0082 }
0083 
0084 void testHandle::checkValidHandle() {
0085   using namespace edm;
0086 
0087   {
0088     ProductID dummyID;
0089     int value = 3;
0090 
0091     ValidHandle<int> h(&value, dummyID);
0092 
0093     CPPUNIT_ASSERT(h.product() == &value);
0094     CPPUNIT_ASSERT(*h == value);
0095     CPPUNIT_ASSERT(h.operator->() == &value);
0096     CPPUNIT_ASSERT(dummyID == h.id());
0097   }
0098 
0099   {
0100     ProductID dummyID;
0101     CPPUNIT_ASSERT_THROW(ValidHandle<int>(nullptr, dummyID), cms::Exception);
0102   }
0103   {
0104     Handle<int> hDefault;
0105     CPPUNIT_ASSERT_THROW(makeValid(hDefault), cms::Exception);
0106     //This doesn't throw
0107     //CPPUNIT_ASSERT_THROW([&hDefault](){*hDefault;},
0108     //                     cms::Exception);
0109   }
0110 
0111   {
0112     Provenance provDummy;
0113     int value = 3;
0114 
0115     Handle<int> h(&value, &provDummy);
0116 
0117     auto hv = makeValid(h);
0118 
0119     CPPUNIT_ASSERT(3 == *hv);
0120     CPPUNIT_ASSERT(h.id() == hv.id());
0121 
0122     ValidHandle<int> hCopy(hv);
0123     CPPUNIT_ASSERT(3 == *hCopy);
0124 
0125     int value2 = 2;
0126     ValidHandle<int> hOpEq(&value2, provDummy.productID());
0127     CPPUNIT_ASSERT(2 == *hOpEq);
0128     hOpEq = ValidHandle<int>(value, provDummy.productID());
0129   }
0130 
0131   {
0132     Handle<int> hFail(makeHandleExceptionFactory(
0133         []() -> std::shared_ptr<cms::Exception> { return std::make_shared<cms::Exception>("DUMMY"); }));
0134 
0135     CPPUNIT_ASSERT_THROW(makeValid(hFail), cms::Exception);
0136   }
0137 }