Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-29 06:08:24

0001 // -*- C++ -*-
0002 //
0003 // Package:     Geometry/CaloGeometry
0004 // Class  :     calocellgeometryptr_catch2
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Thu, 24 Oct 2024 17:35:52 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "Geometry/CaloGeometry/interface/CaloCellGeometryPtr.h"
0017 #include "Geometry/CaloGeometry/interface/CaloCellGeometryMayOwnPtr.h"
0018 #define CATCH_CONFIG_MAIN
0019 #include "catch.hpp"
0020 
0021 namespace {
0022   class DummyCell : public CaloCellGeometry {
0023   public:
0024     DummyCell() { ++nLive; }
0025     ~DummyCell() { --nLive; }
0026     DummyCell(DummyCell const&) = delete;
0027     DummyCell(DummyCell&&) = delete;
0028     DummyCell& operator=(DummyCell const&) = delete;
0029     DummyCell& operator=(DummyCell&&) = delete;
0030 
0031     void vocalCorners(Pt3DVec& vec, const CCGFloat* pv, Pt3D& ref) const final {}
0032     void initCorners(CornersVec&) final {}
0033 
0034     static int nLive;
0035   };
0036   int DummyCell::nLive = 0;
0037 }  // namespace
0038 
0039 TEST_CASE("Test CaloCellGeometryPtr", "[CaloCellGeometryPtr]") {
0040   SECTION("Default Constructor") {
0041     CaloCellGeometryPtr ptr;
0042     CHECK(ptr.get() == nullptr);
0043     CHECK(static_cast<CaloCellGeometry const*>(ptr) == nullptr);
0044   }
0045   SECTION("Pointer Constructor") {
0046     {
0047       DummyCell dummy;
0048       REQUIRE(DummyCell::nLive == 1);
0049 
0050       {
0051         CaloCellGeometryPtr ptr(&dummy);
0052         CHECK(ptr.get() == &dummy);
0053         CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0054         CHECK(ptr.operator->() == &dummy);
0055         CHECK(&(*ptr) == &dummy);
0056       }
0057       REQUIRE(DummyCell::nLive == 1);
0058     }
0059     REQUIRE(DummyCell::nLive == 0);
0060   }
0061 }
0062 
0063 TEST_CASE("Test CaloCellGeometryMayOwnPtr", "[CaloCellGeometryPtr]") {
0064   SECTION("Default Constructed") {
0065     CaloCellGeometryMayOwnPtr ptr;
0066     CHECK(ptr.get() == nullptr);
0067     CHECK(static_cast<CaloCellGeometry const*>(ptr) == nullptr);
0068   }
0069   SECTION("From CaloCellGeometryPtr") {
0070     {
0071       DummyCell dummy;
0072       REQUIRE(DummyCell::nLive == 1);
0073       CaloCellGeometryPtr p(&dummy);
0074       {
0075         CaloCellGeometryMayOwnPtr ptr(p);
0076         CHECK(ptr.get() == &dummy);
0077         CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0078         CHECK(ptr.operator->() == &dummy);
0079         CHECK(&(*ptr) == &dummy);
0080       }
0081       REQUIRE(DummyCell::nLive == 1);
0082     }
0083     REQUIRE(DummyCell::nLive == 0);
0084   }
0085 
0086   SECTION("From unique_ptr") {
0087     {
0088       auto dummy = std::make_unique<DummyCell>();
0089       auto dummyAddress = dummy.get();
0090       REQUIRE(DummyCell::nLive == 1);
0091       {
0092         CaloCellGeometryMayOwnPtr ptr(std::move(dummy));
0093         CHECK(ptr.get() == dummyAddress);
0094         CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0095         CHECK(ptr.operator->() == dummyAddress);
0096         CHECK(&(*ptr) == dummyAddress);
0097       }
0098       REQUIRE(DummyCell::nLive == 0);
0099     }
0100   }
0101 
0102   SECTION("move constructor") {
0103     SECTION("non-owning") {
0104       DummyCell dummy;
0105       REQUIRE(DummyCell::nLive == 1);
0106       CaloCellGeometryPtr p(&dummy);
0107       {
0108         CaloCellGeometryMayOwnPtr from(p);
0109         {
0110           CaloCellGeometryMayOwnPtr ptr(std::move(from));
0111           CHECK(from.get() == nullptr);
0112           CHECK(ptr.get() == &dummy);
0113           CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0114           CHECK(ptr.operator->() == &dummy);
0115           CHECK(&(*ptr) == &dummy);
0116           REQUIRE(DummyCell::nLive == 1);
0117         }
0118         REQUIRE(DummyCell::nLive == 1);
0119       }
0120       REQUIRE(DummyCell::nLive == 1);
0121     }
0122     SECTION("owning") {
0123       auto dummy = std::make_unique<DummyCell>();
0124       auto dummyAddress = dummy.get();
0125       REQUIRE(DummyCell::nLive == 1);
0126       {
0127         CaloCellGeometryMayOwnPtr from(std::move(dummy));
0128         {
0129           CaloCellGeometryMayOwnPtr ptr(std::move(from));
0130           CHECK(from.get() == nullptr);
0131           CHECK(ptr.get() == dummyAddress);
0132           CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0133           CHECK(ptr.operator->() == dummyAddress);
0134           CHECK(&(*ptr) == dummyAddress);
0135         }
0136         REQUIRE(DummyCell::nLive == 0);
0137       }
0138     }
0139   }
0140 
0141   SECTION("copy constructor") {
0142     SECTION("non-owning") {
0143       DummyCell dummy;
0144       REQUIRE(DummyCell::nLive == 1);
0145       {
0146         CaloCellGeometryPtr p(&dummy);
0147         {
0148           CaloCellGeometryMayOwnPtr from(p);
0149           {
0150             CaloCellGeometryMayOwnPtr ptr(from);
0151             CHECK(from.get() == &dummy);
0152             CHECK(ptr.get() == &dummy);
0153             CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0154             CHECK(ptr.operator->() == &dummy);
0155             CHECK(&(*ptr) == &dummy);
0156             REQUIRE(DummyCell::nLive == 1);
0157           }
0158           REQUIRE(DummyCell::nLive == 1);
0159         }
0160         REQUIRE(DummyCell::nLive == 1);
0161       }
0162       REQUIRE(DummyCell::nLive == 1);
0163     }
0164     SECTION("owning") {
0165       auto dummy = std::make_unique<DummyCell>();
0166       auto dummyAddress = dummy.get();
0167       REQUIRE(DummyCell::nLive == 1);
0168       {
0169         CaloCellGeometryMayOwnPtr from(std::move(dummy));
0170         {
0171           CaloCellGeometryMayOwnPtr ptr(from);
0172           CHECK(from.get() == dummyAddress);
0173           CHECK(ptr.get() == dummyAddress);
0174           CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0175           CHECK(ptr.operator->() == dummyAddress);
0176           CHECK(&(*ptr) == dummyAddress);
0177         }
0178         REQUIRE(DummyCell::nLive == 1);
0179       }
0180       REQUIRE(DummyCell::nLive == 0);
0181     }
0182   }
0183 
0184   SECTION("move assignment") {
0185     SECTION("from non-owning") {
0186       DummyCell oldDummy;
0187       CaloCellGeometryPtr p(&oldDummy);
0188       REQUIRE(DummyCell::nLive == 1);
0189       SECTION("to non-owning") {
0190         DummyCell dummy;
0191         REQUIRE(DummyCell::nLive == 2);
0192         {
0193           CaloCellGeometryMayOwnPtr ptr(p);
0194           CaloCellGeometryPtr p(&dummy);
0195           {
0196             CaloCellGeometryMayOwnPtr from(p);
0197             {
0198               ptr = std::move(from);
0199               CHECK(from.get() == nullptr);
0200               CHECK(ptr.get() == &dummy);
0201               CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0202               CHECK(ptr.operator->() == &dummy);
0203               CHECK(&(*ptr) == &dummy);
0204             }
0205             REQUIRE(DummyCell::nLive == 2);
0206           }
0207           REQUIRE(DummyCell::nLive == 2);
0208         }
0209         REQUIRE(DummyCell::nLive == 2);
0210       }
0211       SECTION("to owning") {
0212         {
0213           CaloCellGeometryMayOwnPtr ptr(p);
0214           auto dummy = std::make_unique<DummyCell>();
0215           auto dummyAddress = dummy.get();
0216           REQUIRE(DummyCell::nLive == 2);
0217           {
0218             CaloCellGeometryMayOwnPtr from(std::move(dummy));
0219             {
0220               ptr = std::move(from);
0221               CHECK(from.get() == nullptr);
0222               CHECK(ptr.get() == dummyAddress);
0223               CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0224               CHECK(ptr.operator->() == dummyAddress);
0225               CHECK(&(*ptr) == dummyAddress);
0226             }
0227           }
0228           REQUIRE(DummyCell::nLive == 2);
0229         }
0230         REQUIRE(DummyCell::nLive == 1);
0231       }
0232     }
0233     SECTION("from owning") {
0234       SECTION("to non-owning") {
0235         auto oldDummy = std::make_unique<DummyCell>();
0236         REQUIRE(DummyCell::nLive == 1);
0237         DummyCell dummy;
0238         REQUIRE(DummyCell::nLive == 2);
0239         {
0240           CaloCellGeometryMayOwnPtr ptr(std::move(oldDummy));
0241           CaloCellGeometryPtr p(&dummy);
0242           {
0243             CaloCellGeometryMayOwnPtr from(p);
0244             {
0245               REQUIRE(DummyCell::nLive == 2);
0246               ptr = std::move(from);
0247               REQUIRE(DummyCell::nLive == 1);
0248               CHECK(from.get() == nullptr);
0249               CHECK(ptr.get() == &dummy);
0250               CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0251               CHECK(ptr.operator->() == &dummy);
0252               CHECK(&(*ptr) == &dummy);
0253             }
0254           }
0255           REQUIRE(DummyCell::nLive == 1);
0256         }
0257         REQUIRE(DummyCell::nLive == 1);
0258       }
0259       SECTION("to owning") {
0260         REQUIRE(DummyCell::nLive == 0);
0261         {
0262           auto oldDummy = std::make_unique<DummyCell>();
0263           REQUIRE(DummyCell::nLive == 1);
0264           CaloCellGeometryMayOwnPtr ptr(std::move(oldDummy));
0265           auto dummy = std::make_unique<DummyCell>();
0266           auto dummyAddress = dummy.get();
0267           REQUIRE(DummyCell::nLive == 2);
0268           {
0269             CaloCellGeometryMayOwnPtr from(std::move(dummy));
0270             {
0271               REQUIRE(DummyCell::nLive == 2);
0272               ptr = std::move(from);
0273               REQUIRE(DummyCell::nLive == 1);
0274               CHECK(from.get() == nullptr);
0275               CHECK(ptr.get() == dummyAddress);
0276               CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0277               CHECK(ptr.operator->() == dummyAddress);
0278               CHECK(&(*ptr) == dummyAddress);
0279             }
0280           }
0281           REQUIRE(DummyCell::nLive == 1);
0282         }
0283         REQUIRE(DummyCell::nLive == 0);
0284       }
0285     }
0286   }
0287 
0288   SECTION("copy assignment") {
0289     SECTION("from non-owning") {
0290       DummyCell oldDummy;
0291       CaloCellGeometryPtr p(&oldDummy);
0292       REQUIRE(DummyCell::nLive == 1);
0293       SECTION("to non-owning") {
0294         DummyCell dummy;
0295         REQUIRE(DummyCell::nLive == 2);
0296         {
0297           CaloCellGeometryMayOwnPtr ptr(p);
0298           CaloCellGeometryPtr p(&dummy);
0299           {
0300             CaloCellGeometryMayOwnPtr from(p);
0301             {
0302               ptr = from;
0303               CHECK(from.get() == &dummy);
0304               CHECK(ptr.get() == &dummy);
0305               CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0306               CHECK(ptr.operator->() == &dummy);
0307               CHECK(&(*ptr) == &dummy);
0308             }
0309             REQUIRE(DummyCell::nLive == 2);
0310           }
0311           REQUIRE(DummyCell::nLive == 2);
0312         }
0313         REQUIRE(DummyCell::nLive == 2);
0314       }
0315       SECTION("to owning") {
0316         {
0317           CaloCellGeometryMayOwnPtr ptr(p);
0318           auto dummy = std::make_unique<DummyCell>();
0319           auto dummyAddress = dummy.get();
0320           REQUIRE(DummyCell::nLive == 2);
0321           {
0322             CaloCellGeometryMayOwnPtr from(std::move(dummy));
0323             {
0324               ptr = from;
0325               CHECK(from.get() == dummyAddress);
0326               CHECK(ptr.get() == dummyAddress);
0327               CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0328               CHECK(ptr.operator->() == dummyAddress);
0329               CHECK(&(*ptr) == dummyAddress);
0330             }
0331           }
0332           REQUIRE(DummyCell::nLive == 2);
0333         }
0334         REQUIRE(DummyCell::nLive == 1);
0335       }
0336     }
0337     SECTION("from owning") {
0338       SECTION("to non-owning") {
0339         auto oldDummy = std::make_unique<DummyCell>();
0340         REQUIRE(DummyCell::nLive == 1);
0341         DummyCell dummy;
0342         REQUIRE(DummyCell::nLive == 2);
0343         {
0344           CaloCellGeometryMayOwnPtr ptr(std::move(oldDummy));
0345           CaloCellGeometryPtr p(&dummy);
0346           {
0347             CaloCellGeometryMayOwnPtr from(p);
0348             {
0349               REQUIRE(DummyCell::nLive == 2);
0350               ptr = from;
0351               REQUIRE(DummyCell::nLive == 1);
0352               CHECK(from.get() == &dummy);
0353               CHECK(ptr.get() == &dummy);
0354               CHECK(static_cast<CaloCellGeometry const*>(ptr) == &dummy);
0355               CHECK(ptr.operator->() == &dummy);
0356               CHECK(&(*ptr) == &dummy);
0357             }
0358           }
0359           REQUIRE(DummyCell::nLive == 1);
0360         }
0361         REQUIRE(DummyCell::nLive == 1);
0362       }
0363       SECTION("to owning") {
0364         REQUIRE(DummyCell::nLive == 0);
0365         {
0366           auto oldDummy = std::make_unique<DummyCell>();
0367           REQUIRE(DummyCell::nLive == 1);
0368           CaloCellGeometryMayOwnPtr ptr(std::move(oldDummy));
0369           auto dummy = std::make_unique<DummyCell>();
0370           auto dummyAddress = dummy.get();
0371           REQUIRE(DummyCell::nLive == 2);
0372           {
0373             CaloCellGeometryMayOwnPtr from(std::move(dummy));
0374             {
0375               REQUIRE(DummyCell::nLive == 2);
0376               ptr = from;
0377               REQUIRE(DummyCell::nLive == 1);
0378               CHECK(from.get() == dummyAddress);
0379               CHECK(ptr.get() == dummyAddress);
0380               CHECK(static_cast<CaloCellGeometry const*>(ptr) == dummyAddress);
0381               CHECK(ptr.operator->() == dummyAddress);
0382               CHECK(&(*ptr) == dummyAddress);
0383             }
0384           }
0385           REQUIRE(DummyCell::nLive == 1);
0386         }
0387         REQUIRE(DummyCell::nLive == 0);
0388       }
0389     }
0390   }
0391   SECTION("reference counting") {
0392     auto dummy = std::make_unique<DummyCell>();
0393     auto dummyAddress = dummy.get();
0394     REQUIRE(DummyCell::nLive == 1);
0395     {
0396       CaloCellGeometryMayOwnPtr ptr1;
0397       {
0398         CaloCellGeometryMayOwnPtr ptr2(std::move(dummy));
0399         REQUIRE(DummyCell::nLive == 1);
0400         ptr1 = ptr2;
0401         REQUIRE(DummyCell::nLive == 1);
0402         CHECK(ptr1.get() == dummyAddress);
0403         {
0404           CaloCellGeometryMayOwnPtr ptr3(ptr1);
0405           CHECK(ptr3.get() == dummyAddress);
0406         }
0407         REQUIRE(DummyCell::nLive == 1);
0408       }
0409       REQUIRE(DummyCell::nLive == 1);
0410     }
0411     REQUIRE(DummyCell::nLive == 0);
0412   }
0413 }