Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:36

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   CPPUNIT_ASSERT(matches.isFullyResolved(0) == false);
0130   CPPUNIT_ASSERT(matches.isFullyResolved(1) == true);
0131 
0132   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_EventID);
0133   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0134 
0135   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_ProductID);
0136   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0137 
0138   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_ProductID, "labelA", "instanceA");
0139   CPPUNIT_ASSERT(matches.numberOfMatches() == 0);
0140 
0141   {
0142     auto indexToModules = helper.indiciesForModulesInProcess("processA");
0143     CPPUNIT_ASSERT(indexToModules.size() == 1);
0144     CPPUNIT_ASSERT(indexToModules.count("labelA") == 1);
0145     auto const& range = indexToModules.equal_range("labelA");
0146     CPPUNIT_ASSERT(std::get<2>(range.first->second) == indexWithProcess);
0147   }
0148 
0149   {
0150     auto indexToModules = helper.indiciesForModulesInProcess("processNotHere");
0151     CPPUNIT_ASSERT(indexToModules.size() == 0);
0152   }
0153 }
0154 
0155 void TestProductResolverIndexHelper::testManyEntries() {
0156   edm::ProductResolverIndexHelper helper;
0157 
0158   TypeID typeIDProductID(typeid(ProductID));
0159   TypeID typeIDEventID(typeid(EventID));
0160   TypeID typeIDVectorInt(typeid(std::vector<int>));
0161   TypeID typeIDSetInt(typeid(std::set<int>));
0162   TypeID typeIDVSimpleDerived(typeid(std::vector<edmtest::SimpleDerived>));
0163 
0164   helper.insert(typeIDVectorInt, "labelC", "instanceC", "processC");                                        // 0, 1, 2
0165   helper.insert(typeIDVectorInt, "label", "instance", "process");                                           // 3, 4, 5
0166   helper.insert(typeIDEventID, "labelB", "instanceB", "processB");                                          // 6, 7
0167   helper.insert(typeIDEventID, "label", "instanceB", "processB");                                           // 8, 9
0168   helper.insert(typeIDEventID, "labelX", "instanceB", "processB");                                          // 10, 11
0169   helper.insert(typeIDEventID, "labelB", "instance", "processB");                                           // 12, 13
0170   helper.insert(typeIDEventID, "labelB", "instanceX", "processB");                                          // 14, 15
0171   helper.insert(typeIDEventID, "labelB", "instanceB", "processB1");                                         // 16, 5
0172   helper.insert(typeIDEventID, "labelB", "instanceB", "processB3");                                         // 17, 5
0173   helper.insert(typeIDEventID, "labelB", "instanceB", "processB2");                                         // 18, 5
0174   helper.insert(typeIDProductID, "label", "instance", "process");                                           // 19, 20
0175   helper.insert(typeIDEventID, "label", "instance", "process");                                             // 21, 22
0176   helper.insert(typeIDProductID, "labelA", "instanceA", "processA");                                        // 23, 24
0177   CPPUNIT_ASSERT_THROW(helper.insert(typeIDProductID, "labelA", "instanceA", "processA"), cms::Exception);  // duplicate
0178 
0179   helper.insert(typeIDSetInt, "labelC", "instanceC", "processC");  // 25, 26
0180 
0181   helper.insert(typeIDVSimpleDerived, "labelC", "instanceC", "processC");  // 27, 28, 29, 30
0182 
0183   // helper.print(std::cout);
0184   helper.setFrozen();
0185   // helper.print(std::cout);
0186 
0187   TypeID typeID_int(typeid(int));
0188   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC", "processC") ==
0189                  ProductResolverIndexAmbiguous);
0190   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC", "processQ") ==
0191                  ProductResolverIndexInvalid);
0192   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_int, "labelC", "instanceC") == 2);
0193   edm::ProductResolverIndexHelper::Matches matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_int);
0194   CPPUNIT_ASSERT(matches.numberOfMatches() == 4);
0195   CPPUNIT_ASSERT(matches.index(0) == 5);
0196   CPPUNIT_ASSERT(matches.index(1) == 3);
0197   CPPUNIT_ASSERT(matches.index(2) == 2);
0198   CPPUNIT_ASSERT(matches.index(3) == ProductResolverIndexAmbiguous);
0199 
0200   TypeID typeID_vint(typeid(std::vector<int>));
0201   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_vint, "labelC", "instanceC", "processC") == 0);
0202   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_vint, "labelC", "instanceC") == 1);
0203 
0204   TypeID typeID_sint(typeid(std::set<int>));
0205   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_sint, "labelC", "instanceC", "processC") == 25);
0206   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_sint, "labelC", "instanceC") == 26);
0207 
0208   TypeID typeID_Simple(typeid(edmtest::Simple));
0209   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_Simple, "labelC", "instanceC") == 30);
0210   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_Simple, "labelC", "instanceC", "processC") == 27);
0211 
0212   TypeID typeID_SimpleDerived(typeid(edmtest::SimpleDerived));
0213   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_SimpleDerived, "labelC", "instanceC") == 29);
0214   CPPUNIT_ASSERT(helper.index(ELEMENT_TYPE, typeID_SimpleDerived, "labelC", "instanceC", "processC") == 27);
0215 
0216   TypeID typeID_VSimpleDerived(typeid(std::vector<edmtest::SimpleDerived>));
0217   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_VSimpleDerived, "labelC", "instanceC") == 28);
0218   CPPUNIT_ASSERT(helper.index(PRODUCT_TYPE, typeID_VSimpleDerived, "labelC", "instanceC", "processC") == 27);
0219 
0220   matches = helper.relatedIndexes(PRODUCT_TYPE, typeID_EventID, "labelB", "instanceB");
0221   CPPUNIT_ASSERT(matches.numberOfMatches() == 5);
0222   ProductResolverIndex indexEmptyProcess = matches.index(0);
0223   ProductResolverIndex indexB = matches.index(1);
0224   ProductResolverIndex indexB1 = matches.index(2);
0225   ProductResolverIndex indexB2 = matches.index(3);
0226   ProductResolverIndex indexB3 = matches.index(4);
0227   CPPUNIT_ASSERT_THROW(matches.index(5), cms::Exception);
0228   CPPUNIT_ASSERT(indexEmptyProcess == 7);
0229   CPPUNIT_ASSERT(indexB == 6);
0230   CPPUNIT_ASSERT(indexB1 == 16);
0231   CPPUNIT_ASSERT(indexB2 == 18);
0232   CPPUNIT_ASSERT(indexB3 == 17);
0233 
0234   CPPUNIT_ASSERT(std::string(matches.moduleLabel(4)) == "labelB");
0235   CPPUNIT_ASSERT(std::string(matches.productInstanceName(4)) == "instanceB");
0236   CPPUNIT_ASSERT(std::string(matches.processName(4)) == "processB3");
0237   CPPUNIT_ASSERT(std::string(matches.processName(0)) == "");
0238 
0239   matches = helper.relatedIndexes(ELEMENT_TYPE, typeID_Simple);
0240   CPPUNIT_ASSERT(matches.numberOfMatches() == 2);
0241   ProductResolverIndex indexC = matches.index(1);
0242   CPPUNIT_ASSERT_THROW(matches.index(2), cms::Exception);
0243   CPPUNIT_ASSERT(indexC == 27);
0244 
0245   {
0246     auto indexToModules = helper.indiciesForModulesInProcess("processA");
0247     CPPUNIT_ASSERT(indexToModules.size() == 1);
0248   }
0249   {
0250     auto indexToModules = helper.indiciesForModulesInProcess("processB");
0251     CPPUNIT_ASSERT(indexToModules.size() == 5);
0252   }
0253   {
0254     auto indexToModules = helper.indiciesForModulesInProcess("processB1");
0255     CPPUNIT_ASSERT(indexToModules.size() == 1);
0256   }
0257   {
0258     auto indexToModules = helper.indiciesForModulesInProcess("processB2");
0259     CPPUNIT_ASSERT(indexToModules.size() == 1);
0260   }
0261   {
0262     auto indexToModules = helper.indiciesForModulesInProcess("processB3");
0263     CPPUNIT_ASSERT(indexToModules.size() == 1);
0264   }
0265   {
0266     auto indexToModules = helper.indiciesForModulesInProcess("processC");
0267     CPPUNIT_ASSERT(indexToModules.size() == 3);
0268   }
0269 }