Add and execute unit tests for run limit feature

This commit is contained in:
2025-08-22 12:42:53 +03:00
parent 29803c063e
commit fb4f34e895
6 changed files with 232 additions and 9 deletions

View File

@@ -28,6 +28,7 @@ void tearDown()
reset_channels_to_idle_state();
}
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
static void test_nvs_flash_init(void)
{
esp_err_t ret;
@@ -52,9 +53,11 @@ static void test_nvs_flash_init(void)
}
}
#endif
TEST_ESP_OK(ret);
TEST_ESP_OK(ret);
}
#endif
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
static void test_nvs_flash_deinit(void)
{
esp_err_t ret;
@@ -65,11 +68,14 @@ static void test_nvs_flash_deinit(void)
#endif
TEST_ESP_OK(ret);
}
#endif
void app_main(void)
{
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
// Init NVS once for all tests
test_nvs_flash_init();
#endif
// Create relay_chn once for all tests
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
@@ -92,8 +98,10 @@ void app_main(void)
// Destroy relay_chn
relay_chn_destroy();
#if CONFIG_RELAY_CHN_ENABLE_NVS == 1
// Deinit NVS
test_nvs_flash_deinit();
#endif
ESP_LOGI(TEST_TAG, "All tests complete.");

View File

@@ -344,4 +344,100 @@ TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][c
relay_chn_flip_direction(invalid_ch); // Call with an invalid ID
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(invalid_ch));
}
}
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#define TEST_RUN_LIMIT_SEC 5
#define TEST_SHORT_RUN_LIMIT_SEC 2
// ### Run Limit Tests
TEST_CASE("Test run limit initialization", "[relay_chn][run_limit]")
{
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Should initialize with default value
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC, relay_chn_get_run_limit(i));
}
}
TEST_CASE("Test run limit setting boundaries", "[relay_chn][run_limit]")
{
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Test minimum boundary
relay_chn_set_run_limit(i, CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC - 1);
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC, relay_chn_get_run_limit(i));
// Test maximum boundary
relay_chn_set_run_limit(i, CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC + 1);
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC, relay_chn_get_run_limit(i));
// Test valid value
relay_chn_set_run_limit(i, TEST_RUN_LIMIT_SEC);
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit(i));
}
}
TEST_CASE("Test run limit stops channel after timeout", "[relay_chn][run_limit]")
{
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Set a short run limit for testing
relay_chn_set_run_limit(i, TEST_SHORT_RUN_LIMIT_SEC);
}
relay_chn_run_forward(RELAY_CHN_ID_ALL);
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Check running forward
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
}
// Wait for run limit timeout
vTaskDelay(pdMS_TO_TICKS(TEST_SHORT_RUN_LIMIT_SEC * 1000 + test_delay_margin_ms));
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
}
}
TEST_CASE("Test run limit reset on direction change and time out finally", "[relay_chn][run_limit]")
{
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Set a short run limit
relay_chn_set_run_limit(i, TEST_SHORT_RUN_LIMIT_SEC);
// Start running forward
relay_chn_run_forward(i);
}
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait 1 second
// Change direction before timeout
relay_chn_run_reverse(RELAY_CHN_ID_ALL);
// Wait for the inertia period (after which the reverse command will be dispatched)
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state(i));
}
// Timer should time out and stop the channel after the run limit time
vTaskDelay(pdMS_TO_TICKS(TEST_SHORT_RUN_LIMIT_SEC * 1000 + test_delay_margin_ms));
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(i));
}
}
TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit]")
{
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
// Set initial run limit
relay_chn_set_run_limit(i, TEST_RUN_LIMIT_SEC);
// Stop and start channel
relay_chn_stop(i);
relay_chn_run_forward(i);
// Run limit should persist
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit(i));
}
}
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1

View File

@@ -171,4 +171,80 @@ TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction());
}
}
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#define TEST_RUN_LIMIT_SEC 5
#define TEST_SHORT_RUN_LIMIT_SEC 2
// ### Run Limit Tests
TEST_CASE("Test run limit initialization", "[relay_chn][run_limit]")
{
// Should initialize with default value
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC, relay_chn_get_run_limit());
}
TEST_CASE("Test run limit setting boundaries", "[relay_chn][run_limit]")
{
// Test minimum boundary
relay_chn_set_run_limit(CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC - 1);
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_MIN_SEC, relay_chn_get_run_limit());
// Test maximum boundary
relay_chn_set_run_limit(CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC + 1);
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_MAX_SEC, relay_chn_get_run_limit());
// Test valid value
relay_chn_set_run_limit(TEST_RUN_LIMIT_SEC);
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit());
}
TEST_CASE("Test run limit stops channel after timeout", "[relay_chn][run_limit]")
{
// Set a short run limit for testing
relay_chn_set_run_limit(TEST_SHORT_RUN_LIMIT_SEC);
// Start running forward
relay_chn_run_forward();
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state());
// Wait for run limit timeout
vTaskDelay(pdMS_TO_TICKS(TEST_SHORT_RUN_LIMIT_SEC * 1000 + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state());
}
TEST_CASE("Test run limit reset on direction change and time out finally", "[relay_chn][run_limit]")
{
// Set a short run limit
relay_chn_set_run_limit(TEST_SHORT_RUN_LIMIT_SEC);
// Start running forward
relay_chn_run_forward();
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait 1 second
// Change direction before timeout
relay_chn_run_reverse();
// Wait for the inertia period (after which the reverse command will be dispatched)
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
// Timer should time out and stop the channel after the run limit time
vTaskDelay(pdMS_TO_TICKS(TEST_SHORT_RUN_LIMIT_SEC * 1000 + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state());
}
TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit]")
{
// Set initial run limit
relay_chn_set_run_limit(TEST_RUN_LIMIT_SEC);
// Stop and start channel
relay_chn_stop();
relay_chn_run_forward();
// Run limit should persist
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit());
}
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1

View File

@@ -84,6 +84,23 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#ifdef CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint16_t run_limit_sec = 32;
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ESP_OK(relay_chn_nvs_set_run_limit(i, run_limit_sec));
uint16_t run_limit_read;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read));
TEST_ASSERT_EQUAL(run_limit_sec, run_limit_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif
#ifdef RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{

View File

@@ -77,6 +77,22 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#ifdef CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint16_t run_limit_sec = 32;
TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, run_limit_sec));
uint16_t run_limit_read;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &run_limit_read));
TEST_ASSERT_EQUAL(run_limit_sec, run_limit_read);
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif
#ifdef RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{