Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include <algorithm>
0003 
0004 #include "cppunit/extensions/HelperMacros.h"
0005 #include "DataFormats/Common/interface/TestHandle.h"
0006 #include "DataFormats/Common/interface/RefToBaseVector.h"
0007 #include "DataFormats/Common/interface/RefVector.h"
0008 #include "DataFormats/Common/interface/Ref.h"
0009 
0010 #include <vector>
0011 
0012 class testRefToBaseVector : public CppUnit::TestFixture {
0013   CPPUNIT_TEST_SUITE(testRefToBaseVector);
0014   CPPUNIT_TEST(check);
0015   CPPUNIT_TEST_SUITE_END();
0016 
0017 public:
0018   void setUp() {}
0019   void tearDown() {}
0020   void check();
0021 };
0022 
0023 CPPUNIT_TEST_SUITE_REGISTRATION(testRefToBaseVector);
0024 
0025 namespace testreftobase {
0026   struct Base {
0027     virtual ~Base() {}
0028     virtual int val() const = 0;
0029     bool operator==(Base const& other) const { return val() == other.val(); }
0030   };
0031 
0032   struct Inherit1 : public Base {
0033     virtual int val() const { return 1; }
0034   };
0035   struct Inherit2 : public Base {
0036     virtual int val() const { return 2; }
0037   };
0038 }  // namespace testreftobase
0039 
0040 using namespace testreftobase;
0041 
0042 void do_some_tests(edm::RefToBaseVector<Base> x) {
0043   edm::RefToBaseVector<Base> copy(x);
0044 
0045   CPPUNIT_ASSERT(x.empty() == copy.empty());
0046   CPPUNIT_ASSERT(x.size() == copy.size());
0047   edm::RefToBaseVector<Base>::const_iterator b = x.begin(), e = x.end(), cb = copy.begin(), ce = copy.end();
0048   CPPUNIT_ASSERT(e - b == ce - cb);
0049   CPPUNIT_ASSERT(std::distance(b, e) == std::distance(cb, ce));
0050 }
0051 
0052 void testRefToBaseVector::check() {
0053   using namespace edm;
0054 
0055   std::vector<Inherit1> v1(2, Inherit1());
0056   std::vector<Inherit2> v2(2, Inherit2());
0057 
0058   TestHandle<std::vector<Inherit1> > h1(&v1, ProductID(1, 1));
0059   RefVector<std::vector<Inherit1> > rv1;
0060   rv1.push_back(Ref<std::vector<Inherit1> >(h1, 0));
0061   rv1.push_back(Ref<std::vector<Inherit1> >(h1, 1));
0062   TestHandle<std::vector<Inherit2> > h2(&v2, ProductID(1, 2));
0063   RefVector<std::vector<Inherit2> > rv2;
0064   rv2.push_back(Ref<std::vector<Inherit2> >(h2, 0));
0065   rv2.push_back(Ref<std::vector<Inherit2> >(h2, 1));
0066 
0067   RefToBaseVector<Base> empty;
0068   RefToBaseVector<Base> copy_of_empty(empty);
0069 
0070   CPPUNIT_ASSERT(empty == copy_of_empty);
0071 
0072   RefToBaseVector<Base> bv1(rv1);
0073   RefToBase<Base> r1_0 = bv1[0];
0074   RefToBase<Base> r1_1 = bv1[1];
0075   RefToBaseVector<Base> bv2(rv2);
0076   RefToBase<Base> r2_0 = bv2[0];
0077   RefToBase<Base> r2_1 = bv2[1];
0078 
0079   CPPUNIT_ASSERT(bv1.empty() == false);
0080   CPPUNIT_ASSERT(bv1.size() == 2);
0081   CPPUNIT_ASSERT(bv2.size() == 2);
0082   CPPUNIT_ASSERT(r1_0->val() == 1);
0083   CPPUNIT_ASSERT(r1_1->val() == 1);
0084   CPPUNIT_ASSERT(r2_0->val() == 2);
0085   CPPUNIT_ASSERT(r2_1->val() == 2);
0086 
0087   RefToBaseVector<Base>::const_iterator b = bv1.begin(), e = bv1.end();
0088   RefToBaseVector<Base>::const_iterator i = b;
0089   CPPUNIT_ASSERT((*i)->val() == 1);
0090   CPPUNIT_ASSERT(i != e);
0091   CPPUNIT_ASSERT(i - b == 0);
0092   ++i;
0093   CPPUNIT_ASSERT((*i)->val() == 1);
0094   CPPUNIT_ASSERT(i != e);
0095   CPPUNIT_ASSERT(i - b == 1);
0096   ++i;
0097   CPPUNIT_ASSERT(i == e);
0098 
0099   RefToBaseVector<Base> assigned_from_bv1;
0100   do_some_tests(assigned_from_bv1);
0101   CPPUNIT_ASSERT(assigned_from_bv1.empty());
0102   assigned_from_bv1 = bv1;
0103   CPPUNIT_ASSERT(assigned_from_bv1.size() == bv1.size());
0104   CPPUNIT_ASSERT(std::equal(bv1.begin(), bv1.end(), assigned_from_bv1.begin()));
0105   CPPUNIT_ASSERT(assigned_from_bv1 == bv1);
0106 
0107   do_some_tests(assigned_from_bv1);
0108 
0109   /// creation of empty vector adding with push_back
0110   RefToBaseVector<Base> bv3;
0111   bv3.push_back(r1_0);
0112   CPPUNIT_ASSERT(bv3.size() == 1);
0113   CPPUNIT_ASSERT(&(*r1_0) == &(*bv3[0]));
0114   bv3.push_back(r1_1);
0115   CPPUNIT_ASSERT(bv3.size() == 2);
0116   CPPUNIT_ASSERT(&(*r1_1) == &(*bv3[1]));
0117 }