Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:58

0001 /*
0002  */
0003 #define CATCH_CONFIG_MAIN
0004 #include "catch.hpp"
0005 
0006 #include <algorithm>
0007 #include <iostream>
0008 #include <limits>
0009 #include <string>
0010 #include <cassert>
0011 
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/types.h"
0014 #include "FWCore/Utilities/interface/EDMException.h"
0015 #include "FWCore/Utilities/interface/Algorithms.h"
0016 #include "FWCore/Utilities/interface/Digest.h"
0017 namespace edm {
0018   bool operator!=(const edm::EventRange& iLHS, const edm::EventRange& iRHS) {
0019     return ((iLHS.startEventID() != iRHS.startEventID()) or (iLHS.endEventID() != iRHS.endEventID()));
0020   }
0021 }  // namespace edm
0022 namespace {
0023   void do_compare(const edm::EventRange& iLHS, const edm::EventRange& iRHS) {
0024     CHECK(iLHS.startEventID() == iRHS.startEventID());
0025     CHECK(iLHS.endEventID() == iRHS.endEventID());
0026   }
0027 
0028   template <class T>
0029   void do_compare(const T& iLHS, const T& iRHS) {
0030     CHECK(iLHS == iRHS);
0031   }
0032 
0033   template <class T, class A>
0034   void do_compare(const std::vector<T, A>& iLHS, const std::vector<T, A>& iRHS) {
0035     REQUIRE_THAT(iLHS, Catch::Matchers::Equals(iRHS));
0036   }
0037 
0038   template <class T>
0039   void trackedTestbody(const T& value) {
0040     edm::ParameterSet p1;
0041     p1.template addParameter<T>("x", value);
0042     p1.registerIt();
0043     do_compare(p1.template getParameter<T>("x"), value);
0044     std::string p1_encoded = p1.toString();
0045     edm::ParameterSet p2(p1_encoded);
0046     REQUIRE(p1 == p2);
0047     do_compare(p2.template getParameter<T>("x"), value);
0048   }
0049 
0050   template <class T>
0051   void untrackedTestbody(const T& value) {
0052     edm::ParameterSet p;
0053     p.template addUntrackedParameter<T>("x", value);
0054     do_compare(p.template getUntrackedParameter<T>("x"), value);
0055 
0056     REQUIRE_THROWS_AS(p.template getUntrackedParameter<T>("does not exist"), cms::Exception);
0057   }
0058 
0059   template <class T>
0060   void testbody(T value) {
0061     trackedTestbody<T>(value);
0062     untrackedTestbody<T>(value);
0063   }
0064 
0065   template <class T>
0066   void test_for_name() {
0067     edm::ParameterSet preal;
0068     edm::ParameterSet const& ps = preal;
0069     // Use 'ps' to make sure we're only getting 'const' access;
0070     // use 'preal' when we need to modify the underlying ParameterSet.
0071 
0072     std::vector<std::string> names = ps.getParameterNames();
0073     REQUIRE(names.empty());
0074 
0075     T value{};
0076     preal.template addParameter<T>("x", value);
0077     names = ps.getParameterNames();
0078     REQUIRE(names.size() == 1);
0079     REQUIRE(names[0] == "x");
0080     T stored_value = ps.template getParameter<T>(names[0]);
0081     REQUIRE(stored_value == value);
0082 
0083     preal.template addUntrackedParameter<T>("y", value);
0084     preal.registerIt();
0085     names = ps.getParameterNames();
0086     REQUIRE(names.size() == 2);
0087 
0088     edm::sort_all(names);
0089     REQUIRE(edm::binary_search_all(names, "x"));
0090     REQUIRE(edm::binary_search_all(names, "y"));
0091     names = ps.template getParameterNamesForType<T>();
0092     REQUIRE(names.size() == 1);
0093     edm::sort_all(names);
0094     REQUIRE(edm::binary_search_all(names, "x"));
0095     names = ps.template getParameterNamesForType<T>(false);
0096     REQUIRE(names.size() == 1);
0097     edm::sort_all(names);
0098     REQUIRE(edm::binary_search_all(names, "y"));
0099 
0100     std::string firstString = ps.toString();
0101     edm::ParameterSet p2(firstString);
0102 
0103     p2.registerIt();
0104     // equality tests toStringOfTracked internally
0105     REQUIRE(ps == p2);
0106     std::string allString;
0107     ps.allToString(allString);
0108     //since have untracked parameter these strings will not be identical
0109     REQUIRE(firstString != allString);
0110 
0111     edm::ParameterSet pAll(allString);
0112     REQUIRE(pAll.getParameterNames().size() == 2);
0113   }
0114 
0115 }  // namespace
0116 
0117 TEST_CASE("test ParameterSet", "[ParameterSet]") {
0118   SECTION("empty") {
0119     edm::ParameterSet p1;
0120     std::string p1_encoded = p1.toString();
0121     edm::ParameterSet p2(p1_encoded);
0122     REQUIRE(p1 == p2);
0123   }
0124 
0125   SECTION("bool") {
0126     testbody<bool>(false);
0127     testbody<bool>(true);
0128   }
0129 
0130   SECTION("int") {
0131     testbody<int>(-std::numeric_limits<int>::max());
0132     testbody<int>(-2112);
0133     testbody<int>(-0);
0134     testbody<int>(0);
0135     testbody<int>(35621);
0136     testbody<int>(std::numeric_limits<int>::max());
0137   }
0138 
0139   SECTION("uint") {
0140     testbody<unsigned int>(0);
0141     testbody<unsigned int>(35621);
0142     testbody<unsigned int>(std::numeric_limits<unsigned int>::max());
0143 
0144     testbody<std::vector<unsigned int>>(std::vector<unsigned int>());
0145     testbody<std::vector<unsigned int>>(std::vector<unsigned int>(1, 35621));
0146     testbody<std::vector<unsigned int>>(std::vector<unsigned int>(1, std::numeric_limits<unsigned int>::max()));
0147   }
0148 
0149   SECTION("double") {
0150     testbody<double>(-1.25);
0151     testbody<double>(-0.0);
0152     testbody<double>(0.0);
0153     testbody<double>(1.25);
0154     //testbody<double>(1.0/0.0);  // parameter set does not handle infinity?
0155     //testbody<double>(0.0/0.0);  // parameter set does not handle NaN?
0156     testbody<double>(-2.3456789e-231);
0157     testbody<double>(-2.3456789e231);
0158     testbody<double>(2.3456789e-231);
0159     testbody<double>(2.3456789e231);
0160     double oneThird = 1.0 / 3.0;
0161     testbody<double>(oneThird);
0162   }
0163 
0164   SECTION("edm::decode_vstring_extent") {
0165     SECTION("empty") {
0166       auto ret = edm::decode_vstring_extent("{}");
0167       REQUIRE(ret);
0168       CHECK(*ret == "{}");
0169     }
0170     SECTION("empty stuff after") {
0171       auto ret = edm::decode_vstring_extent("{}xava");
0172       REQUIRE(ret);
0173       CHECK(*ret == "{}");
0174     }
0175     SECTION("one empty string") {
0176       auto ret = edm::decode_vstring_extent(std::string_view("{\0\0}", 4));
0177       REQUIRE(ret);
0178       CHECK(*ret == std::string_view("{\0\0}", 4));
0179     }
0180     SECTION("one string with a null") {
0181       auto ret = edm::decode_vstring_extent(std::string_view("{\0\0\0\0}", 6));
0182       REQUIRE(ret);
0183       CHECK(*ret == std::string_view("{\0\0\0\0}", 6));
0184     }
0185     SECTION("one empty string, stuff after") {
0186       auto ret = edm::decode_vstring_extent(std::string_view("{\0\0})aa", 6));
0187       REQUIRE(ret);
0188       CHECK(*ret == std::string_view("{\0\0}", 4));
0189     }
0190     SECTION("simple multi-entry") {
0191       auto ret = edm::decode_vstring_extent(std::string_view("{\0one\0,1\0,TWO\0,three\0}", 22));
0192       REQUIRE(ret);
0193       CHECK(*ret == std::string_view(std::string_view("{\0one\0,1\0,TWO\0,three\0}", 22)));
0194     }
0195     SECTION("simple multi-entry, stuff after") {
0196       auto ret = edm::decode_vstring_extent(std::string_view("{\0one\0,1\0,TWO\0,three\0});", 24));
0197       REQUIRE(ret);
0198       CHECK(*ret == std::string_view(std::string_view("{\0one\0,1\0,TWO\0,three\0}", 22)));
0199     }
0200   }
0201   SECTION("edm::decode_string_extent") {
0202     SECTION("empty") {
0203       auto ret = edm::decode_string_extent(std::string_view("\0", 1));
0204       REQUIRE(ret);
0205       CHECK(*ret == std::string_view("\0", 1));
0206     }
0207     SECTION("empty stuff after") {
0208       auto ret = edm::decode_string_extent(std::string_view("\0xava", 5));
0209       REQUIRE(ret);
0210       CHECK(*ret == std::string_view("\0", 1));
0211     }
0212     SECTION("string with null") {
0213       auto ret = edm::decode_string_extent(std::string_view("\0\0\0", 3));
0214       REQUIRE(ret);
0215       CHECK(*ret == std::string_view("\0\0\0", 3));
0216     }
0217     SECTION("string with null, stuff after") {
0218       auto ret = edm::decode_string_extent(std::string_view("\0\0\0)aa", 6));
0219       REQUIRE(ret);
0220       CHECK(*ret == std::string_view("\0\0\0", 3));
0221     }
0222     SECTION("null separator") {
0223       auto ret = edm::decode_string_extent(std::string_view("a\0\0b\0", 5));
0224       REQUIRE(ret);
0225       CHECK(*ret == std::string_view("a\0\0b\0", 5));
0226     }
0227     SECTION("null separator, stuff after") {
0228       auto ret = edm::decode_string_extent(std::string_view("a\0\0b\0)>", 7));
0229       REQUIRE(ret);
0230       CHECK(*ret == std::string_view("a\0\0b\0", 5));
0231     }
0232     SECTION("null start") {
0233       auto ret = edm::decode_string_extent(std::string_view("\0\0ab\0", 5));
0234       REQUIRE(ret);
0235       CHECK(*ret == std::string_view("\0\0ab\0", 5));
0236     }
0237     SECTION("null start, stuff after") {
0238       auto ret = edm::decode_string_extent(std::string_view("\0\0ab\0)>", 7));
0239       REQUIRE(ret);
0240       CHECK(*ret == std::string_view("\0\0ab\0", 5));
0241     }
0242     SECTION("null end") {
0243       auto ret = edm::decode_string_extent(std::string_view("ab\0\0\0", 5));
0244       REQUIRE(ret);
0245       CHECK(*ret == std::string_view("ab\0\0\0", 5));
0246     }
0247     SECTION("null end, stuff after") {
0248       auto ret = edm::decode_string_extent(std::string_view("ab\0\0\0)>", 7));
0249       REQUIRE(ret);
0250       CHECK(*ret == std::string_view("ab\0\0\0", 5));
0251     }
0252   }
0253   SECTION("string") {
0254     SECTION("simple") {
0255       testbody<std::string>("");
0256       testbody<std::string>("Hello there");
0257       testbody<std::string>("123");
0258     }
0259     SECTION("escaped characters") { testbody<std::string>("This\nis\tsilly\n"); }
0260     SECTION("special to PSet characters") { testbody<std::string>("{This,is`silly}"); }
0261     SECTION("all characters") {
0262       std::array<char, 256> allChars;
0263       for (int i = 0; i < 256; ++i) {
0264         allChars[i] = static_cast<char>(i);
0265       }
0266       std::string const all(allChars.begin(), allChars.end());
0267       REQUIRE(all.size() == 256);
0268       testbody<std::string>(all);
0269     }
0270     SECTION("lots of nulls") {
0271       testbody<std::string>(std::string("\0", 1));
0272       testbody<std::string>(std::string("\0\0", 2));
0273       testbody<std::string>(std::string("\0\0\0", 3));
0274       testbody<std::string>(std::string("\0\0\0\0", 4));
0275     }
0276     SECTION("null separator") { testbody<std::string>(std::string("a\0b", 3U)); }
0277     SECTION("null begin") { testbody<std::string>(std::string("\0ab", 3U)); }
0278     SECTION("null end") { testbody<std::string>(std::string("ab\0", 3U)); }
0279     SECTION(":") { testbody<std::string>("ab:c"); }
0280     SECTION("existsAs") {
0281       std::string s = "some value";
0282       edm::ParameterSet p1;
0283       p1.addParameter<std::string>("s", s);
0284       p1.registerIt();
0285       REQUIRE(p1.existsAs<std::string>("s"));
0286       REQUIRE(not p1.existsAs<std::string>("not_here"));
0287     }
0288   }
0289   SECTION("vstring") {
0290     SECTION("simple") {
0291       std::vector<std::string> vs;
0292       vs.push_back("one");
0293       vs.push_back("1");
0294       vs.push_back("TWO");
0295       vs.push_back("three");
0296       testbody<std::vector<std::string>>(vs);
0297     }
0298     SECTION("vstring with empty strings") {
0299       std::vector<std::string> vs;
0300       vs.push_back("");
0301       vs.push_back("1");
0302       vs.push_back("");
0303       vs.push_back("three");
0304       testbody<std::vector<std::string>>(vs);
0305     }
0306     SECTION("empty vstring") {
0307       std::vector<std::string> vs;
0308       testbody<std::vector<std::string>>(vs);
0309     }
0310     SECTION("with one empty string") {
0311       std::vector<std::string> vs;
0312       vs.push_back("");
0313       testbody<std::vector<std::string>>(vs);
0314     }
0315     SECTION("special characters") {
0316       //special characters
0317       std::vector<std::string> vs;
0318       vs.push_back("{This,is`silly}");
0319       vs.push_back("1");
0320       vs.push_back("three");
0321       testbody<std::vector<std::string>>(vs);
0322     }
0323     SECTION("leading '}'") {
0324       std::vector<std::string> vs;
0325       vs.push_back("}");
0326       testbody<std::vector<std::string>>(vs);
0327     }
0328     SECTION("leading ','") {
0329       std::vector<std::string> vs;
0330       vs.push_back(",");
0331       testbody<std::vector<std::string>>(vs);
0332     }
0333     SECTION("leading '<'") {
0334       std::vector<std::string> vs;
0335       vs.push_back("<");
0336       testbody<std::vector<std::string>>(vs);
0337     }
0338     SECTION("leading '>'") {
0339       std::vector<std::string> vs;
0340       vs.push_back(">");
0341       testbody<std::vector<std::string>>(vs);
0342     }
0343     SECTION("leading ';'") {
0344       std::vector<std::string> vs;
0345       vs.push_back(";");
0346       testbody<std::vector<std::string>>(vs);
0347     }
0348     SECTION("leading ':'") {
0349       std::vector<std::string> vs;
0350       vs.push_back(":");
0351       testbody<std::vector<std::string>>(vs);
0352     }
0353     SECTION("existsAs") {
0354       std::vector<std::string> vs;
0355       vs.push_back("some value");
0356       edm::ParameterSet p1;
0357       p1.addParameter<std::vector<std::string>>("vs", vs);
0358       p1.registerIt();
0359       REQUIRE(p1.existsAs<std::vector<std::string>>("vs"));
0360       REQUIRE(not p1.existsAs<std::vector<std::string>>("not_here"));
0361     }
0362   }
0363   SECTION("deprecated") {
0364     SECTION("string") {
0365       std::string to;
0366       std::string const value = "this is deprecated";
0367       REQUIRE(edm::encode_deprecated(to, value));
0368       std::string pset_encode;
0369       pset_encode += "<label=+S(";
0370       pset_encode += to;
0371       pset_encode += ")>";
0372       edm::ParameterSet pset(pset_encode);
0373       pset.registerIt();
0374       REQUIRE(pset.getParameter<std::string>("label") == value);
0375       REQUIRE(pset.existsAs<std::string>("label"));
0376     }
0377     SECTION("vstring") {
0378       std::string to;
0379       std::string const value = "this is deprecated";
0380       std::vector<std::string> const from(1, value);
0381       REQUIRE(edm::encode_deprecated(to, from));
0382       std::string pset_encode;
0383       pset_encode += "<label=+s(";
0384       pset_encode += to;
0385       pset_encode += ")>";
0386       edm::ParameterSet pset(pset_encode);
0387       pset.registerIt();
0388       REQUIRE(pset.getParameter<std::vector<std::string>>("label") == from);
0389       REQUIRE(pset.existsAs<std::vector<std::string>>("label"));
0390     }
0391   }
0392   SECTION("eventID") {
0393     testbody<edm::EventID>(edm::EventID());
0394     testbody<edm::EventID>(edm::EventID::firstValidEvent());
0395     testbody<edm::EventID>(edm::EventID(2, 3, 4));
0396     testbody<edm::EventID>(edm::EventID(2, 3, edm::EventID::maxEventNumber()));
0397     testbody<edm::EventID>(edm::EventID(
0398         edm::EventID::maxRunNumber(), edm::EventID::maxLuminosityBlockNumber(), edm::EventID::maxEventNumber()));
0399   }
0400 
0401   SECTION("eventRange") {
0402     testbody<edm::EventRange>(edm::EventRange());
0403     testbody<edm::EventRange>(edm::EventRange(1,
0404                                               1,
0405                                               1,
0406                                               edm::EventID::maxRunNumber(),
0407                                               edm::EventID::maxLuminosityBlockNumber(),
0408                                               edm::EventID::maxEventNumber()));
0409     testbody<edm::EventRange>(edm::EventRange(2, 3, 4, 2, 3, 10));
0410 
0411     testbody<edm::EventRange>(
0412         edm::EventRange(1, 0, 1, edm::EventID::maxRunNumber(), 0, edm::EventID::maxEventNumber()));
0413     testbody<edm::EventRange>(edm::EventRange(2, 0, 4, 2, 0, 10));
0414   }
0415 
0416   SECTION("vEventRange") {
0417     testbody<std::vector<edm::EventRange>>(std::vector<edm::EventRange>());
0418     testbody<std::vector<edm::EventRange>>(
0419         std::vector<edm::EventRange>(1,
0420                                      edm::EventRange(1,
0421                                                      1,
0422                                                      1,
0423                                                      edm::EventID::maxRunNumber(),
0424                                                      edm::EventID::maxLuminosityBlockNumber(),
0425                                                      edm::EventID::maxEventNumber())));
0426 
0427     testbody<std::vector<edm::EventRange>>(std::vector<edm::EventRange>(
0428         1, edm::EventRange(1, 0, 1, edm::EventID::maxRunNumber(), 0, edm::EventID::maxEventNumber())));
0429 
0430     std::vector<edm::EventRange> er;
0431     er.reserve(2);
0432     er.push_back(edm::EventRange(2, 3, 4, 2, 3, 10));
0433     er.push_back(edm::EventRange(5, 1, 1, 10, 3, 10));
0434 
0435     testbody<std::vector<edm::EventRange>>(er);
0436   }
0437 
0438   SECTION("fileInPath") {
0439     edm::ParameterSet p;
0440     edm::FileInPath fip("FWCore/ParameterSet/python/Config.py");
0441     p.addParameter<edm::FileInPath>("fip", fip);
0442     REQUIRE(p.existsAs<edm::FileInPath>("fip"));
0443     REQUIRE(p.getParameterNamesForType<edm::FileInPath>()[0] == "fip");
0444   }
0445 
0446   SECTION("InputTag") {
0447     SECTION("label only") {
0448       edm::InputTag t("foo");
0449       testbody<edm::InputTag>(t);
0450     }
0451     SECTION("label and instance") {
0452       edm::InputTag t("foo", "bar");
0453       testbody<edm::InputTag>(t);
0454     }
0455     SECTION("label, instance and process") {
0456       edm::InputTag t("foo", "bar", "PROC");
0457       testbody<edm::InputTag>(t);
0458     }
0459     SECTION("from string, label only") {
0460       edm::ParameterSet p;
0461       p.addParameter<std::string>("tag", "foo");
0462       p.registerIt();
0463       auto t = p.getParameter<edm::InputTag>("tag");
0464       REQUIRE(t.label() == "foo");
0465       REQUIRE(t.instance().empty());
0466       REQUIRE(t.process().empty());
0467     }
0468     SECTION("from string, label & instance") {
0469       edm::ParameterSet p;
0470       p.addParameter<std::string>("tag", "foo:bar");
0471       p.registerIt();
0472       auto t = p.getParameter<edm::InputTag>("tag");
0473       REQUIRE(t.label() == "foo");
0474       REQUIRE(t.instance() == "bar");
0475       REQUIRE(t.process().empty());
0476     }
0477     SECTION("from string, label , instance & process") {
0478       edm::ParameterSet p;
0479       p.addParameter<std::string>("tag", "foo:bar:PROC");
0480       p.registerIt();
0481       auto t = p.getParameter<edm::InputTag>("tag");
0482       REQUIRE(t.label() == "foo");
0483       REQUIRE(t.instance() == "bar");
0484       REQUIRE(t.process() == "PROC");
0485     }
0486   }
0487 
0488   SECTION("doubleEquality") {
0489     edm::ParameterSet p1, p2, p3;
0490     p1.addParameter<double>("x", 0.1);
0491     p2.addParameter<double>("x", 1.0e-1);
0492     p3.addParameter<double>("x", 0.100);
0493     p1.registerIt();
0494     p2.registerIt();
0495     p3.registerIt();
0496     REQUIRE(p1 == p2);
0497     REQUIRE(p1 == p3);
0498     REQUIRE(p2 == p3);
0499 
0500     REQUIRE(p1.toString() == p2.toString());
0501     REQUIRE(p1.toString() == p3.toString());
0502     REQUIRE(p2.toString() == p3.toString());
0503   }
0504 
0505   SECTION("negativeZero") {
0506     edm::ParameterSet a1, a2;
0507     a1.addParameter<double>("x", 0.0);
0508     a2.addParameter<double>("x", -0.0);
0509     a1.registerIt();
0510     a2.registerIt();
0511     // Negative and positive zero should be coded differently.
0512     REQUIRE(a1.toString() != a2.toString());
0513     REQUIRE(a1 != a2);
0514     // Negative and positive zero should test equal.
0515     REQUIRE(a1.getParameter<double>("x") == a2.getParameter<double>("x"));
0516   }
0517 
0518   SECTION("id") {
0519     edm::ParameterSet a;
0520     a.registerIt();
0521     edm::ParameterSetID a_id = a.id();
0522     edm::ParameterSet b;
0523     b.addParameter<int>("x", -23);
0524     b.registerIt();
0525     edm::ParameterSetID b_id = b.id();
0526 
0527     REQUIRE(a != b);
0528     REQUIRE(a.id() != b.id());
0529 
0530     {
0531       //Check that changes to ESInputTag do not affect ID
0532       // as that would affect reading back stored PSets
0533 
0534       edm::ParameterSet ps;
0535       ps.addParameter<edm::ESInputTag>("default", edm::ESInputTag());
0536       ps.addParameter<edm::ESInputTag>("moduleOnly", edm::ESInputTag("Prod", ""));
0537       ps.addParameter<edm::ESInputTag>("dataOnly", edm::ESInputTag("", "data"));
0538       ps.addParameter<edm::ESInputTag>("allLabels", edm::ESInputTag("Prod", "data"));
0539       ps.registerIt();
0540 
0541       std::string stringValue;
0542       ps.id().toString(stringValue);
0543       REQUIRE(stringValue == "01642a9a7311dea2df2f9ee430855a99");
0544     }
0545   }
0546 
0547   SECTION("calculateID") {
0548     std::vector<int> thousand(1000, 0);
0549     std::vector<int> hundred(100, 0);
0550     edm::ParameterSet a;
0551     edm::ParameterSet b;
0552     edm::ParameterSet c;
0553     a.addParameter<double>("pi", 3.14);
0554     a.addParameter<int>("answer", 42), a.addParameter<std::string>("amiga", "rules");
0555     a.addParameter<std::vector<int>>("thousands", thousand);
0556     a.addUntrackedParameter<double>("e", 2.72);
0557     a.addUntrackedParameter<int>("question", 41 + 1);
0558     a.addUntrackedParameter<std::string>("atari", "too");
0559     a.addUntrackedParameter<std::vector<int>>("hundred", hundred);
0560 
0561     b.addParameter<double>("pi", 3.14);
0562     b.addParameter<int>("answer", 42), b.addParameter<std::string>("amiga", "rules");
0563     b.addParameter<std::vector<int>>("thousands", thousand);
0564     b.addUntrackedParameter<double>("e", 2.72);
0565     b.addUntrackedParameter<int>("question", 41 + 1);
0566     b.addUntrackedParameter<std::string>("atari", "too");
0567     b.addUntrackedParameter<std::vector<int>>("hundred", hundred);
0568 
0569     c.addParameter<double>("pi", 3.14);
0570     c.addParameter<int>("answer", 42), c.addParameter<std::string>("amiga", "rules");
0571     c.addParameter<std::vector<int>>("thousands", thousand);
0572     c.addUntrackedParameter<double>("e", 2.72);
0573     c.addUntrackedParameter<int>("question", 41 + 1);
0574     c.addUntrackedParameter<std::string>("atari", "too");
0575     c.addUntrackedParameter<std::vector<int>>("hundred", hundred);
0576 
0577     b.addParameter<edm::ParameterSet>("nested", c);
0578     std::vector<edm::ParameterSet> vb;
0579     vb.push_back(b);
0580 
0581     a.addUntrackedParameter<std::vector<edm::ParameterSet>>("vps", vb);
0582     std::string stringrep;
0583     a.toString(stringrep);
0584     cms::Digest md5alg(stringrep);
0585     cms::Digest newDigest;
0586     a.toDigest(newDigest);
0587     REQUIRE(md5alg.digest().toString() == newDigest.digest().toString());
0588   }
0589 
0590   SECTION("mapById") {
0591     // makes parameter sets and ids
0592     edm::ParameterSet a;
0593     a.addParameter<double>("pi", 3.14);
0594     a.addParameter<std::string>("name", "Bub");
0595     a.registerIt();
0596     REQUIRE(a.exists("pi"));
0597     REQUIRE(!a.exists("pie"));
0598 
0599     edm::ParameterSet b;
0600     b.addParameter<bool>("b", false);
0601     b.addParameter<std::vector<int>>("three_zeros", std::vector<int>(3, 0));
0602     b.registerIt();
0603 
0604     edm::ParameterSet c;
0605     c.registerIt();
0606 
0607     edm::ParameterSet d;
0608     d.addParameter<unsigned int>("hundred", 100);
0609     d.addParameter<std::vector<double>>("empty", std::vector<double>());
0610     d.registerIt();
0611 
0612     edm::ParameterSetID id_a = a.id();
0613     edm::ParameterSetID id_b = b.id();
0614     edm::ParameterSetID id_c = c.id();
0615     edm::ParameterSetID id_d = d.id();
0616 
0617     // fill map
0618     typedef std::map<edm::ParameterSetID, edm::ParameterSet> map_t;
0619     map_t psets;
0620 
0621     psets.insert(std::make_pair(id_a, a));
0622     psets.insert(std::make_pair(id_b, b));
0623     psets.insert(std::make_pair(id_c, c));
0624     psets.insert(std::make_pair(id_d, d));
0625 
0626     // query map
0627     REQUIRE(psets.size() == 4);
0628     REQUIRE(psets[id_a] == a);
0629     REQUIRE(psets[id_b] == b);
0630     REQUIRE(psets[id_c] == c);
0631     REQUIRE(psets[id_d] == d);
0632   }
0633 
0634   SECTION("name Access") {
0635     test_for_name<bool>();
0636 
0637     test_for_name<int>();
0638     test_for_name<std::vector<int>>();
0639 
0640     test_for_name<unsigned int>();
0641     test_for_name<std::vector<unsigned int>>();
0642 
0643     test_for_name<double>();
0644     test_for_name<std::vector<double>>();
0645 
0646     test_for_name<std::string>();
0647     test_for_name<std::vector<std::string>>();
0648     test_for_name<edm::ParameterSet>();
0649     test_for_name<std::vector<edm::ParameterSet>>();
0650 
0651     // Can't make default FileInPath objects...
0652 
0653     // Now make sure that if we put in a parameter of type A, we don't
0654     // see it when we ask for names of type B != A.
0655     {
0656       edm::ParameterSet p;
0657       p.addParameter<double>("a", 2.5);
0658       p.registerIt();
0659       const bool tracked = true;
0660       std::vector<std::string> names = p.getParameterNamesForType<int>(tracked);
0661       REQUIRE(names.empty());
0662     }
0663   }
0664 
0665   SECTION("Embedded PSet") {
0666     edm::ParameterSet ps;
0667     edm::ParameterSet psEmbedded, psDeeper;
0668     psEmbedded.addUntrackedParameter<std::string>("p1", "wham");
0669     psEmbedded.addParameter<std::string>("p2", "bam");
0670     psDeeper.addParameter<int>("deepest", 6);
0671     psDeeper.registerIt();
0672     edm::InputTag it("label", "instance");
0673     std::vector<edm::InputTag> vit;
0674     vit.push_back(it);
0675     psEmbedded.addParameter<edm::InputTag>("it", it);
0676     psEmbedded.addParameter<std::vector<edm::InputTag>>("vit", vit);
0677     psEmbedded.addParameter<edm::ParameterSet>("psDeeper", psDeeper);
0678     psEmbedded.registerIt();
0679     ps.addParameter<edm::ParameterSet>("psEmbedded", psEmbedded);
0680     ps.addParameter<double>("topLevel", 1.);
0681     ps.addUntrackedParameter<unsigned long long>("u64", 64);
0682 
0683     std::vector<edm::ParameterSet> vpset;
0684     edm::ParameterSet pset1;
0685     pset1.addParameter<int>("int1", 1);
0686     edm::ParameterSet pset2;
0687     pset2.addParameter<int>("int2", 2);
0688     edm::ParameterSet pset3;
0689     pset3.addParameter<int>("int3", 3);
0690     vpset.push_back(pset1);
0691     vpset.push_back(pset2);
0692     vpset.push_back(pset3);
0693     ps.addParameter<std::vector<edm::ParameterSet>>("psVPset", vpset);
0694 
0695     ps.registerIt();
0696 
0697     std::string rep = ps.toString();
0698     edm::ParameterSet defrosted(rep);
0699     defrosted.registerIt();
0700     edm::ParameterSet trackedPart(ps.trackedPart());
0701 
0702     REQUIRE(defrosted == ps);
0703     REQUIRE(trackedPart.exists("psEmbedded"));
0704     REQUIRE(trackedPart.getParameterSet("psEmbedded").exists("p2"));
0705     REQUIRE(!trackedPart.getParameterSet("psEmbedded").exists("p1"));
0706     REQUIRE(trackedPart.getParameterSet("psEmbedded").getParameterSet("psDeeper").getParameter<int>("deepest") == 6);
0707     REQUIRE(ps.getUntrackedParameter<unsigned long long>("u64") == 64);
0708     REQUIRE(!trackedPart.exists("u64"));
0709     std::vector<edm::ParameterSet> const& vpset1 = trackedPart.getParameterSetVector("psVPset");
0710     REQUIRE(vpset1[0].getParameter<int>("int1") == 1);
0711     REQUIRE(vpset1[1].getParameter<int>("int2") == 2);
0712     REQUIRE(vpset1[2].getParameter<int>("int3") == 3);
0713 
0714     SECTION("deprecated pset encoding") {
0715       //Used in Utilities/StorageFactory
0716       char const* const psetChar =
0717           "<destinations=-s({63657272})"                                  // cerr
0718           ";cerr=-P(<noTimeStamps=-B(true);threshold=-S(5741524e494e47)"  // WARNING
0719           ";WARNING=-P(<limit=-I(+0)>);default=-P(<limit=-I(-1)>)>)>";
0720       edm::ParameterSet pset(psetChar);
0721       pset.registerIt();
0722       {
0723         auto p = pset.getUntrackedParameter<std::vector<std::string>>("destinations");
0724         REQUIRE(p.size() == 1);
0725         REQUIRE(p[0] == "cerr");
0726         REQUIRE(not pset.exists("threshold"));
0727       }
0728       {
0729         auto p = pset.getUntrackedParameter<edm::ParameterSet>("cerr");
0730         REQUIRE(p.getUntrackedParameter<bool>("noTimeStamps"));
0731         REQUIRE(p.getUntrackedParameter<std::string>("threshold") == "WARNING");
0732         {
0733           auto p2 = p.getUntrackedParameter<edm::ParameterSet>("WARNING");
0734           REQUIRE(p2.getUntrackedParameter<int>("limit") == 0);
0735           REQUIRE(not p2.exists("default"));
0736         }
0737         {
0738           auto p3 = p.getUntrackedParameter<edm::ParameterSet>("default");
0739           REQUIRE(p3.getUntrackedParameter<int>("limit") == -1);
0740         }
0741       }
0742     }
0743     SECTION("deprecated vstring encoding") {
0744       char const* const psetChar =
0745           "<empty=-p({});"
0746           "simple=-p({<a=-I(+0)>,<a=-I(+1)>,<b=-i({+1,+2})>});"
0747           "recurse=-p({<r=-p({<a=-I(+0)>,<a=-I(+1)>})>})"
0748           ">";
0749       edm::ParameterSet pset(psetChar);
0750       pset.registerIt();
0751       {
0752         auto empty = pset.getUntrackedParameter<std::vector<edm::ParameterSet>>("empty");
0753         REQUIRE(empty.empty());
0754       }
0755       {
0756         auto simple = pset.getUntrackedParameter<std::vector<edm::ParameterSet>>("simple");
0757         REQUIRE(simple.size() == 3);
0758         REQUIRE(simple[0].getUntrackedParameter<int>("a") == 0);
0759         REQUIRE(simple[1].getUntrackedParameter<int>("a") == 1);
0760         REQUIRE(simple[2].getUntrackedParameter<std::vector<int>>("b").size() == 2);
0761       }
0762       {
0763         auto recurse = pset.getUntrackedParameter<std::vector<edm::ParameterSet>>("recurse");
0764         REQUIRE(recurse.size() == 1);
0765         REQUIRE(recurse[0].getUntrackedParameter<std::vector<edm::ParameterSet>>("r").size() == 2);
0766       }
0767     }
0768   }
0769 
0770   SECTION("Registration") {
0771     edm::ParameterSet ps;
0772     edm::ParameterSet psEmbedded, psDeeper;
0773     psEmbedded.addUntrackedParameter<std::string>("p1", "wham");
0774     psEmbedded.addParameter<std::string>("p2", "bam");
0775     psDeeper.addParameter<int>("deepest", 6);
0776     psDeeper.registerIt();
0777     edm::InputTag it("label", "instance");
0778     std::vector<edm::InputTag> vit;
0779     vit.push_back(it);
0780     psEmbedded.addParameter<edm::InputTag>("it", it);
0781     psEmbedded.addParameter<std::vector<edm::InputTag>>("vit", vit);
0782     psEmbedded.addParameter<edm::ParameterSet>("psDeeper", psDeeper);
0783     psEmbedded.registerIt();
0784     ps.addParameter<edm::ParameterSet>("psEmbedded", psEmbedded);
0785     ps.addParameter<double>("topLevel", 1.);
0786     ps.addUntrackedParameter<unsigned long long>("u64", 64);
0787     ps.registerIt();
0788     REQUIRE(ps.isRegistered());
0789     REQUIRE(psEmbedded.isRegistered());
0790     REQUIRE(psDeeper.isRegistered());
0791     psEmbedded.addParameter<std::string>("p3", "slam");
0792     REQUIRE(ps.isRegistered());
0793     REQUIRE(!psEmbedded.isRegistered());
0794     REQUIRE(psDeeper.isRegistered());
0795   }
0796 
0797   SECTION("Copy From") {
0798     edm::ParameterSet psOld;
0799     edm::ParameterSet psNew;
0800     edm::ParameterSet psInternal;
0801     std::vector<edm::ParameterSet> vpset;
0802     vpset.push_back(psInternal);
0803     psOld.addParameter<int>("i", 5);
0804     psOld.addParameter<edm::ParameterSet>("ps", psInternal);
0805     psOld.addParameter<std::vector<edm::ParameterSet>>("vps", vpset);
0806     psNew.copyFrom(psOld, "i");
0807     psNew.copyFrom(psOld, "ps");
0808     psNew.copyFrom(psOld, "vps");
0809     REQUIRE(psNew.existsAs<int>("i"));
0810     REQUIRE(psNew.existsAs<edm::ParameterSet>("ps"));
0811     REQUIRE(psNew.existsAs<std::vector<edm::ParameterSet>>("vps"));
0812   }
0813 
0814   SECTION("Get Parameter As String") {
0815     edm::ParameterSet ps;
0816     edm::ParameterSet psInternal;
0817     std::vector<edm::ParameterSet> vpset;
0818     vpset.push_back(psInternal);
0819     ps.addParameter<int>("i", 5);
0820     ps.addParameter<edm::ParameterSet>("ps", psInternal);
0821     ps.addParameter<std::vector<edm::ParameterSet>>("vps", vpset);
0822     ps.registerIt();
0823     psInternal.registerIt();
0824     std::string parStr = ps.getParameterAsString("i");
0825     std::string psetStr = ps.getParameterAsString("ps");
0826     std::string vpsetStr = ps.getParameterAsString("vps");
0827     std::string parStr2 = ps.retrieve("i").toString();
0828     std::string psetStr2 = ps.retrieveParameterSet("ps").toString();
0829     std::string vpsetStr2 = ps.retrieveVParameterSet("vps").toString();
0830     REQUIRE(parStr == parStr2);
0831     REQUIRE(psetStr == psetStr2);
0832     REQUIRE(vpsetStr == vpsetStr2);
0833   }
0834 }