Skip to content

Commit

Permalink
Merge pull request #6 from QuangHaiNguyen/rework_unit_tests
Browse files Browse the repository at this point in the history
Rework on unit tests
  • Loading branch information
QuangHaiNguyen authored Mar 7, 2024
2 parents f262a76 + cbffab0 commit 05923e8
Show file tree
Hide file tree
Showing 33 changed files with 1,829 additions and 2,029 deletions.
2 changes: 1 addition & 1 deletion easy_embedded/app/data_model/ez_data_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void DataModel_ReleaseDataPoint(DataPoint data_point);
* @see DataModel_Initialization, DataModel_CreateDataPoint
*
*****************************************************************************/
bool DataModel_ReadDataPoint(DataPoint data_point,
bool DataModel_WriteDataPoint(DataPoint data_point,
void* data,
uint32_t size);

Expand Down
56 changes: 24 additions & 32 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@
#build the unit tests
add_executable(ez_unit_test)
# ----------------------------------------------------------------------------
# Author: Hai Nguyen
# License: This file is published under the license described in LICENSE.md
# Description: CMake file to build unit tests
# ----------------------------------------------------------------------------

message(STATUS "************************************")
message(STATUS "* Building unit tests")
message(STATUS "************************************")

#include directory
target_include_directories(ez_unit_test PRIVATE
.
../ezmsdk
)

target_link_libraries(ez_unit_test PUBLIC unity easy_embedded_lib)

# activate test depend on the activated module
if(ENABLE_DATA_MODEL)
add_subdirectory(app/data_model)
endif()

if(ENABLE_EZ_EVENT_NOTIFIER)
message(STATUS "Test for event notifier is activated")
add_subdirectory(service/event_notifier)
endif()

if(ENABLE_EZ_KERNEL)
message(STATUS "Test for kernel is activated")
add_subdirectory(service/kernel)
endif()

if(ENABLE_EZ_QUEUE)
message(STATUS "Test for queue is activated")
add_subdirectory(utilities/queue)
endif()

if(ENABLE_EZ_HAL_ECHO)
message(STATUS "Test for hal echo is activated")
add_subdirectory(service/driver/hal_echo)
endif()

if(ENABLE_EZ_HAL_UART)
message(STATUS "Test for hal uart is activated")
add_subdirectory(service/driver/hal_uart)
endif()

# Service module
target_sources(ez_unit_test PRIVATE
main.c
)
if(ENABLE_EZ_QUEUE)
add_subdirectory(utilities/queue)
endif()

if(ENABLE_EZ_LINKEDLIST)
add_subdirectory(utilities/linked_list)
endif()

target_link_libraries(ez_unit_test PRIVATE
$<$<BOOL:${ENABLE_EZ_EVENT_NOTIFIER}>:ez_unit_test_event_notifier>
$<$<BOOL:${ENABLE_EZ_QUEUE}>:ez_unit_test_queue>
$<$<BOOL:${ENABLE_EZ_KERNEL}>:ez_unit_test_kernel>
$<$<BOOL:${ENABLE_EZ_HAL_ECHO}>:ez_unit_test_hal_echo>
$<$<BOOL:${ENABLE_EZ_HAL_UART}>:ez_unit_test_hal_uart>
)
if(ENABLE_EZ_STATIC_ALLOC)
add_subdirectory(utilities/static_alloc)
endif()

if(ENABLE_EZ_RING_BUFFER)
add_subdirectory(utilities/ring_buffer)
endif()

add_test(NAME ez_unit_test
COMMAND ez_unit_test
)
# End of file
55 changes: 55 additions & 0 deletions tests/app/data_model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ----------------------------------------------------------------------------
# Author: Hai Nguyen
# Name: ez_data_model_test
# License: This file is published under the license described in LICENSE.md
# Description: CMake file for data_model component unit test
# ----------------------------------------------------------------------------

add_executable(ez_data_model_test)

message(STATUS "**********************************************************")
message(STATUS "* Generating ez_data_model_test build files")
message(STATUS "**********************************************************")


# Source files ---------------------------------------------------------------
target_sources(ez_data_model_test
PRIVATE
unittest_ez_data_model.c
)


# Definitions ----------------------------------------------------------------
target_compile_definitions(ez_data_model_test
PUBLIC
# Please add definitions here
)


# Include directory -----------------------------------------------------------
target_include_directories(ez_data_model_test
PUBLIC
# Please add private folders here
PRIVATE
# Please add private folders here
INTERFACE
# Please add interface folders here
)


# Link libraries -------------------------------------------------------------
target_link_libraries(ez_data_model_test
PUBLIC
# Please add public libraries
PRIVATE
unity
easy_embedded_lib
INTERFACE
# Please add interface libraries
)

add_test(NAME ez_data_model_test
COMMAND ez_data_model_test
)

# End of file
160 changes: 160 additions & 0 deletions tests/app/data_model/unittest_ez_data_model.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*****************************************************************************
* Filename: unittest_ez_data_model.c
* Author: Hai Nguyen
* Original Date: 07.03.2024
*
* ----------------------------------------------------------------------------
* Contact: Hai Nguyen
* hainguyen.eeit@gmail.com
*
* ----------------------------------------------------------------------------
* License: This file is published under the license described in LICENSE.md
*
*****************************************************************************/

/** @file unittest_ez_data_model.c
* @author Hai Nguyen
* @date 07.03.2024
* @brief Unit test for data model component
*
* @details
*
*/

/******************************************************************************
* Includes
*******************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "unity.h"
#include "unity_fixture.h"
#include "ez_data_model.h"
#include "ez_event_notifier.h"

TEST_GROUP(ez_data_model);

/******************************************************************************
* Module Preprocessor Macros
*******************************************************************************/
/* None */


/******************************************************************************
* Module Typedefs
*******************************************************************************/
/* None */


/******************************************************************************
* Module Variable Definitions
*******************************************************************************/
static uint32_t test_data_1 = 0;
static char test_data_2[16] = { 0 };


/******************************************************************************
* Function Definitions
*******************************************************************************/
static void RunAllTests(void);
int DataPointModifyCallback(uint32_t event_code, void* param1, void* param2);


/******************************************************************************
* External functions
*******************************************************************************/
int main(int argc, const char *argv[])
{
return UnityMain(argc, argv, RunAllTests);
}


TEST_SETUP(ez_data_model)
{
DataModel_Initialization();
}


TEST_TEAR_DOWN(ez_data_model)
{
}


TEST_GROUP_RUNNER(ez_data_model)
{
RUN_TEST_CASE(ez_data_model, simple_read_write_test);
}


TEST(ez_data_model, simple_read_write_test)
{
bool is_success = true;

DataPoint data_point_1;
DataPoint data_point_2;

ezObserver obs1;
ezObserver obs2;

uint32_t helper_1;
char helper_2[16] = { 0 };

data_point_1 = DataModel_CreateDataPoint((void*)&test_data_1, sizeof(test_data_1));
TEST_ASSERT_NOT_EQUAL(data_point_1, DATA_POINT_INVALID);

data_point_2 = DataModel_CreateDataPoint((void*)test_data_2, sizeof(test_data_2));
TEST_ASSERT_NOT_EQUAL(data_point_2, DATA_POINT_INVALID);

ezEventNotifier_CreateObserver(&obs1, DataPointModifyCallback);
ezEventNotifier_CreateObserver(&obs2, DataPointModifyCallback);
DataModel_SubscribeDataPointEvent(data_point_1, &obs1);
DataModel_SubscribeDataPointEvent(data_point_2, &obs2);

helper_1 = 0xFF;
is_success = DataModel_WriteDataPoint(data_point_1, (void*)&helper_1, sizeof(helper_1));
TEST_ASSERT_EQUAL(is_success, true);
TEST_ASSERT_EQUAL(test_data_1, helper_1);

is_success = DataModel_WriteDataPoint(data_point_1, (void*)helper_2, sizeof(helper_2));
TEST_ASSERT_EQUAL(is_success, false);

helper_1 = 0x0;
is_success = DataModel_ReadDataPoint(data_point_1, (void*)&helper_1, sizeof(helper_1));
TEST_ASSERT_EQUAL(is_success, true);
TEST_ASSERT_EQUAL(test_data_1, helper_1);

memset(helper_2, 0xAA, sizeof(helper_2));
is_success = DataModel_WriteDataPoint(data_point_2, (void*)helper_2, sizeof(helper_2));
TEST_ASSERT_EQUAL(is_success, true);
TEST_ASSERT_EQUAL(memcmp(test_data_2, helper_2, sizeof(helper_2)), 0);

memset(helper_2, 0x00, sizeof(helper_2));
is_success = DataModel_ReadDataPoint(data_point_2, (void*)helper_2, sizeof(helper_2));
TEST_ASSERT_EQUAL(is_success, true);
TEST_ASSERT_EQUAL(memcmp(test_data_2, helper_2, sizeof(helper_2)), 0);
}


/******************************************************************************
* Internal functions
*******************************************************************************/
static void RunAllTests(void)
{
RUN_TEST_GROUP(ez_data_model);
}


int DataPointModifyCallback(uint32_t event_code, void* param1, void* param2)
{
switch ((DATA_POINT_EVENT)event_code)
{
case DATA_MODIFY:
break;
default:
break;
}
return 0;
}


/* End of file */
19 changes: 0 additions & 19 deletions tests/main.c

This file was deleted.

Loading

0 comments on commit 05923e8

Please sign in to comment.