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

Fix #501, add PIC library for ut assert #504

Merged
merged 1 commit into from
Jun 23, 2020
Merged
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
21 changes: 21 additions & 0 deletions ut_assert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,25 @@ target_compile_definitions(ut_assert PUBLIC
target_link_libraries(ut_assert osal_bsp)


# The "pic" variant of ut_assert is compiled as an
# object library to be included in another object,
# such as a loadable test app for CFE.
# It is compiled as position independent code (PIC)
# to support dynamic loading.
add_library(ut_assert_pic OBJECT EXCLUDE_FROM_ALL
src/utassert.c
src/utlist.c
src/utstubs.c
src/uttest.c
src/uttools.c
)

set_target_properties(ut_assert_pic PROPERTIES
POSITION_INDEPENDENT_CODE TRUE
)

target_include_directories(ut_assert_pic PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/inc"
)


32 changes: 32 additions & 0 deletions ut_assert/inc/utassert.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,37 @@ void UtAssert_Abort(const char *Message);
*/
void UtAssert_Message(uint8 MessageType, const char *File, uint32 Line, const char *Spec, ...) OS_PRINTF(4,5);

/**
* The BSP single test case reporting function.
*
* Invokes the BSP-specific pass/fail reporting mechanism based on the MessageType.
*
* This is typically output as a message to the test log but may be fancier if the BSP requires it.
* One example might be to toggle a GPIO bit or LED if the test is running on a separate processor board.
*
* \param File File containing the test case
* \param LineNum Line number containing the test case
* \param MessageType Should be set to either UT_MESSAGE_PASS or UT_MESSAGE_FAILURE.
* \param SubsysName The subsystem under test (abbreviated name)
* \param ShortDesc Short description of the test case
* \param SegmentNum Sequence among the overall/global test Segments
* \param TestDescr Sequence within the current test Segment
*/
void UtAssert_DoReport(const char *File, uint32 LineNum, uint32 SegmentNum, uint32 SegmentSeq, uint8 MessageType,
const char *SubsysName, const char *ShortDesc);

/**
* The BSP overall test reporting function.
*
* Invokes the BSP-specific overall pass/fail reporting mechanism based the subsystem pass/fail counters.
*
* Like the UtAssert_DoReport() function, this is typically done as a message on the console/log however
* it might be different for embedded targets.
*
* \param Appname The application under test
* \param TestCounters Counter object for the completed test
*/
void UtAssert_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCounter_t *TestCounters);


#endif
32 changes: 0 additions & 32 deletions ut_assert/inc/utbsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,6 @@ void UT_BSP_StartTestSegment(uint32 SegmentNumber, const char *SegmentName);
*/
void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage);

/**
* The BSP single test case reporting function.
*
* Invokes the BSP-specific pass/fail reporting mechanism based on the MessageType.
*
* This is typically output as a message to the test log but may be fancier if the BSP requires it.
* One example might be to toggle a GPIO bit or LED if the test is running on a separate processor board.
*
* \param File File containing the test case
* \param LineNum Line number containing the test case
* \param MessageType Should be set to either UT_MESSAGE_PASS or UT_MESSAGE_FAILURE.
* \param SubsysName The subsystem under test (abbreviated name)
* \param ShortDesc Short description of the test case
* \param SegmentNum Sequence among the overall/global test Segments
* \param TestDescr Sequence within the current test Segment
*/
void UT_BSP_DoReport(const char *File, uint32 LineNum, uint32 SegmentNum, uint32 SegmentSeq, uint8 MessageType,
const char *SubsysName, const char *ShortDesc);

/**
* The BSP overall test reporting function.
*
* Invokes the BSP-specific overall pass/fail reporting mechanism based the subsystem pass/fail counters.
*
* Like the UT_BSP_DoReport() function, this is typically done as a message on the console/log however
* it might be different for embedded targets.
*
* \param Appname The application under test
* \param TestCounters Counter object for the completed test
*/
void UT_BSP_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCounter_t *TestCounters);

/**
* The BSP overall test end function.
*
Expand Down
35 changes: 31 additions & 4 deletions ut_assert/inc/uttest.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,41 @@
* Exported Functions
*/

