Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:27

0001 /*
0002  *  serviceregistry_t.cppunit.cc
0003  *  CMSSW
0004  *
0005  *  Created by Chris Jones on 9/7/05.
0006  *
0007  */
0008 
0009 //need to open a 'back door' to be able to setup the SimActivityRegistry
0010 #include "SimG4Core/Notification/interface/SimActivityRegistry.h"
0011 #include "SimG4Core/Notification/interface/SimActivityRegistryEnroller.h"
0012 #include "SimG4Core/Notification/interface/Observer.h"
0013 
0014 #include <cppunit/extensions/HelperMacros.h>
0015 
0016 class testSimActivityRegistry : public CppUnit::TestFixture {
0017   CPPUNIT_TEST_SUITE(testSimActivityRegistry);
0018 
0019   CPPUNIT_TEST(signalTest);
0020   CPPUNIT_TEST(signalForwardingTest);
0021   CPPUNIT_TEST(enrollerTest);
0022 
0023   CPPUNIT_TEST_SUITE_END();
0024 
0025 public:
0026   void setUp() override {}
0027   void tearDown() override {}
0028 
0029   void signalTest();
0030   void signalForwardingTest();
0031   void enrollerTest();
0032 };
0033 
0034 ///registration of the test so that the runner can find it
0035 CPPUNIT_TEST_SUITE_REGISTRATION(testSimActivityRegistry);
0036 
0037 namespace {
0038   template <class T>
0039   struct MyObserver : public Observer<const T*> {
0040     mutable bool saw_;
0041     MyObserver() : saw_(false) {}
0042     void update(const T*) override { saw_ = true; }
0043   };
0044 }  // namespace
0045 
0046 #define TEST(signal, SIGNAL)           \
0047   MyObserver<SIGNAL> watch##SIGNAL;    \
0048   registry.connect(&watch##SIGNAL);    \
0049   const SIGNAL* p##SIGNAL = 0;         \
0050   registry.signal##Signal_(p##SIGNAL); \
0051   CPPUNIT_ASSERT(watch##SIGNAL.saw_);
0052 
0053 void testSimActivityRegistry::signalTest() {
0054   SimActivityRegistry registry;
0055 
0056   for (int i = 0; i < 1000; i++) {
0057     TEST(beginOfRun, BeginOfRun);
0058     TEST(beginOfJob, BeginOfJob);
0059     TEST(beginOfEvent, BeginOfEvent);
0060     TEST(beginOfTrack, BeginOfTrack);
0061     TEST(dddWorld, DDDWorld);
0062     TEST(g4Step, G4Step);
0063 
0064     TEST(endOfRun, EndOfRun);
0065     TEST(endOfEvent, EndOfEvent);
0066     TEST(endOfTrack, EndOfTrack);
0067   }
0068 }
0069 
0070 #define TESTF(signal, SIGNAL)          \
0071   MyObserver<SIGNAL> watch##SIGNAL;    \
0072   registry2.connect(&watch##SIGNAL);   \
0073   const SIGNAL* p##SIGNAL = 0;         \
0074   registry.signal##Signal_(p##SIGNAL); \
0075   CPPUNIT_ASSERT(watch##SIGNAL.saw_);
0076 
0077 void testSimActivityRegistry::signalForwardingTest() {
0078   SimActivityRegistry registry;
0079   SimActivityRegistry registry2;
0080   registry.connect(registry2);
0081 
0082   for (int i = 0; i < 1000; i++) {
0083     TESTF(beginOfRun, BeginOfRun);
0084     TESTF(beginOfJob, BeginOfJob);
0085     TESTF(beginOfEvent, BeginOfEvent);
0086     TESTF(beginOfTrack, BeginOfTrack);
0087     TESTF(dddWorld, DDDWorld);
0088     TESTF(g4Step, G4Step);
0089 
0090     TESTF(endOfRun, EndOfRun);
0091     TESTF(endOfEvent, EndOfEvent);
0092     TESTF(endOfTrack, EndOfTrack);
0093   }
0094 }
0095 
0096 namespace {
0097 
0098   template <class T>
0099   struct Counting : public Observer<const T*> {
0100     int& count_;
0101     Counting(int& iCount) : count_(iCount) {}
0102     void update(const T*) override { ++count_; }
0103   };
0104   struct NoSignal {};
0105 
0106   struct OneSignal : public Counting<BeginOfEvent> {
0107     OneSignal(int& iCount) : Counting<BeginOfEvent>(iCount) {}
0108   };
0109 
0110   struct TwoSignals : public Counting<BeginOfEvent>, public Counting<EndOfEvent> {
0111     TwoSignals(int& iCount) : Counting<BeginOfEvent>(iCount), Counting<EndOfEvent>(iCount) {}
0112   };
0113 
0114 }  // namespace
0115 
0116 #define TESTREG(signal, SIGNAL)                  \
0117   int count##SIGNAL = 0;                         \
0118   Counting<SIGNAL> watch##SIGNAL(count##SIGNAL); \
0119   enroller.enroll(registry, &watch##SIGNAL);     \
0120   const SIGNAL* p##SIGNAL = 0;                   \
0121   registry.signal##Signal_(p##SIGNAL);           \
0122   CPPUNIT_ASSERT(1 == watch##SIGNAL.count_);
0123 
0124 void testSimActivityRegistry::enrollerTest() {
0125   SimActivityRegistry registry;
0126 
0127   NoSignal noSignal;
0128   SimActivityRegistryEnroller enroller;
0129   enroller.enroll(registry, &noSignal);
0130 
0131   int int1Signal = 0;
0132   OneSignal oneSignal(int1Signal);
0133   enroller.enroll(registry, &oneSignal);
0134 
0135   int int2Signals = 0;
0136   TwoSignals twoSignals(int2Signals);
0137   enroller.enroll(registry, &twoSignals);
0138 
0139   const BeginOfEvent* pBegin = nullptr;
0140   registry.beginOfEventSignal_(pBegin);
0141 
0142   const EndOfEvent* pEnd = nullptr;
0143   registry.endOfEventSignal_(pEnd);
0144 
0145   CPPUNIT_ASSERT(1 == int1Signal);
0146   CPPUNIT_ASSERT(2 == int2Signals);
0147 
0148   TESTREG(beginOfRun, BeginOfRun);
0149   TESTREG(beginOfJob, BeginOfJob);
0150   TESTREG(beginOfEvent, BeginOfEvent);
0151   TESTREG(beginOfTrack, BeginOfTrack);
0152   TESTREG(dddWorld, DDDWorld);
0153   TESTREG(g4Step, G4Step);
0154 
0155   TESTREG(endOfRun, EndOfRun);
0156   TESTREG(endOfEvent, EndOfEvent);
0157   TESTREG(endOfTrack, EndOfTrack);
0158 }
0159 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>