Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:15

0001 #include "catch.hpp"
0002 #include "FWCore/Utilities/interface/value_ptr.h"
0003 
0004 #include <memory>
0005 #include <atomic>
0006 
0007 namespace {
0008   class simple {
0009     int i;
0010 
0011   public:
0012     static std::atomic<int> count;
0013     static std::atomic<int> count1;
0014     simple() : i(0) {
0015       ++count;
0016       ++count1;
0017     }
0018     explicit simple(int j) : i(j) {
0019       ++count;
0020       ++count1;
0021     }
0022     simple(simple const& s) : i(s.i) {
0023       ++count;
0024       ++count1;
0025     }
0026     ~simple() { --count; }
0027     bool operator==(simple const& o) const { return i == o.i; }
0028     bool isSame(simple const& o) const { return &o == this; }
0029     simple& operator=(simple const& o) {
0030       simple temp(o);
0031       std::swap(*this, temp);
0032       return *this;
0033     }
0034   };
0035 
0036   std::atomic<int> simple::count{0};
0037   std::atomic<int> simple::count1{0};
0038 }  // namespace
0039 TEST_CASE("test edm::value_ptr", "[value_ptr]") {
0040   REQUIRE(simple::count == 0);
0041   {
0042     edm::value_ptr<simple> a(new simple(10));
0043     REQUIRE(simple::count == 1);
0044     edm::value_ptr<simple> b(a);
0045     REQUIRE(simple::count == 2);
0046 
0047     REQUIRE(*a == *b);
0048     REQUIRE(a->isSame(*b) == false);
0049   }  // a and b destroyed
0050   REQUIRE(simple::count == 0);
0051 
0052   {
0053     auto c = std::make_unique<simple>(11);
0054     auto d = std::make_unique<simple>(11);
0055     REQUIRE(c.get() != nullptr);
0056     REQUIRE(d.get() != nullptr);
0057     simple* pc = c.get();
0058     simple* pd = d.get();
0059 
0060     edm::value_ptr<simple> e(std::move(c));
0061     REQUIRE(c.get() == nullptr);
0062     REQUIRE(*d == *e);
0063     REQUIRE(e.operator->() == pc);
0064 
0065     edm::value_ptr<simple> f;
0066     REQUIRE(not bool(f));
0067     f = std::move(d);
0068     REQUIRE(d.get() == nullptr);
0069     REQUIRE(*e == *f);
0070     REQUIRE(f.operator->() == pd);
0071     REQUIRE(bool(f));
0072 
0073     REQUIRE(simple::count == 2);
0074     REQUIRE(simple::count1 == 4);
0075   }
0076   REQUIRE(simple::count == 0);
0077   REQUIRE(simple::count1 == 4);
0078 }