Base

Inherit

testCloningPtr

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "cppunit/extensions/HelperMacros.h"
#include "DataFormats/Common/interface/CloningPtr.h"

#include <vector>

class testCloningPtr : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(testCloningPtr);
  CPPUNIT_TEST(check);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() {}
  void tearDown() {}
  void check();
};

CPPUNIT_TEST_SUITE_REGISTRATION(testCloningPtr);
namespace testcloningptr {
  struct Base {
    virtual ~Base() {}
    virtual int val() const = 0;
    virtual Base* clone() const = 0;
  };

  struct Inherit : public Base {
    Inherit(int iValue) : val_(iValue) {}
    virtual int val() const { return val_; }
    virtual Base* clone() const { return new Inherit(*this); }
    int val_;
  };
}  // namespace testcloningptr

using namespace testcloningptr;

void testCloningPtr::check() {
  using namespace edm;

  Inherit one(1);
  CloningPtr<Base> cpOne(one);
  CPPUNIT_ASSERT(&one != cpOne.get());
  CPPUNIT_ASSERT(1 == cpOne->val());
  CPPUNIT_ASSERT(1 == (*cpOne).val());

  CloningPtr<Base> cpOtherOne(cpOne);
  CPPUNIT_ASSERT(cpOne.get() != cpOtherOne.get());
  CPPUNIT_ASSERT(cpOtherOne->val() == 1);

  CloningPtr<Base> eqOne;
  eqOne = cpOne;

  CPPUNIT_ASSERT(cpOne.get() != eqOne.get());
  CPPUNIT_ASSERT(eqOne->val() == 1);
}