Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-29 01:21:25

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     unittest_fwmodelid
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Jan 18 10:19:07 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <boost/test/unit_test.hpp>
0015 #include <boost/test/test_tools.hpp>
0016 #include <stdexcept>
0017 #include <iostream>
0018 #include <ios>
0019 #include <fstream>
0020 
0021 // user include files
0022 #include "Fireworks/Core/interface/FWConfiguration.h"
0023 #include "Fireworks/Core/interface/FWConfigurable.h"
0024 #include "Fireworks/Core/interface/FWConfigurationManager.h"
0025 
0026 namespace {
0027   struct Conf : public FWConfigurable {
0028     virtual void addTo(FWConfiguration& iTop) const { iTop = m_config; }
0029 
0030     virtual void setFrom(const FWConfiguration& iFrom) { m_config = iFrom; }
0031 
0032     FWConfiguration m_config;
0033   };
0034 }  // namespace
0035 //
0036 // constants, enums and typedefs
0037 //
0038 BOOST_AUTO_TEST_CASE(fwconfiguration) {
0039   FWConfiguration config;
0040   BOOST_CHECK(1 == config.version());
0041   BOOST_CHECK(0 == config.stringValues());
0042   BOOST_CHECK(0 == config.keyValues());
0043 
0044   const std::string kValue("1.0");
0045   config.addValue(kValue);
0046   BOOST_REQUIRE(0 != config.stringValues());
0047   BOOST_CHECK(1 == config.stringValues()->size());
0048   BOOST_CHECK(0 == config.keyValues());
0049   BOOST_CHECK(kValue == config.value());
0050   BOOST_CHECK_THROW(config.addKeyValue("one", FWConfiguration("two")), std::runtime_error);
0051 
0052   //copy constructor
0053   FWConfiguration config2(config);
0054   BOOST_CHECK(1 == config2.version());
0055   BOOST_REQUIRE(0 != config2.stringValues());
0056   BOOST_CHECK(1 == config2.stringValues()->size());
0057   BOOST_CHECK(0 == config2.keyValues());
0058   BOOST_CHECK(kValue == config2.value());
0059 
0060   //operator=
0061   FWConfiguration config3;
0062   config3 = config;
0063   BOOST_CHECK(1 == config3.version());
0064   BOOST_REQUIRE(0 != config3.stringValues());
0065   BOOST_CHECK(1 == config3.stringValues()->size());
0066   BOOST_CHECK(0 == config3.keyValues());
0067   BOOST_CHECK(kValue == config3.value());
0068 
0069   FWConfiguration valueForConst(kValue);
0070   BOOST_CHECK(0 == valueForConst.version());
0071   BOOST_REQUIRE(0 != valueForConst.stringValues());
0072   BOOST_CHECK(1 == valueForConst.stringValues()->size());
0073   BOOST_CHECK(0 == valueForConst.keyValues());
0074   BOOST_CHECK(kValue == valueForConst.value());
0075 
0076   FWConfiguration topConfig;
0077   topConfig.addKeyValue("first", config);
0078   BOOST_REQUIRE(0 != topConfig.keyValues());
0079   BOOST_CHECK(1 == topConfig.keyValues()->size());
0080   BOOST_CHECK(0 == topConfig.stringValues());
0081   BOOST_CHECK(std::string("first") == topConfig.keyValues()->front().first);
0082   BOOST_CHECK(kValue == topConfig.keyValues()->front().second.value());
0083   BOOST_CHECK_THROW(topConfig.addValue("one"), std::runtime_error);
0084   const FWConfiguration* found = topConfig.valueForKey("second");
0085   BOOST_CHECK(0 == found);
0086   found = topConfig.valueForKey("first");
0087   BOOST_REQUIRE(0 != found);
0088   BOOST_CHECK(found->value() == kValue);
0089   BOOST_CHECK_THROW(config.valueForKey("blah"), std::runtime_error);
0090 
0091   FWConfiguration::streamTo(std::cout, topConfig, "top");
0092 
0093   //Test manager
0094   std::unique_ptr<Conf> pConf(new Conf());
0095 
0096   FWConfigurationManager confMgr;
0097   confMgr.add("first", pConf.get());
0098 
0099   confMgr.setFrom(topConfig);
0100   BOOST_REQUIRE(0 != pConf->m_config.stringValues());
0101   BOOST_CHECK(1 == pConf->m_config.stringValues()->size());
0102   BOOST_CHECK(0 == pConf->m_config.keyValues());
0103   BOOST_CHECK(kValue == pConf->m_config.value());
0104 
0105   {
0106     FWConfiguration topConfig;
0107     confMgr.to(topConfig);
0108 
0109     BOOST_REQUIRE(0 != topConfig.keyValues());
0110     BOOST_CHECK(1 == topConfig.keyValues()->size());
0111     BOOST_CHECK(0 == topConfig.stringValues());
0112     BOOST_CHECK(std::string("first") == topConfig.keyValues()->front().first);
0113     BOOST_CHECK(kValue == topConfig.keyValues()->front().second.value());
0114     found = topConfig.valueForKey("first");
0115     BOOST_REQUIRE(0 != found);
0116     BOOST_CHECK(found->value() == kValue);
0117   }
0118   confMgr.writeToFile("testConfig");
0119   {
0120     FWConfiguration temp;
0121     pConf->m_config.swap(temp);
0122   }
0123   confMgr.readFromFile("testConfig");
0124   BOOST_REQUIRE(0 != pConf->m_config.stringValues());
0125   BOOST_CHECK(1 == pConf->m_config.stringValues()->size());
0126   BOOST_CHECK(0 == pConf->m_config.keyValues());
0127   BOOST_CHECK(kValue == pConf->m_config.value());
0128 
0129   std::ofstream log("testConfig", std::ios_base::app | std::ios_base::out);
0130   log << "line\n";
0131   log.close();
0132   try {
0133     confMgr.readFromFile("testConfig");
0134   } catch (...) {
0135     std::cerr << "OK, checked parser exception \n";
0136   }
0137 }