Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:56

0001 // Test of the DictionaryTools functions.
0002 
0003 #include "DataFormats/Common/interface/Wrapper.h"
0004 #include "FWCore/Utilities/interface/TypeDemangler.h"
0005 #include "FWCore/Utilities/interface/TypeID.h"
0006 #include "FWCore/Reflection/interface/TypeWithDict.h"
0007 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"
0008 
0009 #include "cppunit/extensions/HelperMacros.h"
0010 
0011 #include <typeinfo>
0012 #include <map>
0013 #include <vector>
0014 
0015 class TestDictionaries : public CppUnit::TestFixture {
0016   CPPUNIT_TEST_SUITE(TestDictionaries);
0017   CPPUNIT_TEST(default_is_invalid);
0018   CPPUNIT_TEST(no_dictionary_is_invalid);
0019   CPPUNIT_TEST(not_a_template_instance);
0020   CPPUNIT_TEST(demangling);
0021   CPPUNIT_TEST_SUITE_END();
0022 
0023 public:
0024   TestDictionaries() {}
0025   ~TestDictionaries() {}
0026   void setUp() {}
0027   void tearDown() {}
0028 
0029   void default_is_invalid();
0030   void no_dictionary_is_invalid();
0031   void not_a_template_instance();
0032   void demangling();
0033 
0034 private:
0035 };
0036 
0037 CPPUNIT_TEST_SUITE_REGISTRATION(TestDictionaries);
0038 
0039 void TestDictionaries::default_is_invalid() {
0040   edm::TypeWithDict t;
0041   CPPUNIT_ASSERT(!t);
0042 }
0043 
0044 void TestDictionaries::no_dictionary_is_invalid() {
0045   edm::TypeWithDict t(edm::TypeWithDict::byName("ThereIsNoTypeWithThisName"));
0046   CPPUNIT_ASSERT(!t);
0047 }
0048 
0049 void TestDictionaries::not_a_template_instance() {
0050   edm::TypeWithDict not_a_template(edm::TypeWithDict::byName("double"));
0051   CPPUNIT_ASSERT(not_a_template);
0052   std::string nonesuch(not_a_template.templateName());
0053   CPPUNIT_ASSERT(nonesuch.empty());
0054 }
0055 
0056 namespace {
0057   template <typename T>
0058   void checkIt() {
0059     edm::TypeWithDict type(typeid(T));
0060     // Test only if class has dictionary
0061     if (bool(type)) {
0062       std::string demangledName(edm::typeDemangle(typeid(T).name()));
0063       CPPUNIT_ASSERT(type.name() == demangledName);
0064 
0065       edm::TypeID tid(typeid(T));
0066       CPPUNIT_ASSERT(tid.className() == demangledName);
0067 
0068       edm::TypeWithDict typeFromName = edm::TypeWithDict::byName(demangledName);
0069       CPPUNIT_ASSERT(typeFromName.name() == demangledName);
0070       if (type.isClass()) {
0071         edm::TypeID tidFromName(typeFromName.typeInfo());
0072         CPPUNIT_ASSERT(tidFromName.className() == demangledName);
0073       }
0074     }
0075   }
0076 
0077   template <typename T>
0078   void checkDemangling() {
0079     checkIt<std::vector<T> >();
0080     checkIt<edm::Wrapper<T> >();
0081     checkIt<edm::Wrapper<std::vector<T> > >();
0082     checkIt<T>();
0083     checkIt<T[1]>();
0084   }
0085 }  // namespace
0086 
0087 void TestDictionaries::demangling() {
0088   checkDemangling<int>();
0089   checkDemangling<unsigned int>();
0090   checkDemangling<unsigned long>();
0091   checkDemangling<long>();
0092   checkDemangling<unsigned long>();
0093   checkDemangling<long long>();
0094   checkDemangling<unsigned long long>();
0095   checkDemangling<short>();
0096   checkDemangling<unsigned short>();
0097   checkDemangling<char>();
0098   checkDemangling<unsigned char>();
0099   checkDemangling<float>();
0100   checkDemangling<double>();
0101   checkDemangling<bool>();
0102   checkDemangling<std::string>();
0103 }