Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: [#1478] Initial demo of self registration of tests for ut_assert. #1479

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,50 @@ if (ENABLE_UNIT_TESTS)

enable_testing()

# Set the output paths
set(DEFAULT_LD_FULL_PATH "${CMAKE_CURRENT_BINARY_DIR}/default_full.ld")
set(DEFAULT_LD_PATH "${CMAKE_CURRENT_BINARY_DIR}/default.ld")
set(DEFAULT_LD_PATH_ESCAPED "${DEFAULT_LD_PATH}")
string(REPLACE "\\" "/" DEFAULT_LD_PATH_ESCAPED "${DEFAULT_LD_PATH_ESCAPED}")
set(DEFAULT_LD_PATH_ESCAPED "${DEFAULT_LD_PATH_ESCAPED}")

# Add a custom command to generate the default linker script
add_custom_command(
OUTPUT ${DEFAULT_LD_PATH}
COMMAND ${CMAKE_LINKER} --verbose > ${DEFAULT_LD_FULL_PATH}
COMMAND ${CMAKE_COMMAND} -DDEFAULT_LD_FULL_PATH=${DEFAULT_LD_FULL_PATH} -DDEFAULT_LD_PATH=${DEFAULT_LD_PATH} -P ${CMAKE_SOURCE_DIR}/ut_assert/extract_linker_script.cmake
COMMENT "Generating default linker script (default.ld)"
)


# Configure the custom linker script
configure_file(
${CMAKE_SOURCE_DIR}/ut_assert/utest_linker.ld.in
${CMAKE_BINARY_DIR}/utest_linker.ld
@ONLY
)

add_custom_target(
generate_linker_scripts
DEPENDS ${DEFAULT_LD_PATH} ${CMAKE_BINARY_DIR}/utest_linker.ld
)

# Generic function for consistent definition of a unit testing target
# This is defined here in the top-level OSAL CMakeLists so it can be used
# in both the "tests" and "unit-tests" subdirectories.
function(add_osal_ut_exe TGTNAME)

add_executable(${TGTNAME} ${ARGN})
add_dependencies(${TGTNAME} generate_linker_scripts)

# Specify the path to your custom linker script
set(LINKER_SCRIPT "${CMAKE_BINARY_DIR}/utest_linker.ld")

# Pass the linker script to the linker via linker flags
target_link_options(${TGTNAME} PRIVATE
"-Wl,-T${LINKER_SCRIPT}"
)

target_link_libraries(${TGTNAME} PUBLIC ut_assert osal)
add_test(${TGTNAME} ${TGTNAME})
foreach(TGT ${INSTALL_TARGET_LIST})
Expand Down
15 changes: 13 additions & 2 deletions src/tests/bin-sem-test/bin-sem-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ void Test_BinSem_Task2(void)
}
}

void Test_BinSem(void)
//void Test_BinSem(void)
UTEST(BinSem, Test)
{

osal_id_t sem_id[2];
Expand Down Expand Up @@ -214,6 +215,16 @@ void Test_BinSem(void)
UtAssert_INT32_EQ(OS_BinSemDelete(sem_id[1]), OS_SUCCESS);
}

UTEST(BinSem, Test2)
{
printf("A");
}

UTEST(BinSem, Test3)
{
printf("B");
}

void UtTest_Setup(void)
{
if (OS_API_Init() != OS_SUCCESS)
Expand All @@ -227,5 +238,5 @@ void UtTest_Setup(void)
/*
* Register the test setup and check routines in UT assert
*/
UtTest_Add(Test_BinSem, NULL, NULL, "Test_BinSem");
//UtTest_Add(Test_BinSem, NULL, NULL, "Test_BinSem");
}
10 changes: 10 additions & 0 deletions ut_assert/extract_linker_script.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# extract_linker_script.cmake

# Read the full linker output
file(READ "${DEFAULT_LD_FULL_PATH}" LD_OUTPUT)

# Use a simple regex to extract the default linker script between '====' lines
string(REGEX REPLACE ".*=+\n(.*)\n=+.*" "\\1" LD_SCRIPT_CONTENTS "${LD_OUTPUT}")

