Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:08

0001 #include "cppunit/extensions/HelperMacros.h"
0002 #include <algorithm>
0003 #include <cstring>
0004 #include <iterator>
0005 #include <iostream>
0006 #include "DataFormats/Common/interface/OwnVector.h"
0007 #include "DataFormats/Common/interface/Ptr.h"
0008 #include "FWCore/Utilities/interface/propagate_const.h"
0009 
0010 class testOwnVector : public CppUnit::TestFixture {
0011   CPPUNIT_TEST_SUITE(testOwnVector);
0012   CPPUNIT_TEST(checkAll);
0013   CPPUNIT_TEST_SUITE_END();
0014 
0015 public:
0016   void setUp() {}
0017   void tearDown() {}
0018   void checkAll();
0019 };
0020 
0021 CPPUNIT_TEST_SUITE_REGISTRATION(testOwnVector);
0022 
0023 namespace test {
0024   struct Dummy {
0025     Dummy(int n, bool* r) : value(n), ref(r) {}
0026     ~Dummy() { *ref = true; }
0027     int value;
0028     bool operator<(const test::Dummy& o) const { return value < o.value; }
0029 
0030   private:
0031     edm::propagate_const<bool*> ref;
0032   };
0033 
0034   struct DummyComp {
0035     bool operator()(const Dummy& d1, const Dummy& d2) const { return d1.value < d2.value; }
0036   };
0037 
0038   class a {
0039   public:
0040     virtual ~a() {}
0041     virtual int f() const = 0;
0042   };
0043 
0044   class ClassB : public a {
0045   public:
0046     ClassB(int i) : ii(i) { memset(&waste, 0, sizeof(waste)); }
0047     virtual ~ClassB() {}
0048     virtual int f() const { return ii; }
0049     int ii;
0050 
0051   private:
0052     char waste[1024 * 1024];
0053   };
0054 
0055   class ss {
0056   public:
0057     bool operator()(const a& a1, const a& a2) const { return (a1.f() > a2.f()); }
0058   };
0059 
0060   std::ostream& operator<<(std::ostream& os, const a& aa) {
0061     os << aa.f();
0062     return os;
0063   }
0064 }  // namespace test
0065 
0066 void testOwnVector::checkAll() {
0067   {
0068     edm::OwnVector<test::Dummy> v;
0069     CPPUNIT_ASSERT(v.size() == 0);
0070     CPPUNIT_ASSERT(v.empty());
0071     bool deleted[4] = {false, false, false, false};
0072     v.push_back(new test::Dummy(0, deleted + 0));
0073     v.push_back(new test::Dummy(1, deleted + 1));
0074     v.push_back(new test::Dummy(2, deleted + 2));
0075     v.push_back(new test::Dummy(3, deleted + 3));
0076     CPPUNIT_ASSERT(v.size() == 4);
0077     edm::OwnVector<test::Dummy>::iterator i;
0078     i = v.begin();
0079     edm::OwnVector<test::Dummy>::const_iterator ci = i;
0080     *ci;
0081     v.sort();
0082     v.sort(test::DummyComp());
0083     CPPUNIT_ASSERT(!v.empty());
0084     CPPUNIT_ASSERT(v[0].value == 0);
0085     CPPUNIT_ASSERT(v[1].value == 1);
0086     CPPUNIT_ASSERT(v[2].value == 2);
0087     CPPUNIT_ASSERT(v[3].value == 3);
0088     i = v.begin() + 1;
0089     v.erase(i);
0090     CPPUNIT_ASSERT(!deleted[0]);
0091     CPPUNIT_ASSERT(deleted[1]);
0092     CPPUNIT_ASSERT(!deleted[2]);
0093     CPPUNIT_ASSERT(!deleted[3]);
0094     CPPUNIT_ASSERT(v.size() == 3);
0095     CPPUNIT_ASSERT(v[0].value == 0);
0096     CPPUNIT_ASSERT(v[1].value == 2);
0097     CPPUNIT_ASSERT(v[2].value == 3);
0098     edm::OwnVector<test::Dummy>::iterator b = v.begin(), e = b + 1;
0099     v.erase(b, e);
0100     CPPUNIT_ASSERT(v.size() == 2);
0101     CPPUNIT_ASSERT(deleted[0]);
0102     CPPUNIT_ASSERT(deleted[1]);
0103     CPPUNIT_ASSERT(!deleted[2]);
0104     CPPUNIT_ASSERT(!deleted[3]);
0105     v.clear();
0106     CPPUNIT_ASSERT(v.size() == 0);
0107     CPPUNIT_ASSERT(v.empty());
0108     CPPUNIT_ASSERT(deleted[0]);
0109     CPPUNIT_ASSERT(deleted[1]);
0110     CPPUNIT_ASSERT(deleted[2]);
0111     CPPUNIT_ASSERT(deleted[3]);
0112   }
0113   {
0114     edm::OwnVector<test::a> v;
0115     test::a* aa = new test::ClassB(2);
0116     v.push_back(aa);
0117     aa = new test::ClassB(1);
0118     v.push_back(aa);
0119     aa = new test::ClassB(3);
0120     v.push_back(aa);
0121     v.sort(test::ss());
0122     std::cout << "OwnVector : dumping contents" << std::endl;
0123     std::copy(v.begin(), v.end(), std::ostream_iterator<test::a>(std::cout, "\t"));
0124 
0125     edm::Ptr<test::a> ptr_v;
0126     unsigned long index(0);
0127     void const* data = &v[0];
0128     v.setPtr(typeid(test::a), index, data);
0129     test::a const* data_a = static_cast<test::a const*>(data);
0130     test::ClassB const* data_b = dynamic_cast<test::ClassB const*>(data_a);
0131     CPPUNIT_ASSERT(data != 0);
0132     CPPUNIT_ASSERT(data_a != 0);
0133     CPPUNIT_ASSERT(data_b != 0);
0134     if (data_b != 0) {  // To silence Coverity
0135       CPPUNIT_ASSERT(data_b->f() == 3);
0136     }
0137   }
0138 }