From 5e21c6ec3a6b7fbb6fefdbce3e56e79f9c2b2cd9 Mon Sep 17 00:00:00 2001 From: t0mpr1c3 Date: Mon, 2 Oct 2023 00:47:35 -0400 Subject: [PATCH 1/2] pointers -> references --- src/ayab/analogReadAsyncWrapper.h | 6 +- src/ayab/beeper.h | 6 +- src/ayab/com.cpp | 13 +- src/ayab/com.h | 6 +- src/ayab/controller.cpp | 6 +- src/ayab/controller.h | 12 +- src/ayab/encoders.h | 6 +- src/ayab/global_OpError.cpp | 12 +- src/ayab/global_OpIdle.cpp | 12 +- src/ayab/global_OpInit.cpp | 14 +- src/ayab/global_OpKnit.cpp | 24 +- src/ayab/global_OpReady.cpp | 12 +- src/ayab/global_OpTest.cpp | 36 +-- src/ayab/global_analogReadAsyncWrapper.cpp | 2 +- src/ayab/global_beeper.cpp | 14 +- src/ayab/global_com.cpp | 36 ++- src/ayab/global_controller.cpp | 28 +- src/ayab/global_encoders.cpp | 22 +- src/ayab/global_packetSerialWrapper.cpp | 8 +- src/ayab/global_solenoids.cpp | 6 +- src/ayab/main.cpp | 54 ++-- src/ayab/opError.h | 6 +- src/ayab/opIdle.h | 6 +- src/ayab/opInit.cpp | 2 +- src/ayab/opInit.h | 6 +- src/ayab/opKnit.cpp | 4 +- src/ayab/opKnit.h | 6 +- src/ayab/opReady.h | 6 +- src/ayab/opTest.h | 6 +- src/ayab/packetSerialWrapper.h | 6 +- src/ayab/solenoids.h | 6 +- test/test_OpError.cpp | 32 +-- test/test_OpIdle.cpp | 24 +- test/test_OpInit.cpp | 46 ++-- test/test_OpKnit.cpp | 246 +++++++++--------- test/test_OpReady.cpp | 30 +-- test/test_OpTest.cpp | 112 ++++---- test/test_all.cpp | 58 ++--- test/test_beeper.cpp | 44 ++-- test/test_boards.cpp | 60 ++--- test/test_com.cpp | 116 +++++---- test/test_controller.cpp | 112 ++++---- test/test_encoders.cpp | 286 ++++++++++----------- test/test_solenoids.cpp | 34 +-- 44 files changed, 797 insertions(+), 792 deletions(-) diff --git a/src/ayab/analogReadAsyncWrapper.h b/src/ayab/analogReadAsyncWrapper.h index ab99a4e5c..79b3a1f7e 100644 --- a/src/ayab/analogReadAsyncWrapper.h +++ b/src/ayab/analogReadAsyncWrapper.h @@ -36,7 +36,7 @@ class AnalogReadAsyncWrapperInterface { }; // Container class for the static method analogReadAsync. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `AnalogReadAsyncWrapper` or `AnalogReadAsyncWrapperMock`, // both of which classes implement the // pure virtual methods of `AnalogReadAsyncWrapperInterface`. @@ -47,8 +47,8 @@ class GlobalAnalogReadAsyncWrapper final { GlobalAnalogReadAsyncWrapper() = default; public: - // pointer to global instance whose methods are implemented - static AnalogReadAsyncWrapperInterface *m_instance; + // reference to global instance whose methods are implemented + static AnalogReadAsyncWrapperInterface& m_instance; static void analogReadAsyncWrapped(uint8_t pin, analogReadCompleteCallback_t cb = nullptr, const void *data = nullptr); }; diff --git a/src/ayab/beeper.h b/src/ayab/beeper.h index 263dcd55b..ff586b472 100644 --- a/src/ayab/beeper.h +++ b/src/ayab/beeper.h @@ -53,7 +53,7 @@ class BeeperInterface { }; // Container class for the static methods that control the beeper. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `Beeper` or `BeeperMock`, both of which classes implement the // pure virtual methods of `BeeperInterface`. @@ -63,8 +63,8 @@ class GlobalBeeper final { GlobalBeeper() = default; public: - // pointer to global instance whose methods are implemented - static BeeperInterface *m_instance; + // reference to global instance whose methods are implemented + static BeeperInterface& m_instance; static void init(bool enabled); static void update(); diff --git a/src/ayab/com.cpp b/src/ayab/com.cpp index a54eff8c8..24d7c8e8b 100644 --- a/src/ayab/com.cpp +++ b/src/ayab/com.cpp @@ -152,7 +152,7 @@ void Com::send_indState(Err_t error) const { send(static_cast(payload), INDSTATE_LEN); } -/*! GCOVR_EXCL_START +/*! * * \brief Callback for PacketSerial. * \param buffer A pointer to a data buffer. @@ -161,7 +161,6 @@ void Com::send_indState(Err_t error) const { void Com::onPacketReceived(const uint8_t *buffer, size_t size) { GlobalController::getState()->com(buffer, size); } -// GCOVR_EXCL_STOP // Serial command handling @@ -191,7 +190,7 @@ void Com::h_reqInit(const uint8_t *buffer, size_t size) { } GlobalController::setMachineType(machineType); - GlobalController::setState(GlobalOpInit::m_instance); + GlobalController::setState(&GlobalOpInit::m_instance); send_cnfInit(Err_t::Success); } @@ -277,8 +276,6 @@ void Com::h_cnfLine(const uint8_t *buffer, size_t size) { /*! * \brief Handle `reqInfo` (request information) command. - * \param buffer A pointer to a data buffer. - * \param size The number of bytes in the data buffer. */ void Com::h_reqInfo() const { send_cnfInfo(); @@ -288,7 +285,7 @@ void Com::h_reqInfo() const { * \brief Handle `reqTest` (request hardware test) command. */ void Com::h_reqTest() const { - GlobalController::setState(GlobalOpTest::m_instance); + GlobalController::setState(&GlobalOpTest::m_instance); send_cnfTest(Err_t::Success); } @@ -296,14 +293,13 @@ void Com::h_reqTest() const { * \brief Handle `quitCmd` (cancel) command. */ void Com::h_quitCmd() const { - GlobalController::setState(GlobalOpInit::m_instance); + GlobalController::setState(&GlobalOpInit::m_instance); } /*! * \brief Handle unrecognized command. */ void Com::h_unrecognized() const { - // do nothing } /*! @@ -334,7 +330,6 @@ void Com::send_cnfInit(Err_t error) const { send(payload, 2); } - /*! * \brief Send `cnfStart` message. * \param error Error code (0 = success, other values = error). diff --git a/src/ayab/com.h b/src/ayab/com.h index 0474dd070..dbd898075 100644 --- a/src/ayab/com.h +++ b/src/ayab/com.h @@ -102,7 +102,7 @@ class ComInterface { }; // Container class for the static methods that implement the serial API. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `Com` or `ComMock`, both of which classes implement the // pure virtual methods of `ComInterface`. @@ -112,8 +112,8 @@ class GlobalCom final { GlobalCom() = default; public: - // pointer to global instance whose methods are implemented - static ComInterface *m_instance; + // reference to global instance whose methods are implemented + static ComInterface& m_instance; static void init(); static void update(); diff --git a/src/ayab/controller.cpp b/src/ayab/controller.cpp index fc8fdd909..3a3f75407 100644 --- a/src/ayab/controller.cpp +++ b/src/ayab/controller.cpp @@ -53,8 +53,8 @@ void Controller::init() { m_hallActive = Direction_t::NoDirection; m_beltShift = BeltShift_t::Unknown; m_position = 0; - m_currentState = GlobalOpIdle::m_instance; - m_nextState = GlobalOpIdle::m_instance; + m_currentState = &GlobalOpIdle::m_instance; + m_nextState = &GlobalOpIdle::m_instance; } /*! @@ -109,7 +109,7 @@ void Controller::setState(OpInterface *state) { * \brief Get machine state. * \return Current state of Finite State Machine. */ -OpInterface *Controller::getState() { +OpInterface* Controller::getState() { return m_currentState; } diff --git a/src/ayab/controller.h b/src/ayab/controller.h index 2b9d0912b..b4f75c7e3 100644 --- a/src/ayab/controller.h +++ b/src/ayab/controller.h @@ -38,7 +38,7 @@ class ControllerInterface { virtual void update() = 0; virtual void cacheEncoders() = 0; virtual void setState(OpInterface *state) = 0; - virtual OpInterface *getState() = 0; + virtual OpInterface* getState() = 0; virtual void setMachineType(Machine_t) = 0; virtual Machine_t getMachineType() = 0; virtual BeltShift_t getBeltShift() = 0; @@ -49,7 +49,7 @@ class ControllerInterface { }; // Singleton container class for static methods. -// Dependency injection is enabled using a pointer +// Dependency injection is enabled using a reference // to a global instance of either `Controller` or `ControllerMock` // both of which classes implement the pure virtual methods // of the `ControllerInterface` class. @@ -60,14 +60,14 @@ class GlobalController final { GlobalController() = default; public: - // pointer to global instance whose methods are implemented - static ControllerInterface *m_instance; + // reference to global instance whose methods are implemented + static ControllerInterface& m_instance; static void init(); static void update(); static void cacheEncoders(); static void setState(OpInterface *state); - static OpInterface *getState(); + static OpInterface* getState(); static void setMachineType(Machine_t); static Machine_t getMachineType(); static BeltShift_t getBeltShift(); @@ -83,7 +83,7 @@ class Controller : public ControllerInterface { void update() final; void cacheEncoders() final; void setState(OpInterface *state) final; - OpInterface *getState() final; + OpInterface* getState() final; void setMachineType(Machine_t) final; Machine_t getMachineType() final; BeltShift_t getBeltShift() final; diff --git a/src/ayab/encoders.h b/src/ayab/encoders.h index 55382d411..4577b43b0 100644 --- a/src/ayab/encoders.h +++ b/src/ayab/encoders.h @@ -135,7 +135,7 @@ class EncodersInterface { }; // Container class for the static methods for the encoders. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `Encoders` or `EncodersMock`, both of which classes implement the // pure virtual methods of `EncodersInterface`. @@ -145,8 +145,8 @@ class GlobalEncoders final { GlobalEncoders() = default; public: - // pointer to global instance whose methods are implemented - static EncodersInterface *m_instance; + // reference to global instance whose methods are implemented + static EncodersInterface& m_instance; static void init(Machine_t machineType); static void setUpInterrupt(); diff --git a/src/ayab/global_OpError.cpp b/src/ayab/global_OpError.cpp index 6d06dd5fb..e9e7b9d1b 100644 --- a/src/ayab/global_OpError.cpp +++ b/src/ayab/global_OpError.cpp @@ -27,25 +27,25 @@ // static member functions OpState_t GlobalOpError::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpError::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpError::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpError::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpError::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpError::end() { - m_instance->end(); + m_instance.end(); } diff --git a/src/ayab/global_OpIdle.cpp b/src/ayab/global_OpIdle.cpp index e08f39a9b..8b3559f3c 100644 --- a/src/ayab/global_OpIdle.cpp +++ b/src/ayab/global_OpIdle.cpp @@ -27,25 +27,25 @@ // static member functions OpState_t GlobalOpIdle::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpIdle::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpIdle::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpIdle::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpIdle::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpIdle::end() { - m_instance->end(); + m_instance.end(); } diff --git a/src/ayab/global_OpInit.cpp b/src/ayab/global_OpInit.cpp index 9e7237119..81cef7356 100644 --- a/src/ayab/global_OpInit.cpp +++ b/src/ayab/global_OpInit.cpp @@ -27,30 +27,30 @@ // static member functions OpState_t GlobalOpInit::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpInit::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpInit::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpInit::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpInit::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpInit::end() { - m_instance->end(); + m_instance.end(); } bool GlobalOpInit::isReady() { - return m_instance->isReady(); + return m_instance.isReady(); } diff --git a/src/ayab/global_OpKnit.cpp b/src/ayab/global_OpKnit.cpp index 467c7ea4d..9ce19a886 100644 --- a/src/ayab/global_OpKnit.cpp +++ b/src/ayab/global_OpKnit.cpp @@ -28,53 +28,53 @@ // static member functions OpState_t GlobalOpKnit::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpKnit::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpKnit::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpKnit::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpKnit::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpKnit::end() { - m_instance->end(); + m_instance.end(); } Err_t GlobalOpKnit::startKnitting(uint8_t startNeedle, uint8_t stopNeedle, uint8_t *pattern_start, bool continuousReportingEnabled) { - return m_instance->startKnitting(startNeedle, stopNeedle, + return m_instance.startKnitting(startNeedle, stopNeedle, pattern_start, continuousReportingEnabled); } void GlobalOpKnit::encodePosition() { - m_instance->encodePosition(); + m_instance.encodePosition(); } void GlobalOpKnit::knit() { - m_instance->knit(); + m_instance.knit(); } uint8_t GlobalOpKnit::getStartOffset(const Direction_t direction) { - return m_instance->getStartOffset(direction); + return m_instance.getStartOffset(direction); } bool GlobalOpKnit::setNextLine(uint8_t lineNumber) { - return m_instance->setNextLine(lineNumber); + return m_instance.setNextLine(lineNumber); } void GlobalOpKnit::setLastLine() { - m_instance->setLastLine(); + m_instance.setLastLine(); } diff --git a/src/ayab/global_OpReady.cpp b/src/ayab/global_OpReady.cpp index 7cfbd58c2..3ec6272c7 100644 --- a/src/ayab/global_OpReady.cpp +++ b/src/ayab/global_OpReady.cpp @@ -27,25 +27,25 @@ // static member functions OpState_t GlobalOpReady::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpReady::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpReady::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpReady::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpReady::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpReady::end() { - m_instance->end(); + m_instance.end(); } diff --git a/src/ayab/global_OpTest.cpp b/src/ayab/global_OpTest.cpp index 929eb73d2..b5fe26cef 100644 --- a/src/ayab/global_OpTest.cpp +++ b/src/ayab/global_OpTest.cpp @@ -27,76 +27,76 @@ // static member functions OpState_t GlobalOpTest::state() { - return m_instance->state(); + return m_instance.state(); } void GlobalOpTest::init() { - m_instance->init(); + m_instance.init(); } void GlobalOpTest::begin() { - m_instance->begin(); + m_instance.begin(); } void GlobalOpTest::update() { - m_instance->update(); + m_instance.update(); } void GlobalOpTest::com(const uint8_t *buffer, size_t size) { - m_instance->com(buffer, size); + m_instance.com(buffer, size); } void GlobalOpTest::end() { - m_instance->end(); + m_instance.end(); } bool GlobalOpTest::enabled() { - return m_instance->enabled(); + return m_instance.enabled(); } void GlobalOpTest::helpCmd() { - m_instance->helpCmd(); + m_instance.helpCmd(); } void GlobalOpTest::sendCmd() { - m_instance->sendCmd(); + m_instance.sendCmd(); } void GlobalOpTest::beepCmd() { - m_instance->beepCmd(); + m_instance.beepCmd(); } void GlobalOpTest::setSingleCmd(const uint8_t *buffer, size_t size) { - m_instance->setSingleCmd(buffer, size); + m_instance.setSingleCmd(buffer, size); } void GlobalOpTest::setAllCmd(const uint8_t *buffer, size_t size) { - m_instance->setAllCmd(buffer, size); + m_instance.setAllCmd(buffer, size); } void GlobalOpTest::readEOLsensorsCmd() { - m_instance->readEOLsensorsCmd(); + m_instance.readEOLsensorsCmd(); } void GlobalOpTest::readEncodersCmd() { - m_instance->readEncodersCmd(); + m_instance.readEncodersCmd(); } void GlobalOpTest::autoReadCmd() { - m_instance->autoReadCmd(); + m_instance.autoReadCmd(); } void GlobalOpTest::autoTestCmd() { - m_instance->autoTestCmd(); + m_instance.autoTestCmd(); } void GlobalOpTest::stopCmd() { - m_instance->stopCmd(); + m_instance.stopCmd(); } #ifndef AYAB_TESTS void GlobalOpTest::encoderAChange() { - m_instance->encoderAChange(); + m_instance.encoderAChange(); } #endif // AYAB_TESTS diff --git a/src/ayab/global_analogReadAsyncWrapper.cpp b/src/ayab/global_analogReadAsyncWrapper.cpp index a17d041ea..3727b68c3 100644 --- a/src/ayab/global_analogReadAsyncWrapper.cpp +++ b/src/ayab/global_analogReadAsyncWrapper.cpp @@ -26,5 +26,5 @@ // static member functions void GlobalAnalogReadAsyncWrapper::analogReadAsyncWrapped(uint8_t pin, analogReadCompleteCallback_t cb, const void *data) { - m_instance->analogReadAsyncWrapped(pin, cb, data); + m_instance.analogReadAsyncWrapped(pin, cb, data); } diff --git a/src/ayab/global_beeper.cpp b/src/ayab/global_beeper.cpp index 987e0d0d5..15c3cff44 100644 --- a/src/ayab/global_beeper.cpp +++ b/src/ayab/global_beeper.cpp @@ -28,29 +28,29 @@ // static member functions void GlobalBeeper::init(bool enabled) { - m_instance->init(enabled); + m_instance.init(enabled); } void GlobalBeeper::update() { - m_instance->update(); + m_instance.update(); } void GlobalBeeper::ready() { - m_instance->ready(); + m_instance.ready(); } void GlobalBeeper::finishedLine() { - m_instance->finishedLine(); + m_instance.finishedLine(); } void GlobalBeeper::endWork() { - m_instance->endWork(); + m_instance.endWork(); } BeepState GlobalBeeper::getState() { - return m_instance->getState(); + return m_instance.getState(); } bool GlobalBeeper::enabled() { - return m_instance->enabled(); + return m_instance.enabled(); } diff --git a/src/ayab/global_com.cpp b/src/ayab/global_com.cpp index 81b5b75b5..dc8346caa 100644 --- a/src/ayab/global_com.cpp +++ b/src/ayab/global_com.cpp @@ -26,70 +26,66 @@ // static member functions -// GCOVR_EXCL_START void GlobalCom::init() { - m_instance->init(); + m_instance.init(); } -// GCOVR_EXCL_STOP void GlobalCom::update() { - m_instance->update(); + m_instance.update(); } uint8_t GlobalCom::CRC8(const uint8_t *buffer, size_t len) { - return m_instance->CRC8(buffer, len); + return m_instance.CRC8(buffer, len); } void GlobalCom::send(uint8_t *payload, size_t length) { - m_instance->send(payload, length); + m_instance.send(payload, length); } void GlobalCom::sendMsg(API_t id, const char *msg) { - m_instance->sendMsg(id, msg); + m_instance.sendMsg(id, msg); } void GlobalCom::sendMsg(API_t id, char *msg) { - m_instance->sendMsg(id, msg); + m_instance.sendMsg(id, msg); } void GlobalCom::send_reqLine(const uint8_t lineNumber, Err_t error) { - m_instance->send_reqLine(lineNumber, error); + m_instance.send_reqLine(lineNumber, error); } void GlobalCom::send_indState(Err_t error) { - m_instance->send_indState(error); + m_instance.send_indState(error); } -// GCOVR_EXCL_START void GlobalCom::onPacketReceived(const uint8_t *buffer, size_t size) { - m_instance->onPacketReceived(buffer, size); + m_instance.onPacketReceived(buffer, size); } -// GCOVR_EXCL_STOP void GlobalCom::h_reqInit(const uint8_t *buffer, size_t size) { - m_instance->h_reqInit(buffer, size); + m_instance.h_reqInit(buffer, size); } void GlobalCom::h_reqStart(const uint8_t *buffer, size_t size) { - m_instance->h_reqStart(buffer, size); + m_instance.h_reqStart(buffer, size); } void GlobalCom::h_cnfLine(const uint8_t *buffer, size_t size) { - m_instance->h_cnfLine(buffer, size); + m_instance.h_cnfLine(buffer, size); } void GlobalCom::h_reqInfo() { - m_instance->h_reqInfo(); + m_instance.h_reqInfo(); } void GlobalCom::h_reqTest() { - m_instance->h_reqTest(); + m_instance.h_reqTest(); } void GlobalCom::h_quitCmd() { - m_instance->h_quitCmd(); + m_instance.h_quitCmd(); } void GlobalCom::h_unrecognized() { - m_instance->h_unrecognized(); + m_instance.h_unrecognized(); } diff --git a/src/ayab/global_controller.cpp b/src/ayab/global_controller.cpp index 49297c5f8..a8c383609 100644 --- a/src/ayab/global_controller.cpp +++ b/src/ayab/global_controller.cpp @@ -25,49 +25,49 @@ // static member functions void GlobalController::init() { - m_instance->init(); + m_instance.init(); } void GlobalController::update() { - m_instance->update(); + m_instance.update(); } void GlobalController::cacheEncoders() { - m_instance->cacheEncoders(); + m_instance.cacheEncoders(); } -void GlobalController::setState(OpInterface* state) { - m_instance->setState(state); +void GlobalController::setState(OpInterface *state) { + m_instance.setState(state); } -OpInterface *GlobalController::getState() { - return m_instance->getState(); +OpInterface* GlobalController::getState() { + return m_instance.getState(); } void GlobalController::setMachineType(Machine_t machineType) { - m_instance->setMachineType(machineType); + m_instance.setMachineType(machineType); } Machine_t GlobalController::getMachineType() { - return m_instance->getMachineType(); + return m_instance.getMachineType(); } BeltShift_t GlobalController::getBeltShift() { - return m_instance->getBeltShift(); + return m_instance.getBeltShift(); } Carriage_t GlobalController::getCarriage() { - return m_instance->getCarriage(); + return m_instance.getCarriage(); } Direction_t GlobalController::getDirection() { - return m_instance->getDirection(); + return m_instance.getDirection(); } Direction_t GlobalController::getHallActive() { - return m_instance->getHallActive(); + return m_instance.getHallActive(); } uint8_t GlobalController::getPosition() { - return m_instance->getPosition(); + return m_instance.getPosition(); } diff --git a/src/ayab/global_encoders.cpp b/src/ayab/global_encoders.cpp index 189295629..054c82710 100644 --- a/src/ayab/global_encoders.cpp +++ b/src/ayab/global_encoders.cpp @@ -27,47 +27,47 @@ #include "opKnit.h" void GlobalEncoders::init(Machine_t machineType) { - m_instance->init(machineType); + m_instance.init(machineType); } void GlobalEncoders::setUpInterrupt() { - m_instance->setUpInterrupt(); + m_instance.setUpInterrupt(); } #ifndef AYAB_TESTS void GlobalEncoders::isr() { - m_instance->isr(); + m_instance.isr(); } #endif // AYAB_TESTS void GlobalEncoders::hallLeftCallback(uint16_t hallValue, void *data) { - m_instance->hallLeftCallback(hallValue, data); + m_instance.hallLeftCallback(hallValue, data); } void GlobalEncoders::hallRightCallback(uint16_t hallValue, void *data) { - m_instance->hallRightCallback(hallValue, data); + m_instance.hallRightCallback(hallValue, data); } uint16_t GlobalEncoders::getHallValue(Direction_t pSensor) { - return m_instance->getHallValue(pSensor); + return m_instance.getHallValue(pSensor); } BeltShift_t GlobalEncoders::getBeltShift() { - return m_instance->getBeltShift(); + return m_instance.getBeltShift(); } Carriage_t GlobalEncoders::getCarriage() { - return m_instance->getCarriage(); + return m_instance.getCarriage(); } Direction_t GlobalEncoders::getDirection() { - return m_instance->getDirection(); + return m_instance.getDirection(); } Direction_t GlobalEncoders::getHallActive() { - return m_instance->getHallActive(); + return m_instance.getHallActive(); } uint8_t GlobalEncoders::getPosition() { - return m_instance->getPosition(); + return m_instance.getPosition(); } diff --git a/src/ayab/global_packetSerialWrapper.cpp b/src/ayab/global_packetSerialWrapper.cpp index 84719a606..c1fdc6793 100644 --- a/src/ayab/global_packetSerialWrapper.cpp +++ b/src/ayab/global_packetSerialWrapper.cpp @@ -26,17 +26,17 @@ // static member functions void GlobalPacketSerialWrapper::begin(uint32_t speed) { - m_instance->begin(speed); + m_instance.begin(speed); } void GlobalPacketSerialWrapper::send(const uint8_t *buffer, size_t size) { - m_instance->send(buffer, size); + m_instance.send(buffer, size); } void GlobalPacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) { - m_instance->setPacketHandler(onPacketFunction); + m_instance.setPacketHandler(onPacketFunction); } void GlobalPacketSerialWrapper::update() { - m_instance->update(); + m_instance.update(); } diff --git a/src/ayab/global_solenoids.cpp b/src/ayab/global_solenoids.cpp index 556561aa6..d6ff3d6a6 100644 --- a/src/ayab/global_solenoids.cpp +++ b/src/ayab/global_solenoids.cpp @@ -28,13 +28,13 @@ // static member functions void GlobalSolenoids::init() { - m_instance->init(); + m_instance.init(); } void GlobalSolenoids::setSolenoid(uint8_t solenoid, bool state) { - m_instance->setSolenoid(solenoid, state); + m_instance.setSolenoid(solenoid, state); } void GlobalSolenoids::setSolenoids(uint16_t state) { - m_instance->setSolenoids(state); + m_instance.setSolenoids(state); } diff --git a/src/ayab/main.cpp b/src/ayab/main.cpp index c69824739..24bbc4672 100644 --- a/src/ayab/main.cpp +++ b/src/ayab/main.cpp @@ -43,41 +43,41 @@ // Global definitions: references elsewhere must use `extern`. // Each of the following is a pointer to a singleton class // containing static methods. -constexpr GlobalAnalogReadAsyncWrapper *analogReadAsyncWrapper; +constexpr GlobalAnalogReadAsyncWrapper *analogReadasyncWrapper; constexpr GlobalPacketSerialWrapper *packetSerialWrapper; -constexpr GlobalBeeper *beeper; -constexpr GlobalCom *com; -constexpr GlobalController *controller; -constexpr GlobalEncoders *encoders; -constexpr GlobalSolenoids *solenoids; +constexpr GlobalBeeper *beeper; +constexpr GlobalCom *com; +constexpr GlobalController *controller; +constexpr GlobalEncoders *encoders; +constexpr GlobalSolenoids *solenoids; -constexpr GlobalOpIdle *opIdle; -constexpr GlobalOpInit *opInit; -constexpr GlobalOpReady *opReady; -constexpr GlobalOpKnit *opKnit; -constexpr GlobalOpTest *opTest; -constexpr GlobalOpError *opError; +constexpr GlobalOpIdle *opIdle; +constexpr GlobalOpInit *opInit; +constexpr GlobalOpReady *opReady; +constexpr GlobalOpKnit *opKnit; +constexpr GlobalOpTest *opTest; +constexpr GlobalOpError *opError; // Initialize static members. -// Each singleton class contains a pointer to a static instance -// that implements a public interface. When testing, a pointer +// Each singleton class contains a reference to a static instance +// that implements a public interface. When testing, a reference // to an instance of a mock class can be substituted. -AnalogReadAsyncWrapperInterface *GlobalAnalogReadAsyncWrapper::m_instance = new AnalogReadAsyncWrapper(); -PacketSerialWrapperInterface *GlobalPacketSerialWrapper::m_instance = new PacketSerialWrapper(); +AnalogReadAsyncWrapperInterface& GlobalAnalogReadAsyncWrapper::m_instance = *new AnalogReadAsyncWrapper(); +PacketSerialWrapperInterface& GlobalPacketSerialWrapper::m_instance = *new PacketSerialWrapper(); -BeeperInterface *GlobalBeeper::m_instance = new Beeper(); -ComInterface *GlobalCom::m_instance = new Com(); -EncodersInterface *GlobalEncoders::m_instance = new Encoders(); -ControllerInterface *GlobalController::m_instance = new Controller(); -SolenoidsInterface *GlobalSolenoids::m_instance = new Solenoids(); +BeeperInterface& GlobalBeeper::m_instance = *new Beeper(); +ComInterface& GlobalCom::m_instance = *new Com(); +EncodersInterface& GlobalEncoders::m_instance = *new Encoders(); +ControllerInterface& GlobalController::m_instance = *new Controller(); +SolenoidsInterface& GlobalSolenoids::m_instance = *new Solenoids(); -OpIdleInterface *GlobalOpIdle::m_instance = new OpIdle(); -OpInitInterface *GlobalOpInit::m_instance = new OpInit(); -OpReadyInterface *GlobalOpReady::m_instance = new OpReady(); -OpKnitInterface *GlobalOpKnit::m_instance = new OpKnit(); -OpTestInterface *GlobalOpTest::m_instance = new OpTest(); -OpErrorInterface *GlobalOpError::m_instance = new OpError(); +OpIdleInterface& GlobalOpIdle::m_instance = *new OpIdle(); +OpInitInterface& GlobalOpInit::m_instance = *new OpInit(); +OpReadyInterface& GlobalOpReady::m_instance = *new OpReady(); +OpKnitInterface& GlobalOpKnit::m_instance = *new OpKnit(); +OpTestInterface& GlobalOpTest::m_instance = *new OpTest(); +OpErrorInterface& GlobalOpError::m_instance = *new OpError(); /*! * Setup - do once before going to the main loop. diff --git a/src/ayab/opError.h b/src/ayab/opError.h index 919fc397c..891067989 100644 --- a/src/ayab/opError.h +++ b/src/ayab/opError.h @@ -35,7 +35,7 @@ class OpErrorInterface : public OpInterface { }; // Container class for the static methods that implement the hardware test -// commands. Dependency injection is enabled using a pointer to a global +// commands. Dependency injection is enabled using a reference to a global // instance of either `OpError` or `OpErrorMock`, both of which classes // implement the pure virtual methods of the `OpErrorInterface` class. @@ -45,8 +45,8 @@ class GlobalOpError final { GlobalOpError() = default; public: - // pointer to global instance whose methods are implemented - static OpErrorInterface *m_instance; + // reference to global instance whose methods are implemented + static OpErrorInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/opIdle.h b/src/ayab/opIdle.h index 0bc04d274..6c5cc9489 100644 --- a/src/ayab/opIdle.h +++ b/src/ayab/opIdle.h @@ -31,7 +31,7 @@ class OpIdleInterface : public OpInterface { }; // Container class for the static methods that implement the hardware test -// commands. Dependency injection is enabled using a pointer to a global +// commands. Dependency injection is enabled using a reference to a global // instance of either `OpIdle` or `OpIdleMock`, both of which classes // implement the pure virtual methods of the `OpIdleInterface` class. @@ -41,8 +41,8 @@ class GlobalOpIdle final { GlobalOpIdle() = default; public: - // pointer to global instance whose methods are implemented - static OpIdleInterface *m_instance; + // reference to global instance whose methods are implemented + static OpIdleInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/opInit.cpp b/src/ayab/opInit.cpp index a4c37ef81..a1be5e6cb 100644 --- a/src/ayab/opInit.cpp +++ b/src/ayab/opInit.cpp @@ -66,7 +66,7 @@ void OpInit::begin() { */ void OpInit::update() { if (isReady()) { - GlobalController::setState(GlobalOpReady::m_instance); + GlobalController::setState(&GlobalOpReady::m_instance); } } diff --git a/src/ayab/opInit.h b/src/ayab/opInit.h index 085c2c306..23c05b371 100644 --- a/src/ayab/opInit.h +++ b/src/ayab/opInit.h @@ -34,7 +34,7 @@ class OpInitInterface : public OpInterface { }; // Container class for the static methods that implement the hardware test -// commands. Dependency injection is enabled using a pointer to a global +// commands. Dependency injection is enabled using a reference to a global // instance of either `OpInit` or `OpInitMock`, both of which classes // implement the pure virtual methods of the `OpInitInterface` class. @@ -44,8 +44,8 @@ class GlobalOpInit final { GlobalOpInit() = default; public: - // pointer to global instance whose methods are implemented - static OpInitInterface *m_instance; + // reference to global instance whose methods are implemented + static OpInitInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/opKnit.cpp b/src/ayab/opKnit.cpp index 2412a6475..56b9eb80e 100644 --- a/src/ayab/opKnit.cpp +++ b/src/ayab/opKnit.cpp @@ -160,7 +160,7 @@ Err_t OpKnit::startKnitting(uint8_t startNeedle, m_lastLineFlag = false; // proceed to next state - GlobalController::setState(GlobalOpKnit::m_instance); + GlobalController::setState(&GlobalOpKnit::m_instance); GlobalBeeper::ready(); // success @@ -253,7 +253,7 @@ void OpKnit::knit() { reqLine(m_currentLineNumber); } else if (m_lastLineFlag) { // move to state `OpInit` - GlobalController::setState(GlobalOpInit::m_instance); + GlobalController::setState(&GlobalOpInit::m_instance); } } #endif // DBG_NOMACHINE diff --git a/src/ayab/opKnit.h b/src/ayab/opKnit.h index 04a080674..152fb6079 100644 --- a/src/ayab/opKnit.h +++ b/src/ayab/opKnit.h @@ -43,7 +43,7 @@ class OpKnitInterface : public OpInterface { }; // Singleton container class for static methods. -// Dependency injection is enabled using a pointer +// Dependency injection is enabled using a reference // to a global instance of either `Knitter` or `KnitterMock` // both of which classes implement the pure virtual methods // of the `KnitterInterface` class. @@ -54,8 +54,8 @@ class GlobalOpKnit final { GlobalOpKnit() = default; public: - // pointer to global instance whose methods are implemented - static OpKnitInterface *m_instance; + // reference to global instance whose methods are implemented + static OpKnitInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/opReady.h b/src/ayab/opReady.h index 32f9eab00..f5b07ad9e 100644 --- a/src/ayab/opReady.h +++ b/src/ayab/opReady.h @@ -31,7 +31,7 @@ class OpReadyInterface : public OpInterface { }; // Container class for the static methods that implement the hardware test -// commands. Dependency injection is enabled using a pointer to a global +// commands. Dependency injection is enabled using a reference to a global // instance of either `OpReady` or `OpReadyMock`, both of which classes // implement the pure virtual methods of the `OpReadyInterface` class. @@ -41,8 +41,8 @@ class GlobalOpReady final { GlobalOpReady() = default; public: - // pointer to global instance whose methods are implemented - static OpReadyInterface *m_instance; + // reference to global instance whose methods are implemented + static OpReadyInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/opTest.h b/src/ayab/opTest.h index f1b03aba1..856ca37e6 100644 --- a/src/ayab/opTest.h +++ b/src/ayab/opTest.h @@ -55,7 +55,7 @@ class OpTestInterface : public OpInterface { }; // Container class for the static methods that implement the hardware test -// commands. Dependency injection is enabled using a pointer to a global +// commands. Dependency injection is enabled using a reference to a global // instance of either `OpTest` or `OpTestMock`, both of which classes // implement the pure virtual methods of the `OpTestInterface` class. @@ -65,8 +65,8 @@ class GlobalOpTest final { GlobalOpTest() = default; public: - // pointer to global instance whose methods are implemented - static OpTestInterface *m_instance; + // reference to global instance whose methods are implemented + static OpTestInterface& m_instance; static OpState_t state(); static void init(); diff --git a/src/ayab/packetSerialWrapper.h b/src/ayab/packetSerialWrapper.h index 8896de7af..264920732 100644 --- a/src/ayab/packetSerialWrapper.h +++ b/src/ayab/packetSerialWrapper.h @@ -39,7 +39,7 @@ class PacketSerialWrapperInterface { }; // Container class for the static method packetSerial. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `PacketSerialWrapper` or `PacketSerialWrapperMock`, // both of which classes implement the // pure virtual methods of `PacketSerialWrapperInterface`. @@ -50,8 +50,8 @@ class GlobalPacketSerialWrapper final { GlobalPacketSerialWrapper() = default; public: - // pointer to global instance whose methods are implemented - static PacketSerialWrapperInterface *m_instance; + // reference to global instance whose methods are implemented + static PacketSerialWrapperInterface& m_instance; static void begin(uint32_t speed); static void send(const uint8_t *buffer, size_t size); diff --git a/src/ayab/solenoids.h b/src/ayab/solenoids.h index 793e5d63e..a2bbf5e76 100644 --- a/src/ayab/solenoids.h +++ b/src/ayab/solenoids.h @@ -49,7 +49,7 @@ class SolenoidsInterface { }; // Container class for the static methods that control the solenoids. -// Dependency injection is enabled using a pointer to a global instance of +// Dependency injection is enabled using a reference to a global instance of // either `Solenoids` or `SolenoidsMock`, both of which classes implement // the pure virtual methods of `SolenoidsInterface`. @@ -59,8 +59,8 @@ class GlobalSolenoids final { GlobalSolenoids() = default; public: - // pointer to global instance whose methods are implemented - static SolenoidsInterface *m_instance; + // reference to global instance whose methods are implemented + static SolenoidsInterface& m_instance; static void init(); static void setSolenoid(uint8_t solenoid, bool state); diff --git a/test/test_OpError.cpp b/test/test_OpError.cpp index ef269d247..a13db4ff3 100644 --- a/test/test_OpError.cpp +++ b/test/test_OpError.cpp @@ -34,10 +34,10 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern OpError *opError; +extern OpError& opError; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; class OpErrorTest : public ::testing::Test { protected: @@ -45,8 +45,8 @@ class OpErrorTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - controllerMock = controller; - opKnitMock = opKnit; + controllerMock = &controller; + opKnitMock = &opKnit; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -65,62 +65,62 @@ class OpErrorTest : public ::testing::Test { }; TEST_F(OpErrorTest, test_state) { - ASSERT_EQ(opError->state(), OpState_t::Error); + ASSERT_EQ(opError.state(), OpState_t::Error); } TEST_F(OpErrorTest, test_begin) { EXPECT_CALL(*arduinoMock, millis); - opError->begin(); + opError.begin(); } TEST_F(OpErrorTest, test_init) { // no calls expected - opError->init(); + opError.init(); } TEST_F(OpErrorTest, test_com) { // no calls expected const uint8_t *buffer = {}; - opError->com(buffer, 0); + opError.com(buffer, 0); } TEST_F(OpErrorTest, test_end) { EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, LOW)); EXPECT_CALL(*opKnitMock, init()); - opError->end(); + opError.end(); } TEST_F(OpErrorTest, test_update) { EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - opError->begin(); + opError.begin(); // too soon to flash EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(FLASH_DELAY - 1)); EXPECT_CALL(*arduinoMock, digitalWrite).Times(0); - opError->update(); + opError.update(); // flash first time EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(FLASH_DELAY)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH)); // send_indState - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opError)); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError)); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); - opError->update(); + opError.update(); // alternate flash EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(FLASH_DELAY * 2)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, LOW)); // send_indState - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opError)); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError)); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); - opError->update(); + opError.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); diff --git a/test/test_OpIdle.cpp b/test/test_OpIdle.cpp index 15ae26a95..74b8fd00d 100644 --- a/test/test_OpIdle.cpp +++ b/test/test_OpIdle.cpp @@ -35,10 +35,10 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern OpIdle *opIdle; +extern OpIdle& opIdle; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; class OpIdleTest : public ::testing::Test { protected: @@ -46,8 +46,8 @@ class OpIdleTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - controllerMock = controller; - opKnitMock = opKnit; + controllerMock = &controller; + opKnitMock = &opKnit; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -66,38 +66,38 @@ class OpIdleTest : public ::testing::Test { }; TEST_F(OpIdleTest, test_state) { - ASSERT_EQ(opIdle->state(), OpState_t::Idle); + ASSERT_EQ(opIdle.state(), OpState_t::Idle); } TEST_F(OpIdleTest, test_begin) { EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); - opIdle->begin(); + opIdle.begin(); } TEST_F(OpIdleTest, test_init) { // no calls expected - opIdle->init(); + opIdle.init(); } TEST_F(OpIdleTest, test_reqTest) { // no calls expected // can't enter state `OpTest` from state `OpIdle` const uint8_t buffer[] = {static_cast(API_t::reqTest)}; - opIdle->com(buffer, 1); + opIdle.com(buffer, 1); } TEST_F(OpIdleTest, test_unrecognized) { // no calls expected const uint8_t buffer[] = {0xFF}; - opIdle->com(buffer, 1); + opIdle.com(buffer, 1); } TEST_F(OpIdleTest, test_update) { // no calls expected - opIdle->update(); + opIdle.update(); } TEST_F(OpIdleTest, test_end) { // no calls expected - opIdle->end(); + opIdle.end(); } diff --git a/test/test_OpInit.cpp b/test/test_OpInit.cpp index a30cff699..e9cbe9088 100644 --- a/test/test_OpInit.cpp +++ b/test/test_OpInit.cpp @@ -36,12 +36,12 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern OpInit *opInit; -extern OpReady *opReady; -extern OpTest *opTest; +extern OpInit& opInit; +extern OpReady& opReady; +extern OpTest& opTest; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; class OpInitTest : public ::testing::Test { protected: @@ -49,8 +49,8 @@ class OpInitTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - controllerMock = controller; - opKnitMock = opKnit; + controllerMock = &controller; + opKnitMock = &opKnit; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -80,31 +80,31 @@ class OpInitTest : public ::testing::Test { EXPECT_CALL(*controllerMock, getDirection).WillRepeatedly(Return(dir)); EXPECT_CALL(*controllerMock, getHallActive).WillRepeatedly(Return(hall)); EXPECT_CALL(*controllerMock, getMachineType).WillRepeatedly(Return(Machine_t::Kh910)); - EXPECT_CALL(*controllerMock, getState).WillRepeatedly(Return(opInit)); + EXPECT_CALL(*controllerMock, getState).WillRepeatedly(Return(&opInit)); } void expect_ready(bool ready) { if (ready) { - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opInit)); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); } - ASSERT_EQ(opInit->isReady(), ready); + ASSERT_EQ(opInit.isReady(), ready); } }; TEST_F(OpInitTest, test_state) { - ASSERT_EQ(opInit->state(), OpState_t::Init); + ASSERT_EQ(opInit.state(), OpState_t::Init); } TEST_F(OpInitTest, test_init) { // no calls expected - opInit->init(); - ASSERT_EQ(opInit->m_lastHall, Direction_t::NoDirection); + opInit.init(); + ASSERT_EQ(opInit.m_lastHall, Direction_t::NoDirection); } TEST_F(OpInitTest, test_reqTest) { - EXPECT_CALL(*controllerMock, setState(opTest)); + EXPECT_CALL(*controllerMock, setState(&opTest)); const uint8_t buffer[] = {static_cast(API_t::reqTest)}; - opInit->com(buffer, 1); + opInit.com(buffer, 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); @@ -113,18 +113,18 @@ TEST_F(OpInitTest, test_reqTest) { TEST_F(OpInitTest, test_com_unrecognized) { // no calls expected const uint8_t buffer[] = {0xFF}; - opInit->com(buffer, 1); + opInit.com(buffer, 1); } TEST_F(OpInitTest, test_end) { // no calls expected - opInit->end(); + opInit.end(); } TEST_F(OpInitTest, test_begin910) { EXPECT_CALL(*controllerMock, getMachineType()); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); - opInit->begin(); + opInit.begin(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); @@ -134,8 +134,8 @@ TEST_F(OpInitTest, test_updateF) { // isReady() == false expect_update(get_position_past_right(Machine_t::Kh910), Direction_t::Left, Direction_t::Left); EXPECT_CALL(*controllerMock, getState).Times(0); - EXPECT_CALL(*controllerMock, setState(opReady)).Times(0); - opInit->update(); + EXPECT_CALL(*controllerMock, setState(&opReady)).Times(0); + opInit.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); @@ -144,9 +144,9 @@ TEST_F(OpInitTest, test_updateF) { TEST_F(OpInitTest, test_updateT) { // isReady() == true expect_update(get_position_past_left(Machine_t::Kh910), Direction_t::Right, Direction_t::Left); - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opInit)); - EXPECT_CALL(*controllerMock, setState(opReady)); - opInit->update(); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); + EXPECT_CALL(*controllerMock, setState(&opReady)); + opInit.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); diff --git a/test/test_OpKnit.cpp b/test/test_OpKnit.cpp index 3d79c88ae..cfa246d55 100644 --- a/test/test_OpKnit.cpp +++ b/test/test_OpKnit.cpp @@ -44,18 +44,18 @@ using ::testing::Mock; using ::testing::Return; using ::testing::TypedEq; -extern OpKnit *opKnit; -extern Controller *controller; +extern OpKnit& opKnit; +extern Controller& controller; -extern BeeperMock *beeper; -extern ComMock *com; -extern EncodersMock *encoders; -extern SolenoidsMock *solenoids; +extern BeeperMock& beeper; +extern ComMock& com; +extern EncodersMock& encoders; +extern SolenoidsMock& solenoids; -extern OpIdleMock *opIdle; -extern OpInitMock *opInit; -extern OpTestMock *opTest; -extern OpReadyMock *opReady; +extern OpIdleMock& opIdle; +extern OpInitMock& opInit; +extern OpTestMock& opTest; +extern OpReadyMock& opReady; class OpKnitTest : public ::testing::Test { protected: @@ -63,15 +63,15 @@ class OpKnitTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - beeperMock = beeper; - comMock = com; - encodersMock = encoders; - solenoidsMock = solenoids; + beeperMock = &beeper; + comMock = &com; + encodersMock = &encoders; + solenoidsMock = &solenoids; - opIdleMock = opIdle; - opInitMock = opInit; - opReadyMock = opReady; - opTestMock = opTest; + opIdleMock = &opIdle; + opInitMock = &opInit; + opReadyMock = &opReady; + opTestMock = &opTest; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instances would be local and such behaviour would @@ -87,10 +87,10 @@ class OpKnitTest : public ::testing::Test { Mock::AllowLeak(opTestMock); // start in state `OpIdle` - controller->init(); - opIdle->init(); - opInit->init(); - opKnit->init(); + controller.init(); + opIdle.init(); + opInit.init(); + opKnit.init(); expected_cacheISR(Direction_t::NoDirection, Direction_t::NoDirection); } @@ -139,12 +139,12 @@ class OpKnitTest : public ::testing::Test { void expected_cacheISR(uint16_t pos, Direction_t dir, Direction_t hall, BeltShift_t belt, Carriage_t carriage) { expect_cacheISR(pos, dir, hall, belt, carriage); - controller->cacheEncoders(); + controller.cacheEncoders(); } void expected_cacheISR(uint16_t pos, Direction_t dir, BeltShift_t belt, Carriage_t carriage) { expect_cacheISR(pos, dir, Direction_t::NoDirection, belt, carriage); - controller->cacheEncoders(); + controller.cacheEncoders(); } void expect_cacheISR(Direction_t dir, Direction_t hall) { @@ -153,12 +153,12 @@ class OpKnitTest : public ::testing::Test { void expected_cacheISR(uint8_t pos, Direction_t dir, Direction_t hall) { expect_cacheISR(pos, dir, hall, BeltShift::Regular, Carriage_t::Knit); - controller->cacheEncoders(); + controller.cacheEncoders(); } void expected_cacheISR(Direction_t dir, Direction_t hall) { expect_cacheISR(dir, hall); - controller->cacheEncoders(); + controller.cacheEncoders(); } void expect_cacheISR(uint16_t pos) { @@ -167,7 +167,7 @@ class OpKnitTest : public ::testing::Test { void expected_cacheISR(uint16_t pos) { expect_cacheISR(pos); - controller->cacheEncoders(); + controller.cacheEncoders(); } void expected_cacheISR() { @@ -184,25 +184,25 @@ class OpKnitTest : public ::testing::Test { void expected_init_machine(Machine_t m) { // starts in state `OpIdle` - ASSERT_EQ(controller->getState(), opIdle); + ASSERT_EQ(controller.getState(), &opIdle); - controller->setMachineType(m); - controller->setState(opInit); + controller.setMachineType(m); + controller.setState(&opInit); expected_update_idle(); // transition to state `OpInit` - ASSERT_EQ(controller->getState(), opInit); + ASSERT_EQ(controller.getState(), &opInit); } void expected_get_ready() { - controller->setState(opReady); + controller.setState(&opReady); expected_update_init(); } void get_to_ready(Machine_t m) { expected_init_machine(m); expected_get_ready(); - ASSERT_EQ(controller->getState(), opReady); + ASSERT_EQ(controller.getState(), &opReady); } void get_to_knit(Machine_t m) { @@ -210,19 +210,19 @@ class OpKnitTest : public ::testing::Test { uint8_t pattern[] = {1}; EXPECT_CALL(*beeperMock, ready); - ASSERT_EQ(opKnit->startKnitting(0, NUM_NEEDLES[static_cast(m)] - 1, pattern, false), Err_t::Success); - ASSERT_EQ(controller->getState(), opReady); + ASSERT_EQ(opKnit.startKnitting(0, NUM_NEEDLES[static_cast(m)] - 1, pattern, false), Err_t::Success); + ASSERT_EQ(controller.getState(), &opReady); EXPECT_CALL(*encodersMock, init); EXPECT_CALL(*encodersMock, setUpInterrupt); expected_update_ready(); // ends in state `OpKnit` - ASSERT_EQ(controller->getState(), opKnit); + ASSERT_EQ(controller.getState(), &opKnit); } void expected_update() { - controller->update(); + controller.update(); } void expected_update_knit(bool first) { @@ -233,14 +233,14 @@ class OpKnitTest : public ::testing::Test { expected_update(); return; } - ASSERT_EQ(controller->getState(), opKnit); + ASSERT_EQ(controller.getState(), &opKnit); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)); // green LED on expected_update(); } void expected_update_idle() { // starts in state `OpIdle` - ASSERT_EQ(controller->getState(), opIdle); + ASSERT_EQ(controller.getState(), &opIdle); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); expected_update(); @@ -248,7 +248,7 @@ class OpKnitTest : public ::testing::Test { void expected_update_init() { // starts in state `OpInit` - ASSERT_EQ(controller->getState(), opInit); + ASSERT_EQ(controller.getState(), &opInit); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); expected_update(); @@ -256,7 +256,7 @@ class OpKnitTest : public ::testing::Test { void expected_update_ready() { // starts in state `OpReady` - ASSERT_EQ(controller->getState(), opReady); + ASSERT_EQ(controller.getState(), &opReady); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); expected_update(); @@ -264,7 +264,7 @@ class OpKnitTest : public ::testing::Test { void expected_update_test() { // starts in state `OpTest` - ASSERT_EQ(controller->getState(), opTest); + ASSERT_EQ(controller.getState(), &opTest); //expect_indState(); EXPECT_CALL(*opTestMock, update); @@ -279,7 +279,7 @@ class OpKnitTest : public ::testing::Test { }; TEST_F(OpKnitTest, test_state) { - ASSERT_EQ(opKnit->state(), OpState_t::Knit); + ASSERT_EQ(opKnit.state(), OpState_t::Knit); } TEST_F(OpKnitTest, test_send) { @@ -294,28 +294,28 @@ TEST_F(OpKnitTest, test_send) { TEST_F(OpKnitTest, test_com) { const uint8_t cnf[] = {static_cast(API_t::cnfLine)}; EXPECT_CALL(*comMock, h_cnfLine); - opKnit->com(cnf, 1); + opKnit.com(cnf, 1); const uint8_t reqTest[] = {static_cast(API_t::reqTest)}; EXPECT_CALL(*comMock, h_reqTest); - opKnit->com(reqTest, 1); + opKnit.com(reqTest, 1); const uint8_t unrec[] = {0xFF}; EXPECT_CALL(*comMock, h_unrecognized); - opKnit->com(unrec, 1); + opKnit.com(unrec, 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(comMock)); } TEST_F(OpKnitTest, test_encodePosition) { - opKnit->m_sOldPosition = controller->getPosition(); + opKnit.m_sOldPosition = controller.getPosition(); EXPECT_CALL(*comMock, send_indState).Times(0); - opKnit->encodePosition(); + opKnit.encodePosition(); - opKnit->m_sOldPosition += 1; + opKnit.m_sOldPosition += 1; EXPECT_CALL(*comMock, send_indState).Times(1); - opKnit->encodePosition(); + opKnit.encodePosition(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(comMock)); @@ -338,12 +338,12 @@ TEST_F(OpKnitTest, test_init_machine) { TEST_F(OpKnitTest, test_startKnitting_NoMachine) { uint8_t pattern[] = {1}; - Machine_t m = controller->getMachineType(); + Machine_t m = controller.getMachineType(); ASSERT_EQ(m, Machine_t::NoMachine); - opKnit->begin(); + opKnit.begin(); ASSERT_TRUE( - opKnit->startKnitting(0, NUM_NEEDLES[static_cast(m)] - 1, pattern, false) != Err_t::Success); + opKnit.startKnitting(0, NUM_NEEDLES[static_cast(m)] - 1, pattern, false) != Err_t::Success); } TEST_F(OpKnitTest, test_startKnitting_Kh910) { @@ -373,14 +373,14 @@ TEST_F(OpKnitTest, test_startKnitting_failures) { get_to_ready(Machine_t::Kh910); // `m_stopNeedle` lower than `m_startNeedle` - ASSERT_TRUE(opKnit->startKnitting(1, 0, pattern, false) != Err_t::Success); + ASSERT_TRUE(opKnit.startKnitting(1, 0, pattern, false) != Err_t::Success); // `m_stopNeedle` out of range - ASSERT_TRUE(opKnit->startKnitting(0, NUM_NEEDLES[static_cast(Machine_t::Kh910)], pattern, + ASSERT_TRUE(opKnit.startKnitting(0, NUM_NEEDLES[static_cast(Machine_t::Kh910)], pattern, false) != Err_t::Success); // null pattern - ASSERT_TRUE(opKnit->startKnitting(0, NUM_NEEDLES[static_cast(Machine_t::Kh910)] - 1, nullptr, + ASSERT_TRUE(opKnit.startKnitting(0, NUM_NEEDLES[static_cast(Machine_t::Kh910)] - 1, nullptr, false) != Err_t::Success); // test expectations without destroying instance @@ -391,26 +391,26 @@ TEST_F(OpKnitTest, test_startKnitting_failures) { TEST_F(OpKnitTest, test_setNextLine) { // set `m_lineRequested` - ASSERT_EQ(opKnit->setNextLine(1), false); + ASSERT_EQ(opKnit.setNextLine(1), false); expected_update_knit(true); // outside of the active needles - expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit->getStartOffset(Direction_t::Left)); + expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit.getStartOffset(Direction_t::Left)); EXPECT_CALL(*solenoidsMock, setSolenoid).Times(1); expected_update_knit(false); // wrong line number EXPECT_CALL(*beeperMock, finishedLine).Times(0); expect_reqLine(); - ASSERT_EQ(opKnit->setNextLine(1), false); + ASSERT_EQ(opKnit.setNextLine(1), false); // correct line number EXPECT_CALL(*beeperMock, finishedLine).Times(1); - ASSERT_EQ(opKnit->setNextLine(0), true); + ASSERT_EQ(opKnit.setNextLine(0), true); // `m_lineRequested` has been set to `false` - ASSERT_EQ(opKnit->setNextLine(0), false); + ASSERT_EQ(opKnit.setNextLine(0), false); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(solenoidsMock)); @@ -429,7 +429,7 @@ TEST_F(OpKnitTest, test_knit_Kh910) { EXPECT_CALL(*beeperMock, ready); const uint8_t START_NEEDLE = NUM_NEEDLES[static_cast(Machine_t::Kh910)] - 2; const uint8_t STOP_NEEDLE = NUM_NEEDLES[static_cast(Machine_t::Kh910)] - 1; - opKnit->startKnitting(START_NEEDLE, STOP_NEEDLE, pattern, true); + opKnit.startKnitting(START_NEEDLE, STOP_NEEDLE, pattern, true); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); // green LED off expected_update(); @@ -473,7 +473,7 @@ TEST_F(OpKnitTest, test_knit_Kh270) { EXPECT_CALL(*beeperMock, ready); const uint8_t START_NEEDLE = NUM_NEEDLES[static_cast(Machine_t::Kh270)] - 2; const uint8_t STOP_NEEDLE = NUM_NEEDLES[static_cast(Machine_t::Kh270)] - 1; - opKnit->startKnitting(START_NEEDLE, STOP_NEEDLE, pattern, true); + opKnit.startKnitting(START_NEEDLE, STOP_NEEDLE, pattern, true); //EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); expected_update(); @@ -540,16 +540,16 @@ TEST_F(OpKnitTest, test_knit_lastLine) { // Run one knit inside the working needles. EXPECT_CALL(*solenoidsMock, setSolenoid); - expected_cacheISR(opKnit->getStartOffset(Direction_t::Left) + 20); + expected_cacheISR(opKnit.getStartOffset(Direction_t::Left) + 20); // `m_workedOnLine` is set to true expected_update_knit(false); // Position has changed since last call to operate function // `m_pixelToSet` is above `m_stopNeedle` + END_OF_LINE_OFFSET_R - expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit->getStartOffset(Direction_t::Left)); + expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit.getStartOffset(Direction_t::Left)); // `m_lastLineFlag` is `true` - opKnit->setLastLine(); + opKnit.setLastLine(); EXPECT_CALL(*solenoidsMock, setSolenoid); EXPECT_CALL(*beeperMock, endWork); @@ -569,19 +569,19 @@ TEST_F(OpKnitTest, test_knit_lastLine_and_no_req) { // Run one knit inside the working needles. EXPECT_CALL(*solenoidsMock, setSolenoid); - expected_cacheISR(opKnit->getStartOffset(Direction_t::Left) + 20); + expected_cacheISR(opKnit.getStartOffset(Direction_t::Left) + 20); // `m_workedOnLine` is set to true expected_update_knit(false); // Position has changed since last call to operate function // `m_pixelToSet` is above `m_stopNeedle` + END_OF_LINE_OFFSET_R - expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit->getStartOffset(Direction_t::Left)); + expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit.getStartOffset(Direction_t::Left)); // `m_lastLineFlag` is `true` - opKnit->setLastLine(); + opKnit.setLastLine(); // Note: probing private data and methods to get full branch coverage. - opKnit->m_lineRequested = false; + opKnit.m_lineRequested = false; // EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, 1)); EXPECT_CALL(*solenoidsMock, setSolenoid); @@ -617,17 +617,17 @@ TEST_F(OpKnitTest, test_knit_new_line) { // Run one knit inside the working needles. EXPECT_CALL(*solenoidsMock, setSolenoid); - expected_cacheISR(opKnit->getStartOffset(Direction_t::Left) + 20); + expected_cacheISR(opKnit.getStartOffset(Direction_t::Left) + 20); // `m_workedOnLine` is set to true expected_update_knit(false); // Position has changed since last call to operate function // `m_pixelToSet` is above `m_stopNeedle` + END_OF_LINE_OFFSET_R - expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit->getStartOffset(Direction_t::Left)); + expected_cacheISR(NUM_NEEDLES[static_cast(Machine_t::Kh910)] + END_OF_LINE_OFFSET_R[static_cast(Machine_t::Kh910)] + 1 + opKnit.getStartOffset(Direction_t::Left)); // set `m_lineRequested` to `false` EXPECT_CALL(*beeperMock, finishedLine); - opKnit->setNextLine(0); + opKnit.setNextLine(0); EXPECT_CALL(*solenoidsMock, setSolenoid); @@ -645,92 +645,92 @@ TEST_F(OpKnitTest, test_knit_new_line) { TEST_F(OpKnitTest, test_calculatePixelAndSolenoid) { // Initialize KH910 expected_init_machine(Machine_t::Kh910); - controller->setState(opKnit); + controller.setState(&opKnit); expected_update_init(); // No direction // Lace carriage, no direction, need to change position to enter test expected_cacheISR(100, Direction_t::NoDirection, BeltShift::Shifted, Carriage_t::Lace); - ASSERT_FALSE(opKnit->calculatePixelAndSolenoid()); - // opKnit->pixelToSet not set - // opKnit->m_solenoidToSet not set + ASSERT_FALSE(opKnit.calculatePixelAndSolenoid()); + // opKnit.pixelToSet not set + // opKnit.m_solenoidToSet not set // Right direction // Lace carriage, no belt on Right, have not reached offset expected_cacheISR(39, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace); - ASSERT_FALSE(opKnit->calculatePixelAndSolenoid()); - // opKnit->pixelToSet not set - // opKnit->m_solenoidToSet not set + ASSERT_FALSE(opKnit.calculatePixelAndSolenoid()); + // opKnit.pixelToSet not set + // opKnit.m_solenoidToSet not set // Lace carriage, no belt on Right, need to change position to enter test expected_cacheISR(100, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8); - // opKnit->m_solenoidToSet not set + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, (100 - 40) + 8); + // opKnit.m_solenoidToSet not set // Lace carriage, regular belt on Right expected_cacheISR(100, Direction_t::Right, BeltShift::Regular, Carriage_t::Lace); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8); - ASSERT_EQ(opKnit->m_solenoidToSet, 100 % 16); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, (100 - 40) + 8); + ASSERT_EQ(opKnit.m_solenoidToSet, 100 % 16); // Lace carriage, shifted belt on Right expected_cacheISR(100, Direction_t::Right, BeltShift::Shifted, Carriage_t::Lace); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8); - ASSERT_EQ(opKnit->m_solenoidToSet, (100 - 8) % 16); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, (100 - 40) + 8); + ASSERT_EQ(opKnit.m_solenoidToSet, (100 - 8) % 16); // Left direction // Lace carriage, no belt on Left, off of Right end, position is changed expected_cacheISR(END_RIGHT[static_cast(Machine_t::Kh910)] - 15, Direction_t::Left, BeltShift::Unknown, Carriage_t::Lace); - ASSERT_FALSE(opKnit->calculatePixelAndSolenoid()); - // opKnit->pixelToSet not set - // opKnit->m_solenoidToSet not set + ASSERT_FALSE(opKnit.calculatePixelAndSolenoid()); + // opKnit.pixelToSet not set + // opKnit.m_solenoidToSet not set // Lace carriage, no belt on Left expected_cacheISR(100, Direction_t::Left, BeltShift::Unknown, Carriage_t::Lace); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, 100 - 16 - 16); - // opKnit->m_solenoidToSet not set + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, 100 - 16 - 16); + // opKnit.m_solenoidToSet not set // Lace carriage, regular belt on Left expected_cacheISR(100, Direction_t::Left, BeltShift::Regular, Carriage_t::Lace); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, 100 - 16 - 16); - ASSERT_EQ(opKnit->m_solenoidToSet, (100 + 8) % 16); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, 100 - 16 - 16); + ASSERT_EQ(opKnit.m_solenoidToSet, (100 + 8) % 16); // Garter Carriage, no belt on Left, need to change position to enter test expected_cacheISR(100, Direction_t::Left, BeltShift::Unknown, Carriage_t::Garter); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56); - // opKnit->m_solenoidToSet not set + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, 100 - 56); + // opKnit.m_solenoidToSet not set // Garter carriage, regular belt on Left, need to change position to enter test expected_cacheISR(100, Direction_t::Left, BeltShift::Regular, Carriage_t::Garter); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56); - ASSERT_EQ(opKnit->m_solenoidToSet, (100 + 8) % 16); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, 100 - 56); + ASSERT_EQ(opKnit.m_solenoidToSet, (100 + 8) % 16); // Garter carriage, shifted belt on Left, need to change position to enter test expected_cacheISR(100, Direction_t::Left, BeltShift::Shifted, Carriage_t::Garter); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56); - ASSERT_EQ(opKnit->m_solenoidToSet, 100 % 16); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, 100 - 56); + ASSERT_EQ(opKnit.m_solenoidToSet, 100 % 16); // KH270 - controller->setMachineType(Machine_t::Kh270); + controller.setMachineType(Machine_t::Kh270); // K carriage, no belt on Left expected_cacheISR(0, Direction_t::Left, BeltShift::Unknown, Carriage_t::Knit); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, static_cast(0 - 16)); - ASSERT_EQ(opKnit->m_solenoidToSet, static_cast(0 + 6) % 12 + 3); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, static_cast(0 - 16)); + ASSERT_EQ(opKnit.m_solenoidToSet, static_cast(0 + 6) % 12 + 3); // K carriage, no belt on Right expected_cacheISR(END_RIGHT[static_cast(Machine_t::Kh270)], Direction_t::Right, BeltShift::Unknown, Carriage_t::Knit); - ASSERT_TRUE(opKnit->calculatePixelAndSolenoid()); - ASSERT_EQ(opKnit->m_pixelToSet, (140 - 28)); - ASSERT_EQ(opKnit->m_solenoidToSet, 140 % 12 + 3); + ASSERT_TRUE(opKnit.calculatePixelAndSolenoid()); + ASSERT_EQ(opKnit.m_pixelToSet, (140 - 28)); + ASSERT_EQ(opKnit.m_solenoidToSet, 140 % 12 + 3); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(solenoidsMock)); @@ -740,15 +740,15 @@ TEST_F(OpKnitTest, test_calculatePixelAndSolenoid) { TEST_F(OpKnitTest, test_getStartOffset) { // out of range values - controller->m_carriage = Carriage_t::Knit; - ASSERT_EQ(opKnit->getStartOffset(Direction_t::NoDirection), 0); + controller.m_carriage = Carriage_t::Knit; + ASSERT_EQ(opKnit.getStartOffset(Direction_t::NoDirection), 0); - controller->m_carriage = Carriage_t::NoCarriage; - ASSERT_EQ(opKnit->getStartOffset(Direction_t::Left), 0); - ASSERT_EQ(opKnit->getStartOffset(Direction_t::Right), 0); + controller.m_carriage = Carriage_t::NoCarriage; + ASSERT_EQ(opKnit.getStartOffset(Direction_t::Left), 0); + ASSERT_EQ(opKnit.getStartOffset(Direction_t::Right), 0); - controller->m_carriage = Carriage_t::Lace; - controller->m_machineType = Machine_t::NoMachine; - ASSERT_EQ(opKnit->getStartOffset(Direction_t::Left), 0); - ASSERT_EQ(opKnit->getStartOffset(Direction_t::Right), 0); + controller.m_carriage = Carriage_t::Lace; + controller.m_machineType = Machine_t::NoMachine; + ASSERT_EQ(opKnit.getStartOffset(Direction_t::Left), 0); + ASSERT_EQ(opKnit.getStartOffset(Direction_t::Right), 0); } diff --git a/test/test_OpReady.cpp b/test/test_OpReady.cpp index 33f75376c..c41ddb003 100644 --- a/test/test_OpReady.cpp +++ b/test/test_OpReady.cpp @@ -37,11 +37,11 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern OpReady *opReady; -extern OpTest *opTest; +extern OpReady& opReady; +extern OpTest& opTest; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; class OpReadyTest : public ::testing::Test { protected: @@ -49,8 +49,8 @@ class OpReadyTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - controllerMock = controller; - opKnitMock = opKnit; + controllerMock = &controller; + opKnitMock = &opKnit; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -69,43 +69,43 @@ class OpReadyTest : public ::testing::Test { }; TEST_F(OpReadyTest, test_state) { - ASSERT_EQ(opReady->state(), OpState_t::Ready); + ASSERT_EQ(opReady.state(), OpState_t::Ready); } TEST_F(OpReadyTest, test_begin) { EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); - opReady->begin(); + opReady.begin(); } TEST_F(OpReadyTest, test_init) { // no calls expected - opReady->init(); + opReady.init(); } TEST_F(OpReadyTest, test_reqStart) { EXPECT_CALL(*opKnitMock, startKnitting); const uint8_t buffer[] = {static_cast(API_t::reqStart), 0, 10, 1, 0x36}; - opReady->com(buffer, 5); + opReady.com(buffer, 5); } TEST_F(OpReadyTest, test_reqTest) { - EXPECT_CALL(*controllerMock, setState(opTest)); + EXPECT_CALL(*controllerMock, setState(&opTest)); const uint8_t buffer[] = {static_cast(API_t::reqTest)}; - opReady->com(buffer, 1); + opReady.com(buffer, 1); } TEST_F(OpReadyTest, test_unrecognized) { // no calls expected const uint8_t buffer[] = {0xFF}; - opReady->com(buffer, 1); + opReady.com(buffer, 1); } TEST_F(OpReadyTest, test_update) { // no calls expected - opReady->update(); + opReady.update(); } TEST_F(OpReadyTest, test_end) { // no calls expected - opReady->end(); + opReady.end(); } diff --git a/test/test_OpTest.cpp b/test/test_OpTest.cpp index 7567b2a84..a83cb9027 100644 --- a/test/test_OpTest.cpp +++ b/test/test_OpTest.cpp @@ -39,15 +39,15 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern Beeper *beeper; +extern Beeper& beeper; -extern OpInit *opInit; -extern OpReady *opReady; -extern OpTest *opTest; +extern OpInit& opInit; +extern OpReady& opReady; +extern OpTest& opTest; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; -extern PacketSerialWrapperMock *packetSerialWrapper; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; +extern PacketSerialWrapperMock& packetSerialWrapper; class OpTestTest : public ::testing::Test { protected: @@ -55,9 +55,9 @@ class OpTestTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - controllerMock = controller; - opKnitMock = opKnit; - packetSerialWrapperMock = packetSerialWrapper; + controllerMock = &controller; + opKnitMock = &opKnit; + packetSerialWrapperMock = &packetSerialWrapper; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -66,7 +66,7 @@ class OpTestTest : public ::testing::Test { Mock::AllowLeak(opKnitMock); Mock::AllowLeak(packetSerialWrapperMock); - beeper->init(true); + beeper.init(true); } void TearDown() override { @@ -89,7 +89,7 @@ class OpTestTest : public ::testing::Test { void expect_startTest(uint32_t t) { expect_send(false); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(t)); - opTest->begin(); + opTest.begin(); } void expect_readEOLsensors(bool flag) { @@ -107,32 +107,32 @@ class OpTestTest : public ::testing::Test { }; TEST_F(OpTestTest, test_state) { - ASSERT_EQ(opTest->state(), OpState_t::Test); + ASSERT_EQ(opTest.state(), OpState_t::Test); } TEST_F(OpTestTest, test_init) { // no calls expected - opTest->init(); + opTest.init(); } TEST_F(OpTestTest, test_enabled) { const uint8_t stopCmd[] = {static_cast(API_t::stopCmd)}; const uint8_t autoReadCmd[] = {static_cast(API_t::autoReadCmd)}; const uint8_t autoTestCmd[] = {static_cast(API_t::autoTestCmd)}; - opTest->com(stopCmd, 1); - ASSERT_EQ(opTest->enabled(), false); - opTest->com(autoReadCmd, 1); - ASSERT_EQ(opTest->enabled(), true); - opTest->com(autoTestCmd, 1); - ASSERT_EQ(opTest->enabled(), true); - opTest->com(stopCmd, 1); - opTest->com(autoTestCmd, 1); - ASSERT_EQ(opTest->enabled(), true); + opTest.com(stopCmd, 1); + ASSERT_EQ(opTest.enabled(), false); + opTest.com(autoReadCmd, 1); + ASSERT_EQ(opTest.enabled(), true); + opTest.com(autoTestCmd, 1); + ASSERT_EQ(opTest.enabled(), true); + opTest.com(stopCmd, 1); + opTest.com(autoTestCmd, 1); + ASSERT_EQ(opTest.enabled(), true); } TEST_F(OpTestTest, test_helpCmd) { expect_send(false); - opTest->helpCmd(); + opTest.helpCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -140,7 +140,7 @@ TEST_F(OpTestTest, test_helpCmd) { TEST_F(OpTestTest, test_sendCmd) { expect_send(false); - opTest->sendCmd(); + opTest.sendCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -148,12 +148,12 @@ TEST_F(OpTestTest, test_sendCmd) { TEST_F(OpTestTest, test_beepCmd) { expect_send(true); - opTest->beepCmd(); + opTest.beepCmd(); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - beeper->update(); + beeper.update(); EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_ON_DUTY)); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(1U)); - beeper->update(); + beeper.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -162,7 +162,7 @@ TEST_F(OpTestTest, test_beepCmd) { TEST_F(OpTestTest, test_setSingleCmd_fail1) { const uint8_t buf[] = {static_cast(API_t::setSingleCmd), 0}; expect_send(false); - opTest->setSingleCmd(buf, 2); + opTest.setSingleCmd(buf, 2); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -171,7 +171,7 @@ TEST_F(OpTestTest, test_setSingleCmd_fail1) { TEST_F(OpTestTest, test_setSingleCmd_fail2) { const uint8_t buf[] = {static_cast(API_t::setSingleCmd), 16, 0}; expect_send(false); - opTest->setSingleCmd(buf, 3); + opTest.setSingleCmd(buf, 3); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -180,7 +180,7 @@ TEST_F(OpTestTest, test_setSingleCmd_fail2) { TEST_F(OpTestTest, test_setSingleCmd_fail3) { const uint8_t buf[] = {static_cast(API_t::setSingleCmd), 15, 2}; expect_send(false); - opTest->setSingleCmd(buf, 3); + opTest.setSingleCmd(buf, 3); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -189,7 +189,7 @@ TEST_F(OpTestTest, test_setSingleCmd_fail3) { TEST_F(OpTestTest, test_setSingleCmd_success) { const uint8_t buf[] = {static_cast(API_t::setSingleCmd), 15, 1}; expect_send(true); - opTest->setSingleCmd(buf, 3); + opTest.setSingleCmd(buf, 3); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -198,7 +198,7 @@ TEST_F(OpTestTest, test_setSingleCmd_success) { TEST_F(OpTestTest, test_setAllCmd_fail1) { const uint8_t buf[] = {static_cast(API_t::setAllCmd), 0}; expect_send(false); - opTest->setAllCmd(buf, 2); + opTest.setAllCmd(buf, 2); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -207,7 +207,7 @@ TEST_F(OpTestTest, test_setAllCmd_fail1) { TEST_F(OpTestTest, test_setAllCmd_success) { const uint8_t buf[] = {static_cast(API_t::setAllCmd), 0xFF, 0xFF}; expect_send(true); - opTest->setAllCmd(buf, 3); + opTest.setAllCmd(buf, 3); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -216,7 +216,7 @@ TEST_F(OpTestTest, test_setAllCmd_success) { TEST_F(OpTestTest, test_readEOLsensorsCmd) { expect_send(false); expect_readEOLsensors(true); - opTest->readEOLsensorsCmd(); + opTest.readEOLsensorsCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -225,7 +225,7 @@ TEST_F(OpTestTest, test_readEOLsensorsCmd) { TEST_F(OpTestTest, test_readEncodersCmd_low) { expect_send(false); EXPECT_CALL(*arduinoMock, digitalRead).WillRepeatedly(Return(LOW)); - opTest->readEncodersCmd(); + opTest.readEncodersCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -234,7 +234,7 @@ TEST_F(OpTestTest, test_readEncodersCmd_low) { TEST_F(OpTestTest, test_readEncodersCmd_high) { expect_send(false); EXPECT_CALL(*arduinoMock, digitalRead).WillRepeatedly(Return(HIGH)); - opTest->readEncodersCmd(); + opTest.readEncodersCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -243,7 +243,7 @@ TEST_F(OpTestTest, test_readEncodersCmd_high) { TEST_F(OpTestTest, test_autoReadCmd) { const uint8_t buf[] = {static_cast(API_t::autoReadCmd)}; expect_send(true); - opTest->com(buf, 1); + opTest.com(buf, 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -252,7 +252,7 @@ TEST_F(OpTestTest, test_autoReadCmd) { TEST_F(OpTestTest, test_autoTestCmd) { const uint8_t buf[] = {static_cast(API_t::autoTestCmd)}; expect_send(true); - opTest->com(buf, 1); + opTest.com(buf, 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -260,8 +260,8 @@ TEST_F(OpTestTest, test_autoTestCmd) { TEST_F(OpTestTest, test_quitCmd) { const uint8_t buf[] = {static_cast(API_t::quitCmd)}; - EXPECT_CALL(*controllerMock, setState(opInit)); - opTest->com(buf, 1); + EXPECT_CALL(*controllerMock, setState(&opInit)); + opTest.com(buf, 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); @@ -269,7 +269,7 @@ TEST_F(OpTestTest, test_quitCmd) { TEST_F(OpTestTest, test_end) { EXPECT_CALL(*opKnitMock, init); - opTest->end(); + opTest.end(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -278,26 +278,26 @@ TEST_F(OpTestTest, test_end) { TEST_F(OpTestTest, test_loop_null) { expect_startTest(0U); EXPECT_CALL(*arduinoMock, millis).Times(0); - opTest->update(); + opTest.update(); } TEST_F(OpTestTest, test_autoRead) { expect_startTest(0U); - opTest->autoReadCmd(); + opTest.autoReadCmd(); // nothing has happened yet EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(TEST_LOOP_DELAY - 1)); EXPECT_CALL(*opKnitMock, encodePosition); expect_readEOLsensors(false); expect_readEncoders(false); - opTest->update(); + opTest.update(); // m_timerEventOdd = false EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(TEST_LOOP_DELAY)); EXPECT_CALL(*opKnitMock, encodePosition); expect_readEOLsensors(false); expect_readEncoders(false); - opTest->update(); + opTest.update(); // m_timerEventOdd = true EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(2 * TEST_LOOP_DELAY)); @@ -305,15 +305,15 @@ TEST_F(OpTestTest, test_autoRead) { expect_send(false); expect_readEOLsensors(true); expect_readEncoders(true); - opTest->update(); + opTest.update(); // after `stopCmd()` - opTest->stopCmd(); + opTest.stopCmd(); EXPECT_CALL(*arduinoMock, millis).Times(0); EXPECT_CALL(*opKnitMock, encodePosition).Times(0); expect_readEOLsensors(false); expect_readEncoders(false); - opTest->update(); + opTest.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -322,14 +322,14 @@ TEST_F(OpTestTest, test_autoRead) { TEST_F(OpTestTest, test_autoTest) { expect_startTest(0U); - opTest->autoTestCmd(); + opTest.autoTestCmd(); // nothing has happened yet EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(TEST_LOOP_DELAY - 1)); EXPECT_CALL(*opKnitMock, encodePosition); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)).Times(0); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH)).Times(0); - opTest->update(); + opTest.update(); // m_timerEventOdd = false EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(TEST_LOOP_DELAY)); @@ -337,7 +337,7 @@ TEST_F(OpTestTest, test_autoTest) { expect_send(true); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH)); - opTest->update(); + opTest.update(); // m_timerEventOdd = true EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(2 * TEST_LOOP_DELAY)); @@ -345,15 +345,15 @@ TEST_F(OpTestTest, test_autoTest) { expect_send(true); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, LOW)); - opTest->update(); + opTest.update(); // after `stopCmd()` - opTest->stopCmd(); + opTest.stopCmd(); EXPECT_CALL(*arduinoMock, millis).Times(0); EXPECT_CALL(*opKnitMock, encodePosition).Times(0); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, _)).Times(0); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, _)).Times(0); - opTest->update(); + opTest.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -367,5 +367,5 @@ TEST_F(OpTestTest, test_startTest_success) { TEST_F(OpTestTest, test_unrecognized) { // no calls expected const uint8_t buffer[] = {0xFF}; - opTest->com(buffer, 1); + opTest.com(buffer, 1); } diff --git a/test/test_all.cpp b/test/test_all.cpp index 57d3b3995..8c0673b3d 100644 --- a/test/test_all.cpp +++ b/test/test_all.cpp @@ -40,42 +40,42 @@ #include #include -// global definitions -// references everywhere else must use `extern` -AnalogReadAsyncWrapper *analogReadAsyncWrapper = new AnalogReadAsyncWrapper(); -PacketSerialWrapper *packetSerialWrapper = new PacketSerialWrapper(); +// Global definitions +// References everywhere else must use `extern` +BeeperMock& beeper = *new BeeperMock(); +ComMock& com = *new ComMock(); +EncodersMock& encoders = *new EncodersMock(); +SolenoidsMock& solenoids = *new SolenoidsMock(); -Controller *controller = new Controller(); -OpKnit *opKnit = new OpKnit(); +OpIdleMock& opIdle = *new OpIdleMock(); +OpInitMock& opInit = *new OpInitMock(); +OpReadyMock& opReady = *new OpReadyMock(); +OpTestMock& opTest = *new OpTestMock(); +OpErrorMock& opError = *new OpErrorMock(); -BeeperMock *beeper = new BeeperMock(); -ComMock *com = new ComMock(); -EncodersMock *encoders = new EncodersMock(); -SolenoidsMock *solenoids = new SolenoidsMock(); +Controller& controller = *new Controller(); +OpKnit& opKnit = *new OpKnit(); -OpIdleMock *opIdle = new OpIdleMock(); -OpInitMock *opInit = new OpInitMock(); -OpReadyMock *opReady = new OpReadyMock(); -OpTestMock *opTest = new OpTestMock(); -OpErrorMock *opError = new OpErrorMock(); +AnalogReadAsyncWrapper& analogReadAsyncWrapper = *new AnalogReadAsyncWrapper(); +PacketSerialWrapper& packetSerialWrapper = *new PacketSerialWrapper(); -// instantiate singleton classes with mock objects -AnalogReadAsyncWrapperInterface *GlobalAnalogReadAsyncWrapper::m_instance = analogReadAsyncWrapper; -PacketSerialWrapperInterface *GlobalPacketSerialWrapper::m_instance = packetSerialWrapper; +// Instantiate singleton classes with mock objects +BeeperInterface& GlobalBeeper::m_instance = beeper; +ComInterface& GlobalCom::m_instance = com; +EncodersInterface& GlobalEncoders::m_instance = encoders; +SolenoidsInterface& GlobalSolenoids::m_instance = solenoids; -ControllerInterface *GlobalController::m_instance = controller; -OpKnitInterface *GlobalOpKnit::m_instance = opKnit; +OpIdleInterface& GlobalOpIdle::m_instance = opIdle; +OpInitInterface& GlobalOpInit::m_instance = opInit; +OpReadyInterface& GlobalOpReady::m_instance = opReady; +OpTestInterface& GlobalOpTest::m_instance = opTest; +OpErrorInterface& GlobalOpError::m_instance = opError; -BeeperInterface *GlobalBeeper::m_instance = beeper; -ComInterface *GlobalCom::m_instance = com; -EncodersInterface *GlobalEncoders::m_instance = encoders; -SolenoidsInterface *GlobalSolenoids::m_instance = solenoids; +ControllerInterface& GlobalController::m_instance = controller; +OpKnitInterface& GlobalOpKnit::m_instance = opKnit; -OpIdleInterface *GlobalOpIdle::m_instance = opIdle; -OpInitInterface *GlobalOpInit::m_instance = opInit; -OpReadyInterface *GlobalOpReady::m_instance = opReady; -OpTestInterface *GlobalOpTest::m_instance = opTest; -OpErrorInterface *GlobalOpError::m_instance = opError; +AnalogReadAsyncWrapperInterface& GlobalAnalogReadAsyncWrapper::m_instance = analogReadAsyncWrapper; +PacketSerialWrapperInterface& GlobalPacketSerialWrapper::m_instance = packetSerialWrapper; int main(int argc, char *argv[]) { ::testing::InitGoogleMock(&argc, argv); diff --git a/test/test_beeper.cpp b/test/test_beeper.cpp index 5e990bcba..90904bbdd 100644 --- a/test/test_beeper.cpp +++ b/test/test_beeper.cpp @@ -28,7 +28,7 @@ using ::testing::Return; -extern Beeper *beeper; +extern Beeper& beeper; class BeeperTest : public ::testing::Test { protected: @@ -44,72 +44,72 @@ class BeeperTest : public ::testing::Test { void expectedBeepSchedule(unsigned long t) { EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(t)); - beeper->update(); + beeper.update(); } void expectedBeepRepeats(uint8_t repeats) { - if (beeper->enabled()) { - ASSERT_EQ(beeper->getState(), BeepState::Wait); + if (beeper.enabled()) { + ASSERT_EQ(beeper.getState(), BeepState::Wait); for (uint8_t i = 0; i < repeats; i++) { expectedBeepSchedule(BEEP_DELAY * 2 * i); - ASSERT_EQ(beeper->getState(), BeepState::On); + ASSERT_EQ(beeper.getState(), BeepState::On); EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_ON_DUTY)); expectedBeepSchedule(BEEP_DELAY * 2 * i); - ASSERT_EQ(beeper->getState(), BeepState::Wait); + ASSERT_EQ(beeper.getState(), BeepState::Wait); expectedBeepSchedule(BEEP_DELAY * (2 * i + 1) - 1); - ASSERT_EQ(beeper->getState(), BeepState::Wait); + ASSERT_EQ(beeper.getState(), BeepState::Wait); expectedBeepSchedule(BEEP_DELAY * (2 * i + 1)); - ASSERT_EQ(beeper->getState(), BeepState::Off); + ASSERT_EQ(beeper.getState(), BeepState::Off); EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_OFF_DUTY)); expectedBeepSchedule(BEEP_DELAY * (2 * i + 1)); - ASSERT_EQ(beeper->getState(), BeepState::Wait); + ASSERT_EQ(beeper.getState(), BeepState::Wait); } EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_NO_DUTY)); expectedBeepSchedule(BEEP_DELAY * (2 * repeats)); } - ASSERT_EQ(beeper->getState(), BeepState::Idle); + ASSERT_EQ(beeper.getState(), BeepState::Idle); expectedBeepSchedule(BEEP_DELAY * (2 * repeats) + 1); } }; TEST_F(BeeperTest, test_ready_enabled) { - beeper->init(true); + beeper.init(true); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - beeper->ready(); + beeper.ready(); expectedBeepRepeats(BEEP_NUM_READY); } TEST_F(BeeperTest, test_finishedLine_enabled) { - beeper->init(true); + beeper.init(true); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - beeper->finishedLine(); + beeper.finishedLine(); expectedBeepRepeats(BEEP_NUM_FINISHEDLINE); } TEST_F(BeeperTest, test_endWork_enabled) { - beeper->init(true); + beeper.init(true); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - beeper->endWork(); + beeper.endWork(); expectedBeepRepeats(BEEP_NUM_ENDWORK); } TEST_F(BeeperTest, test_ready_disabled) { - beeper->init(false); + beeper.init(false); EXPECT_CALL(*arduinoMock, millis).Times(0); - beeper->ready(); + beeper.ready(); expectedBeepRepeats(BEEP_NUM_READY); } TEST_F(BeeperTest, test_finishedLine_disabled) { - beeper->init(false); + beeper.init(false); EXPECT_CALL(*arduinoMock, millis).Times(0); - beeper->finishedLine(); + beeper.finishedLine(); expectedBeepRepeats(BEEP_NUM_FINISHEDLINE); } TEST_F(BeeperTest, test_endWork_disabled) { - beeper->init(false); + beeper.init(false); EXPECT_CALL(*arduinoMock, millis).Times(0); - beeper->endWork(); + beeper.endWork(); expectedBeepRepeats(BEEP_NUM_ENDWORK); } diff --git a/test/test_boards.cpp b/test/test_boards.cpp index 104333ed0..5042fc0cf 100644 --- a/test/test_boards.cpp +++ b/test/test_boards.cpp @@ -39,40 +39,42 @@ #include #include -// global definitions -// references everywhere else must use `extern` -Beeper *beeper = new Beeper(); -Com *com = new Com(); -Encoders *encoders = new Encoders(); -Solenoids *solenoids = new Solenoids(); +// Global definitions +// References everywhere else must use `extern` +Beeper& beeper = *new Beeper(); +Com& com = *new Com(); +Encoders& encoders = *new Encoders(); +Solenoids& solenoids = *new Solenoids(); -OpIdle *opIdle = new OpIdle(); -OpInit *opInit = new OpInit(); -OpReady *opReady = new OpReady(); -OpTest *opTest = new OpTest(); -OpError *opError = new OpError(); +OpIdle& opIdle = *new OpIdle(); +OpInit& opInit = *new OpInit(); +OpReady& opReady = *new OpReady(); +OpTest& opTest = *new OpTest(); +OpError& opError = *new OpError(); -AnalogReadAsyncWrapperMock *analogReadAsyncWrapper = new AnalogReadAsyncWrapperMock(); -PacketSerialWrapperMock *packetSerialWrapper = new PacketSerialWrapperMock(); -ControllerMock *controller = new ControllerMock(); -OpKnitMock *opKnit = new OpKnitMock(); +ControllerMock& controller = *new ControllerMock(); +OpKnitMock& opKnit = *new OpKnitMock(); -// initialize static members -BeeperInterface *GlobalBeeper::m_instance = beeper; -ComInterface *GlobalCom::m_instance = com; -EncodersInterface *GlobalEncoders::m_instance = encoders; -SolenoidsInterface *GlobalSolenoids::m_instance = solenoids; +AnalogReadAsyncWrapperMock& analogReadAsyncWrapper = *new AnalogReadAsyncWrapperMock(); +PacketSerialWrapperMock& packetSerialWrapper = *new PacketSerialWrapperMock(); -OpIdleInterface *GlobalOpIdle::m_instance = opIdle; -OpInitInterface *GlobalOpInit::m_instance = opInit; -OpReadyInterface *GlobalOpReady::m_instance = opReady; -OpTestInterface *GlobalOpTest::m_instance = opTest; -OpErrorInterface *GlobalOpError::m_instance = opError; +// Initialize static members +BeeperInterface& GlobalBeeper::m_instance = beeper; +ComInterface& GlobalCom::m_instance = com; +EncodersInterface& GlobalEncoders::m_instance = encoders; +SolenoidsInterface& GlobalSolenoids::m_instance = solenoids; -AnalogReadAsyncWrapperInterface *GlobalAnalogReadAsyncWrapper::m_instance = analogReadAsyncWrapper; -PacketSerialWrapperInterface *GlobalPacketSerialWrapper::m_instance = packetSerialWrapper; -ControllerInterface *GlobalController::m_instance = controller; -OpKnitInterface *GlobalOpKnit::m_instance = opKnit; +OpIdleInterface& GlobalOpIdle::m_instance = opIdle; +OpInitInterface& GlobalOpInit::m_instance = opInit; +OpReadyInterface& GlobalOpReady::m_instance = opReady; +OpTestInterface& GlobalOpTest::m_instance = opTest; +OpErrorInterface& GlobalOpError::m_instance = opError; + +ControllerInterface& GlobalController::m_instance = controller; +OpKnitInterface& GlobalOpKnit::m_instance = opKnit; + +AnalogReadAsyncWrapperInterface& GlobalAnalogReadAsyncWrapper::m_instance = analogReadAsyncWrapper; +PacketSerialWrapperInterface& GlobalPacketSerialWrapper::m_instance = packetSerialWrapper; int main(int argc, char *argv[]) { ::testing::InitGoogleMock(&argc, argv); diff --git a/test/test_com.cpp b/test/test_com.cpp index 943413046..02d409bf2 100644 --- a/test/test_com.cpp +++ b/test/test_com.cpp @@ -39,16 +39,16 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern Com *com; -extern Beeper *beeper; +extern Com& com; +extern Beeper& beeper; -extern OpIdle *opIdle; -extern OpInit *opInit; -extern OpTest *opTest; +extern OpIdle& opIdle; +extern OpInit& opInit; +extern OpTest& opTest; -extern ControllerMock *controller; -extern OpKnitMock *opKnit; -extern PacketSerialWrapperMock *packetSerialWrapper; +extern ControllerMock& controller; +extern OpKnitMock& opKnit; +extern PacketSerialWrapperMock& packetSerialWrapper; class ComTest : public ::testing::Test { protected: @@ -56,9 +56,9 @@ class ComTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointer to global instance - controllerMock = controller; - opKnitMock = opKnit; - packetSerialWrapperMock = packetSerialWrapper; + controllerMock = &controller; + opKnitMock = &opKnit; + packetSerialWrapperMock = &packetSerialWrapper; // The global instance does not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -67,9 +67,9 @@ class ComTest : public ::testing::Test { Mock::AllowLeak(opKnitMock); Mock::AllowLeak(packetSerialWrapperMock); - beeper->init(true); + beeper.init(true); expect_init(); - com->init(); + com.init(); controllerMock->init(); } @@ -97,23 +97,24 @@ class ComTest : public ::testing::Test { void expected_write_onPacketReceived(uint8_t *buffer, size_t size, bool once) { expect_send(once); - opTest->com(buffer, size); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opTest)); + com.onPacketReceived(buffer, size); } void reqInit(Machine_t machine) { uint8_t buffer[] = {static_cast(API_t::reqInit), static_cast(machine), 0}; - buffer[2] = com->CRC8(buffer, 2); - EXPECT_CALL(*controllerMock, setState(opInit)); + buffer[2] = com.CRC8(buffer, 2); + EXPECT_CALL(*controllerMock, setState(&opInit)); expect_send(true); - opIdle->com(buffer, sizeof(buffer)); + opIdle.com(buffer, sizeof(buffer)); } }; TEST_F(ComTest, test_reqInit_fail1) { uint8_t buffer[] = {static_cast(API_t::reqInit), static_cast(Machine_t::Kh930)}; - EXPECT_CALL(*controllerMock, setState(opInit)).Times(0); + EXPECT_CALL(*controllerMock, setState(&opInit)).Times(0); expect_send(true); - opIdle->com(buffer, sizeof(buffer)); + opIdle.com(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -121,10 +122,10 @@ TEST_F(ComTest, test_reqInit_fail1) { TEST_F(ComTest, test_reqInit_fail2) { uint8_t buffer[] = {static_cast(API_t::reqInit), static_cast(Machine_t::Kh930), 0}; - buffer[2] = com->CRC8(buffer, 2) ^ 1; - EXPECT_CALL(*controllerMock, setState(opInit)).Times(0); + buffer[2] = com.CRC8(buffer, 2) ^ 1; + EXPECT_CALL(*controllerMock, setState(&opInit)).Times(0); expect_send(true); - opIdle->com(buffer, sizeof(buffer)); + opIdle.com(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -132,10 +133,10 @@ TEST_F(ComTest, test_reqInit_fail2) { TEST_F(ComTest, test_reqInit_fail3) { uint8_t buffer[] = {static_cast(API_t::reqInit), static_cast(Machine_t::NoMachine), 0}; - buffer[2] = com->CRC8(buffer, 2); - EXPECT_CALL(*controllerMock, setState(opInit)).Times(0); + buffer[2] = com.CRC8(buffer, 2); + EXPECT_CALL(*controllerMock, setState(&opInit)).Times(0); expect_send(true); - opIdle->com(buffer, sizeof(buffer)); + opIdle.com(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -151,7 +152,7 @@ TEST_F(ComTest, test_API) { TEST_F(ComTest, test_reqtest_fail) { // no machineType uint8_t buffer[] = {static_cast(API_t::reqTest)}; - EXPECT_CALL(*controllerMock, setState(opTest)).Times(0); + EXPECT_CALL(*controllerMock, setState(&opTest)).Times(0); expected_write_onPacketReceived(buffer, sizeof(buffer), true); // test expectations without destroying instance @@ -160,9 +161,9 @@ TEST_F(ComTest, test_reqtest_fail) { */ TEST_F(ComTest, test_reqtest) { - EXPECT_CALL(*controllerMock, setState(opTest)); + EXPECT_CALL(*controllerMock, setState(&opTest)); expect_send(true); - com->h_reqTest(); + com.h_reqTest(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); @@ -174,7 +175,7 @@ TEST_F(ComTest, test_reqstart_fail1) { uint8_t buffer[] = {static_cast(API_t::reqStart), 0, 10, 1, 0x73}; EXPECT_CALL(*opKnitMock, startKnitting).Times(0); expect_send(true); - com->h_reqStart(buffer, sizeof(buffer)); + com.h_reqStart(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -186,7 +187,7 @@ TEST_F(ComTest, test_reqstart_fail2) { uint8_t buffer[] = {static_cast(API_t::reqStart), 0, 1, 0x74}; EXPECT_CALL(*opKnitMock, startKnitting).Times(0); expect_send(true); - com->h_reqStart(buffer, sizeof(buffer) - 1); + com.h_reqStart(buffer, sizeof(buffer) - 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -198,7 +199,7 @@ TEST_F(ComTest, test_reqstart_success_KH910) { uint8_t buffer[] = {static_cast(API_t::reqStart), 0, 10, 1, 0x36}; EXPECT_CALL(*opKnitMock, startKnitting); expect_send(true); - com->h_reqStart(buffer, sizeof(buffer)); + com.h_reqStart(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -210,7 +211,7 @@ TEST_F(ComTest, test_reqstart_success_KH270) { uint8_t buffer[] = {static_cast(API_t::reqStart), 0, 10, 1, 0x36}; EXPECT_CALL(*opKnitMock, startKnitting); expect_send(true); - com->h_reqStart(buffer, sizeof(buffer)); + com.h_reqStart(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -219,7 +220,7 @@ TEST_F(ComTest, test_reqstart_success_KH270) { TEST_F(ComTest, test_reqinfo) { expect_send(true); - com->h_reqInfo(); + com.h_reqInfo(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -231,6 +232,7 @@ TEST_F(ComTest, test_helpCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_sendCmd) { @@ -239,19 +241,21 @@ TEST_F(ComTest, test_sendCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_beepCmd) { uint8_t buffer[] = {static_cast(API_t::beepCmd)}; expected_write_onPacketReceived(buffer, sizeof(buffer), true); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(0U)); - beeper->update(); + beeper.update(); EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, BEEP_ON_DUTY)); EXPECT_CALL(*arduinoMock, millis).WillOnce(Return(1U)); - beeper->update(); + beeper.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_setSingleCmd) { @@ -260,6 +264,7 @@ TEST_F(ComTest, test_setSingleCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_setAllCmd) { @@ -268,6 +273,7 @@ TEST_F(ComTest, test_setAllCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_readEOLsensorsCmd) { @@ -278,6 +284,7 @@ TEST_F(ComTest, test_readEOLsensorsCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_readEncodersCmd) { @@ -289,6 +296,7 @@ TEST_F(ComTest, test_readEncodersCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_autoReadCmd) { @@ -297,6 +305,7 @@ TEST_F(ComTest, test_autoReadCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } TEST_F(ComTest, test_autoTestCmd) { @@ -305,23 +314,25 @@ TEST_F(ComTest, test_autoTestCmd) { // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } /* TEST_F(ComTest, test_stopCmd) { uint8_t buffer[] = {static_cast(API_t::stopCmd)}; - com->onPacketReceived(buffer, sizeof(buffer)); + com.onPacketReceived(buffer, sizeof(buffer)); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } */ /* TEST_F(ComTest, test_quitCmd) { - EXPECT_CALL(*controllerMock, setState(opInit)); + EXPECT_CALL(*controllerMock, setState(&opInit)); EXPECT_CALL(*opKnitMock, init); - com->h_quitCmd(); + com.h_quitCmd(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -351,28 +362,28 @@ TEST_F(ComTest, test_cnfline_kh910) { // first call increments line number to zero, not accepted EXPECT_CALL(*opKnitMock, setNextLine).WillOnce(Return(false)); EXPECT_CALL(*opKnitMock, setLastLine).Times(0); - com->h_cnfLine(buffer, sizeof(buffer)); + com.h_cnfLine(buffer, sizeof(buffer)); // second call Line accepted, last line EXPECT_CALL(*opKnitMock, setNextLine).WillOnce(Return(true)); EXPECT_CALL(*opKnitMock, setLastLine).Times(1); - com->h_cnfLine(buffer, sizeof(buffer)); + com.h_cnfLine(buffer, sizeof(buffer)); // not last line buffer[3] = 0x00; buffer[29] = 0xC0; EXPECT_CALL(*opKnitMock, setNextLine).WillOnce(Return(true)); EXPECT_CALL(*opKnitMock, setLastLine).Times(0); - com->h_cnfLine(buffer, sizeof(buffer)); + com.h_cnfLine(buffer, sizeof(buffer)); // checksum wrong EXPECT_CALL(*opKnitMock, setNextLine).Times(0); buffer[29]--; - com->h_cnfLine(buffer, sizeof(buffer)); + com.h_cnfLine(buffer, sizeof(buffer)); // not enough bytes in buffer EXPECT_CALL(*opKnitMock, setNextLine).Times(0); - com->h_cnfLine(buffer, sizeof(buffer) - 1); + com.h_cnfLine(buffer, sizeof(buffer) - 1); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock)); @@ -401,20 +412,21 @@ TEST_F(ComTest, test_cnfline_kh270) { // Last line accepted EXPECT_CALL(*opKnitMock, setNextLine).WillOnce(Return(true)); EXPECT_CALL(*opKnitMock, setLastLine).Times(1); - com->h_cnfLine(buffer, sizeof(buffer)); + com.h_cnfLine(buffer, sizeof(buffer)); } */ /* TEST_F(ComTest, test_debug) { uint8_t buffer[] = {static_cast(API_t::debug)}; - com->onPacketReceived(buffer, sizeof(buffer)); + com.onPacketReceived(buffer, sizeof(buffer)); + ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); } */ TEST_F(ComTest, test_update) { EXPECT_CALL(*packetSerialWrapperMock, update); - com->update(); + com.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -423,7 +435,7 @@ TEST_F(ComTest, test_update) { TEST_F(ComTest, test_send) { expect_send(true); uint8_t p[] = {1, 2, 3}; - com->send(p, 3); + com.send(p, 3); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -431,7 +443,7 @@ TEST_F(ComTest, test_send) { TEST_F(ComTest, test_sendMsg1) { expect_send(true); - com->sendMsg(API_t::testRes, "abc"); + com.sendMsg(API_t::testRes, "abc"); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -440,7 +452,7 @@ TEST_F(ComTest, test_sendMsg1) { TEST_F(ComTest, test_sendMsg2) { char buf[] = "abc\0"; expect_send(true); - com->sendMsg(API_t::testRes, buf); + com.sendMsg(API_t::testRes, buf); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -448,7 +460,7 @@ TEST_F(ComTest, test_sendMsg2) { TEST_F(ComTest, test_send_reqLine) { expect_send(true); - com->send_reqLine(0); + com.send_reqLine(0); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(packetSerialWrapperMock)); @@ -457,12 +469,12 @@ TEST_F(ComTest, test_send_reqLine) { TEST_F(ComTest, test_send_indState) { EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L)); EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R)); - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opInit)); + EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); expect_send(true); - com->send_indState(Err_t::Success); + com.send_indState(Err_t::Success); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(controllerMock)); diff --git a/test/test_controller.cpp b/test/test_controller.cpp index 96919ab7b..983b7ae34 100644 --- a/test/test_controller.cpp +++ b/test/test_controller.cpp @@ -44,19 +44,19 @@ using ::testing::Mock; using ::testing::Return; using ::testing::Test; -extern Controller *controller; -extern OpKnit *opKnit; +extern Controller& controller; +extern OpKnit& opKnit; -extern BeeperMock *beeper; -extern ComMock *com; -extern EncodersMock *encoders; -extern SolenoidsMock *solenoids; +extern BeeperMock& beeper; +extern ComMock& com; +extern EncodersMock& encoders; +extern SolenoidsMock& solenoids; -extern OpIdleMock *opIdle; -extern OpInitMock *opInit; -extern OpReadyMock *opReady; -extern OpTestMock *opTest; -extern OpErrorMock *opError; +extern OpIdleMock& opIdle; +extern OpInitMock& opInit; +extern OpReadyMock& opReady; +extern OpTestMock& opTest; +extern OpErrorMock& opError; class ControllerTest : public ::testing::Test { protected: @@ -64,16 +64,16 @@ class ControllerTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - beeperMock = beeper; - comMock = com; - encodersMock = encoders; - solenoidsMock = solenoids; + beeperMock = &beeper; + comMock = &com; + encodersMock = &encoders; + solenoidsMock = &solenoids; - opIdleMock = opIdle; - opInitMock = opInit; - opReadyMock = opReady; - opTestMock = opTest; - opErrorMock = opError; + opIdleMock = &opIdle; + opInitMock = &opInit; + opReadyMock = &opReady; + opTestMock = &opTest; + opErrorMock = &opError; // The global instance does not get destroyed at the end of each test. // Ordinarily the mock instance would be local and such behaviour would @@ -90,9 +90,9 @@ class ControllerTest : public ::testing::Test { Mock::AllowLeak(opErrorMock); // start in state `OpIdle` - controller->init(); - opKnit->init(); - controller->setMachineType(Machine_t::Kh910); + controller.init(); + opKnit.init(); + controller.setMachineType(Machine_t::Kh910); expected_isready(Direction_t::NoDirection, Direction_t::NoDirection, 0); } @@ -121,14 +121,14 @@ class ControllerTest : public ::testing::Test { } void expected_isready(Direction_t dir, Direction_t hall, uint8_t position) { - controller->m_direction = dir; - controller->m_hallActive = hall; - controller->m_position = position; + controller.m_direction = dir; + controller.m_hallActive = hall; + controller.m_position = position; } void expected_state(OpInterface *state) { - controller->setState(state); - controller->update(); + controller.setState(state); + controller.update(); } void expected_update() { @@ -137,7 +137,7 @@ class ControllerTest : public ::testing::Test { EXPECT_CALL(*encodersMock, getHallActive).Times(1); EXPECT_CALL(*encodersMock, getBeltShift).Times(1); EXPECT_CALL(*encodersMock, getCarriage).Times(1); - controller->update(); + controller.update(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(encodersMock)); @@ -145,7 +145,7 @@ class ControllerTest : public ::testing::Test { void expected_update_idle() { // starts in state `OpIdle` - ASSERT_EQ(controller->getState(), opIdleMock); + ASSERT_EQ(controller.getState(), &opIdle); EXPECT_CALL(*opIdleMock, update); expected_update(); @@ -156,7 +156,7 @@ class ControllerTest : public ::testing::Test { void expected_update_init() { // starts in state `OpInit` - ASSERT_EQ(controller->getState(), opInitMock); + ASSERT_EQ(controller.getState(), &opInit); EXPECT_CALL(*opInitMock, update); expected_update(); @@ -167,7 +167,7 @@ class ControllerTest : public ::testing::Test { void expected_update_ready() { // starts in state `OpReady` - ASSERT_EQ(controller->getState(), opReadyMock); + ASSERT_EQ(controller.getState(), &opReady); EXPECT_CALL(*opReadyMock, update); expected_update(); @@ -178,14 +178,14 @@ class ControllerTest : public ::testing::Test { void expected_update_knit() { // starts in state `OpKnit` - ASSERT_EQ(controller->getState(), opKnit); + ASSERT_EQ(controller.getState(), &opKnit); expected_update(); } void expected_update_test() { // starts in state `OpTest` - ASSERT_EQ(controller->getState(), opTestMock); + ASSERT_EQ(controller.getState(), &opTest); EXPECT_CALL(*opTestMock, update); expected_update(); @@ -209,19 +209,19 @@ TEST_F(ControllerTest, test_init) { EXPECT_CALL(*arduinoMock, pinMode(LED_PIN_B, OUTPUT)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH)); - controller->init(); + controller.init(); } TEST_F(ControllerTest, test_setState) { - controller->setState(opInitMock); + controller.setState(&opInit); - EXPECT_CALL(*opIdle, end); - EXPECT_CALL(*opInit, begin); + EXPECT_CALL(*opIdleMock, end); + EXPECT_CALL(*opInitMock, begin); expected_update_idle(); - ASSERT_EQ(controller->getState(), opInitMock); + ASSERT_EQ(controller.getState(), &opInit); EXPECT_CALL(*opInitMock, state).WillOnce(Return(OpState_t::Init)); - controller->getState()->state(); + controller.getState()->state(); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opIdleMock)); @@ -229,28 +229,28 @@ TEST_F(ControllerTest, test_setState) { } TEST_F(ControllerTest, test_getHallActive) { - controller->init(); - ASSERT_EQ(controller->getHallActive(), Direction_t::NoDirection); + controller.init(); + ASSERT_EQ(controller.getHallActive(), Direction_t::NoDirection); } TEST_F(ControllerTest, test_ready_state) { - controller->setState(opReadyMock); + controller.setState(&opReady); expected_update_idle(); - ASSERT_EQ(controller->getState(), opReadyMock); + ASSERT_EQ(controller.getState(), &opReady); EXPECT_CALL(*opReadyMock, state).WillOnce(Return(OpState_t::Ready)); - controller->getState()->state(); + controller.getState()->state(); } TEST_F(ControllerTest, test_update_knit) { // get to state `OpReady` - controller->setState(opReadyMock); + controller.setState(&opReady); expected_update_idle(); // get to state `OpKnit` - controller->setState(opKnit); + controller.setState(&opKnit); expected_update_ready(); - ASSERT_EQ(controller->getState(), opKnit); + ASSERT_EQ(controller.getState(), &opKnit); // now in state `OpKnit` expect_first_knit(); @@ -263,22 +263,22 @@ TEST_F(ControllerTest, test_update_knit) { TEST_F(ControllerTest, test_update_test) { // get in state `OpTest` - controller->setState(opTestMock); + controller.setState(&opTest); expected_update_idle(); // now in state `OpTest` expected_update_test(); - ASSERT_EQ(controller->getState(), opTestMock); + ASSERT_EQ(controller.getState(), &opTest); EXPECT_CALL(*opTestMock, state).WillOnce(Return(OpState_t::Test)); - controller->getState()->state(); + controller.getState()->state(); // now quit test - controller->setState(opInitMock); + controller.setState(&opInit); EXPECT_CALL(*opTestMock, end); EXPECT_CALL(*opInitMock, begin); expected_update_test(); - ASSERT_EQ(controller->getState(), opInitMock); + ASSERT_EQ(controller.getState(), &opInit); // test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(opInitMock)); @@ -286,10 +286,10 @@ TEST_F(ControllerTest, test_update_test) { } TEST_F(ControllerTest, test_error_state) { - controller->setState(opErrorMock); + controller.setState(&opError); expected_update_idle(); - ASSERT_EQ(controller->getState(), opErrorMock); + ASSERT_EQ(controller.getState(), &opError); EXPECT_CALL(*opErrorMock, state).WillOnce(Return(OpState_t::Error)); - controller->getState()->state(); + controller.getState()->state(); } diff --git a/test/test_encoders.cpp b/test/test_encoders.cpp index 277bb5470..d50e65429 100644 --- a/test/test_encoders.cpp +++ b/test/test_encoders.cpp @@ -33,9 +33,9 @@ using ::testing::AtLeast; using ::testing::Mock; using ::testing::Return; -extern Encoders *encoders; +extern Encoders& encoders; -extern AnalogReadAsyncWrapperMock *analogReadAsyncWrapper; +extern AnalogReadAsyncWrapperMock& analogReadAsyncWrapper; class EncodersTest : public ::testing::Test { protected: @@ -43,14 +43,14 @@ class EncodersTest : public ::testing::Test { arduinoMock = arduinoMockInstance(); // pointers to global instances - analogReadAsyncWrapperMock = analogReadAsyncWrapper; + analogReadAsyncWrapperMock = &analogReadAsyncWrapper; // The global instances do not get destroyed at the end of each test. // Ordinarily the mock instances would be local and such behaviour would // cause a memory leak. We must notify the test that this is not the case. Mock::AllowLeak(analogReadAsyncWrapperMock); - encoders->init(Machine_t::Kh910); + encoders.init(Machine_t::Kh910); } void TearDown() override { @@ -66,266 +66,266 @@ TEST_F(EncodersTest, test_encA_rising_not_in_front) { EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); // We should not enter the falling function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())], nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), 0x01); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), 0x01); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Enter falling function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders->getMachineType())]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders.getMachineType())]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders->getMachineType())]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders.getMachineType())]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_rising_in_front_notKH270) { - ASSERT_FALSE(encoders->getMachineType() == Machine_t::Kh270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_FALSE(encoders.getMachineType() == Machine_t::Kh270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Create a falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); // We should not enter the falling function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift is regular EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).WillOnce(Return(true)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getHallActive(), Direction_t::Left); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders->getMachineType())]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Lace); - ASSERT_EQ(encoders->getBeltShift(), BeltShift::Regular); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getHallActive(), Direction_t::Left); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders.getMachineType())]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Lace); + ASSERT_EQ(encoders.getBeltShift(), BeltShift::Regular); // Enter falling function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // BeltShift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())], nullptr); - encoders->m_position = 0; + encoders.m_position = 0; // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders->getMachineType())]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders.getMachineType())]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_rising_in_front_KH270) { - encoders->init(Machine_t::Kh270); - ASSERT_EQ(encoders->getMachineType(), Machine_t::Kh270); + encoders.init(Machine_t::Kh270); + ASSERT_EQ(encoders.getMachineType(), Machine_t::Kh270); // Create a rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); // We should not enter the falling function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift is ignored EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(Machine_t::Kh270)] - 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(Machine_t::Kh270)] - 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getHallActive(), Direction_t::Left); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(Machine_t::Kh270)]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); - ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getHallActive(), Direction_t::Left); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(Machine_t::Kh270)]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getBeltShift(), BeltShift::Unknown); // Enter falling function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // BeltShift is ignored EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getHallActive(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(Machine_t::Kh270)]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); - ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getHallActive(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(Machine_t::Kh270)]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getBeltShift(), BeltShift::Unknown); // Create a rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); // We will not enter the rising function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_rising_after_KH270) { - encoders->init(Machine_t::Kh270); - ASSERT_EQ(encoders->getMachineType(), Machine_t::Kh270); + encoders.init(Machine_t::Kh270); + ASSERT_EQ(encoders.getMachineType(), Machine_t::Kh270); // Create a falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); // We should not enter the falling function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift ignored EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getHallActive(), Direction_t::Left); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(Machine_t::Kh270)] + MAGNET_DISTANCE_270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); - ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getHallActive(), Direction_t::Left); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(Machine_t::Kh270)] + MAGNET_DISTANCE_270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getBeltShift(), BeltShift::Unknown); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_G_carriage) { - ASSERT_FALSE(encoders->getMachineType() == Machine_t::Kh270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_FALSE(encoders.getMachineType() == Machine_t::Kh270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // BeltShift is regular EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).WillOnce(Return(true)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); // Enter falling function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallRightCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Garter); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Garter); // Enter falling function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())], nullptr); // Create a rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); // We will not enter the rising function EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Create a falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right: EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); @@ -335,143 +335,143 @@ TEST_F(EncodersTest, test_encA_falling_not_in_front) { // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())], nullptr); // Create rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); // Rising function not entered EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped).Times(0); - encoders->isr(); + encoders.isr(); // Falling edge - encoders->m_position = END_LEFT[static_cast(encoders->getMachineType())] + 1; + encoders.m_position = END_LEFT[static_cast(encoders.getMachineType())] + 1; EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())], nullptr); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_falling_in_front) { - ASSERT_FALSE(encoders->getMachineType() == Machine_t::Kh270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_FALSE(encoders.getMachineType() == Machine_t::Kh270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Enter rising function EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())], nullptr); // Falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // BeltShift is shifted EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).WillOnce(Return(true)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallRightCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getDirection(), Direction_t::Right); - ASSERT_EQ(encoders->getHallActive(), Direction_t::Right); - ASSERT_EQ(encoders->getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders->getMachineType())]); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); - ASSERT_EQ(encoders->getBeltShift(), BeltShift::Shifted); + ASSERT_EQ(encoders.getDirection(), Direction_t::Right); + ASSERT_EQ(encoders.getHallActive(), Direction_t::Right); + ASSERT_EQ(encoders.getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders.getMachineType())]); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); + ASSERT_EQ(encoders.getBeltShift(), BeltShift::Shifted); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_falling_at_end) { - ASSERT_FALSE(encoders->getMachineType() == Machine_t::Kh270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_FALSE(encoders.getMachineType() == Machine_t::Kh270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())], nullptr); // Falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallRightCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders->getMachineType())]); + ASSERT_EQ(encoders.getPosition(), END_RIGHT_MINUS_OFFSET[static_cast(encoders.getMachineType())]); - uint16_t pos = END_RIGHT_MINUS_OFFSET[static_cast(encoders->getMachineType())]; - while (pos < END_RIGHT[static_cast(encoders->getMachineType())]) { + uint16_t pos = END_RIGHT_MINUS_OFFSET[static_cast(encoders.getMachineType())]; + while (pos < END_RIGHT[static_cast(encoders.getMachineType())]) { // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())], nullptr); - ASSERT_EQ(encoders->getPosition(), ++pos); + ASSERT_EQ(encoders.getPosition(), ++pos); // Falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction does not matter EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())], nullptr); + encoders.hallRightCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())], nullptr); - ASSERT_EQ(encoders->getPosition(), pos); + ASSERT_EQ(encoders.getPosition(), pos); } - ASSERT_EQ(encoders->getPosition(), pos); + ASSERT_EQ(encoders.getPosition(), pos); // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MAX[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MAX[static_cast(encoders.getMachineType())], nullptr); - ASSERT_EQ(encoders->getPosition(), pos); + ASSERT_EQ(encoders.getPosition(), pos); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); @@ -479,119 +479,119 @@ TEST_F(EncodersTest, test_encA_falling_at_end) { // requires FILTER_R_MIN != 0 TEST_F(EncodersTest, test_encA_falling_set_K_carriage_KH910) { - ASSERT_EQ(encoders->getMachineType(), Machine_t::Kh910); + ASSERT_EQ(encoders.getMachineType(), Machine_t::Kh910); // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction does not matter EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_L_MIN[static_cast(encoders->getMachineType())], nullptr); + encoders.hallLeftCallback(FILTER_L_MIN[static_cast(encoders.getMachineType())], nullptr); // Falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction does not matter EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MIN[static_cast(encoders->getMachineType())] - 1, nullptr); + encoders.hallRightCallback(FILTER_R_MIN[static_cast(encoders.getMachineType())] - 1, nullptr); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::Knit); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_encA_falling_not_at_end) { - ASSERT_FALSE(encoders->getMachineType() == Machine_t::Kh270); - ASSERT_EQ(encoders->getCarriage(), Carriage_t::NoCarriage); + ASSERT_FALSE(encoders.getMachineType() == Machine_t::Kh270); + ASSERT_EQ(encoders.getCarriage(), Carriage_t::NoCarriage); // Rising edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Left EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)); // In front of Left Hall Sensor - encoders->hallLeftCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())] + 1, nullptr); + encoders.hallLeftCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())] + 1, nullptr); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders->getMachineType())]); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders.getMachineType())]); // Falling edge EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)); EXPECT_CALL(*analogReadAsyncWrapperMock, analogReadAsyncWrapped); - encoders->isr(); + encoders.isr(); // Direction is Right EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)); // Beltshift is not decided EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0); // Not in front of Right Hall Sensor - encoders->hallRightCallback(FILTER_R_MAX[static_cast(encoders->getMachineType())], nullptr); + encoders.hallRightCallback(FILTER_R_MAX[static_cast(encoders.getMachineType())], nullptr); - ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders->getMachineType())]); + ASSERT_EQ(encoders.getPosition(), END_LEFT_PLUS_OFFSET[static_cast(encoders.getMachineType())]); // Test expectations without destroying instance ASSERT_TRUE(Mock::VerifyAndClear(analogReadAsyncWrapperMock)); } TEST_F(EncodersTest, test_getPosition) { - uint8_t p = encoders->getPosition(); + uint8_t p = encoders.getPosition(); ASSERT_EQ(p, 0x00); } TEST_F(EncodersTest, test_getBeltShift) { - BeltShift_t b = encoders->getBeltShift(); + BeltShift_t b = encoders.getBeltShift(); ASSERT_EQ(b, BeltShift::Unknown); } TEST_F(EncodersTest, test_getDirection) { - Direction_t d = encoders->getDirection(); + Direction_t d = encoders.getDirection(); ASSERT_EQ(d, Direction_t::NoDirection); } TEST_F(EncodersTest, test_getHallActive) { - Direction_t d = encoders->getHallActive(); + Direction_t d = encoders.getHallActive(); ASSERT_EQ(d, Direction_t::NoDirection); } TEST_F(EncodersTest, test_getCarriage) { - Carriage_t c = encoders->getCarriage(); + Carriage_t c = encoders.getCarriage(); ASSERT_EQ(c, Carriage_t::NoCarriage); } TEST_F(EncodersTest, test_getMachineType) { - Machine_t m = encoders->getMachineType(); + Machine_t m = encoders.getMachineType(); ASSERT_EQ(m, Machine_t::Kh910); } TEST_F(EncodersTest, test_init) { - encoders->init(Machine_t::Kh270); - Machine_t m = encoders->getMachineType(); + encoders.init(Machine_t::Kh270); + Machine_t m = encoders.getMachineType(); ASSERT_EQ(m, Machine_t::Kh270); } TEST_F(EncodersTest, test_getHallValue) { - uint16_t v = encoders->getHallValue(Direction_t::NoDirection); + uint16_t v = encoders.getHallValue(Direction_t::NoDirection); ASSERT_EQ(v, 0U); EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L)); - v = encoders->getHallValue(Direction_t::Left); + v = encoders.getHallValue(Direction_t::Left); ASSERT_EQ(v, 0U); EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R)); - v = encoders->getHallValue(Direction_t::Right); + v = encoders.getHallValue(Direction_t::Right); ASSERT_EQ(v, 0U); EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R)).WillOnce(Return(0xBEEFU)); - v = encoders->getHallValue(Direction_t::Right); + v = encoders.getHallValue(Direction_t::Right); ASSERT_EQ(v, 0xBEEFU); } diff --git a/test/test_solenoids.cpp b/test/test_solenoids.cpp index ce5580aab..607f20d54 100644 --- a/test/test_solenoids.cpp +++ b/test/test_solenoids.cpp @@ -28,7 +28,7 @@ using ::testing::Return; -extern Solenoids *solenoids; +extern Solenoids& solenoids; class SolenoidsTest : public ::testing::Test { protected: @@ -49,29 +49,29 @@ TEST_F(SolenoidsTest, test_construct) { } TEST_F(SolenoidsTest, test_init) { - solenoids->init(); - ASSERT_EQ(solenoids->solenoidState, 0U); + solenoids.init(); + ASSERT_EQ(solenoids.solenoidState, 0U); } TEST_F(SolenoidsTest, test_setSolenoid1) { - solenoids->setSolenoids(0); - ASSERT_EQ(solenoids->solenoidState, 0U); - solenoids->setSolenoid(0, true); - ASSERT_EQ(solenoids->solenoidState, 1U); + solenoids.setSolenoids(0); + ASSERT_EQ(solenoids.solenoidState, 0U); + solenoids.setSolenoid(0, true); + ASSERT_EQ(solenoids.solenoidState, 1U); } TEST_F(SolenoidsTest, test_setSolenoid2) { - solenoids->setSolenoids(0); - ASSERT_EQ(solenoids->solenoidState, 0U); - solenoids->setSolenoids(0); - ASSERT_EQ(solenoids->solenoidState, 0U); - solenoids->setSolenoid(0, false); - ASSERT_EQ(solenoids->solenoidState, 0U); + solenoids.setSolenoids(0); + ASSERT_EQ(solenoids.solenoidState, 0U); + solenoids.setSolenoids(0); + ASSERT_EQ(solenoids.solenoidState, 0U); + solenoids.setSolenoid(0, false); + ASSERT_EQ(solenoids.solenoidState, 0U); } TEST_F(SolenoidsTest, test_setSolenoid3) { - solenoids->setSolenoids(0x8000); - ASSERT_EQ(solenoids->solenoidState, 0x8000U); - solenoids->setSolenoid(16, false); - ASSERT_EQ(solenoids->solenoidState, 0x8000U); + solenoids.setSolenoids(0x8000); + ASSERT_EQ(solenoids.solenoidState, 0x8000U); + solenoids.setSolenoid(16, false); + ASSERT_EQ(solenoids.solenoidState, 0x8000U); } From e20915061f84a8ce32d21cd139859b6d6b763d8c Mon Sep 17 00:00:00 2001 From: t0mpr1c3 Date: Mon, 2 Oct 2023 19:43:29 -0400 Subject: [PATCH 2/2] control --- src/ayab/com.cpp | 4 ++-- src/ayab/controller.cpp | 14 ++++++++++++++ src/ayab/controller.h | 6 ++++++ src/ayab/global_controller.cpp | 8 ++++++++ src/ayab/main.cpp | 26 +++++++++++++------------- test/mocks/controller_mock.cpp | 10 ++++++++++ test/mocks/controller_mock.h | 2 ++ test/test_OpError.cpp | 4 ++-- test/test_OpInit.cpp | 4 ++-- test/test_com.cpp | 8 +++++--- test/test_controller.cpp | 17 +++++++++++++++++ 11 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/ayab/com.cpp b/src/ayab/com.cpp index 24d7c8e8b..35098358d 100644 --- a/src/ayab/com.cpp +++ b/src/ayab/com.cpp @@ -140,7 +140,7 @@ void Com::send_indState(Err_t error) const { uint8_t payload[INDSTATE_LEN] = { static_cast(API_t::indState), static_cast(error), - static_cast(GlobalController::getState()->state()), + static_cast(GlobalController::state()), highByte(leftHallValue), lowByte(leftHallValue), highByte(rightHallValue), @@ -159,7 +159,7 @@ void Com::send_indState(Err_t error) const { * \param size The number of bytes in the data buffer. */ void Com::onPacketReceived(const uint8_t *buffer, size_t size) { - GlobalController::getState()->com(buffer, size); + GlobalController::com(buffer, size); } // Serial command handling diff --git a/src/ayab/controller.cpp b/src/ayab/controller.cpp index 3a3f75407..8a8e0deec 100644 --- a/src/ayab/controller.cpp +++ b/src/ayab/controller.cpp @@ -32,6 +32,13 @@ // Public methods +/*! + * \brief Return current machine state + */ +OpState_t Controller::state() { + return m_currentState->state(); +} + /*! * \brief Initialize Finite State Machine. */ @@ -57,6 +64,13 @@ void Controller::init() { m_nextState = &GlobalOpIdle::m_instance; } +/*! + * \brief Call communication method of current machine state + */ +void Controller::com(const uint8_t *buffer, size_t size) { + m_currentState->com(buffer, size); +} + /*! * \brief Dispatch on machine state; update machine state */ diff --git a/src/ayab/controller.h b/src/ayab/controller.h index b4f75c7e3..4515529ff 100644 --- a/src/ayab/controller.h +++ b/src/ayab/controller.h @@ -34,7 +34,9 @@ class ControllerInterface { virtual ~ControllerInterface() = default; // any methods that need to be mocked should go here + virtual OpState_t state() = 0; virtual void init() = 0; + virtual void com(const uint8_t *buffer, size_t size) = 0; virtual void update() = 0; virtual void cacheEncoders() = 0; virtual void setState(OpInterface *state) = 0; @@ -63,7 +65,9 @@ class GlobalController final { // reference to global instance whose methods are implemented static ControllerInterface& m_instance; + static OpState_t state(); static void init(); + static void com(const uint8_t *buffer, size_t size); static void update(); static void cacheEncoders(); static void setState(OpInterface *state); @@ -79,7 +83,9 @@ class GlobalController final { class Controller : public ControllerInterface { public: + OpState_t state() final; void init() final; + void com(const uint8_t *buffer, size_t size) final; void update() final; void cacheEncoders() final; void setState(OpInterface *state) final; diff --git a/src/ayab/global_controller.cpp b/src/ayab/global_controller.cpp index a8c383609..21b1d976b 100644 --- a/src/ayab/global_controller.cpp +++ b/src/ayab/global_controller.cpp @@ -24,10 +24,18 @@ // static member functions +OpState_t GlobalController::state() { + return m_instance.state(); +} + void GlobalController::init() { m_instance.init(); } +void GlobalController::com(const uint8_t *buffer, size_t size) { + m_instance.com(buffer, size); +} + void GlobalController::update() { m_instance.update(); } diff --git a/src/ayab/main.cpp b/src/ayab/main.cpp index 24bbc4672..cf985694c 100644 --- a/src/ayab/main.cpp +++ b/src/ayab/main.cpp @@ -43,21 +43,21 @@ // Global definitions: references elsewhere must use `extern`. // Each of the following is a pointer to a singleton class // containing static methods. -constexpr GlobalAnalogReadAsyncWrapper *analogReadasyncWrapper; -constexpr GlobalPacketSerialWrapper *packetSerialWrapper; +const GlobalAnalogReadAsyncWrapper *analogReadasyncWrapper; +const GlobalPacketSerialWrapper *packetSerialWrapper; -constexpr GlobalBeeper *beeper; -constexpr GlobalCom *com; -constexpr GlobalController *controller; -constexpr GlobalEncoders *encoders; -constexpr GlobalSolenoids *solenoids; +const GlobalBeeper *beeper; +const GlobalCom *com; +const GlobalController *controller; +const GlobalEncoders *encoders; +const GlobalSolenoids *solenoids; -constexpr GlobalOpIdle *opIdle; -constexpr GlobalOpInit *opInit; -constexpr GlobalOpReady *opReady; -constexpr GlobalOpKnit *opKnit; -constexpr GlobalOpTest *opTest; -constexpr GlobalOpError *opError; +const GlobalOpIdle *opIdle; +const GlobalOpInit *opInit; +const GlobalOpReady *opReady; +const GlobalOpKnit *opKnit; +const GlobalOpTest *opTest; +const GlobalOpError *opError; // Initialize static members. // Each singleton class contains a reference to a static instance diff --git a/test/mocks/controller_mock.cpp b/test/mocks/controller_mock.cpp index 886fb018a..fdd3428bb 100644 --- a/test/mocks/controller_mock.cpp +++ b/test/mocks/controller_mock.cpp @@ -40,11 +40,21 @@ void releaseControllerMock() { } } +OpState_t Controller::state() { + assert(gControllerMock != nullptr); + return gControllerMock->state(); +} + void Controller::init() { assert(gControllerMock != nullptr); gControllerMock->init(); } +void Controller::com(const uint8_t *buffer, size_t size) { + assert(gControllerMock != nullptr); + gControllerMock->com(buffer, size); +} + void Controller::update() { assert(gControllerMock != nullptr); gControllerMock->update(); diff --git a/test/mocks/controller_mock.h b/test/mocks/controller_mock.h index 1afd15e47..a4b6df211 100644 --- a/test/mocks/controller_mock.h +++ b/test/mocks/controller_mock.h @@ -31,7 +31,9 @@ class ControllerMock : public ControllerInterface { public: + MOCK_METHOD0(state, OpState_t()); MOCK_METHOD0(init, void()); + MOCK_METHOD2(com, void(const uint8_t *buffer, size_t size)); MOCK_METHOD0(update, void()); MOCK_METHOD0(cacheEncoders, void()); MOCK_METHOD1(setState, void(OpInterface *state)); diff --git a/test/test_OpError.cpp b/test/test_OpError.cpp index a13db4ff3..9c630fc5e 100644 --- a/test/test_OpError.cpp +++ b/test/test_OpError.cpp @@ -105,7 +105,7 @@ TEST_F(OpErrorTest, test_update) { EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, HIGH)); // send_indState - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError)); + EXPECT_CALL(*controllerMock, state); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); @@ -116,7 +116,7 @@ TEST_F(OpErrorTest, test_update) { EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, HIGH)); EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_B, LOW)); // send_indState - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opError)); + EXPECT_CALL(*controllerMock, state); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); diff --git a/test/test_OpInit.cpp b/test/test_OpInit.cpp index e9cbe9088..e03efe173 100644 --- a/test/test_OpInit.cpp +++ b/test/test_OpInit.cpp @@ -85,7 +85,7 @@ class OpInitTest : public ::testing::Test { void expect_ready(bool ready) { if (ready) { - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); + EXPECT_CALL(*controllerMock, state); } ASSERT_EQ(opInit.isReady(), ready); } @@ -144,7 +144,7 @@ TEST_F(OpInitTest, test_updateF) { TEST_F(OpInitTest, test_updateT) { // isReady() == true expect_update(get_position_past_left(Machine_t::Kh910), Direction_t::Right, Direction_t::Left); - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); + EXPECT_CALL(*controllerMock, state); EXPECT_CALL(*controllerMock, setState(&opReady)); opInit.update(); diff --git a/test/test_com.cpp b/test/test_com.cpp index 02d409bf2..283bd0dc3 100644 --- a/test/test_com.cpp +++ b/test/test_com.cpp @@ -96,9 +96,11 @@ class ComTest : public ::testing::Test { void expected_write_onPacketReceived(uint8_t *buffer, size_t size, bool once) { - expect_send(once); - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opTest)); + EXPECT_CALL(*controllerMock, com); com.onPacketReceived(buffer, size); + + expect_send(once); + opTest.com(buffer, size); } void reqInit(Machine_t machine) { @@ -469,7 +471,7 @@ TEST_F(ComTest, test_send_reqLine) { TEST_F(ComTest, test_send_indState) { EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L)); EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R)); - EXPECT_CALL(*controllerMock, getState).WillOnce(Return(&opInit)); + EXPECT_CALL(*controllerMock, state); EXPECT_CALL(*controllerMock, getCarriage); EXPECT_CALL(*controllerMock, getPosition); EXPECT_CALL(*controllerMock, getDirection); diff --git a/test/test_controller.cpp b/test/test_controller.cpp index 983b7ae34..904cea705 100644 --- a/test/test_controller.cpp +++ b/test/test_controller.cpp @@ -201,6 +201,14 @@ class ControllerTest : public ::testing::Test { } }; +TEST_F(ControllerTest, test_state) { + EXPECT_CALL(*opIdleMock, state); + controller.state(); + + // test expectations without destroying instance + ASSERT_TRUE(Mock::VerifyAndClear(opIdleMock)); +} + TEST_F(ControllerTest, test_init) { EXPECT_CALL(*arduinoMock, pinMode(ENC_PIN_A, INPUT)); EXPECT_CALL(*arduinoMock, pinMode(ENC_PIN_B, INPUT)); @@ -285,6 +293,15 @@ TEST_F(ControllerTest, test_update_test) { ASSERT_TRUE(Mock::VerifyAndClear(opTestMock)); } +TEST_F(ControllerTest, test_com) { + const uint8_t buffer[] = {0xFF}; + EXPECT_CALL(*opIdleMock, com); + controller.com(buffer, 1); + + // test expectations without destroying instance + ASSERT_TRUE(Mock::VerifyAndClear(opIdleMock)); +} + TEST_F(ControllerTest, test_error_state) { controller.setState(&opError); expected_update_idle();