I am unit testing the following function, using Ceedling.
bool PRM_inputVoltageIsGood(ParameterHandle_t p, float voltage) {
static bool voltageGoodHyst = false;
/* Check if voltage has risen above ON point. */
if ((!voltageGoodHyst) && (voltage > p->vin_on)) {
voltageGoodHyst = true;
}
/* Check if voltage has fallen below OFF point. */
else if ((voltageGoodHyst) && (voltage < p->vin_off)) {
voltageGoodHyst = false;
}
return voltageGoodHyst;
}
With tests like the following:
void test_PRM_inputVoltageIsGood_should_stayAtFalse_before_risingOverVInOn(void)
{
float i;
p->vin_on = 10.0;
p->vin_off = 5.0;
for (i = 0; i <= p->vin_on; ++i)
TEST_ASSERT_FALSE(PRM_inputVoltageIsGood(p, i));
}
void test_PRM_inputVoltageIsGood_should_switchToTrue_when_risingOverVInOn(void)
{
p->vin_on = 10.0;
p->vin_off = 5.0;
TEST_ASSERT_FALSE(PRM_inputVoltageIsGood(p, p->vin_on));
TEST_ASSERT_TRUE(PRM_inputVoltageIsGood(p, (p->vin_on + 1)));
}
void test_PRM_inputVoltageIsGood_should_stayAtTrue_before_fallingUnderVInOff(void)
{
float i = 0;
p->vin_on = 10.0;
p->vin_off = 5.0;
for (i = p->vin_on + 1; i >= p->vin_off; --i)
TEST_ASSERT_TRUE(PRM_inputVoltageIsGood(p, i));
}
void test_PRM_inputVoltageIsGood_should_switchToFalse_when_fallingUnderVInOff(void)
{
p->vin_on = 10.0;
p->vin_off = 5.0;
TEST_ASSERT_TRUE(PRM_inputVoltageIsGood(p, p->vin_off));
TEST_ASSERT_FALSE(PRM_inputVoltageIsGood(p, (p->vin_off - 1)));
}
The function has internal state in the form of the function-scope static variable voltageGoodhyst
(because the output is hysteretic).
Because of the hysteresis my tests have to occur in a certain order. which is bad. I kind of want to avoid having to make the variable have any wider scope as this is the only function concerned with this functionality (the object pointed to by ParametersHandle_t
conforms to a specific layout so I can't add it there either)... Is there any other way I can adjust this or write tests so that they are not required to execute in a specific order?