Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:23

0001 #include <cppunit/extensions/HelperMacros.h>
0002 
0003 #include "FWCore/Utilities/interface/VecArray.h"
0004 
0005 class testVecArray : public CppUnit::TestFixture {
0006   CPPUNIT_TEST_SUITE(testVecArray);
0007   CPPUNIT_TEST(test);
0008   CPPUNIT_TEST_SUITE_END();
0009 
0010 public:
0011   void setUp() {}
0012   void tearDown() {}
0013 
0014   void test();
0015   template <typename T>
0016   void testIterators(T& array);
0017   template <typename T>
0018   void testIterator(T iter, T end);
0019 };
0020 
0021 ///registration of the test so that the runner can find it
0022 CPPUNIT_TEST_SUITE_REGISTRATION(testVecArray);
0023 
0024 void testVecArray::test() {
0025   edm::VecArray<int, 4> array;
0026   CPPUNIT_ASSERT(array.empty());
0027   CPPUNIT_ASSERT(array.size() == 0);
0028   CPPUNIT_ASSERT(array.capacity() == 4);
0029   CPPUNIT_ASSERT((edm::VecArray<int, 4>::capacity() == 4));
0030 
0031   auto iter = array.begin();
0032   auto end = array.end();
0033   CPPUNIT_ASSERT(iter == end);
0034 
0035   array.push_back_unchecked(1);
0036   CPPUNIT_ASSERT(!array.empty());
0037   CPPUNIT_ASSERT(array.size() == 1);
0038   CPPUNIT_ASSERT(array.front() == 1);
0039   CPPUNIT_ASSERT(array.back() == 1);
0040   iter = array.begin();
0041   end = array.end();
0042   CPPUNIT_ASSERT(iter != end);
0043   CPPUNIT_ASSERT(*iter == 1);
0044   ++iter;
0045   CPPUNIT_ASSERT(iter == end);
0046 
0047   array.emplace_back_unchecked(2);
0048   CPPUNIT_ASSERT(array.size() == 2);
0049   CPPUNIT_ASSERT(array.back() == 2);
0050   array.push_back(3);
0051   CPPUNIT_ASSERT(array.size() == 3);
0052   CPPUNIT_ASSERT(array.back() == 3);
0053   array.emplace_back(4);
0054   CPPUNIT_ASSERT(array.size() == 4);
0055   CPPUNIT_ASSERT(array.front() == 1);
0056   CPPUNIT_ASSERT(array.back() == 4);
0057   CPPUNIT_ASSERT(array[0] == 1 && array[1] == 2 && array[2] == 3 && array[3] == 4);
0058 
0059   CPPUNIT_ASSERT_THROW(array.push_back(5), std::length_error);
0060   CPPUNIT_ASSERT_THROW(array.emplace_back(5), std::length_error);
0061 
0062   auto ptr = array.data();
0063   CPPUNIT_ASSERT(ptr[0] == 1 && ptr[1] == 2 && ptr[2] == 3 && ptr[3] == 4);
0064 
0065   testIterators(array);
0066   testIterators(const_cast<const decltype(array)&>(array));
0067 
0068   edm::VecArray<int, 4> array2;
0069   array2.push_back(11);
0070   array2.push_back(12);
0071 
0072   array.swap(array2);
0073   CPPUNIT_ASSERT(array.size() == 2);
0074   CPPUNIT_ASSERT(array2.size() == 4);
0075   CPPUNIT_ASSERT(array[0] == 11 && array[1] == 12);
0076   CPPUNIT_ASSERT(array2[0] == 1 && array2[1] == 2 && array2[2] == 3 && array2[3] == 4);
0077 
0078   array = array2;
0079   CPPUNIT_ASSERT(array.size() == array2.size());
0080   CPPUNIT_ASSERT(array.size() == 4);
0081   CPPUNIT_ASSERT(array[0] == 1 && array[1] == 2 && array[2] == 3 && array[3] == 4);
0082   CPPUNIT_ASSERT(array2[0] == 1 && array2[1] == 2 && array2[2] == 3 && array2[3] == 4);
0083 
0084   ptr = array.data();
0085   ptr[1] = 10;
0086   iter = array.begin() + 1;
0087   CPPUNIT_ASSERT(*iter == 10);
0088 
0089   iter = array.begin() + 2;
0090   *iter = 50;
0091   CPPUNIT_ASSERT(array[0] == 1 && array[1] == 10 && array[2] == 50 && array[3] == 4);
0092 
0093   CPPUNIT_ASSERT(!array.empty());
0094   array.clear();
0095   CPPUNIT_ASSERT(array.empty());
0096   CPPUNIT_ASSERT(array.size() == 0);
0097   CPPUNIT_ASSERT(array2.size() == 4);
0098 
0099   array.resize(2);
0100   CPPUNIT_ASSERT(array.size() == 2);
0101   array.pop_back();
0102   CPPUNIT_ASSERT(array.size() == 1);
0103   CPPUNIT_ASSERT_THROW(array.resize(6), std::length_error);
0104   CPPUNIT_ASSERT(array.size() == 1);
0105   array.resize(4);
0106   CPPUNIT_ASSERT(array.size() == 4);
0107   array.resize(1);
0108   CPPUNIT_ASSERT(array.size() == 1);
0109 }
0110 
0111 template <typename T>
0112 void testVecArray::testIterators(T& array) {
0113   testIterator(array.begin(), array.end());
0114   testIterator(array.cbegin(), array.cend());
0115 }
0116 template <typename T>
0117 void testVecArray::testIterator(T iter, T end) {
0118   CPPUNIT_ASSERT(iter != end);
0119   CPPUNIT_ASSERT(*iter == 1);
0120   ++iter;
0121   CPPUNIT_ASSERT(iter != end);
0122   CPPUNIT_ASSERT(*iter == 2);
0123   ++iter;
0124   CPPUNIT_ASSERT(iter != end);
0125   CPPUNIT_ASSERT(*iter == 3);
0126   ++iter;
0127   CPPUNIT_ASSERT(iter != end);
0128   CPPUNIT_ASSERT(*iter == 4);
0129   ++iter;
0130   CPPUNIT_ASSERT(iter == end);
0131 }