- Optimized tilt counting data by reducing the tilt counter variables into one for smaller memory footprint. So the `relay_chn_tilt_counter_t` type is replaced by a single `uint16_t` variable in the `relay_chn_tilt_ctl_t` structure. Hence the `relay_chn_tilt_counter_t` type has been removed since it is not necessary anymore. - Refactored tilt count handling in NVS: consolidate forward and reverse counts into a single tilt count parameter. - Updated NVS test files that affected by the data type and function signature changes. Fixes #1079
135 lines
4.1 KiB
C
135 lines
4.1 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));
|
|
}
|
|
#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));
|
|
|
|
uint16_t tilt_count;
|
|
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_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 uint16_t tilt_count = 100;
|
|
uint16_t tilt_count_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, tilt_count));
|
|
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &tilt_count_read));
|
|
TEST_ASSERT_EQUAL(tilt_count, tilt_count_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());
|
|
|
|
// 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));
|
|
}
|
|
|
|
TEST_ESP_OK(relay_chn_nvs_deinit());
|
|
}
|
|
#endif // RELAY_CHN_ENABLE_TILTING
|