Skip to content

Commit 97790b2

Browse files
committed
Fix #1784, add CFE assert macros to functional test
Adds the following macros to CFE assert library in cfe_assert.h: - CFE_UtAssert_SETUP - CFE_UtAssert_SUCCESS - CFE_UtAssert_NOT_SUCCESS - CFE_UtAssert_TEARDOWN - CFE_UtAssert_RESOURCEID_EQ - CFE_UtAssert_RESOURCEID_UNDEFINED - CFE_UtAssert_MEMOFFSET_EQ - CFE_UtAssert_MSGID_EQ
1 parent cfadad6 commit 97790b2

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

modules/cfe_assert/inc/cfe_assert.h

+159
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,147 @@
3939
*************************************************************************/
4040
#include "common_types.h"
4141
#include "cfe_es_api_typedefs.h"
42+
#include "utassert.h"
43+
#include "cfe_error.h"
4244

4345
/************************************************************************
4446
** Type Definitions
4547
*************************************************************************/
4648

4749
typedef void (*CFE_Assert_StatusCallback_t)(uint8 MessageType, const char *Prefix, const char *OutputMessage);
4850

51+
/*************************************************************************
52+
** CFE-specific assertion macros
53+
** (similar to macros in the CFE coverage test)
54+
*************************************************************************/
55+
56+
/*****************************************************************************/
57+
/**
58+
** \brief Checks the successful execution of a setup function.
59+
**
60+
** \par Description
61+
** Many tests require a number of steps of setup to configure CFE such
62+
** that the actual test can be performed. Failure of any setup steps
63+
** result in a text message and the test being considered failed.
64+
**
65+
** \par Assumptions, External Events, and Notes:
66+
** To keep logs clean, this only generates a log message if it fails
67+
**
68+
******************************************************************************/
69+
#define CFE_UtAssert_SETUP(FN) CFE_UtAssert_SuccessCheck(FN, true, UTASSERT_CASETYPE_TSF, __FILE__, __LINE__, #FN)
70+
71+
/*****************************************************************************/
72+
/**
73+
** \brief Asserts the nominal execution of the function being tested.
74+
**
75+
** \par Description
76+
** The core of each unit test is the execution of the function being tested.
77+
** This function and macro should be used to test the nominal execution of the
78+
** function; the expectation is that it will return CFE_SUCCESS or an
79+
** unspecified positive value.
80+
**
81+
** \par Assumptions, External Events, and Notes:
82+
** None
83+
**
84+
******************************************************************************/
85+
#define CFE_UtAssert_SUCCESS(FN) CFE_UtAssert_SuccessCheck(FN, true, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #FN)
86+
87+
/*****************************************************************************/
88+
/**
89+
** \brief Asserts the off-nominal execution of the function being tested.
90+
**
91+
** \par Description
92+
** The core of each unit test is the execution of the function being tested.
93+
** This function and macro should be used to test the generic off-nominal execution
94+
** of the function; the expectation is that it will return an unspecified negative
95+
** value.
96+
**
97+
** \par Assumptions, External Events, and Notes:
98+
** This should be used in cases where a specific error for a particular condition
99+
** is not known/documented. Whenever a specific error is indicated by the documentation,
100+
** tests should check for that error instead of using this.
101+
**
102+
******************************************************************************/
103+
#define CFE_UtAssert_NOT_SUCCESS(FN) \
104+
CFE_UtAssert_SuccessCheck(FN, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #FN)
105+
106+
/*****************************************************************************/
107+
/**
108+
** \brief Checks the successful execution of a teardown function.
109+
**
110+
** \par Description
111+
** Many tests require a number of steps of setup to configure CFE such that the actual test
112+
** can be performed, and undoing that configuration is the role of the teardown steps. Failure
113+
** of any teardown steps result in a text message and the test being considered failed.
114+
**
115+
** \par Assumptions, External Events, and Notes:
116+
** To keep logs clean, this only generates a log message if it fails
117+
**
118+
******************************************************************************/
119+
#define CFE_UtAssert_TEARDOWN(FN) CFE_UtAssert_SuccessCheck(FN, true, UTASSERT_CASETYPE_TTF, __FILE__, __LINE__, #FN)
120+
121+
/*****************************************************************************/
122+
/**
123+
** \brief Macro to check CFE resource ID for equality
124+
**
125+
** \par Description
126+
** A macro that checks two resource ID values for equality.
127+
**
128+
** \par Assumptions, External Events, and Notes:
129+
** The generic #UtAssert_UINT32_EQ check should not be used, as ID values
130+
** and integers may not be interchangable with strict type checking.
131+
**
132+
******************************************************************************/
133+
#define CFE_UtAssert_RESOURCEID_EQ(id1, id2) \
134+
UtAssert_GenericUnsignedCompare(CFE_RESOURCEID_TO_ULONG(id1), UtAssert_Compare_EQ, CFE_RESOURCEID_TO_ULONG(id2), \
135+
UtAssert_Radix_HEX, __FILE__, __LINE__, "Resource ID Check: ", #id1, #id2)
136+
137+
/*****************************************************************************/
138+
/**
139+
** \brief Check if a Resource ID is Undefined
140+
**
141+
** \par Description
142+
** A macro that checks if resource ID value is undefined.
143+
**
144+
** \par Assumptions, External Events, and Notes:
145+
** This utilizes the "TEST_DEFINED" macro provided by the resourceid module, as the
146+
** set of undefined IDs is more than the single value of CFE_RESOURCEID_UNDEFINED.
147+
**
148+
******************************************************************************/
149+
#define CFE_UtAssert_RESOURCEID_UNDEFINED(id) \
150+
UtAssert_True(!CFE_RESOURCEID_TEST_DEFINED(id), "%s (0x%lx) not defined", #id, CFE_RESOURCEID_TO_ULONG(id))
151+
152+
/*****************************************************************************/
153+
/**
154+
** \brief Macro to check CFE memory size/offset for equality
155+
**
156+
** \par Description
157+
** A macro that checks two memory offset/size values for equality.
158+
**
159+
** \par Assumptions, External Events, and Notes:
160+
** This is a simple unsigned comparison which logs the values as hexadecimal
161+
**
162+
******************************************************************************/
163+
#define CFE_UtAssert_MEMOFFSET_EQ(off1, off2) \
164+
UtAssert_GenericUnsignedCompare(off1, UtAssert_Compare_EQ, off2, UtAssert_Radix_HEX, __FILE__, __LINE__, \
165+
"Offset Check: ", #off1, #off2)
166+
167+
/*****************************************************************************/
168+
/**
169+
** \brief Macro to check CFE message ID for equality
170+
**
171+
** \par Description
172+
** A macro that checks two message ID values for equality.
173+
**
174+
** \par Assumptions, External Events, and Notes:
175+
** The generic #UtAssert_UINT32_EQ check should not be used, as CFE_SB_MsgId_t values
176+
** and integers may not be interchangable with strict type checking.
177+
**
178+
******************************************************************************/
179+
#define CFE_UtAssert_MSGID_EQ(mid1, mid2) \
180+
UtAssert_GenericUnsignedCompare(CFE_SB_MsgIdToValue(mid1), UtAssert_Compare_EQ, CFE_SB_MsgIdToValue(mid2), \
181+
UtAssert_Radix_HEX, __FILE__, __LINE__, "MsgId Check: ", #mid1, #mid2)
182+
49183
/*************************************************************************
50184
** Exported Functions
51185
*************************************************************************/
@@ -145,4 +279,29 @@ int32 CFE_Assert_OpenLogFile(const char *Filename);
145279
*/
146280
void CFE_Assert_CloseLogFile(void);
147281

282+
/*****************************************************************************/
283+
/**
284+
** \brief Helper function for nominal CFE calls
285+
**
286+
** \par Description
287+
** This helper function wraps the normal UtAssert function, intended for verifying
288+
** CFE API calls that are expected to return successfully (#CFE_SUCCESS typically).
289+
**
290+
** This can also be used to confirm setup and teardown operations by passing the CaseType
291+
** parameter appropriately (UTASSERT_CASETYPE_TSF or UTASSERT_CASETYPE_TTF, respectively).
292+
**
293+
** \par Assumptions, External Events, and Notes:
294+
** When used for setup (TSF) or teardown (TTF) then the test case is only logged to
295+
** the output if it fails. This is to keep logs more concise, by not including
296+
** test cases that are not related to the main focus of the code under test.
297+
**
298+
** Note this will accept any non-negative value as logical "success", so it
299+
** also works with functions that return a size or other non-error status.
300+
**
301+
** \returns Test pass status, returns true if status was successful, false if it failed.
302+
**
303+
******************************************************************************/
304+
bool CFE_UtAssert_SuccessCheck(CFE_Status_t Status, bool ExpectSuccess, UtAssert_CaseType_t CaseType, const char *File,
305+
uint32 Line, const char *Text);
306+
148307
#endif /* CFE_ASSERT_H */

modules/cfe_assert/src/cfe_assert_runner.c

+25
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ static CFE_EVS_BinFilter_t CFE_TR_EventFilters[] = {
6969
{UTASSERT_CASETYPE_DEBUG, CFE_EVS_NO_FILTER},
7070
};
7171

72+
bool CFE_UtAssert_SuccessCheck(CFE_Status_t Status, bool ExpectSuccess, UtAssert_CaseType_t CaseType, const char *File,
73+
uint32 Line, const char *Text)
74+
{
75+
bool Result = (Status >= CFE_SUCCESS);
76+
char Disp;
77+
78+
if (ExpectSuccess)
79+
{
80+
Disp = '=';
81+
}
82+
else
83+
{
84+
/* expecting non-success; result should be inverted */
85+
Result = !Result;
86+
Disp = '!';
87+
}
88+
89+
if (!Result || (CaseType != UTASSERT_CASETYPE_TSF && CaseType != UTASSERT_CASETYPE_TTF))
90+
{
91+
UtAssertEx(Result, CaseType, File, Line, "%s (0x%lx) %c= CFE_SUCCESS", Text, (unsigned long)Status, Disp);
92+
}
93+
94+
return Result;
95+
}
96+
7297
void CFE_Assert_StatusReport(uint8 MessageType, const char *Prefix, const char *OutputMessage)
7398
{
7499
uint16 EventType;

0 commit comments

Comments
 (0)