added unit tests, minor API change

This commit is contained in:
Robin Müller 2022-04-01 18:38:25 +02:00
parent c20be13733
commit 738f572043
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
9 changed files with 210 additions and 4 deletions

View File

@ -1,8 +1,9 @@
#include "DummyPowerSwitcher.h"
DummyPowerSwitcher::DummyPowerSwitcher(object_id_t objectId, size_t numberOfSwitches,
size_t numberOfFuses, uint32_t switchDelayMs)
: SystemObject(objectId),
size_t numberOfFuses, bool registerGlobally,
uint32_t switchDelayMs)
: SystemObject(objectId, registerGlobally),
switcherList(numberOfSwitches),
fuseList(numberOfFuses),
switchDelayMs(switchDelayMs) {}

View File

@ -11,7 +11,7 @@
class DummyPowerSwitcher : public SystemObject, public PowerSwitchIF {
public:
DummyPowerSwitcher(object_id_t objectId, size_t numberOfSwitches, size_t numberOfFuses,
uint32_t switchDelayMs = 5000);
bool registerGlobally = true, uint32_t switchDelayMs = 5000);
void setInitialSwitcherList(std::vector<ReturnValue_t> switcherList);
void setInitialFusesList(std::vector<ReturnValue_t> switcherList);

View File

@ -11,7 +11,10 @@ target_sources(${FSFW_TEST_TGT} PRIVATE
)
add_subdirectory(testcfg)
add_subdirectory(mocks)
add_subdirectory(action)
add_subdirectory(power)
add_subdirectory(container)
add_subdirectory(osal)
add_subdirectory(serialize)

View File

@ -0,0 +1,3 @@
target_sources(${FSFW_TEST_TGT} PRIVATE
PowerSwitcherMock.cpp
)

View File

@ -0,0 +1,77 @@
#include "PowerSwitcherMock.h"
static uint32_t SWITCH_REQUEST_UPDATE_VALUE = 0;
PowerSwitcherMock::PowerSwitcherMock() {}
ReturnValue_t PowerSwitcherMock::sendSwitchCommand(power::Switch_t switchNr, ReturnValue_t onOff) {
if (switchMap.count(switchNr) == 0) {
switchMap.emplace(switchNr, SwitchInfo(switchNr, onOff));
} else {
SwitchInfo& info = switchMap.at(switchNr);
info.currentState = onOff;
if (onOff == PowerSwitchIF::SWITCH_ON) {
info.timesCalledOn++;
} else {
info.timesCalledOff++;
}
}
return RETURN_OK;
}
ReturnValue_t PowerSwitcherMock::sendFuseOnCommand(uint8_t fuseNr) {
if (fuseMap.count(fuseNr) == 0) {
fuseMap.emplace(fuseNr, FuseInfo(fuseNr));
} else {
FuseInfo& info = fuseMap.at(fuseNr);
info.timesCalled++;
}
return RETURN_OK;
}
ReturnValue_t PowerSwitcherMock::getSwitchState(power::Switch_t switchNr) const {
if (switchMap.count(switchNr) == 1) {
auto& info = switchMap.at(switchNr);
SWITCH_REQUEST_UPDATE_VALUE++;
return info.currentState;
}
return RETURN_FAILED;
}
ReturnValue_t PowerSwitcherMock::getFuseState(uint8_t fuseNr) const {
if (fuseMap.count(fuseNr) == 1) {
return FUSE_ON;
} else {
return FUSE_OFF;
}
return RETURN_FAILED;
}
uint32_t PowerSwitcherMock::getSwitchDelayMs(void) const { return 5000; }
SwitchInfo::SwitchInfo() : switcher(0) {}
SwitchInfo::SwitchInfo(power::Switch_t switcher, ReturnValue_t initState)
: switcher(switcher), currentState(initState) {}
FuseInfo::FuseInfo(uint8_t fuse) : fuse(fuse) {}
void PowerSwitcherMock::getSwitchInfo(power::Switch_t switcher, SwitchInfo& info) {
if (switchMap.count(switcher) == 1) {
info = switchMap.at(switcher);
}
}
void PowerSwitcherMock::getFuseInfo(uint8_t fuse, FuseInfo& info) {
if (fuseMap.count(fuse) == 1) {
info = fuseMap.at(fuse);
}
}
uint32_t PowerSwitcherMock::getAmountSwitchStatWasRequested() {
return SWITCH_REQUEST_UPDATE_VALUE;
}
void PowerSwitcherMock::initSwitch(power::Switch_t switchNr) {
switchMap.emplace(switchNr, SwitchInfo(switchNr, PowerSwitchIF::SWITCH_OFF));
}

View File

