ChimeraTK-DeviceAccess  03.18.00
testLMapMonostableTriggerPlugin.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #define BOOST_TEST_DYN_LINK
5 #define BOOST_TEST_MODULE LMapMonostableTriggerPluginTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 #include "BackendFactory.h"
10 #include "Device.h"
11 #include "DeviceAccessVersion.h"
12 #include "DummyBackend.h"
13 
14 using namespace ChimeraTK;
15 
16 BOOST_AUTO_TEST_SUITE(LMapMonostableTriggerPluginTestSuite)
17 
18 /**********************************************************************************************************************/
19 
20 class TestDummy : public ChimeraTK::DummyBackend {
21  public:
22  TestDummy(std::string mapFileName) : DummyBackend(mapFileName) {}
23 
24  size_t sequenceCounter{0};
25  bool sequenceComplete{true};
26  int32_t active{0};
27  int32_t inactive{0};
28  std::chrono::milliseconds delay;
29  std::chrono::time_point<std::chrono::steady_clock> t0;
30 
31  static boost::shared_ptr<DeviceBackend> createInstance(std::string, std::map<std::string, std::string> parameters) {
32  return boost::shared_ptr<DeviceBackend>(new TestDummy(parameters["map"]));
33  }
34 
35  void write(uint64_t bar, uint64_t address, int32_t const* data, size_t sizeInBytes) override {
36  if(bar == 0 && address == 0x44 && sizeInBytes == 4) { // ADC.WORD_ADC_ENA
37  if(sequenceComplete) {
38  // start new sequence
39  sequenceComplete = false;
40  active = data[0];
42  return;
43  }
44  else {
45  // finish the sequence
46  delay = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - t0);
47  sequenceComplete = true;
48  ++sequenceCounter;
49  inactive = data[0];
50  return;
51  }
52  }
53  }
54 
56  public:
58  };
60 };
61 
63 
65  std::cout << "TestDummy::BackendRegisterer: registering backend type TestDummy" << std::endl;
67 }
68 
69 /**********************************************************************************************************************/
70 
71 BOOST_AUTO_TEST_CASE(testDefaultValues) {
72  std::cout << "testDefaultValues" << std::endl;
74  device.open("(logicalNameMap?map=monostableTriggerPlugin.xlmap)");
75 
76  auto target = boost::dynamic_pointer_cast<TestDummy>(
77  ChimeraTK::BackendFactory::getInstance().createBackend("(TestDummy?map=mtcadummy.map)"));
78  assert(target != nullptr);
79  auto accTrigger1 = device.getScalarRegisterAccessor<double>("testDefaultValues");
80 
81  usleep(10000);
82  BOOST_CHECK_EQUAL(target->sequenceCounter, 0);
83  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
84 
85  for(size_t i = 1; i < 5; ++i) {
86  accTrigger1 = 10 * i; // this should be ignored
87  accTrigger1.write();
88  BOOST_CHECK_EQUAL(target->sequenceCounter, i);
89  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
90  BOOST_CHECK_EQUAL(target->active, 1);
91  BOOST_CHECK_EQUAL(target->inactive, 0);
92  BOOST_CHECK(target->delay.count() > 90);
93  BOOST_CHECK(target->delay.count() < 200);
94  }
95 }
96 
97 /**********************************************************************************************************************/
98 
99 BOOST_AUTO_TEST_CASE(testNonDefaultValues) {
100  std::cout << "testNonDefaultValues" << std::endl;
102  device.open("(logicalNameMap?map=monostableTriggerPlugin.xlmap)");
103 
104  auto target = boost::dynamic_pointer_cast<TestDummy>(
105  ChimeraTK::BackendFactory::getInstance().createBackend("(TestDummy?map=mtcadummy.map)"));
106  assert(target != nullptr);
107  auto accTrigger1 = device.getScalarRegisterAccessor<double>("testNonDefaultValues");
108 
109  usleep(10000);
110  BOOST_CHECK_EQUAL(target->sequenceCounter, 0);
111  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
112 
113  for(size_t i = 1; i < 5; ++i) {
114  accTrigger1 = 10 * i; // this should be ignored
115  accTrigger1.write();
116  BOOST_CHECK_EQUAL(target->sequenceCounter, i);
117  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
118  BOOST_CHECK_EQUAL(target->active, 42);
119  BOOST_CHECK_EQUAL(target->inactive, 120);
120  BOOST_CHECK(target->delay.count() > 90);
121  BOOST_CHECK(target->delay.count() < 200);
122  }
123 }
124 
125 /**********************************************************************************************************************/
126 
127 BOOST_AUTO_TEST_CASE(testDifferentDelay) {
128  std::cout << "testDifferentDelay" << std::endl;
130  device.open("(logicalNameMap?map=monostableTriggerPlugin.xlmap)");
131 
132  auto target = boost::dynamic_pointer_cast<TestDummy>(
133  ChimeraTK::BackendFactory::getInstance().createBackend("(TestDummy?map=mtcadummy.map)"));
134  assert(target != nullptr);
135  auto accTrigger1 = device.getScalarRegisterAccessor<double>("testDifferentDelay");
136 
137  usleep(10000);
138  BOOST_CHECK_EQUAL(target->sequenceCounter, 0);
139  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
140 
141  for(size_t i = 1; i < 5; ++i) {
142  accTrigger1 = 10 * i; // this should be ignored
143  accTrigger1.write();
144  BOOST_CHECK_EQUAL(target->sequenceCounter, i);
145  BOOST_CHECK_EQUAL(target->sequenceComplete, true);
146  BOOST_CHECK_EQUAL(target->active, 1);
147  BOOST_CHECK_EQUAL(target->inactive, 0);
148  BOOST_CHECK(target->delay.count() > 450);
149  BOOST_CHECK(target->delay.count() < 600);
150  }
151 }
152 /**********************************************************************************************************************/
153 
154 BOOST_AUTO_TEST_SUITE_END()
TestDummy::backendRegisterer
static BackendRegisterer backendRegisterer
Definition: testLMapMonostableTriggerPlugin.cpp:59
device
ctk::Device device
Definition: testExceptionDummyDevice.cc:18
TestDummy::BackendRegisterer::BackendRegisterer
BackendRegisterer()
Definition: testLMapMonostableTriggerPlugin.cpp:64
ChimeraTK::BackendFactory::getInstance
static BackendFactory & getInstance()
Static function to get an instance of factory.
Definition: BackendFactory.cc:191
DummyBackend.h
ChimeraTK::testable_rebot_sleep::now
boost::chrono::steady_clock::time_point now()
Definition: testableRebotSleep.cc:7
TestDummy::delay
std::chrono::milliseconds delay
Definition: testLMapMonostableTriggerPlugin.cpp:28
TestDummy
Definition: testLMapMonostableTriggerPlugin.cpp:20
TestDummy::TestDummy
TestDummy(std::string mapFileName)
Definition: testLMapMonostableTriggerPlugin.cpp:22
TestDummy::t0
std::chrono::time_point< std::chrono::steady_clock > t0
Definition: testLMapMonostableTriggerPlugin.cpp:29
Device.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testDefaultValues)
Definition: testLMapMonostableTriggerPlugin.cpp:71
TestDummy::write
void write(uint64_t bar, uint64_t address, int32_t const *data, size_t sizeInBytes) override
Write function to be implemented by backends.
Definition: testLMapMonostableTriggerPlugin.cpp:35
ChimeraTK::Device
Class allows to read/write registers from device.
Definition: Device.h:39
ChimeraTK::Device::open
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition: Device.cc:58
TestDummy::createInstance
static boost::shared_ptr< DeviceBackend > createInstance(std::string, std::map< std::string, std::string > parameters)
Definition: testLMapMonostableTriggerPlugin.cpp:31
ChimeraTK::Device::getScalarRegisterAccessor
ScalarRegisterAccessor< UserType > getScalarRegisterAccessor(const RegisterPath &registerPathName, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a ScalarRegisterObject object for the given register.
Definition: Device.h:263
BackendFactory.h
ChimeraTK::DummyBackend
The dummy device opens a mapping file instead of a device, and implements all registers defined in th...
Definition: DummyBackend.h:45
TestDummy::BackendRegisterer
Definition: testLMapMonostableTriggerPlugin.cpp:55
ChimeraTK::BackendFactory::registerBackendType
void registerBackendType(const std::string &backendType, boost::shared_ptr< DeviceBackend >(*creatorFunction)(std::string address, std::map< std::string, std::string > parameters), const std::vector< std::string > &sdmParameterNames={}, const std::string &deviceAccessVersion=CHIMERATK_DEVICEACCESS_VERSION)
Register a backend by the name backendType with the given creatorFunction.
Definition: BackendFactory.cc:45
ChimeraTK
Definition: DummyBackend.h:16