Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-06 05:39:23

0001 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"  //gives main
0002 #include <cppunit/extensions/HelperMacros.h>
0003 
0004 #include "TClass.h"
0005 #include "TList.h"
0006 #include "TDataMember.h"
0007 
0008 #include <string>
0009 #include <unordered_map>
0010 #include <iostream>
0011 #include <cassert>
0012 
0013 using std::string;
0014 using std::unordered_map;
0015 
0016 class TestSchemaEvolution : public CppUnit::TestFixture {
0017   CPPUNIT_TEST_SUITE(TestSchemaEvolution);
0018   CPPUNIT_TEST(checkVersions);
0019   CPPUNIT_TEST_SUITE_END();
0020 
0021 public:
0022   TestSchemaEvolution() = default;
0023   ~TestSchemaEvolution() override = default;
0024   void setUp() override {}
0025   void tearDown() override {}
0026   void checkVersions();
0027 
0028 private:
0029   void fillBaseline();
0030   void gatherAllClasses();
0031   void runComparison();
0032   void loopOnDataMembers(TClass *);
0033   void loopOnBases(TClass *);
0034   void analyseClass(TClass *);
0035 
0036   unordered_map<string, short> unique_classes_;
0037   unordered_map<string, short> unique_classes_current_;
0038 };
0039 
0040 CPPUNIT_TEST_SUITE_REGISTRATION(TestSchemaEvolution);
0041 
0042 void TestSchemaEvolution::fillBaseline() {
0043   unique_classes_current_.insert(std::make_pair("vector<double>", 6));
0044   unique_classes_current_.insert(std::make_pair("TF1", 12));
0045   unique_classes_current_.insert(std::make_pair("TH3S", 4));
0046   unique_classes_current_.insert(std::make_pair("TAtt3D", 1));
0047   unique_classes_current_.insert(std::make_pair("TH3", 6));
0048   unique_classes_current_.insert(std::make_pair("TH3F", 4));
0049   unique_classes_current_.insert(std::make_pair("TH2D", 4));
0050   unique_classes_current_.insert(std::make_pair("TH2S", 4));
0051   unique_classes_current_.insert(std::make_pair("TArrayI", 1));
0052   unique_classes_current_.insert(std::make_pair("TH2I", 4));
0053   unique_classes_current_.insert(std::make_pair("TH2F", 4));
0054   unique_classes_current_.insert(std::make_pair("TH1I", 3));
0055   unique_classes_current_.insert(std::make_pair("TString", 2));
0056   unique_classes_current_.insert(std::make_pair("TAttLine", 2));
0057   unique_classes_current_.insert(std::make_pair("TObject", 1));
0058   unique_classes_current_.insert(std::make_pair("TH3D", 4));
0059   unique_classes_current_.insert(std::make_pair("TH1S", 3));
0060   unique_classes_current_.insert(std::make_pair("TH3I", 4));
0061   unique_classes_current_.insert(std::make_pair("TAttFill", 2));
0062   unique_classes_current_.insert(std::make_pair("TNamed", 1));
0063   unique_classes_current_.insert(std::make_pair("TH1F", 3));
0064   unique_classes_current_.insert(std::make_pair("TH2", 5));
0065   unique_classes_current_.insert(std::make_pair("TH1", 8));
0066   unique_classes_current_.insert(std::make_pair("TProfile", 7));
0067   unique_classes_current_.insert(std::make_pair("TAttMarker", 2));
0068   unique_classes_current_.insert(std::make_pair("TArray", 1));
0069   unique_classes_current_.insert(std::make_pair("TAxis", 10));
0070   unique_classes_current_.insert(std::make_pair("TProfile2D", 8));
0071   unique_classes_current_.insert(std::make_pair("TH1D", 3));
0072   unique_classes_current_.insert(std::make_pair("TArrayS", 1));
0073   unique_classes_current_.insert(std::make_pair("TAttAxis", 4));
0074   unique_classes_current_.insert(std::make_pair("TArrayD", 1));
0075   unique_classes_current_.insert(std::make_pair("TArrayF", 1));
0076 }
0077 
0078 void TestSchemaEvolution::runComparison() {
0079   CPPUNIT_ASSERT(unique_classes_current_.size() == unique_classes_.size());
0080   for (auto cl : unique_classes_) {
0081     std::cout << "Checking " << cl.first << " " << cl.second << std::endl;
0082     //std::cout << "unique_classes_current_.insert(std::make_pair(\"" << cl.first << "\", " << cl.second << "));" << std::endl;
0083     CPPUNIT_ASSERT(unique_classes_.find(cl.first) != unique_classes_.end());
0084     CPPUNIT_ASSERT(unique_classes_[cl.first] == unique_classes_current_[cl.first]);
0085   }
0086 }
0087 
0088 void TestSchemaEvolution::checkVersions() {
0089   fillBaseline();
0090   gatherAllClasses();
0091   runComparison();
0092 }
0093 
0094 void TestSchemaEvolution::gatherAllClasses() {
0095   static const char *classes[] = {"TH1F",
0096                                   "TH1S",
0097                                   "TH1D",
0098                                   "TH1I",
0099                                   "TH2F",
0100                                   "TH2S",
0101                                   "TH2D",
0102                                   "TH2I",
0103                                   "TH3F",
0104                                   "TH3S",
0105                                   "TH3D",
0106                                   "TH3I",
0107                                   "TProfile",
0108                                   "TProfile2D",
0109                                   "TF1",
0110                                   nullptr};
0111 
0112   int i = 0;
0113   while (classes[i]) {
0114     TClass *tcl = TClass::GetClass(classes[i]);
0115     if (!tcl)
0116       continue;
0117     unique_classes_.insert(std::make_pair(classes[i], tcl->GetClassVersion()));
0118     analyseClass(tcl);
0119     ++i;
0120   }
0121 }
0122 
0123 void TestSchemaEvolution::loopOnDataMembers(TClass *tcl) {
0124   TList *dms = tcl->GetListOfDataMembers();
0125   TIter next(dms);
0126   while (TObject *obj = next()) {
0127     TClass *cl = TClass::GetClass(((TDataMember *)obj)->GetFullTypeName());
0128     if (cl && cl->HasDictionary()) {
0129       unique_classes_.insert(std::make_pair(cl->GetName(), cl->GetClassVersion()));
0130       analyseClass(cl);
0131     }
0132   }
0133 }
0134 
0135 void TestSchemaEvolution::loopOnBases(TClass *tcl) {
0136   TList *bases = tcl->GetListOfBases();
0137   TIter next(bases);
0138   while (TObject *obj = next()) {
0139     TClass *cl = TClass::GetClass(obj->GetName());
0140     if (cl && cl->HasDictionary()) {
0141       unique_classes_.insert(std::make_pair(cl->GetName(), cl->GetClassVersion()));
0142       analyseClass(cl);
0143     }
0144   }
0145 }
0146 
0147 void TestSchemaEvolution::analyseClass(TClass *cl) {
0148   loopOnBases(cl);
0149   loopOnDataMembers(cl);
0150 }