Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:23

0001 /*----------------------------------------------------------------------
0002 
0003 Test program for edm::propagate_const class.
0004 
0005  ----------------------------------------------------------------------*/
0006 
0007 #include <cppunit/extensions/HelperMacros.h>
0008 #include <memory>
0009 #include "FWCore/Utilities/interface/get_underlying_safe.h"
0010 #include "FWCore/Utilities/interface/propagate_const.h"
0011 
0012 class test_propagate_const : public CppUnit::TestFixture {
0013   CPPUNIT_TEST_SUITE(test_propagate_const);
0014 
0015   CPPUNIT_TEST(test);
0016 
0017   CPPUNIT_TEST_SUITE_END();
0018 
0019 public:
0020   void setUp() {}
0021   void tearDown() {}
0022 
0023   void test();
0024 };
0025 
0026 ///registration of the test so that the runner can find it
0027 CPPUNIT_TEST_SUITE_REGISTRATION(test_propagate_const);
0028 
0029 namespace {
0030   class ConstChecker {
0031   public:
0032     int value() { return 0; }
0033 
0034     int const value() const { return 1; }
0035   };
0036 
0037   // used to check that edm::propagate_const<T*> works with incomplete types
0038   class Incomplete;
0039 
0040 }  // namespace
0041 
0042 void test_propagate_const::test() {
0043   // test for edm::propagate_const<T*>
0044   {
0045     ConstChecker checker;
0046     edm::propagate_const<ConstChecker*> pChecker(&checker);
0047 
0048     CPPUNIT_ASSERT(0 == pChecker.get()->value());
0049     CPPUNIT_ASSERT(pChecker.get()->value() == pChecker->value());
0050     CPPUNIT_ASSERT(pChecker.get()->value() == (*pChecker).value());
0051     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)->value());
0052 
0053     const edm::propagate_const<ConstChecker*> pConstChecker(&checker);
0054 
0055     CPPUNIT_ASSERT(1 == pConstChecker.get()->value());
0056     CPPUNIT_ASSERT(pConstChecker.get()->value() == pConstChecker->value());
0057     CPPUNIT_ASSERT(pConstChecker.get()->value() == (*pConstChecker).value());
0058     CPPUNIT_ASSERT(1 == get_underlying_safe(pConstChecker)->value());
0059   }
0060 
0061   // test for edm::propagate_const<shared_ptr<T>>
0062   {
0063     auto checker = std::make_shared<ConstChecker>();
0064     edm::propagate_const<std::shared_ptr<ConstChecker>> pChecker(checker);
0065 
0066     CPPUNIT_ASSERT(0 == pChecker.get()->value());
0067     CPPUNIT_ASSERT(pChecker.get()->value() == pChecker->value());
0068     CPPUNIT_ASSERT(pChecker.get()->value() == (*pChecker).value());
0069     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)->value());
0070 
0071     const edm::propagate_const<std::shared_ptr<ConstChecker>> pConstChecker(checker);
0072 
0073     CPPUNIT_ASSERT(1 == pConstChecker.get()->value());
0074     CPPUNIT_ASSERT(pConstChecker.get()->value() == pConstChecker->value());
0075     CPPUNIT_ASSERT(pConstChecker.get()->value() == (*pConstChecker).value());
0076     CPPUNIT_ASSERT(1 == get_underlying_safe(pConstChecker)->value());
0077   }
0078 
0079   // test for edm::propagate_const<unique_ptr<T>>
0080   {
0081     auto checker = std::make_unique<ConstChecker>();
0082     edm::propagate_const<std::unique_ptr<ConstChecker>> pChecker(std::move(checker));
0083 
0084     CPPUNIT_ASSERT(0 == pChecker.get()->value());
0085     CPPUNIT_ASSERT(pChecker.get()->value() == pChecker->value());
0086     CPPUNIT_ASSERT(pChecker.get()->value() == (*pChecker).value());
0087     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)->value());
0088 
0089     const edm::propagate_const<std::unique_ptr<ConstChecker>> pConstChecker(std::make_unique<ConstChecker>());
0090 
0091     CPPUNIT_ASSERT(1 == pConstChecker.get()->value());
0092     CPPUNIT_ASSERT(pConstChecker.get()->value() == pConstChecker->value());
0093     CPPUNIT_ASSERT(pConstChecker.get()->value() == (*pConstChecker).value());
0094   }
0095 
0096   // test for edm::propagate_const<T*> with incomplete types
0097   {
0098     Incomplete* ptr = nullptr;
0099     edm::propagate_const<Incomplete*> pIncomplete(ptr);
0100 
0101     CPPUNIT_ASSERT(nullptr == pIncomplete.get());
0102   }
0103 }