# Write the extracted linker script to default.ld
file(WRITE "${DEFAULT_LD_PATH}" "${LD_SCRIPT_CONTENTS}")
28 changes: 28 additions & 0 deletions ut_assert/inc/uttest.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,32 @@
*/
void UtTest_Setup(void);


typedef void (*UTTestFunction)(void);

Check notice

Code scanning / CodeQL

Hidden pointer indirection Note

The typedef UTTestFunction hides pointer indirection.

typedef struct UtTestRecord{
int marker;
const char * testSuite;
const char * functionName;
const char * fileName;
const char * testName;
int lineNumber;
UTTestFunction functionPointer;
int padding; /* Size needs to be 64 bytes long for some reason */
}UtTestRecord;

#define UTEST(suiteName, uTtestName) \
static void suiteName##_##uTtestName(void); \
static UtTestRecord Record_##suiteName##_##uTtestName \
__attribute__((section(".utest_records"), used)) = { \
.marker = 0xDeadBeaf, \
.testSuite = #suiteName, \
.functionName = #uTtestName, \
.testName = #suiteName #uTtestName, \
.fileName = __FILE__, \
.lineNumber = __LINE__, \
.functionPointer = suiteName##_##uTtestName \
}; \
static void suiteName##_##uTtestName(void)
Comment on lines +134 to +146

Check notice

Code scanning / CodeQL

Undisciplined macro Note

The macro UTEST(suiteName,uTtestName) uses token pasting and is not allowed.

#endif /* UTTEST_H */
17 changes: 17 additions & 0 deletions ut_assert/src/utbsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@
UtTest_Run();
}

/* Defined in linker script */
extern UtTestRecord __start_utest_records;

Check warning

Code scanning / CppCheck

Comparing pointers that point to different objects Warning

Comparing pointers that point to different objects

Check notice

Code scanning / CodeQL

Variable scope too large Note

The variable __start_utest_records is only accessed in
OS_Application_Startup
and should be scoped accordingly.
The variable __start_utest_records is only accessed in
OS_Application_Startup
and should be scoped accordingly.

Check warning

Code scanning / CodeQL

"extern" declaration in source file Warning

__start_utest_records should be declared only in a header file that is included as needed.
extern UtTestRecord __stop_utest_records;

Check warning

Code scanning / CppCheck

Subtracting pointers that point to different objects Warning

Subtracting pointers that point to different objects

Check notice

Code scanning / CodeQL

Variable scope too large Note

The variable __stop_utest_records is only accessed in
OS_Application_Startup
and should be scoped accordingly.
The variable __stop_utest_records is only accessed in
OS_Application_Startup
and should be scoped accordingly.

Check warning

Code scanning / CodeQL

"extern" declaration in source file Warning

__stop_utest_records should be declared only in a header file that is included as needed.

/*
* Entry point from the BSP.
* When linking with UT-Assert, the test framework (this library) serves
Expand All @@ -242,11 +246,24 @@
UtTest_EarlyInit();
UT_BSP_Setup();


/*
* Wrap the UtTest_Setup() function in a UT segment called "SETUP"
* This allows any assert calls to be used and recorded during setup
*/
UtAssert_BeginTest("SETUP");

/* TODO PROBALBY a better spot for this */
UtTestRecord *test = &__start_utest_records;
// printf("Size of record is %d\n", sizeof(*test));

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
int num_records = (&__stop_utest_records - &__start_utest_records)/ sizeof(*test);

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable num_records is not used.
// printf("Num records is %d \n",num_records);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
while (test < &__stop_utest_records) {
// printf(" Adding test %s\n", test->functionName);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
UtTest_Add(test->functionPointer, NULL, NULL, test->functionName);
test++;
}
Comment on lines +261 to +265

Check warning

Code scanning / CodeQL

Unbounded loop Warning

This loop does not have a fixed bound.

UtTest_Setup();
UtAssert_EndTest();
}
16 changes: 16 additions & 0 deletions ut_assert/utest_linker.ld.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Include the default linker script */
INCLUDE "@DEFAULT_LD_PATH@"


SECTIONS
{

/* Define the .utest_records section */
.utest_records :
{
__start_utest_records = .;
KEEP(*(.utest_records));
__stop_utest_records = .;
}
}

Loading