Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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