Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:25

0001 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"
0002 #include "cppunit/extensions/HelperMacros.h"
0003 
0004 #include "DataFormats/Common/interface/View.h"
0005 
0006 class testView : public CppUnit::TestFixture {
0007   CPPUNIT_TEST_SUITE(testView);
0008   CPPUNIT_TEST(basic);
0009   CPPUNIT_TEST(createFromArray);
0010   CPPUNIT_TEST(directAccess);
0011   CPPUNIT_TEST(iterateForward);
0012   CPPUNIT_TEST(iterateBackward);
0013   CPPUNIT_TEST(cloning);
0014   CPPUNIT_TEST(ptrs);
0015   CPPUNIT_TEST_SUITE_END();
0016 
0017 public:
0018   testView() {}
0019   ~testView() {}
0020   void setUp() {}
0021   void tearDown() {}
0022 
0023   void basic();
0024   void createFromArray();
0025   void directAccess();
0026   void iterateForward();
0027   void iterateBackward();
0028   void cloning();
0029   void ptrs();
0030 
0031 private:
0032   typedef int value_type;
0033   typedef edm::View<value_type> View;
0034   typedef View::const_iterator const_iterator;
0035   typedef View::const_reverse_iterator const_reverse_iterator;
0036   typedef View::const_reference const_reference;
0037   typedef View::size_type size_type;
0038 };
0039 
0040 ///registration of the test so that the runner can find it
0041 CPPUNIT_TEST_SUITE_REGISTRATION(testView);
0042 
0043 void testView::basic() {
0044   View v1;
0045   CPPUNIT_ASSERT(v1.size() == 0);
0046   CPPUNIT_ASSERT(v1.empty());
0047   View v2(v1);
0048   CPPUNIT_ASSERT(v1 == v2);
0049   View v3;
0050   v3 = v1;
0051   CPPUNIT_ASSERT(v3 == v1);
0052 
0053   CPPUNIT_ASSERT(!(v1 < v2));
0054   CPPUNIT_ASSERT(v1 <= v2);
0055   CPPUNIT_ASSERT(!(v1 > v2));
0056   CPPUNIT_ASSERT(v1 >= v2);
0057   CPPUNIT_ASSERT(!(v1 != v2));
0058 }
0059 
0060 void testView::createFromArray() {
0061   value_type vals[] = {1, 2, 3, 4, 5};
0062   size_t sz = sizeof(vals) / sizeof(value_type);
0063 
0064   View v1;
0065   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0066   CPPUNIT_ASSERT(v1.size() == 5);
0067   View v2;
0068   edm::View<int>::fill_from_range(vals, vals + sz, v2);
0069   CPPUNIT_ASSERT(v1 == v2);
0070 }
0071 
0072 void testView::directAccess() {
0073   value_type vals[] = {1, 2, 3, 4, 5};
0074   size_t sz = sizeof(vals) / sizeof(value_type);
0075 
0076   View v1;
0077   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0078   for (size_type i = 0; i < v1.size(); ++i) {
0079     CPPUNIT_ASSERT(v1[i] == vals[i]);
0080   }
0081 }
0082 
0083 void testView::iterateForward() {
0084   value_type vals[] = {1, 2, 3, 4, 5};
0085   size_t sz = sizeof(vals) / sizeof(value_type);
0086 
0087   View v1;
0088   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0089 
0090   const_iterator i = v1.begin();
0091   CPPUNIT_ASSERT(*i == 1);
0092   ++i;
0093   CPPUNIT_ASSERT(*i == 2);
0094 }
0095 
0096 void testView::iterateBackward() {
0097   value_type vals[] = {1, 2, 3, 4, 5};
0098   size_t sz = sizeof(vals) / sizeof(value_type);
0099 
0100   View v1;
0101   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0102 
0103   const_reverse_iterator i = v1.rbegin();
0104   CPPUNIT_ASSERT(*i == 5);
0105   ++i;
0106   CPPUNIT_ASSERT(*i == 4);
0107 }
0108 
0109 void testView::cloning() {
0110   value_type vals[] = {1, 2, 3, 4, 5};
0111   size_t sz = sizeof(vals) / sizeof(value_type);
0112 
0113   View v1;
0114   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0115 
0116   auto base = v1.clone();
0117   CPPUNIT_ASSERT(base);
0118   edm::View<int>* view = dynamic_cast<edm::View<int>*>(base.get());
0119   CPPUNIT_ASSERT(view);
0120   if (view) {
0121     CPPUNIT_ASSERT(*view == v1);
0122   }
0123 }
0124 
0125 void testView::ptrs() {
0126   value_type vals[] = {1, 2, 3, 4, 5};
0127   size_t sz = sizeof(vals) / sizeof(value_type);
0128 
0129   View v1;
0130   edm::View<int>::fill_from_range(vals, vals + sz, v1);
0131   size_t i = 0;
0132   for (auto ptr : v1.ptrs()) {
0133     CPPUNIT_ASSERT(*ptr == vals[i++]);
0134   }
0135 }