Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:58

0001 #include "cppunit/extensions/HelperMacros.h"
0002 #include <algorithm>
0003 #include <iterator>
0004 #include <iostream>
0005 #include "DataFormats/Common/interface/ValueMap.h"
0006 #include "DataFormats/Common/interface/RefProd.h"
0007 #include "DataFormats/Common/interface/TestHandle.h"
0008 using namespace edm;
0009 
0010 class testValueMapNew : public CppUnit::TestFixture {
0011   CPPUNIT_TEST_SUITE(testValueMapNew);
0012   CPPUNIT_TEST(checkAll);
0013   CPPUNIT_TEST_SUITE_END();
0014   typedef std::vector<int> CKey1;
0015   typedef std::vector<float> CKey2;
0016 
0017 public:
0018   testValueMapNew();
0019   void setUp() {}
0020   void tearDown() {}
0021   void checkAll();
0022   void test(const edm::ValueMap<int> &);
0023   CKey1 v1;
0024   CKey2 v2;
0025   edm::TestHandle<CKey1> handleK1;
0026   edm::TestHandle<CKey2> handleK2;
0027   std::vector<int> w1, w2;
0028 };
0029 
0030 CPPUNIT_TEST_SUITE_REGISTRATION(testValueMapNew);
0031 
0032 testValueMapNew::testValueMapNew() {
0033   v1.push_back(1);
0034   v1.push_back(2);
0035   v1.push_back(3);
0036   v1.push_back(4);
0037   ProductID const pidK1(1, 2);
0038   handleK1 = edm::TestHandle<CKey1>(&v1, pidK1);
0039 
0040   v2.push_back(10.);
0041   v2.push_back(20.);
0042   v2.push_back(30.);
0043   v2.push_back(40.);
0044   v2.push_back(50.);
0045   ProductID const pidK2(1, 3);
0046   handleK2 = edm::TestHandle<CKey2>(&v2, pidK2);
0047 
0048   const int ww1[4] = {2, 1, 0, 2};
0049   w1.resize(4);
0050   std::copy(ww1, ww1 + 4, w1.begin());
0051   const int ww2[5] = {1, 0, 2, 1, -1};
0052   w2.resize(5);
0053   std::copy(ww2, ww2 + 5, w2.begin());
0054 }
0055 
0056 void testValueMapNew::checkAll() {
0057   {
0058     edm::ValueMap<int> values;
0059     edm::ValueMap<int>::Filler filler(values);
0060     filler.insert(handleK1, w1.begin(), w1.end());
0061     filler.insert(handleK2, w2.begin(), w2.end());
0062     filler.fill();
0063     test(values);
0064   }
0065   {
0066     edm::ValueMap<int> values;
0067     edm::ValueMap<int>::Filler filler1(values);
0068     filler1.insert(handleK1, w1.begin(), w1.end());
0069     filler1.fill();
0070     edm::ValueMap<int>::Filler filler2(values);
0071     filler2.insert(handleK2, w2.begin(), w2.end());
0072     filler2.fill();
0073     test(values);
0074   }
0075   {
0076     edm::ValueMap<int> values1;
0077     edm::ValueMap<int>::Filler filler1(values1);
0078     filler1.insert(handleK1, w1.begin(), w1.end());
0079     filler1.fill();
0080     edm::ValueMap<int> values2;
0081     edm::ValueMap<int>::Filler filler2(values2);
0082     filler2.insert(handleK2, w2.begin(), w2.end());
0083     filler2.fill();
0084     edm::ValueMap<int> values = values1 + values2;
0085     test(values);
0086   }
0087 }
0088 
0089 void testValueMapNew::test(const edm::ValueMap<int> &values) {
0090   CPPUNIT_ASSERT(values.idSize() == 2);
0091   CPPUNIT_ASSERT(!values.contains(ProductID(1, 0)));
0092   CPPUNIT_ASSERT(!values.contains(ProductID(1, 1)));
0093   CPPUNIT_ASSERT(values.contains(ProductID(1, 2)));
0094   CPPUNIT_ASSERT(values.contains(ProductID(1, 3)));
0095   CPPUNIT_ASSERT(!values.contains(ProductID(1, 4)));
0096   CPPUNIT_ASSERT(!values.contains(ProductID(1, 5)));
0097   int r1 = values[edm::Ref<CKey1>(handleK1, 0)];
0098   int r2 = values[edm::Ref<CKey1>(handleK1, 1)];
0099   int r3 = values[edm::Ref<CKey1>(handleK1, 2)];
0100   int r4 = values[edm::Ref<CKey1>(handleK1, 3)];
0101   CPPUNIT_ASSERT(r1 == w1[0]);
0102   CPPUNIT_ASSERT(r2 == w1[1]);
0103   CPPUNIT_ASSERT(r3 == w1[2]);
0104   CPPUNIT_ASSERT(r4 == w1[3]);
0105   int s1 = values[edm::Ref<CKey2>(handleK2, 0)];
0106   int s2 = values[edm::Ref<CKey2>(handleK2, 1)];
0107   int s3 = values[edm::Ref<CKey2>(handleK2, 2)];
0108   int s4 = values[edm::Ref<CKey2>(handleK2, 3)];
0109   int s5 = values[edm::Ref<CKey2>(handleK2, 4)];
0110   CPPUNIT_ASSERT(s1 == w2[0]);
0111   CPPUNIT_ASSERT(s2 == w2[1]);
0112   CPPUNIT_ASSERT(s3 == w2[2]);
0113   CPPUNIT_ASSERT(s4 == w2[3]);
0114   CPPUNIT_ASSERT(s5 == w2[4]);
0115   CPPUNIT_ASSERT(values.size() == w1.size() + w2.size());
0116   edm::ValueMap<int>::const_iterator b = values.begin(), e = values.end(), i;
0117   CPPUNIT_ASSERT(e - b == 2);
0118   CPPUNIT_ASSERT(b.id() == ProductID(1, 2));
0119   CPPUNIT_ASSERT((b + 1).id() == ProductID(1, 3));
0120   ProductID pids[] = {ProductID(1, 2), ProductID(1, 3)};
0121   const std::vector<int> *w[] = {&w1, &w2};
0122   for (i = b; i != e; ++i) {
0123     size_t idx = i - b;
0124     CPPUNIT_ASSERT(i.id() == pids[idx]);
0125     CPPUNIT_ASSERT(i.size() == w[idx]->size());
0126     {
0127       std::vector<int>::const_iterator bb = i.begin(), ee = i.end(), j;
0128       for (j = bb; j != ee; ++j) {
0129         size_t jdx = j - bb;
0130         CPPUNIT_ASSERT(*j == (*w[idx])[jdx]);
0131         CPPUNIT_ASSERT(i[jdx] == (*w[idx])[jdx]);
0132       }
0133     }
0134   }
0135 }