Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:21

0001 /*
0002  *  recordwriter.cppunit.cc
0003  *  CMSSW
0004  *
0005  *  Created by Chris Jones on 09/12/09.
0006  *
0007  */
0008 #include <vector>
0009 #include <iostream>
0010 
0011 #include "TFile.h"
0012 
0013 #include <cppunit/extensions/HelperMacros.h>
0014 #include "PhysicsTools/CondLiteIO/interface/RecordWriter.h"
0015 #include "DataFormats/FWLite/interface/Record.h"
0016 #include "DataFormats/FWLite/interface/EventSetup.h"
0017 #include "DataFormats/FWLite/interface/ESHandle.h"
0018 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0019 
0020 class testRecord : public CppUnit::TestFixture {
0021   CPPUNIT_TEST_SUITE(testRecord);
0022 
0023   CPPUNIT_TEST(testGood);
0024   CPPUNIT_TEST(testFailures);
0025 
0026   CPPUNIT_TEST_SUITE_END();
0027   static bool s_firstSetup;
0028 
0029 public:
0030   void setUp();
0031   void tearDown() {}
0032 
0033   void testGood();
0034   void testFailures();
0035 };
0036 
0037 ///registration of the test so that the runner can find it
0038 CPPUNIT_TEST_SUITE_REGISTRATION(testRecord);
0039 
0040 bool testRecord::s_firstSetup = true;
0041 
0042 void testRecord::setUp() {
0043   if (s_firstSetup) {
0044     s_firstSetup = false;
0045 
0046     //create the test file
0047     TFile f("testRecord.root", "RECREATE");
0048     fwlite::RecordWriter w("TestRecord", &f);
0049 
0050     std::vector<int> v;
0051     edmtest::SimpleDerived d;
0052     for (int index = 0; index < 6; ++index) {
0053       //std::cout <<" index "<<index<<std::endl;
0054       v.push_back(index);
0055       w.update(&v, typeid(v), "");
0056 
0057       d.key = index;
0058       d.value = index;
0059       d.dummy = index;
0060       w.update(&d, typeid(edmtest::Simple), "");
0061 
0062       w.fill(edm::ESRecordAuxiliary(edm::EventID(index * 2 + 1, 0, 0), edm::Timestamp()));
0063     }
0064     w.write();
0065     f.Close();
0066   }
0067 }
0068 
0069 void testRecord::testGood() {
0070   {
0071     TFile f("testRecord.root", "READ");
0072 
0073     fwlite::EventSetup es(&f);
0074 
0075     std::vector<std::string> recordNames = es.namesOfAvailableRecords();
0076     CPPUNIT_ASSERT(recordNames.size() == 1);
0077     CPPUNIT_ASSERT(recordNames[0] == "TestRecord");
0078 
0079     fwlite::RecordID testRecID = es.recordID("TestRecord");
0080 
0081     std::vector<std::pair<std::string, std::string> > dataIds = es.get(testRecID).typeAndLabelOfAvailableData();
0082 
0083     CPPUNIT_ASSERT(dataIds.size() == 2);
0084     unsigned int matches = 0;
0085     for (auto const& dataId : dataIds) {
0086       std::cout << dataId.first << " '" << dataId.second << "'" << std::endl;
0087       if ((dataId.first == "std::vector<int>") && (dataId.second == "")) {
0088         ++matches;
0089         continue;
0090       }
0091       if ((dataId.first == "edmtest::Simple") && (dataId.second == "")) {
0092         ++matches;
0093       }
0094     }
0095 
0096     CPPUNIT_ASSERT(2 == matches);
0097 
0098     for (unsigned int index = 1; index < 10; ++index) {
0099       es.syncTo(edm::EventID(index, 0, 0), edm::Timestamp());
0100       unsigned int run = index;
0101       if (0 != (run - 1) % 2) {
0102         --run;
0103       }
0104 
0105       fwlite::ESHandle<std::vector<int> > vIntHandle;
0106       CPPUNIT_ASSERT(es.get(testRecID).get(vIntHandle));
0107       CPPUNIT_ASSERT(vIntHandle.isValid());
0108       //std::cout <<" index "<<index<<" size "<<vIntHandle->size()<<" "<<es.get(testRecID).startSyncValue().eventID()<<std::endl;
0109       CPPUNIT_ASSERT(es.get(testRecID).startSyncValue().eventID().run() == run);
0110       CPPUNIT_ASSERT(vIntHandle->size() == (index - 1) / 2 + 1);
0111 
0112       fwlite::ESHandle<edmtest::Simple> simpleHandle;
0113       CPPUNIT_ASSERT(es.get(testRecID).get(simpleHandle));
0114       CPPUNIT_ASSERT(simpleHandle->key == static_cast<int>((index - 1) / 2));
0115     }
0116   }
0117 }
0118 
0119 struct DummyWithNoDictionary {};
0120 
0121 void testRecord::testFailures() {
0122   TFile f("testRecord.root", "READ");
0123 
0124   fwlite::EventSetup es(&f);
0125 
0126   CPPUNIT_ASSERT(not es.exists("DoesNotExist"));
0127   CPPUNIT_ASSERT_THROW(es.recordID("DoesNotExist"), cms::Exception);
0128 
0129   fwlite::RecordID testRecID = es.recordID("TestRecord");
0130 
0131   const fwlite::Record& testRecord = es.get(testRecID);
0132 
0133   fwlite::ESHandle<std::vector<int> > vIntHandle;
0134   CPPUNIT_ASSERT(not vIntHandle.isValid());
0135 
0136   CPPUNIT_ASSERT(not testRecord.get(vIntHandle));
0137   CPPUNIT_ASSERT(not vIntHandle.isValid());
0138   CPPUNIT_ASSERT_THROW(*vIntHandle, cms::Exception);
0139 
0140   es.syncTo(edm::EventID(1, 0, 0), edm::Timestamp());
0141 
0142   CPPUNIT_ASSERT(not testRecord.get(vIntHandle, "notThere"));
0143 
0144   fwlite::ESHandle<std::vector<DummyWithNoDictionary> > noDictHandle;
0145   CPPUNIT_ASSERT(not testRecord.get(noDictHandle));
0146   CPPUNIT_ASSERT_THROW(*noDictHandle, cms::Exception);
0147   CPPUNIT_ASSERT_THROW(noDictHandle.operator->(), cms::Exception);
0148 }