Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:17

0001 #include "CommonTools/Utils/interface/associationMapFilterValues.h"
0002 #include "DataFormats/Common/interface/AssociationMap.h"
0003 
0004 #include <cppunit/extensions/HelperMacros.h>
0005 
0006 class testAssociationMapFilterValues : public CppUnit::TestFixture {
0007   CPPUNIT_TEST_SUITE(testAssociationMapFilterValues);
0008   CPPUNIT_TEST(checkOneToOne);
0009   CPPUNIT_TEST(checkOneToMany);
0010   CPPUNIT_TEST(checkOneToManyQuality);
0011   CPPUNIT_TEST_SUITE_END();
0012 
0013 public:
0014   void setUp() {}
0015   void tearDown() {}
0016   void checkOneToOne();
0017   void checkOneToMany();
0018   void checkOneToManyQuality();
0019 };
0020 
0021 CPPUNIT_TEST_SUITE_REGISTRATION(testAssociationMapFilterValues);
0022 
0023 namespace {
0024   class Base {
0025   public:
0026     explicit Base(double v) : v_(v) {}
0027     virtual ~Base() {}
0028 
0029     double value() const { return v_; }
0030 
0031   private:
0032     double v_;
0033   };
0034 
0035   class Derived : public Base {
0036   public:
0037     explicit Derived(double v) : Base(v) {}
0038     virtual ~Derived() {}
0039   };
0040 }  // namespace
0041 
0042 void testAssociationMapFilterValues::checkOneToOne() {
0043   typedef std::vector<int> CKey;
0044   typedef std::vector<double> CVal;
0045   typedef edm::AssociationMap<edm::OneToOne<CKey, CVal, unsigned char>> Assoc;
0046 
0047   CKey keys{1, 2, 3};
0048   CVal values{1.0, 2.0, 3.0};
0049 
0050   auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0051   auto map = Assoc(refprod);
0052   map.insert(edm::Ref<CKey>(&keys, 0), edm::Ref<CVal>(&values, 0));
0053   map.insert(edm::Ref<CKey>(&keys, 1), edm::Ref<CVal>(&values, 1));
0054   map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 2));
0055 
0056   // vector of Refs
0057   std::vector<edm::Ref<CVal>> keep{edm::Ref<CVal>(&values, 0), edm::Ref<CVal>(&values, 2)};
0058   Assoc filtered = associationMapFilterValues(map, keep);
0059   CPPUNIT_ASSERT(filtered.size() == 2);
0060   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) != filtered.end());
0061   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) == filtered.end());
0062   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 2)) != filtered.end());
0063 
0064   // RefVector
0065   edm::RefVector<CVal> keep2;
0066   keep2.push_back(edm::Ref<CVal>(&values, 1));
0067   filtered = associationMapFilterValues(map, keep2);
0068   CPPUNIT_ASSERT(filtered.size() == 1);
0069   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) == filtered.end());
0070   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) != filtered.end());
0071   CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 2)) == filtered.end());
0072 
0073   // Mostly check that it compiles
0074   edm::View<CVal> keepView;
0075   filtered = associationMapFilterValues(map, keepView);
0076   CPPUNIT_ASSERT(filtered.size() == 0);
0077 }
0078 
0079 void testAssociationMapFilterValues::checkOneToMany() {
0080   // simple data
0081   {
0082     typedef std::vector<int> CKey;
0083     typedef std::vector<double> CVal;
0084     typedef edm::AssociationMap<edm::OneToMany<CKey, CVal, unsigned char>> Assoc;
0085 
0086     CKey keys{1, 2, 3};
0087     CVal values{1.0, 2.0, 3.0, 4.0};
0088 
0089     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0090     auto map = Assoc(refprod);
0091     map.insert(edm::Ref<CKey>(&keys, 0), edm::Ref<CVal>(&values, 0));
0092     map.insert(edm::Ref<CKey>(&keys, 1), edm::Ref<CVal>(&values, 1));
0093     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 2));
0094     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 3));
0095 
0096     std::vector<edm::Ref<CVal>> keep{edm::Ref<CVal>(&values, 0), edm::Ref<CVal>(&values, 2)};
0097 
0098     Assoc filtered = associationMapFilterValues(map, keep);
0099     CPPUNIT_ASSERT(filtered.size() == 2);
0100     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) != filtered.end());
0101     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) == filtered.end());
0102     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 2)) != filtered.end());
0103   }
0104 
0105   // Ref data
0106   {
0107     typedef std::vector<int> CKey;
0108     typedef std::vector<double> Data;
0109     typedef std::vector<edm::Ref<Data>> CVal;
0110     typedef edm::AssociationMap<edm::OneToMany<CKey, CVal, unsigned char>> Assoc;
0111 
0112     CKey keys{1, 2, 3};
0113     Data data{1.0, 2.0, 3.0, 4.0};
0114     CVal values{edm::Ref<Data>(&data, 0), edm::Ref<Data>(&data, 1), edm::Ref<Data>(&data, 2)};
0115 
0116     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0117     auto map = Assoc(refprod);
0118     map.insert(edm::Ref<CKey>(&keys, 0), edm::Ref<CVal>(&values, 0));
0119     map.insert(edm::Ref<CKey>(&keys, 1), edm::Ref<CVal>(&values, 1));
0120     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 1));
0121     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 0));
0122     map.insert(edm::Ref<CKey>(&keys, 3), edm::Ref<CVal>(&values, 2));
0123 
0124     std::vector<edm::Ref<CVal>> keep{edm::Ref<CVal>(&values, 0), edm::Ref<CVal>(&values, 2)};
0125 
0126     Assoc filtered = associationMapFilterValues(map, keep);
0127     CPPUNIT_ASSERT(filtered.size() == 3);
0128     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) != filtered.end());
0129     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) == filtered.end());
0130     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 2)) != filtered.end());
0131     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 3)) != filtered.end());
0132   }
0133 
0134   // RefToBase data
0135   {
0136     typedef std::vector<int> CKey;
0137     typedef std::vector<Derived> Data;
0138     typedef std::vector<edm::RefToBase<Base>> CVal;
0139     typedef edm::AssociationMap<edm::OneToMany<CKey, CVal, unsigned char>> Assoc;
0140 
0141     CKey keys{1, 2, 3};
0142     Data data{Derived(1.0), Derived(2.0), Derived(3.0), Derived(4.0)};
0143 
0144     CVal values{edm::RefToBase<Base>(edm::Ref<Data>(&data, 0)),
0145                 edm::RefToBase<Base>(edm::Ref<Data>(&data, 1)),
0146                 edm::RefToBase<Base>(edm::Ref<Data>(&data, 2))};
0147 
0148     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0149     auto map = Assoc(refprod);
0150     map.insert(edm::Ref<CKey>(&keys, 0), edm::Ref<CVal>(&values, 0));
0151     map.insert(edm::Ref<CKey>(&keys, 1), edm::Ref<CVal>(&values, 1));
0152     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 1));
0153     map.insert(edm::Ref<CKey>(&keys, 2), edm::Ref<CVal>(&values, 0));
0154     map.insert(edm::Ref<CKey>(&keys, 3), edm::Ref<CVal>(&values, 2));
0155 
0156     std::vector<edm::RefToBase<Base>> keep{edm::RefToBase<Base>(edm::Ref<Data>(&data, 0)),
0157                                            edm::RefToBase<Base>(edm::Ref<Data>(&data, 2))};
0158 
0159     Assoc filtered = associationMapFilterValues(map, keep);
0160     CPPUNIT_ASSERT(filtered.size() == 3);
0161     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) != filtered.end());
0162     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) == filtered.end());
0163     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 2)) != filtered.end());
0164     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 3)) != filtered.end());
0165   }
0166 
0167   // View, mostly check that it compiles
0168   {
0169     typedef std::vector<int> CKey;
0170     typedef edm::View<Base> CVal;
0171     typedef edm::AssociationMap<edm::OneToMany<CKey, CVal, unsigned char>> Assoc;
0172 
0173     Assoc map;
0174     edm::View<Base> keep;
0175     Assoc filtered = associationMapFilterValues(map, keep);
0176     CPPUNIT_ASSERT(filtered.size() == 0);
0177   }
0178 }
0179 
0180 void testAssociationMapFilterValues::checkOneToManyQuality() {
0181   // simple data
0182   {
0183     typedef std::vector<int> CKey;
0184     typedef std::vector<double> CVal;
0185     typedef edm::AssociationMap<edm::OneToManyWithQuality<CKey, CVal, double, unsigned char>> Assoc;
0186 
0187     CKey keys{1, 2, 3};
0188     CVal values{1.0, 2.0, 3.0, 4.0};
0189 
0190     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0191     auto map = Assoc(refprod);
0192     map.insert(edm::Ref<CKey>(&keys, 0), std::make_pair(edm::Ref<CVal>(&values, 0), 0.1));
0193     map.insert(edm::Ref<CKey>(&keys, 1), std::make_pair(edm::Ref<CVal>(&values, 1), 0.2));
0194     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 2), 0.3));
0195     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 3), 0.4));
0196 
0197     std::vector<edm::Ref<CVal>> keep{edm::Ref<CVal>(&values, 0), edm::Ref<CVal>(&values, 2)};
0198 
0199     Assoc filtered = associationMapFilterValues(map, keep);
0200     CPPUNIT_ASSERT(filtered.size() == 2);
0201     auto found = filtered.find(edm::Ref<CKey>(&keys, 0));
0202     CPPUNIT_ASSERT(found != filtered.end());
0203     CPPUNIT_ASSERT(found->val.size() == 1);
0204     CPPUNIT_ASSERT(found->val[0].second == 0.1);
0205     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 1)) == filtered.end());
0206     found = filtered.find(edm::Ref<CKey>(&keys, 2));
0207     CPPUNIT_ASSERT(found->val.size() == 1);
0208     CPPUNIT_ASSERT(found->val[0].second == 0.3);
0209   }
0210 
0211   // Ref data
0212   {
0213     typedef std::vector<int> CKey;
0214     typedef std::vector<double> Data;
0215     typedef std::vector<edm::Ref<Data>> CVal;
0216     typedef edm::AssociationMap<edm::OneToManyWithQuality<CKey, CVal, double, unsigned char>> Assoc;
0217 
0218     CKey keys{1, 2, 3};
0219     Data data{1.0, 2.0, 3.0, 4.0};
0220     CVal values{edm::Ref<Data>(&data, 0), edm::Ref<Data>(&data, 1), edm::Ref<Data>(&data, 2)};
0221 
0222     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0223     auto map = Assoc(refprod);
0224     map.insert(edm::Ref<CKey>(&keys, 0), std::make_pair(edm::Ref<CVal>(&values, 0), 0.1));
0225     map.insert(edm::Ref<CKey>(&keys, 1), std::make_pair(edm::Ref<CVal>(&values, 1), 0.2));
0226     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 1), 0.3));
0227     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 0), 0.4));
0228     map.insert(edm::Ref<CKey>(&keys, 3), std::make_pair(edm::Ref<CVal>(&values, 2), 0.5));
0229 
0230     std::vector<edm::Ref<CVal>> keep{edm::Ref<CVal>(&values, 0), edm::Ref<CVal>(&values, 1)};
0231 
0232     Assoc filtered = associationMapFilterValues(map, keep);
0233     CPPUNIT_ASSERT(filtered.size() == 3);
0234     auto found = filtered.find(edm::Ref<CKey>(&keys, 0));
0235     CPPUNIT_ASSERT(found != filtered.end());
0236     CPPUNIT_ASSERT(found->val.size() == 1);
0237     CPPUNIT_ASSERT(found->val[0].second == 0.1);
0238     found = filtered.find(edm::Ref<CKey>(&keys, 1));
0239     CPPUNIT_ASSERT(found != filtered.end());
0240     CPPUNIT_ASSERT(found->val.size() == 1);
0241     CPPUNIT_ASSERT(found->val[0].second == 0.2);
0242     found = filtered.find(edm::Ref<CKey>(&keys, 2));
0243     CPPUNIT_ASSERT(found != filtered.end());
0244     CPPUNIT_ASSERT(found->val.size() == 2);
0245     CPPUNIT_ASSERT(found->val[0].second == 0.3);
0246     CPPUNIT_ASSERT(found->val[1].second == 0.4);
0247     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 3)) == filtered.end());
0248   }
0249 
0250   // RefToBase data
0251   {
0252     typedef std::vector<int> CKey;
0253     typedef std::vector<Derived> Data;
0254     typedef std::vector<edm::RefToBase<Base>> CVal;
0255     typedef edm::AssociationMap<edm::OneToManyWithQuality<CKey, CVal, double, unsigned char>> Assoc;
0256 
0257     CKey keys{1, 2, 3};
0258     Data data{Derived(1.0), Derived(2.0), Derived(3.0), Derived(4.0)};
0259 
0260     CVal values{edm::RefToBase<Base>(edm::Ref<Data>(&data, 0)),
0261                 edm::RefToBase<Base>(edm::Ref<Data>(&data, 1)),
0262                 edm::RefToBase<Base>(edm::Ref<Data>(&data, 2))};
0263 
0264     auto refprod = Assoc::ref_type(edm::RefProd<CKey>(&keys), edm::RefProd<CVal>(&values));
0265     auto map = Assoc(refprod);
0266     map.insert(edm::Ref<CKey>(&keys, 0), std::make_pair(edm::Ref<CVal>(&values, 0), 0.1));
0267     map.insert(edm::Ref<CKey>(&keys, 1), std::make_pair(edm::Ref<CVal>(&values, 1), 0.2));
0268     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 1), 0.3));
0269     map.insert(edm::Ref<CKey>(&keys, 2), std::make_pair(edm::Ref<CVal>(&values, 0), 0.4));
0270     map.insert(edm::Ref<CKey>(&keys, 3), std::make_pair(edm::Ref<CVal>(&values, 1), 0.6));
0271     map.insert(edm::Ref<CKey>(&keys, 3), std::make_pair(edm::Ref<CVal>(&values, 2), 0.5));
0272     map.insert(edm::Ref<CKey>(&keys, 3), std::make_pair(edm::Ref<CVal>(&values, 0), 0.7));
0273 
0274     std::vector<edm::RefToBase<Base>> keep{edm::RefToBase<Base>(edm::Ref<Data>(&data, 2)),
0275                                            edm::RefToBase<Base>(edm::Ref<Data>(&data, 1))};
0276 
0277     Assoc filtered = associationMapFilterValues(map, keep);
0278     CPPUNIT_ASSERT(filtered.size() == 3);
0279     CPPUNIT_ASSERT(filtered.find(edm::Ref<CKey>(&keys, 0)) == filtered.end());
0280     auto found = filtered.find(edm::Ref<CKey>(&keys, 1));
0281     CPPUNIT_ASSERT(found != filtered.end());
0282     CPPUNIT_ASSERT(found->val.size() == 1);
0283     CPPUNIT_ASSERT(found->val[0].second == 0.2);
0284     found = filtered.find(edm::Ref<CKey>(&keys, 2));
0285     CPPUNIT_ASSERT(found != filtered.end());
0286     CPPUNIT_ASSERT(found->val.size() == 1);
0287     CPPUNIT_ASSERT(found->val[0].second == 0.3);
0288     found = filtered.find(edm::Ref<CKey>(&keys, 3));
0289     CPPUNIT_ASSERT(found != filtered.end());
0290     CPPUNIT_ASSERT(found->val.size() == 2);
0291     CPPUNIT_ASSERT(found->val[0].first->get()->value() == 2.0);
0292     CPPUNIT_ASSERT(found->val[0].second == 0.6);
0293     CPPUNIT_ASSERT(found->val[1].first->get()->value() == 3.0);
0294     CPPUNIT_ASSERT(found->val[1].second == 0.5);
0295   }
0296 
0297   // View, mostly check that it compiles
0298   {
0299     typedef std::vector<int> CKey;
0300     typedef edm::View<Base> CVal;
0301     typedef edm::AssociationMap<edm::OneToManyWithQuality<CKey, CVal, double, unsigned char>> Assoc;
0302 
0303     Assoc map;
0304     edm::View<Base> keep;
0305     Assoc filtered = associationMapFilterValues(map, keep);
0306     CPPUNIT_ASSERT(filtered.size() == 0);
0307   }
0308 }