Skip to content

Commit

Permalink
[nasa#1478] Initial demo of self registration of tests for ut_assert.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Price committed Sep 25, 2024
1 parent 8270166 commit b04c578
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
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_Run(void);
*/
void UtTest_Setup(void);


typedef void (*UTTestFunction)(void);

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)

#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 @@ void OS_Application_Run(void)
UtTest_Run();
}

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

/*
* Entry point from the BSP.
* When linking with UT-Assert, the test framework (this library) serves
Expand All @@ -242,11 +246,24 @@ void OS_Application_Startup(void)
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));
int num_records = (&__stop_utest_records - &__start_utest_records)/ sizeof(*test);
// printf("Num records is %d \n",num_records);
while (test < &__stop_utest_records) {
// printf(" Adding test %s\n", test->functionName);
UtTest_Add(test->functionPointer, NULL, NULL, test->functionName);
test++;
}

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 = .;
}
}

0 comments on commit b04c578

Please sign in to comment.