Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:44

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 
0019 // user include files
0020 #include "Fireworks/Core/interface/FWConfiguration.h"
0021 #include "Fireworks/Core/interface/FWLongParameter.h"
0022 #include "Fireworks/Core/interface/FWCompositeParameter.h"
0023 #include "Fireworks/Core/interface/FWParameterizable.h"
0024 
0025 namespace {
0026   struct Test : public FWParameterizable {
0027     Test()
0028         : m_double(this, "double", std::bind(&Test::doubleChanged, this, std::placeholders::_1)),
0029           m_long(this, "long", std::bind(&Test::longChanged, this, std::placeholders::_1)),
0030           m_wasChanged(false) {}
0031 
0032     void doubleChanged(double iValue) {
0033       BOOST_CHECK(iValue == m_double.value());
0034       m_wasChanged = true;
0035     }
0036     void longChanged(long iValue) {
0037       BOOST_CHECK(iValue == m_long.value());
0038       m_wasChanged = true;
0039     }
0040 
0041     FWDoubleParameter m_double;
0042     FWLongParameter m_long;
0043     bool m_wasChanged;
0044   };
0045 
0046   struct CompTest : public FWParameterizable {
0047     FWCompositeParameter m_comp;
0048     FWDoubleParameter m_d1;
0049     FWDoubleParameter m_d2;
0050 
0051     CompTest() : m_comp(this, "comp"), m_d1(&m_comp, "d1"), m_d2(&m_comp, "d2") {}
0052   };
0053 }  // namespace
0054 //
0055 // constants, enums and typedefs
0056 //
0057 BOOST_AUTO_TEST_CASE(parameters) {
0058   Test t;
0059   BOOST_CHECK(0. == t.m_double.value());
0060   BOOST_CHECK(0 == t.m_long.value());
0061   BOOST_CHECK(not t.m_wasChanged);
0062 
0063   t.m_double.set(10.);
0064   BOOST_CHECK(10. == t.m_double.value());
0065   BOOST_CHECK(0 == t.m_long.value());
0066   BOOST_CHECK(t.m_wasChanged);
0067 
0068   t.m_wasChanged = false;
0069   t.m_long.set(2);
0070   BOOST_CHECK(10. == t.m_double.value());
0071   BOOST_CHECK(2 == t.m_long.value());
0072   BOOST_CHECK(t.m_wasChanged);
0073 
0074   //check configuration ability
0075   {
0076     FWConfiguration config;
0077 
0078     t.m_wasChanged = false;
0079     t.m_double.addTo(config);
0080     BOOST_CHECK(not t.m_wasChanged);
0081 
0082     BOOST_REQUIRE(0 != config.keyValues());
0083     BOOST_CHECK(1 == config.keyValues()->size());
0084     BOOST_CHECK(std::string("double") == config.keyValues()->front().first);
0085 
0086     t.m_double.set(1.);
0087     BOOST_CHECK(t.m_wasChanged);
0088     BOOST_CHECK(t.m_double.value() == 1.);
0089 
0090     t.m_wasChanged = false;
0091     t.m_double.setFrom(config);
0092     BOOST_CHECK(t.m_wasChanged);
0093     BOOST_CHECK(10. == t.m_double.value());
0094   }
0095   {
0096     FWConfiguration config;
0097 
0098     t.m_wasChanged = false;
0099     t.m_long.addTo(config);
0100     BOOST_CHECK(not t.m_wasChanged);
0101 
0102     BOOST_REQUIRE(0 != config.keyValues());
0103     BOOST_CHECK(1 == config.keyValues()->size());
0104     BOOST_CHECK(std::string("long") == config.keyValues()->front().first);
0105 
0106     t.m_long.set(1);
0107     BOOST_CHECK(t.m_wasChanged);
0108     BOOST_CHECK(t.m_long.value() == 1);
0109 
0110     t.m_wasChanged = false;
0111     t.m_long.setFrom(config);
0112     BOOST_CHECK(t.m_wasChanged);
0113     BOOST_CHECK(2 == t.m_long.value());
0114   }
0115 
0116   CompTest ct;
0117   BOOST_CHECK(ct.end() - ct.begin() == 1);
0118   BOOST_CHECK(ct.m_comp.end() - ct.m_comp.begin() == 2);
0119   ct.m_d1.set(10.);
0120   ct.m_d2.set(11.);
0121 
0122   FWConfiguration ctConf;
0123   ct.m_comp.addTo(ctConf);
0124 
0125   FWConfiguration::streamTo(std::cout, ctConf, "top");
0126 
0127   ct.m_d1.set(0);
0128   ct.m_d2.set(1);
0129 
0130   ct.m_comp.setFrom(ctConf);
0131   BOOST_CHECK(ct.m_d1.value() == 10.);
0132   BOOST_CHECK(ct.m_d2.value() == 11.);
0133 }