Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-08-19 01:41:30

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