- Removed unnecessary calls to relay_chn_create and g_is_component_initialized in multiple test cases across test_relay_chn_core_single.c, test_relay_chn_listener_multi.c, and test_relay_chn_listener_single.c. - Introduced new test files for NVS functionality: test_relay_chn_nvs_multi.c and test_relay_chn_nvs_single.c, covering initialization, direction setting, invalid parameters, and erase operations. - Updated partition table configuration to support NVS storage, including the addition of a new partition file part_nvs.csv. - Adjusted sdkconfig files to enable NVS support and configure custom partition settings for relay channels.
140 lines
4.4 KiB
C
140 lines
4.4 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include "unity.h"
|
|
#include "esp_system.h"
|
|
#include "nvs_flash.h"
|
|
#include "relay_chn_nvs.h"
|
|
|
|
TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
// Test all channels
|
|
relay_chn_direction_t dir;
|
|
relay_chn_direction_t test_directions[] = {
|
|
RELAY_CHN_DIRECTION_DEFAULT,
|
|
RELAY_CHN_DIRECTION_FLIPPED
|
|
};
|
|
|
|
for (int channel = 0; channel < 2; channel++) {
|
|
TEST_ESP_OK(relay_chn_nvs_set_direction(channel, test_directions[channel]));
|
|
TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir));
|
|
TEST_ASSERT_EQUAL(test_directions[channel], dir);
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
TEST_CASE("Test invalid parameters", "[relay_chn][nvs]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
// Test NULL pointer for all channels
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL));
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
// Store some test data first
|
|
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED;
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
|
|
}
|
|
|
|
#ifdef RELAY_CHN_ENABLE_TILTING
|
|
uint8_t sensitivity = 50;
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity));
|
|
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200));
|
|
}
|
|
#endif
|
|
|
|
// Test erase all
|
|
TEST_ESP_OK(relay_chn_nvs_erase_all());
|
|
|
|
// Verify data was erased by trying to read it back
|
|
relay_chn_direction_t read_direction;
|
|
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction));
|
|
|
|
#ifdef RELAY_CHN_ENABLE_TILTING
|
|
uint8_t read_sensitivity;
|
|
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
|
|
|
|
uint32_t fwd_count, rev_count;
|
|
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count));
|
|
#endif
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
#ifdef RELAY_CHN_ENABLE_TILTING
|
|
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
const uint8_t test_sensitivity = 75;
|
|
uint8_t sensitivity;
|
|
|
|
// Test all channels
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, test_sensitivity));
|
|
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(channel, &sensitivity));
|
|
TEST_ASSERT_EQUAL(test_sensitivity, sensitivity);
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
const uint32_t fwd_count = 100;
|
|
const uint32_t rev_count = 200;
|
|
uint32_t fwd_read, rev_read;
|
|
|
|
// Test all channels
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
// Test setting counters
|
|
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, fwd_count, rev_count));
|
|
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &fwd_read, &rev_read));
|
|
TEST_ASSERT_EQUAL(fwd_count, fwd_read);
|
|
TEST_ASSERT_EQUAL(rev_count, rev_read);
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
|
|
TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
|
|
{
|
|
TEST_ESP_OK(relay_chn_nvs_init());
|
|
|
|
uint32_t fwd_count, rev_count;
|
|
|
|
// Test NULL pointers for all channels
|
|
for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) {
|
|
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL));
|
|
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, &rev_count));
|
|
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, &fwd_count, NULL));
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
#endif // RELAY_CHN_ENABLE_TILTING
|