Skip to content
Snippets Groups Projects
Commit 40cb7fde authored by Jan H. K. Timm's avatar Jan H. K. Timm
Browse files

ExceptionDummy

		add bool thereHaveBeenExceptions
		write test for taht
			testExceptionsDummyDevice.cc
parent a61bfdbf
No related branches found
No related tags found
No related merge requests found
#define BOOST_TEST_MODULE testExceptionsDummy
#include "ExceptionDevice.h"
#include <ChimeraTK/BackendFactory.h>
#include <ChimeraTK/Device.h>
#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test_framework;
namespace ctk = ChimeraTK;
auto exceptionDummy = boost::dynamic_pointer_cast<ExceptionDummy>(ctk::BackendFactory::getInstance().createBackend("(ExceptionDummy:1?map=test3.map)"));
ctk::Device device;
BOOST_AUTO_TEST_CASE(testExceptionsDummyDevice) {
// test general function
BOOST_CHECK(!device.isFunctional());
device.open("(ExceptionDummy:1?map=test3.map)");
BOOST_CHECK(device.isFunctional());
// test throwExceptionRead
exceptionDummy->throwExceptionRead = true;
BOOST_CHECK(!device.isFunctional());
BOOST_CHECK_THROW(device.read<int32_t>("/Integers/signed32"),ChimeraTK::runtime_error);
BOOST_CHECK(!device.isFunctional());
BOOST_CHECK_THROW(device.open("(ExceptionDummy:1?map=test3.map)"),ChimeraTK::runtime_error);
BOOST_CHECK(!device.isFunctional());
exceptionDummy->throwExceptionRead = false;
BOOST_CHECK(!device.isFunctional());
device.open("(ExceptionDummy:1?map=test3.map)");
BOOST_CHECK(device.isFunctional());
// test throwExceptionWrite
exceptionDummy->throwExceptionWrite = true;
BOOST_CHECK(!device.isFunctional());
BOOST_CHECK_THROW(device.write<int32_t>("/Integers/signed32",0),ChimeraTK::runtime_error);
BOOST_CHECK(!device.isFunctional());
BOOST_CHECK_THROW(device.open("(ExceptionDummy:1?map=test3.map)"),ChimeraTK::runtime_error);
BOOST_CHECK(!device.isFunctional());
exceptionDummy->throwExceptionWrite = false;
BOOST_CHECK(!device.isFunctional());
device.open("(ExceptionDummy:1?map=test3.map)");
BOOST_CHECK(device.isFunctional());
// test throwExceptionOpen
exceptionDummy->throwExceptionOpen = true;
BOOST_CHECK(!device.isFunctional());
BOOST_CHECK_THROW(device.open("(ExceptionDummy:1?map=test3.map)"),ChimeraTK::runtime_error);
BOOST_CHECK(!device.isFunctional());
exceptionDummy->throwExceptionOpen = false;
device.open("(ExceptionDummy:1?map=test3.map)");
BOOST_CHECK(device.isFunctional());
}
......@@ -8,22 +8,28 @@ class ExceptionDummy : public ChimeraTK::DummyBackend {
bool throwExceptionOpen{false};
bool throwExceptionRead{false};
bool throwExceptionWrite{false};
bool thereHaveBeenExceptions{false};
static boost::shared_ptr<DeviceBackend> createInstance(std::string, std::map<std::string, std::string> parameters) {
return boost::shared_ptr<DeviceBackend>(new ExceptionDummy(parameters["map"]));
}
void open() override {
if(throwExceptionOpen) {
thereHaveBeenExceptions = true;
throw(ChimeraTK::runtime_error("DummyException: This is a test"));
}
ChimeraTK::DummyBackend::open();
if(throwExceptionRead || throwExceptionWrite) {
thereHaveBeenExceptions = true;
throw(ChimeraTK::runtime_error("DummyException: open throws because of device error when already open."));
}
thereHaveBeenExceptions = false;
}
void read(uint8_t bar, uint32_t address, int32_t* data, size_t sizeInBytes) override {
if(throwExceptionRead) {
thereHaveBeenExceptions = true;
throw(ChimeraTK::runtime_error("DummyException: read throws by request"));
}
ChimeraTK::DummyBackend::read(bar, address, data, sizeInBytes);
......@@ -31,13 +37,14 @@ class ExceptionDummy : public ChimeraTK::DummyBackend {
void write(uint8_t bar, uint32_t address, int32_t const* data, size_t sizeInBytes) override {
if(throwExceptionWrite) {
thereHaveBeenExceptions = true;
throw(ChimeraTK::runtime_error("DummyException: write throws by request"));
}
ChimeraTK::DummyBackend::write(bar, address, data, sizeInBytes);
}
bool isFunctional() const override {
return (_opened && !throwExceptionOpen && !throwExceptionRead && !throwExceptionWrite);
return (_opened && !throwExceptionOpen && !throwExceptionRead && !throwExceptionWrite && !thereHaveBeenExceptions);
}
class BackendRegisterer {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment