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_array 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_array.h"
0011 
0012 class test_propagate_const_array : public CppUnit::TestFixture {
0013   CPPUNIT_TEST_SUITE(test_propagate_const_array);
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_array);
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_array<T[]> works with incomplete types
0038   class Incomplete;
0039 
0040 }  // namespace
0041 
0042 void test_propagate_const_array::test() {
0043   // test for edm::propagate_const_array<T[]>
0044   {
0045     ConstChecker checker[10];
0046     edm::propagate_const_array<ConstChecker[]> pChecker(checker);
0047 
0048     CPPUNIT_ASSERT(0 == pChecker[1].value());
0049     CPPUNIT_ASSERT(0 == pChecker.get()[1].value());
0050     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)[1].value());
0051 
0052     const edm::propagate_const_array<ConstChecker[]> pConstChecker(checker);
0053 
0054     CPPUNIT_ASSERT(1 == pConstChecker[1].value());
0055     CPPUNIT_ASSERT(1 == pConstChecker.get()[1].value());
0056     CPPUNIT_ASSERT(1 == get_underlying_safe(pConstChecker)[1].value());
0057   }
0058 
0059   // test for edm::propagate_const_array<shared_ptr<T>>
0060   {
0061     // std::make_shared<T[]> requires C++20
0062     auto checker = std::shared_ptr<ConstChecker[]>(new ConstChecker[10]);
0063     edm::propagate_const_array<std::shared_ptr<ConstChecker[]>> pChecker(checker);
0064 
0065     CPPUNIT_ASSERT(0 == pChecker[1].value());
0066     CPPUNIT_ASSERT(0 == pChecker.get()[1].value());
0067     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)[1].value());
0068 
0069     const edm::propagate_const_array<std::shared_ptr<ConstChecker[]>> pConstChecker(checker);
0070 
0071     CPPUNIT_ASSERT(1 == pConstChecker[1].value());
0072     CPPUNIT_ASSERT(1 == pConstChecker.get()[1].value());
0073     CPPUNIT_ASSERT(1 == get_underlying_safe(pConstChecker)[1].value());
0074   }
0075 
0076   // test for edm::propagate_const_array<unique_ptr<T>>
0077   {
0078     auto checker = std::make_unique<ConstChecker[]>(10);
0079     edm::propagate_const_array<std::unique_ptr<ConstChecker[]>> pChecker(std::move(checker));
0080 
0081     CPPUNIT_ASSERT(0 == pChecker[1].value());
0082     CPPUNIT_ASSERT(0 == pChecker.get()[1].value());
0083     CPPUNIT_ASSERT(0 == get_underlying_safe(pChecker)[1].value());
0084 
0085     const edm::propagate_const_array<std::unique_ptr<ConstChecker[]>> pConstChecker(
0086         std::make_unique<ConstChecker[]>(10));
0087 
0088     CPPUNIT_ASSERT(1 == pConstChecker[1].value());
0089     CPPUNIT_ASSERT(1 == pConstChecker.get()[1].value());
0090   }
0091 
0092   // test for edm::propagate_const_array<T[]> with incomplete types
0093   {
0094     edm::propagate_const_array<Incomplete[]> pIncomplete(nullptr);
0095 
0096     CPPUNIT_ASSERT(nullptr == get_underlying(pIncomplete));
0097   }
0098 }