Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:07

0001 /*
0002  *  productResolverIndexHelper_t.cppunit.cc
0003  */
0004 
0005 #include "cppunit/extensions/HelperMacros.h"
0006 
0007 #include "DataFormats/Provenance/interface/EventID.h"
0008 #include "DataFormats/Provenance/interface/ProductID.h"
0009 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0010 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0011 
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 #include "FWCore/Utilities/interface/ProductKindOfType.h"
0014 #include "FWCore/Utilities/interface/TypeID.h"
0015 
0016 #include <iostream>
0017 #include <iomanip>
0018 
0019 using namespace edm;
0020 
0021 class TestProductResolverIndexHelper : public CppUnit::TestFixture {
0022   CPPUNIT_TEST_SUITE(TestProductResolverIndexHelper);
0023   CPPUNIT_TEST(testCreateEmpty);
0024   CPPUNIT_TEST(testOneEntry);
0025   CPPUNIT_TEST(testManyEntries);
0026   CPPUNIT_TEST_SUITE_END();
0027 
0028 public:
0029   void setUp();
0030   void tearDown() {}
0031 
0032   void testCreateEmpty();
0033   void testOneEntry();
0034   void testManyEntries();
0035 
0036   TypeID typeID_ProductID;
0037   TypeID typeID_EventID;
0038 };
0039 
0040 ///registration of the test so that the runner can find it
0041 CPPUNIT_TEST_SUITE_REGISTRATION(TestProductResolverIndexHelper);
0042 
0043 void TestProductResolverIndexHelper::setUp() {
0044   typeID_ProductID = TypeID(typeid(ProductID));
0045   typeID_EventID = TypeID(typeid(EventID));
0046 }
0047 
0048 void TestProductResolverIndexHelper::testCreateEmpty() {
0049   edm::ProductResolverIndexHelper helper;
0050   helper.setFrozen();
0051 
0052   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") ==
0053                  ProductResolverIndexInvalid);
0054   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") ==
0055                  ProductResolverIndexInvalid);
0056   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "") ==
0057                  ProductResolverIndexInvalid);
0058   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA") == ProductResolverIndexInvalid);
0059 
0060   edm::ProductResolverIndexHelper::Matches matches =
0061       helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID, "label_A", "instance_A");
0062   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0063   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID);
0064   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0065 
0066   TypeID typeID(typeid(ProductID));
0067   CPPUNIT_ASSERT_THROW(helper.insert(typeID, "labelA", "instanceA", "processA"), cms::Exception);
0068 }
0069 
0070 void TestProductResolverIndexHelper::testOneEntry() {
0071   edm::ProductResolverIndexHelper helper;
0072 
0073   TypeID typeIDProductID(typeid(ProductID));
0074   helper.insert(typeIDProductID, "labelA", "instanceA", "processA");
0075 
0076   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") ==
0077                  ProductResolverIndexInvalid);
0078   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") ==
0079                  ProductResolverIndexInvalid);
0080   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "") ==
0081                  ProductResolverIndexInvalid);
0082   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA") == ProductResolverIndexInvalid);
0083 
0084   edm::ProductResolverIndexHelper::Matches matches =
0085       helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID, "label_A", "instance_A");
0086   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0087   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID);
0088   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0089 
0090   helper.setFrozen();
0091 
0092   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA");
0093   CPPUNIT_ASSERT(matches.numberOfMatches() == 2);
0094   edm::ProductResolverIndex indexEmptyProcess = matches.index(0);
0095   edm::ProductResolverIndex indexWithProcess = matches.index(1);
0096   CPPUNIT_ASSERT_THROW(matches.index(2), cms::Exception);
0097   CPPUNIT_ASSERT(indexEmptyProcess < 2);
0098   CPPUNIT_ASSERT(indexWithProcess < 2);
0099   CPPUNIT_ASSERT(indexEmptyProcess != indexWithProcess);
0100 
0101   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA") == indexEmptyProcess);
0102   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "") == indexEmptyProcess);
0103   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", 0) == indexEmptyProcess);
0104   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") == indexWithProcess);
0105 
0106   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instance", "processA") ==
0107                  ProductResolverIndexInvalid);
0108   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceAX", "processA") ==
0109                  ProductResolverIndexInvalid);
0110   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "label", "instanceA", "processA") ==
0111                  ProductResolverIndexInvalid);
0112   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelAX", "instanceA", "processA") ==
0113                  ProductResolverIndexInvalid);
0114   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "process") ==
0115                  ProductResolverIndexInvalid);
0116   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_ProductID, "labelA", "instanceA", "processAX") ==
0117                  ProductResolverIndexInvalid);
0118   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_EventID, "labelA", "instanceA", "processA") ==
0119                  ProductResolverIndexInvalid);
0120 
0121   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA") == ProductResolverIndexInvalid);
0122   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA", "processA") ==
0123                  ProductResolverIndexInvalid);
0124 
0125   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_ProductID);
0126   CPPUNIT_ASSERT(matches.numberOfMatches() == 2);
0127   CPPUNIT_ASSERT(matches.index(0) == indexEmptyProcess);
0128   CPPUNIT_ASSERT(matches.index(1) == indexWithProcess);
0129 
0130   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_EventID);
0131   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0132 
0133   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_ProductID);
0134   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0135 
0136   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA");
0137   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0138 
0139   {
0140     auto indexToModules = helper.indiciesForModulesInProcess("processA");
0141     CPPUNIT_ASSERT(indexToModules.size() == 1);
0142     CPPUNIT_ASSERT(indexToModules.count("labelA") == 1);
0143     auto const& range = indexToModules.equal_range("labelA");
0144     CPPUNIT_ASSERT(std::get<2>(range.first->second) == indexWithProcess);
0145   }
0146 
0147   {
0148     auto indexToModules = helper.indiciesForModulesInProcess("processNotHere");
0149     CPPUNIT_ASSERT(indexToModules.size() == 0);
0150   }
0151 }
0152 
0153 void TestProductResolverIndexHelper::testManyEntries() {
0154   edm::ProductResolverIndexHelper helper;
0155 
0156   TypeID typeIDProductID(typeid(ProductID));
0157   TypeID typeIDEventID(typeid(EventID));
0158   TypeID typeIDVectorInt(typeid(std::vector<int>));
0159   TypeID typeIDSetInt(typeid(std::set<int>));
0160   TypeID typeIDVSimpleDerived(typeid(std::vector<edmtest::SimpleDerived>));
0161 
0162   helper.insert(typeIDVectorInt, "labelC", "instanceC", "processC");                                        // 0, 1, 2
0163   helper.insert(typeIDVectorInt, "label", "instance", "process");                                           // 3, 4, 5
0164   helper.insert(typeIDEventID, "labelB", "instanceB", "processB");                                          // 6, 7
0165   helper.insert(typeIDEventID, "label", "instanceB", "processB");                                           // 8, 9
0166   helper.insert(typeIDEventID, "labelX", "instanceB", "processB");                                          // 10, 11
0167   helper.insert(typeIDEventID, "labelB", "instance", "processB");                                           // 12, 13
0168   helper.insert(typeIDEventID, "labelB", "instanceX", "processB");                                          // 14, 15
0169   helper.insert(typeIDEventID, "labelB", "instanceB", "processB1");                                         // 16, 5
0170   helper.insert(typeIDEventID, "labelB", "instanceB", "processB3");                                         // 17, 5
0171   helper.insert(typeIDEventID, "labelB", "instanceB", "processB2");                                         // 18, 5
0172   helper.insert(typeIDProductID, "label", "instance", "process");                                           // 19, 20
0173   helper.insert(typeIDEventID, "label", "instance", "process");                                             // 21, 22
0174   helper.insert(typeIDProductID, "labelA", "instanceA", "processA");                                        // 23, 24
0175   CPPUNIT_ASSERT_THROW(helper.insert(typeIDProductID, "labelA", "instanceA", "processA"), cms::Exception);  // duplicate
0176 
0177   helper.insert(typeIDSetInt, "labelC", "instanceC", "processC");  // 25, 26
0178 
0179   helper.insert(typeIDVSimpleDerived, "labelC", "instanceC", "processC");  // 27, 28, 29, 30
0180 
0181   // helper.print(std::cout);
0182   helper.setFrozen();
0183   // helper.print(std::cout);
0184 
0185   TypeID typeID_int(typeid(int));
0186   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC", "processC") ==
0187                  ProductResolverIndexAmbiguous);
0188   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC", "processQ") ==
0189                  ProductResolverIndexInvalid);
0190   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC") == 2);
0191   edm::ProductResolverIndexHelper::Matches matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_int);
0192   CPPUNIT_ASSERT(matches.numberOfMatches() == 4);
0193   CPPUNIT_ASSERT(matches.index(0) == 5);
0194   CPPUNIT_ASSERT(matches.index(1) == 3);
0195   CPPUNIT_ASSERT(matches.index(2) == 2);
0196   CPPUNIT_ASSERT(matches.index(3) == ProductResolverIndexAmbiguous);
0197 
0198   TypeID typeID_vint(typeid(std::vector<int>));
0199   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_vint, "labelC", "instanceC", "processC") == 0);
0200   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_vint, "labelC", "instanceC") == 1);
0201 
0202   TypeID typeID_sint(typeid(std::set<int>));
0203   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_sint, "labelC", "instanceC", "processC") == 25);
0204   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_sint, "labelC", "instanceC") == 26);
0205 
0206   TypeID typeID_Simple(typeid(edmtest::Simple));
0207   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_Simple, "labelC", "instanceC") == 30);
0208   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_Simple, "labelC", "instanceC", "processC") == 27);
0209 
0210   TypeID typeID_SimpleDerived(typeid(edmtest::SimpleDerived));
0211   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_SimpleDerived, "labelC", "instanceC") == 29);
0212   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_SimpleDerived, "labelC", "instanceC", "processC") == 27);
0213 
0214   TypeID typeID_VSimpleDerived(typeid(std::vector<edmtest::SimpleDerived>));
0215   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_VSimpleDerived, "labelC", "instanceC") == 28);
0216   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_VSimpleDerived, "labelC", "instanceC", "processC") == 27);
0217 
0218   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_EventID, "labelB", "instanceB");
0219   CPPUNIT_ASSERT(matches.numberOfMatches() == 5);
0220   ProductResolverIndex indexEmptyProcess = matches.index(0);
0221   ProductResolverIndex indexB = matches.index(1);
0222   ProductResolverIndex indexB1 = matches.index(2);
0223   ProductResolverIndex indexB2 = matches.index(3);
0224   ProductResolverIndex indexB3 = matches.index(4);
0225   CPPUNIT_ASSERT_THROW(matches.index(5), cms::Exception);
0226   CPPUNIT_ASSERT(indexEmptyProcess == 7);
0227   CPPUNIT_ASSERT(indexB == 6);
0228   CPPUNIT_ASSERT(indexB1 == 16);
0229   CPPUNIT_ASSERT(indexB2 == 18);
0230   CPPUNIT_ASSERT(indexB3 == 17);
0231 
0232   CPPUNIT_ASSERT(std::string(matches.moduleLabel(4)) == "labelB");
0233   CPPUNIT_ASSERT(std::string(matches.productInstanceName(4)) == "instanceB");
0234   CPPUNIT_ASSERT(std::string(matches.processName(4)) == "processB3");
0235   CPPUNIT_ASSERT(std::string(matches.processName(0)) == "");
0236 
0237   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_Simple);
0238   CPPUNIT_ASSERT(matches.numberOfMatches() == 2);
0239   ProductResolverIndex indexC = matches.index(1);
0240   CPPUNIT_ASSERT_THROW(matches.index(2), cms::Exception);
0241   CPPUNIT_ASSERT(indexC == 27);
0242 
0243   {
0244     auto indexToModules = helper.indiciesForModulesInProcess("processA");
0245     CPPUNIT_ASSERT(indexToModules.size() == 1);
0246   }
0247   {
0248     auto indexToModules = helper.indiciesForModulesInProcess("processB");
0249     CPPUNIT_ASSERT(indexToModules.size() == 5);
0250   }
0251   {
0252     auto indexToModules = helper.indiciesForModulesInProcess("processB1");
0253     CPPUNIT_ASSERT(indexToModules.size() == 1);
0254   }
0255   {
0256     auto indexToModules = helper.indiciesForModulesInProcess("processB2");
0257     CPPUNIT_ASSERT(indexToModules.size() == 1);
0258   }
0259   {
0260     auto indexToModules = helper.indiciesForModulesInProcess("processB3");
0261     CPPUNIT_ASSERT(indexToModules.size() == 1);
0262   }
0263   {
0264     auto indexToModules = helper.indiciesForModulesInProcess("processC");
0265     CPPUNIT_ASSERT(indexToModules.size() == 3);
0266   }
0267 }