/* Adds a new unit test to the test database. */
/**
* \brief Adds a new unit test to the test database.
*
* Called by the user to register a new test case with the library.
*
* \param Test Main test function to call.
* \param Setup Setup function, called before the test function
* \param Teardown Cleanup function, called after the test function
* \param TestName Name of test for logging purposes
*/
void UtTest_Add(void (*Test)(void), void (*Setup)(void), void (*Teardown)(void), const char *TestName);

/**
* \brief Early initialization function
*
* Reset the global data to a safe state for initial start-up.
* This should be called before any other API.
*/
void UtTest_EarlyInit(void);


/**
* \brief Execute all registered tests
*
* All test functions that were registered with UtTest_Add will be executed in order.
*/
void UtTest_Run(void);

/*
* Set up function for UT-Assert based test routines
* This should call UtTest_Add() for each test set
* \brief Set up function for UT-Assert based test routines
*
* This function must be provided by the user to set up test cases.
* This should call UtTest_Add() for each test case.
*/
void UtTest_Setup(void);
void UtTest_Setup(void);

#endif

47 changes: 45 additions & 2 deletions ut_assert/src/utassert.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,49 @@ static char CurrentSegment[128];
* Function Definitions
*/

void UtAssert_DoReport(const char *File, uint32 LineNum, uint32 SegmentNum, uint32 TestSeq, uint8 MessageType,
const char *SubsysName, const char *ShortDesc)
{
uint32 FileLen;
const char *BasePtr;
char ReportBuffer[320];

FileLen = strlen(File);
BasePtr = File + FileLen;
while (FileLen > 0)
{
--BasePtr;
--FileLen;
if (*BasePtr == '/' || *BasePtr == '\\')
{
++BasePtr;
break;
}
}

snprintf(ReportBuffer, sizeof(ReportBuffer), "%02u.%03u %s:%u - %s", (unsigned int)SegmentNum,
(unsigned int)TestSeq, BasePtr, (unsigned int)LineNum, ShortDesc);

UT_BSP_DoText(MessageType, ReportBuffer);
}

void UtAssert_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCounter_t *TestCounters)
{
char ReportBuffer[128];

snprintf(ReportBuffer, sizeof(ReportBuffer),
"%02u %-20s TOTAL::%-4u PASS::%-4u FAIL::%-4u MIR::%-4u TSF::%-4u N/A::%-4u\n",
(unsigned int)TestCounters->TestSegmentCount, SegmentName, (unsigned int)TestCounters->TotalTestCases,
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_PASS],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_FAILURE],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_MIR],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_TSF],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_NA]);

UT_BSP_DoText(UTASSERT_CASETYPE_END, ReportBuffer);
}


uint32 UtAssert_GetPassCount(void)
{
return(UT_TotalCounters.CaseCount[UTASSERT_CASETYPE_PASS]);
Expand Down Expand Up @@ -80,7 +123,7 @@ void UtAssert_EndTest(void)
{
UT_TotalCounters.CaseCount[Ct] += UT_SegmentCounters.CaseCount[Ct];
}
UT_BSP_DoTestSegmentReport(CurrentSegment, &UT_SegmentCounters);
UtAssert_DoTestSegmentReport(CurrentSegment, &UT_SegmentCounters);
}
else
{
Expand Down Expand Up @@ -126,7 +169,7 @@ bool UtAssertEx(bool Expression, UtAssert_CaseType_t CaseType, const char *File,
vsnprintf(FinalMessage, sizeof(FinalMessage), MessageFormat, va);
va_end(va);

UT_BSP_DoReport(File, Line, 1 + UT_TotalCounters.TestSegmentCount, UT_SegmentCounters.TotalTestCases, CaseType, CurrentSegment, FinalMessage);
UtAssert_DoReport(File, Line, 1 + UT_TotalCounters.TestSegmentCount, UT_SegmentCounters.TotalTestCases, CaseType, CurrentSegment, FinalMessage);

return Expression;
}
Expand Down
84 changes: 41 additions & 43 deletions ut_assert/src/utbsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
** Purpose:
** Unit test BSP interface functions.
**
** This file provides the bindings between the OSAL BSP and UT assert
** when directly running a test program as a standalone OSAL application.
**
** It is not used when loading UT assert into another application (e.g. CFE).
**
******************************************************************************/

#include <stdio.h>
Expand Down Expand Up @@ -178,48 +183,6 @@ void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage)
}
}

