Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  *  parametersetid_t.cppunit.cc
0003  *  CMSSW
0004  *
0005  */
0006 
0007 #include <map>
0008 #include <string>
0009 
0010 #include "cppunit/extensions/HelperMacros.h"
0011 
0012 #include "DataFormats/Provenance/interface/ParameterSetID.h"
0013 #include "FWCore/Utilities/interface/EDMException.h"
0014 
0015 class testParameterSetID : public CppUnit::TestFixture {
0016   CPPUNIT_TEST_SUITE(testParameterSetID);
0017   CPPUNIT_TEST(constructTest);
0018   CPPUNIT_TEST_EXCEPTION(badConstructTest, edm::Exception);
0019   CPPUNIT_TEST(comparisonTest);
0020   CPPUNIT_TEST(suitableForMapTest);
0021   CPPUNIT_TEST(unhexifyTest);
0022   CPPUNIT_TEST(printTest);
0023   CPPUNIT_TEST(oldRootFileCompatibilityTest);
0024   CPPUNIT_TEST_SUITE_END();
0025 
0026   std::string default_id_string;
0027   std::string cow16;
0028 
0029 public:
0030   void setUp() {
0031     default_id_string = "d41d8cd98f00b204e9800998ecf8427e";
0032     cow16 = "DEADBEEFDEADBEEF";
0033   }
0034   void tearDown() {}
0035 
0036   void constructTest();
0037   void badConstructTest();
0038   void comparisonTest();
0039   void suitableForMapTest();
0040   void unhexifyTest();
0041   void printTest();
0042   void oldRootFileCompatibilityTest();
0043 };
0044 
0045 ///registration of the test so that the runner can find it
0046 CPPUNIT_TEST_SUITE_REGISTRATION(testParameterSetID);
0047 
0048 void testParameterSetID::constructTest() {
0049   edm::ParameterSetID id1;
0050   CPPUNIT_ASSERT(!id1.isValid());
0051 
0052   edm::ParameterSetID id2(cow16);
0053   CPPUNIT_ASSERT(id2.isValid());
0054   CPPUNIT_ASSERT(id2.compactForm() == cow16);
0055 }
0056 
0057 void testParameterSetID::badConstructTest() { edm::ParameterSetID a("1"); }
0058 
0059 void testParameterSetID::comparisonTest() {
0060   edm::ParameterSetID a;
0061   edm::ParameterSetID b;
0062   CPPUNIT_ASSERT(a == b);
0063   CPPUNIT_ASSERT(!(a != b));
0064   CPPUNIT_ASSERT(!(a < b));
0065   CPPUNIT_ASSERT(!(a > b));
0066 }
0067 
0068 void testParameterSetID::suitableForMapTest() {
0069   typedef std::map<edm::ParameterSetID, int> map_t;
0070   map_t m;
0071   CPPUNIT_ASSERT(m.empty());
0072 
0073   edm::ParameterSetID a;
0074   m[a] = 100;
0075   CPPUNIT_ASSERT(m.size() == 1);
0076   CPPUNIT_ASSERT(m[a] == 100);
0077 
0078   edm::ParameterSetID b(cow16);
0079   m[b] = 200;
0080   CPPUNIT_ASSERT(m.size() == 2);
0081   CPPUNIT_ASSERT(m[a] == 100);
0082   CPPUNIT_ASSERT(m[b] == 200);
0083 
0084   CPPUNIT_ASSERT(m.erase(a) == 1);
0085   CPPUNIT_ASSERT(m.size() == 1);
0086   CPPUNIT_ASSERT(m[b] == 200);
0087   CPPUNIT_ASSERT(m.find(a) == m.end());
0088 }
0089 
0090 void testParameterSetID::unhexifyTest() {
0091   // 'a' has the MD5 checksum for an empty string.
0092   edm::ParameterSetID a(default_id_string);
0093   std::string a_compact = a.compactForm();
0094   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[0]) == 0xd4);
0095   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[1]) == 0x1d);
0096   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[2]) == 0x8c);
0097   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[3]) == 0xd9);
0098   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[4]) == 0x8f);
0099   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[5]) == 0x00);
0100   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[6]) == 0xb2);
0101   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[7]) == 0x04);
0102   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[8]) == 0xe9);
0103   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[9]) == 0x80);
0104   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[10]) == 0x09);
0105   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[11]) == 0x98);
0106   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[12]) == 0xec);
0107   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[13]) == 0xf8);
0108   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[14]) == 0x42);
0109   CPPUNIT_ASSERT(static_cast<unsigned char>(a_compact[15]) == 0x7e);
0110 
0111   edm::ParameterSetID b;
0112   std::string b_compact = b.compactForm();
0113   CPPUNIT_ASSERT(b_compact.size() == 16);
0114 }
0115 
0116 void testParameterSetID::printTest() {
0117   std::ostringstream os;
0118   edm::ParameterSetID id(default_id_string);
0119   os << id;
0120   std::string output = os.str();
0121   CPPUNIT_ASSERT(output == default_id_string);
0122 
0123   std::ostringstream os2;
0124   std::string s2("0123456789abcdef0123456789abcdef");
0125   edm::ParameterSetID id2(s2);
0126   CPPUNIT_ASSERT(id2.isValid());
0127   os2 << id2;
0128   std::string output2 = os2.str();
0129   CPPUNIT_ASSERT(output2 == s2);
0130 }
0131 
0132 #include <iostream>
0133 void testParameterSetID::oldRootFileCompatibilityTest() {
0134   using namespace edm;
0135   //simulate what ROOT does when reading an old ParameterSetID which has 32 characters
0136   ParameterSetID dflt(default_id_string);
0137   std::string sValue(default_id_string);
0138   ParameterSetID* evil(reinterpret_cast<ParameterSetID*>(&sValue));
0139 
0140   CPPUNIT_ASSERT(not evil->isCompactForm());
0141   CPPUNIT_ASSERT(dflt.isCompactForm());
0142 
0143   ParameterSetID evilCopy(*evil);
0144   CPPUNIT_ASSERT(evilCopy.isCompactForm());
0145 
0146   CPPUNIT_ASSERT(dflt == evilCopy);
0147   CPPUNIT_ASSERT(evilCopy == *evil);
0148 
0149   std::cout << dflt << std::endl;
0150   std::cout << evil << std::endl;
0151   CPPUNIT_ASSERT(dflt == *evil);
0152 
0153   /*Do an 'exhaustive' test to see if comparisons are preserved
0154     in the case of conversion from non-compact to compact form
0155     and that comparision between non-compact to compact form also 
0156     preserves ordering.
0157     Because the 'hex' version is just a repetition of two characters per byte,
0158     we only need to do 2^8-1 comparisions rather than 2^32-1 comparisions when
0159     doing the exhaustive test
0160    */
0161 
0162   const char hexbits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
0163   const size_t nHexBits = sizeof(hexbits) / sizeof(char);
0164   char buffer[3];
0165   buffer[2] = 0;
0166   std::string theOldValue("00000000000000000000000000000000");
0167   ParameterSetID theOldHash(theOldValue);
0168   for (const char* itHigh = hexbits; itHigh != hexbits + nHexBits; ++itHigh) {
0169     const char* lowStart = hexbits;
0170     if (itHigh == hexbits) {
0171       lowStart += 1;
0172     }
0173     for (const char* itLow = lowStart; itLow != hexbits + nHexBits; ++itLow) {
0174       buffer[0] = *itHigh;
0175       buffer[1] = *itLow;
0176       std::string theValue(buffer);
0177       //need to make this 32 bytes long, now we are 2 bytes
0178       theValue = theValue + theValue;  //4
0179       theValue = theValue + theValue;  //8
0180       theValue = theValue + theValue;  //16
0181       theValue = theValue + theValue;  //32
0182       //std::cout <<theValue<<std::endl;
0183       CPPUNIT_ASSERT(theOldValue < theValue);
0184       ParameterSetID theHash(theValue);
0185       CPPUNIT_ASSERT(theOldHash < theHash);
0186 
0187       ParameterSetID* theEvil(reinterpret_cast<ParameterSetID*>(&theValue));
0188       ParameterSetID* theOldEvil(reinterpret_cast<ParameterSetID*>(&theOldValue));
0189       CPPUNIT_ASSERT(*theOldEvil < *theEvil);
0190       CPPUNIT_ASSERT(*theOldEvil < theHash);
0191       CPPUNIT_ASSERT(theOldHash < *theEvil);
0192       theOldValue = theValue;
0193       theOldHash = theHash;
0194     }
0195   }
0196 }