tests: app: migrate unit tests from Unity to ztest

Convert the application unit tests to ztest, so that all tests can be
run with twister and the Unity module is not required anymore.

Each test file becomes a ztest suite. The test names got the prefix
test_ as required by twister. Unity assertions were converted as
follows:

- TEST_ASSERT_EQUAL_FLOAT is replaced by assert_float_equal, which uses
  the same relative precision of 1e-5 as Unity.
- TEST_ASSERT_EQUAL and other integer assertions are replaced by their
  ztest equivalents. Unity compared these as integers, so float values
  were truncated before. All tests still pass with exact comparison.

In contrast to Unity, ztest runs the tests of a suite in alphabetical
order. The setup code for the DAQ tests is moved into a before function
and the half bridge is stopped before each buck converter test (same as
for the boost converter tests), as the buck tests previously relied on
running before the boost tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-10-21 13:40:14 +02:00
co-authored by Claude Opus 5.5
parent a3f888fd79
commit a1fff4ee6f
11 changed files with 335 additions and 541 deletions
+90 -150
View File
@@ -16,6 +16,8 @@
static void init_structs_buck(int num_batteries = 1)
{
half_bridge_stop(dcdc.half_bridge);
dev_stat.error_flags = 0;
hv_terminal.init_solar();
hv_terminal.bus->voltage = 20 * num_batteries;
@@ -51,7 +53,7 @@ static void start_buck()
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
TEST_ASSERT_EQUAL(DCDC_CONTROL_MPPT, dcdc.state);
zassert_equal(dcdc.state, DCDC_CONTROL_MPPT);
}
static void init_structs_boost(int num_batteries = 1)
@@ -92,139 +94,139 @@ static void start_boost()
dcdc.control();
dcdc.control();
dcdc.control(); // call multiple times because of startup delay
TEST_ASSERT_EQUAL(DCDC_CONTROL_MPPT, dcdc.state);
zassert_equal(dcdc.state, DCDC_CONTROL_MPPT);
}
void start_valid_mppt_buck()
ZTEST(dcdc, test_start_valid_mppt_buck)
{
init_structs_buck();
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
}
void start_valid_mppt_buck_dual_battery()
ZTEST(dcdc, test_start_valid_mppt_buck_dual_battery)
{
init_structs_buck(2);
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
}
void start_valid_mppt_boost()
ZTEST(dcdc, test_start_valid_mppt_boost)
{
init_structs_boost();
TEST_ASSERT_EQUAL(DCDC_MODE_BOOST, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BOOST);
}
void start_valid_mppt_boost_dual_battery()
ZTEST(dcdc, test_start_valid_mppt_boost_dual_battery)
{
init_structs_boost(2);
TEST_ASSERT_EQUAL(DCDC_MODE_BOOST, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BOOST);
}
void no_start_before_restart_delay()
ZTEST(dcdc, test_no_start_before_restart_delay)
{
init_structs_buck();
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
dcdc.off_timestamp = time(NULL) - dcdc.restart_interval;
TEST_ASSERT_EQUAL(DCDC_MODE_BUCK, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_BUCK);
}
void no_start_if_dcdc_disabled()
ZTEST(dcdc, test_no_start_if_dcdc_disabled)
{
init_structs_buck();
dcdc.enable = false;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_start_if_dcdc_lv_voltage_low()
ZTEST(dcdc, test_no_start_if_dcdc_lv_voltage_low)
{
init_structs_buck();
lv_terminal.bus->voltage = dcdc.ls_voltage_min - 0.5f;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
// buck
void no_buck_start_if_bat_voltage_high()
ZTEST(dcdc, test_no_buck_start_if_bat_voltage_high)
{
init_structs_buck();
lv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1f;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_buck_start_if_bat_chg_not_allowed()
ZTEST(dcdc, test_no_buck_start_if_bat_chg_not_allowed)
{
init_structs_buck();
lv_terminal.bus->sink_current_margin = 0;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_buck_start_if_solar_voltage_high()
ZTEST(dcdc, test_no_buck_start_if_solar_voltage_high)
{
init_structs_buck();
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 1;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_buck_start_if_solar_voltage_low()
ZTEST(dcdc, test_no_buck_start_if_solar_voltage_low)
{
init_structs_buck();
hv_terminal.bus->voltage = 17;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
// boost
void no_boost_start_if_bat_voltage_high()
ZTEST(dcdc, test_no_boost_start_if_bat_voltage_high)
{
init_structs_boost();
hv_terminal.bus->voltage = bat_conf.topping_voltage + 0.1f;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_boost_start_if_bat_chg_not_allowed()
ZTEST(dcdc, test_no_boost_start_if_bat_chg_not_allowed)
{
init_structs_boost();
hv_terminal.bus->sink_current_margin = 0;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_boost_start_if_solar_voltage_high()
ZTEST(dcdc, test_no_boost_start_if_solar_voltage_high)
{
init_structs_boost();
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 1;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
void no_boost_start_if_solar_voltage_low()
ZTEST(dcdc, test_no_boost_start_if_solar_voltage_low)
{
init_structs_boost();
lv_terminal.bus->voltage = 17;
TEST_ASSERT_EQUAL(DCDC_MODE_OFF, dcdc.check_start_conditions());
zassert_equal(dcdc.check_start_conditions(), DCDC_MODE_OFF);
}
// buck operation
void buck_increasing_power()
ZTEST(dcdc, test_buck_increasing_power)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after > pwm_before);
zassert_true(pwm_after > pwm_before);
}
void buck_derating_output_voltage_too_high()
ZTEST(dcdc, test_buck_derating_output_voltage_too_high)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
lv_terminal.bus->voltage = lv_terminal.bus->sink_voltage_intercept + 0.1f;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
zassert_true(pwm_after < pwm_before); // less duty cycle = higher voltage
zassert_equal(dcdc.state, DCDC_CONTROL_CV_LS);
}
void buck_derating_output_current_too_high()
ZTEST(dcdc, test_buck_derating_output_current_too_high)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
@@ -232,22 +234,22 @@ void buck_derating_output_current_too_high()
lv_terminal.update_bus_current_margins();
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_LS);
}
void buck_derating_inductor_current_too_high()
ZTEST(dcdc, test_buck_derating_inductor_current_too_high)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.inductor_current = dcdc.inductor_current_max + 0.1f;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_LS, dcdc.state);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_LS);
}
void buck_derating_input_voltage_too_low()
ZTEST(dcdc, test_buck_derating_input_voltage_too_low)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
@@ -255,11 +257,11 @@ void buck_derating_input_voltage_too_low()
dcdc.power = 1.2f;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_HS, dcdc.state);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CV_HS);
}
void buck_derating_input_current_too_high()
ZTEST(dcdc, test_buck_derating_input_current_too_high)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
@@ -267,37 +269,37 @@ void buck_derating_input_current_too_high()
hv_terminal.update_bus_current_margins();
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
TEST_ASSERT_EQUAL(DCDC_CONTROL_CC_HS, dcdc.state);
zassert_true(pwm_after < pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CC_HS);
}
void buck_derating_temperature_limits_exceeded()
ZTEST(dcdc, test_buck_derating_temperature_limits_exceeded)
{
start_buck();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.temp_mosfets = 81;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
zassert_true(pwm_after < pwm_before);
}
void buck_stop_input_power_too_low()
ZTEST(dcdc, test_buck_stop_input_power_too_low)
{
start_buck();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc.control();
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
void buck_stop_high_voltage_emergency()
ZTEST(dcdc, test_buck_stop_high_voltage_emergency)
{
start_buck();
lv_terminal.bus->voltage = dcdc.ls_voltage_max + 0.1f;
dcdc.control();
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
void buck_correct_mppt_operation()
ZTEST(dcdc, test_buck_correct_mppt_operation)
{
start_buck();
@@ -307,37 +309,37 @@ void buck_correct_mppt_operation()
dcdc.power = 7;
dcdc.control();
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm2 > pwm1);
zassert_true(pwm2 > pwm1);
dcdc.power = 6; // decrease power to make the direction turn around
dcdc.control();
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm3 < pwm2);
zassert_true(pwm3 < pwm2);
}
// boost operation
void boost_increasing_power()
ZTEST(dcdc, test_boost_increasing_power)
{
// make sure that without changing anything, dcdc_control goes in direction of increasing power
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after < pwm_before);
zassert_true(pwm_after < pwm_before);
}
void boost_derating_output_voltage_too_high()
ZTEST(dcdc, test_boost_derating_output_voltage_too_high)
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
hv_terminal.bus->voltage = hv_terminal.bus->sink_voltage_intercept + 0.5f;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power
zassert_true(pwm_after > pwm_before); // higher duty cycle = less power
}
void boost_derating_output_current_too_high()
ZTEST(dcdc, test_boost_derating_output_current_too_high)
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
@@ -345,10 +347,10 @@ void boost_derating_output_current_too_high()
hv_terminal.update_bus_current_margins();
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after > pwm_before);
zassert_true(pwm_after > pwm_before);
}
void boost_derating_input_voltage_too_low()
ZTEST(dcdc, test_boost_derating_input_voltage_too_low)
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
@@ -357,47 +359,47 @@ void boost_derating_input_voltage_too_low()
hv_terminal.update_bus_current_margins();
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT_EQUAL(DCDC_CONTROL_CV_LS, dcdc.state);
TEST_ASSERT(pwm_after > pwm_before);
zassert_equal(dcdc.state, DCDC_CONTROL_CV_LS);
zassert_true(pwm_after > pwm_before);
}
void boost_derating_input_current_too_high()
ZTEST(dcdc, test_boost_derating_input_current_too_high)
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.inductor_current = lv_terminal.neg_current_limit - 0.1f;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after > pwm_before);
zassert_true(pwm_after > pwm_before);
}
void boost_derating_temperature_limits_exceeded()
ZTEST(dcdc, test_boost_derating_temperature_limits_exceeded)
{
start_boost();
float pwm_before = half_bridge_get_duty_cycle(dcdc.half_bridge);
dcdc.temp_mosfets = 81;
dcdc.control();
float pwm_after = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm_after > pwm_before);
zassert_true(pwm_after > pwm_before);
}
void boost_stop_input_power_too_low()
ZTEST(dcdc, test_boost_stop_input_power_too_low)
{
start_boost();
dcdc.power_good_timestamp = time(NULL) - 11;
dcdc.control();
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
void boost_stop_high_voltage_emergency()
ZTEST(dcdc, test_boost_stop_high_voltage_emergency)
{
start_boost();
hv_terminal.bus->voltage = dcdc.hs_voltage_max + 0.1f;
dcdc.control();
TEST_ASSERT(half_bridge_is_enabled(dcdc.half_bridge) == false);
zassert_true(half_bridge_is_enabled(dcdc.half_bridge) == false);
}
void boost_correct_mppt_operation()
ZTEST(dcdc, test_boost_correct_mppt_operation)
{
start_boost();
@@ -407,15 +409,15 @@ void boost_correct_mppt_operation()
dcdc.power = -7;
dcdc.control();
float pwm2 = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm2 < pwm1);
zassert_true(pwm2 < pwm1);
dcdc.power = -6; // decrease power to make the direction turn around
dcdc.control();
float pwm3 = half_bridge_get_duty_cycle(dcdc.half_bridge);
TEST_ASSERT(pwm3 > pwm2);
zassert_true(pwm3 > pwm2);
}
void fuse_destruction_forces_low_side_on()
ZTEST(dcdc, test_fuse_destruction_forces_low_side_on)
{
start_buck();
dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT);
@@ -424,87 +426,25 @@ void fuse_destruction_forces_low_side_on()
for (int i = 0; i < 21; i++) {
dcdc.fuse_destruction();
}
TEST_ASSERT_NOT_EQUAL(0, half_bridge_get_compare(dcdc.half_bridge));
zassert_not_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
dcdc.fuse_destruction();
TEST_ASSERT_TRUE(half_bridge_is_enabled(dcdc.half_bridge));
TEST_ASSERT_EQUAL(0, half_bridge_get_compare(dcdc.half_bridge));
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
// control loop must not override the forced low side
dcdc.control();
TEST_ASSERT_TRUE(half_bridge_is_enabled(dcdc.half_bridge));
TEST_ASSERT_EQUAL(0, half_bridge_get_compare(dcdc.half_bridge));
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
// forced state is restored after the half bridge was stopped
dcdc.stop();
dcdc.fuse_destruction();
TEST_ASSERT_TRUE(half_bridge_is_enabled(dcdc.half_bridge));
TEST_ASSERT_EQUAL(0, half_bridge_get_compare(dcdc.half_bridge));
zassert_true(half_bridge_is_enabled(dcdc.half_bridge));
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
dcdc.stop();
dev_stat.clear_error(ERR_DCDC_HS_MOSFET_SHORT);
}
int dcdc_tests()
{
UNITY_BEGIN();
// 1. Check of general (re)start conditions
// check if initialization of test is correct and we start at all
RUN_TEST(start_valid_mppt_buck);
RUN_TEST(start_valid_mppt_buck_dual_battery);
RUN_TEST(start_valid_mppt_boost);
RUN_TEST(start_valid_mppt_boost_dual_battery);
RUN_TEST(no_start_before_restart_delay);
RUN_TEST(no_start_if_dcdc_disabled);
RUN_TEST(no_start_if_dcdc_lv_voltage_low);
// 2. Check startup for MPPT buck converter scenario
RUN_TEST(no_buck_start_if_bat_voltage_high);
RUN_TEST(no_buck_start_if_bat_chg_not_allowed);
RUN_TEST(no_buck_start_if_solar_voltage_high);
RUN_TEST(no_buck_start_if_solar_voltage_low);
// 3. Check startup for MPPT boost converter scenario
RUN_TEST(no_boost_start_if_bat_voltage_high);
RUN_TEST(no_boost_start_if_bat_chg_not_allowed);
RUN_TEST(no_boost_start_if_solar_voltage_high);
RUN_TEST(no_boost_start_if_solar_voltage_low);
// 4. Check startup for nanogrid scenario
// TODO
// 5. Check DC/DC control after being started
// buck mode
RUN_TEST(buck_increasing_power);
RUN_TEST(buck_derating_output_voltage_too_high);
RUN_TEST(buck_derating_output_current_too_high);
RUN_TEST(buck_derating_inductor_current_too_high);
RUN_TEST(buck_derating_input_voltage_too_low);
RUN_TEST(buck_derating_input_current_too_high);
RUN_TEST(buck_derating_temperature_limits_exceeded);
RUN_TEST(buck_stop_input_power_too_low);
RUN_TEST(buck_stop_high_voltage_emergency);
RUN_TEST(buck_correct_mppt_operation);
// boost mode
RUN_TEST(boost_increasing_power);
RUN_TEST(boost_derating_output_voltage_too_high);
RUN_TEST(boost_derating_output_current_too_high);
RUN_TEST(boost_derating_input_voltage_too_low);
RUN_TEST(boost_derating_input_current_too_high);
RUN_TEST(boost_derating_temperature_limits_exceeded);
RUN_TEST(boost_stop_input_power_too_low);
RUN_TEST(boost_stop_high_voltage_emergency);
RUN_TEST(boost_correct_mppt_operation);
RUN_TEST(fuse_destruction_forces_low_side_on);
return UNITY_END();
}
ZTEST_SUITE(dcdc, NULL, NULL, NULL, NULL, NULL);