void UT_BSP_DoReport(const char *File, uint32 LineNum, uint32 SegmentNum, uint32 TestSeq, uint8 MessageType,
const char *SubsysName, const char *ShortDesc)
{
uint32 FileLen;
const char *BasePtr;
char ReportBuffer[128];

FileLen = strlen(File);
BasePtr = File + FileLen;
while (FileLen > 0)
{
--BasePtr;
--FileLen;
if (*BasePtr == '/' || *BasePtr == '\\')
{
++BasePtr;
break;
}
}

snprintf(ReportBuffer, sizeof(ReportBuffer), "%02u.%03u %s:%u - %s", (unsigned int)SegmentNum,
(unsigned int)TestSeq, BasePtr, (unsigned int)LineNum, ShortDesc);

UT_BSP_DoText(MessageType, ReportBuffer);
}

void UT_BSP_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCounter_t *TestCounters)
{
char ReportBuffer[128];

snprintf(ReportBuffer, sizeof(ReportBuffer),
"%02u %-20s TOTAL::%-4u PASS::%-4u FAIL::%-4u MIR::%-4u TSF::%-4u N/A::%-4u\n",
(unsigned int)TestCounters->TestSegmentCount, SegmentName, (unsigned int)TestCounters->TotalTestCases,
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_PASS],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_FAILURE],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_MIR],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_TSF],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_NA]);

UT_BSP_DoText(UTASSERT_CASETYPE_END, ReportBuffer);
}

void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters)
{
char Message[128];
Expand All @@ -230,7 +193,7 @@ void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters)
*/
if (TestCounters->TestSegmentCount > 1)
{
UT_BSP_DoTestSegmentReport("SUMMARY", TestCounters);
UtAssert_DoTestSegmentReport("SUMMARY", TestCounters);
}

snprintf(Message, sizeof(Message), "COMPLETE: %u tests Segment(s) executed\n\n",
Expand All @@ -246,3 +209,38 @@ void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters)
OS_BSP_SetExitCode(OS_SUCCESS);
}
}

/*
* -------------------------------------------------------
* ENTRY POINTS from OSAL BSP
* -------------------------------------------------------
*/

void OS_Application_Run(void)
{
UtTest_Run();
}

/*
* Entry point from the BSP.
* When linking with UT-Assert, the test framework (this library) serves
* the role of the "application" being executed.
*
* There is a separate entry point (UT_Test_Setup) to configure the test cases.
*/
void OS_Application_Startup(void)
{

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");
UtTest_Setup();
UtAssert_EndTest();
}



59 changes: 59 additions & 0 deletions ut_assert/src/utglobal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
**
** File: uttest.c
**
** Copyright 2012-2013 United States Government as represented by the
** Administrator of the National Aeronautics and Space Administration.
** All Other Rights Reserved.
**
** This software was created at NASA's Goddard Space Flight Center.
** This software is governed by the NASA Open Source Agreement and may be
** used, distributed and modified only pursuant to the terms of that
** agreement.
**
** Purpose: This file contains functions to implement a standard way to execute unit tests.
**
*/

/*
* Includes
*/

#ifndef INCLUDE_UTASSERT_GLOBAL_H_
#define INCLUDE_UTASSERT_GLOBAL_H_

#include "osapi.h"
#include "utassert.h"
#include "utlist.h"
#include "utbsp.h"
#include "uttest.h"
#include "utstubs.h"

/*
* Type Definitions
*/

typedef struct
{
void (*Test)(void);
void (*Setup)(void);
void (*Teardown)(void);

/* Note - the name entry should be long enough to support a GroupName.TestName pattern,
* hence why it uses double the OS_MAX_API_NAME length */
char TestName[OS_MAX_API_NAME*2];
} UtTestDataBaseEntry_t;

typedef struct
{
UtListHead_t DataBase;
uint32 ExecutedCount;
} UtAssert_Global_t;

/*
* Global Test Data
*/
extern UtAssert_Global_t UtAssert_Global;

#endif /* INCLUDE_UTASSERT_GLOBAL_H_ */

Loading