2

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?

Toby
  • 9,696
  • 16
  • 68
  • 132

1 Answers1

1

One tactic would be to create your tests such that after you run them, the static variable will have the same value.

For example, since it's initialized as static bool voltageGoodHyst = false;, every time you run a unit test that changes it to true, send a final request PRM_inputVoltageIsGood(p, 0) to make the static variable false.

This way, regardless of the order the unit tests execute, you will know what the static variable is false when you enter the next test.

schil227
  • 292
  • 3
  • 11