@ -0,0 +1,52 @@
#ifndef FSFW_TESTS_SRC_FSFW_TESTS_UNIT_MOCKS_POWERSWITCHERMOCK_H_
#define FSFW_TESTS_SRC_FSFW_TESTS_UNIT_MOCKS_POWERSWITCHERMOCK_H_
#include <fsfw/power/PowerSwitchIF.h>
#include <map>
#include <utility>
struct SwitchInfo {
public:
SwitchInfo();
SwitchInfo(power::Switch_t switcher, ReturnValue_t initState);
power::Switch_t switcher;
ReturnValue_t currentState = PowerSwitchIF::SWITCH_OFF;
uint32_t timesCalledOn = 0;
uint32_t timesCalledOff = 0;
uint32_t timesStatusRequested = 0;
};
struct FuseInfo {
public:
FuseInfo(uint8_t fuse);
uint8_t fuse;
uint32_t timesCalled = 0;
};
class PowerSwitcherMock : public PowerSwitchIF {
public:
PowerSwitcherMock();
ReturnValue_t sendSwitchCommand(power::Switch_t switchNr, ReturnValue_t onOff) override;
ReturnValue_t sendFuseOnCommand(uint8_t fuseNr) override;
ReturnValue_t getSwitchState(power::Switch_t switchNr) const override;
ReturnValue_t getFuseState(uint8_t fuseNr) const override;
uint32_t getSwitchDelayMs(void) const override;
void getSwitchInfo(power::Switch_t switcher, SwitchInfo& info);
void getFuseInfo(uint8_t fuse, FuseInfo& info);
uint32_t getAmountSwitchStatWasRequested();
void initSwitch(power::Switch_t switchNr);
private:
using SwitchOnOffPair = std::pair<power::Switch_t, ReturnValue_t>;
using FuseOnOffPair = std::pair<uint8_t, ReturnValue_t>;
std::map<power::Switch_t, SwitchInfo> switchMap;
std::map<uint8_t, FuseInfo> fuseMap;
};
#endif /* FSFW_TESTS_SRC_FSFW_TESTS_UNIT_MOCKS_POWERSWITCHERMOCK_H_ */

View File

@ -0,0 +1,3 @@
target_sources(${FSFW_TEST_TGT} PRIVATE
testPowerSwitcher.cpp
)

View File

@ -0,0 +1,66 @@
#include <fsfw/power/DummyPowerSwitcher.h>
#include <fsfw/power/PowerSwitcher.h>
#include <fsfw_tests/unit/mocks/PowerSwitcherMock.h>
#include <catch2/catch_test_macros.hpp>
#include "objects/systemObjectList.h"
TEST_CASE("Power Switcher", "[power-switcher]") {
PowerSwitcherMock mock;
PowerSwitcher switcher(&mock, 1);
DummyPowerSwitcher dummySwitcher(objects::DUMMY_POWER_SWITCHER, 5, 5, false);
PowerSwitcher switcherUsingDummy(&dummySwitcher, 1);
SwitchInfo switchInfo;
mock.initSwitch(1);
SECTION("Basic Tests") {
REQUIRE(switcher.getFirstSwitch() == 1);
REQUIRE(switcher.getSecondSwitch() == power::NO_SWITCH);
// Default start state
REQUIRE(switcher.getState() == PowerSwitcher::SWITCH_IS_OFF);
switcher.turnOn(true);
REQUIRE(mock.getAmountSwitchStatWasRequested() == 1);
REQUIRE(switcher.getState() == PowerSwitcher::WAIT_ON);
REQUIRE(switcher.active());
switcher.doStateMachine();
REQUIRE(switcher.getState() == PowerSwitcher::SWITCH_IS_ON);
mock.getSwitchInfo(1, switchInfo);
REQUIRE(switchInfo.timesCalledOn == 1);
REQUIRE(not switcher.active());
REQUIRE(mock.getAmountSwitchStatWasRequested() == 2);
switcher.turnOff(false);
REQUIRE(mock.getAmountSwitchStatWasRequested() == 2);
REQUIRE(switcher.getState() == PowerSwitcher::WAIT_OFF);
REQUIRE(switcher.active());
REQUIRE(switcher.getState() == PowerSwitcher::WAIT_OFF);
switcher.doStateMachine();
mock.getSwitchInfo(1, switchInfo);
REQUIRE(switcher.getState() == PowerSwitcher::SWITCH_IS_OFF);
REQUIRE(switchInfo.timesCalledOn == 1);
REQUIRE(switchInfo.timesCalledOff == 1);
REQUIRE(not switcher.active());
REQUIRE(mock.getAmountSwitchStatWasRequested() == 3);
}
SECTION("Dummy Test") {
// Same tests, but we can't really check the dummy
REQUIRE(switcherUsingDummy.getFirstSwitch() == 1);
REQUIRE(switcherUsingDummy.getSecondSwitch() == power::NO_SWITCH);
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::SWITCH_IS_OFF);
switcherUsingDummy.turnOn(true);
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::WAIT_ON);
REQUIRE(switcherUsingDummy.active());
switcherUsingDummy.doStateMachine();
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::SWITCH_IS_ON);
REQUIRE(not switcherUsingDummy.active());
switcherUsingDummy.turnOff(false);
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::WAIT_OFF);
REQUIRE(switcherUsingDummy.active());
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::WAIT_OFF);
switcherUsingDummy.doStateMachine();
REQUIRE(switcherUsingDummy.getState() == PowerSwitcher::SWITCH_IS_OFF);
REQUIRE(not switcherUsingDummy.active());
}
}

View File

@ -21,8 +21,9 @@ enum sourceObjects : uint32_t {
HK_RECEIVER_MOCK = 22,
TEST_LOCAL_POOL_OWNER_BASE = 25,
SHARED_SET_ID = 26
SHARED_SET_ID = 26,
DUMMY_POWER_SWITCHER = 27
};
}