Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include <algorithm>
0003 
0004 #include "cppunit/extensions/HelperMacros.h"
0005 #include "DataFormats/Common/interface/TestHandle.h"
0006 #include "DataFormats/Common/interface/ContainerMask.h"
0007 #include "DataFormats/Common/interface/RefProd.h"
0008 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0009 
0010 #include <vector>
0011 
0012 class testContainerMask : public CppUnit::TestFixture {
0013   CPPUNIT_TEST_SUITE(testContainerMask);
0014   CPPUNIT_TEST(testVector);
0015   CPPUNIT_TEST(testDetSetVector);
0016   CPPUNIT_TEST_SUITE_END();
0017 
0018 public:
0019   void setUp() {}
0020   void tearDown() {}
0021   void testVector();
0022   void testDetSetVector();
0023 };
0024 
0025 CPPUNIT_TEST_SUITE_REGISTRATION(testContainerMask);
0026 
0027 void testContainerMask::testVector() {
0028   const unsigned int values[] = {0, 1, 2, 3};
0029   const bool flags[] = {true, false, true, false};
0030   const unsigned int kSize = 4;
0031   std::vector<unsigned int> c(values, values + kSize);
0032   std::vector<bool> mask(flags, flags + kSize);
0033   CPPUNIT_ASSERT(mask.size() == c.size());
0034   CPPUNIT_ASSERT(mask.size() == kSize);
0035 
0036   typedef std::vector<unsigned int> ContainerType;
0037   edm::TestHandle<ContainerType> h(&c, edm::ProductID(1, 1));
0038 
0039   const edm::RefProd<ContainerType> rp(h);
0040 
0041   edm::ContainerMask<ContainerType> cMask(rp, mask);
0042 
0043   CPPUNIT_ASSERT(cMask.size() == mask.size());
0044 
0045   unsigned int index = 0;
0046   for (std::vector<bool>::iterator it = mask.begin(), itEnd = mask.end(); it != itEnd; ++it, ++index) {
0047     CPPUNIT_ASSERT(*it == cMask.mask(index));
0048   }
0049 
0050   index = 0;
0051   for (std::vector<unsigned int>::iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it, ++index) {
0052     CPPUNIT_ASSERT(mask[index] == cMask.mask(&(*it)));
0053   }
0054 
0055   {
0056     std::vector<bool> alternate(4, false);
0057     alternate[1] = true;
0058     cMask.applyOrTo(alternate);
0059     CPPUNIT_ASSERT(alternate[0]);
0060     CPPUNIT_ASSERT(alternate[1]);
0061     CPPUNIT_ASSERT(alternate[2]);
0062     CPPUNIT_ASSERT(!alternate[3]);
0063   }
0064   {
0065     std::vector<bool> alternate(3, false);
0066     cMask.copyMaskTo(alternate);
0067     CPPUNIT_ASSERT(alternate[0]);
0068     CPPUNIT_ASSERT(!alternate[1]);
0069     CPPUNIT_ASSERT(alternate[2]);
0070     CPPUNIT_ASSERT(!alternate[3]);
0071   }
0072 }
0073 
0074 void testContainerMask::testDetSetVector() {
0075   const bool flags[] = {true, false, true, false};
0076   const unsigned int kSize = 4;
0077   edmNew::DetSetVector<unsigned int> c;
0078   {
0079     edmNew::DetSetVector<unsigned int>::FastFiller filler(c, 0);
0080     filler.push_back(0);
0081     filler.push_back(1);
0082   }
0083   {
0084     edmNew::DetSetVector<unsigned int>::FastFiller filler(c, 1);
0085     filler.push_back(2);
0086     filler.push_back(3);
0087   }
0088   std::vector<bool> mask(flags, flags + kSize);
0089   CPPUNIT_ASSERT(mask.size() == c.dataSize());
0090   CPPUNIT_ASSERT(mask.size() == kSize);
0091 
0092   typedef edmNew::DetSetVector<unsigned int> ContainerType;
0093 
0094   edm::TestHandle<ContainerType> h(&c, edm::ProductID(1, 1));
0095 
0096   const edm::RefProd<ContainerType> rp(h);
0097 
0098   edm::ContainerMask<ContainerType> cMask(rp, mask);
0099 
0100   CPPUNIT_ASSERT(cMask.size() == mask.size());
0101 
0102   unsigned int index = 0;
0103   for (std::vector<bool>::iterator it = mask.begin(), itEnd = mask.end(); it != itEnd; ++it, ++index) {
0104     CPPUNIT_ASSERT(*it == cMask.mask(index));
0105   }
0106 
0107   index = 0;
0108   for (std::vector<unsigned int>::const_iterator it = c.data().begin(), itEnd = c.data().end(); it != itEnd;
0109        ++it, ++index) {
0110     CPPUNIT_ASSERT(mask[index] == cMask.mask(&(*it)));
0111   }
0112 
0113   std::vector<bool> alternate(4, false);
0114   alternate[1] = true;
0115   cMask.applyOrTo(alternate);
0116   CPPUNIT_ASSERT(alternate[0]);
0117   CPPUNIT_ASSERT(alternate[1]);
0118   CPPUNIT_ASSERT(alternate[2]);
0119   CPPUNIT_ASSERT(!alternate[3]);
0120 }