Skip to content

Commit 82d4843

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 82d4843

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

modules/cfe_assert/inc/cfe_assert.h

+158
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,146 @@
3939
*************************************************************************/
4040
#include "common_types.h"
4141
#include "cfe_es_api_typedefs.h"
42+
#include "utassert.h"
4243

4344
/************************************************************************
4445
** Type Definitions
4546
*************************************************************************/
4647

4748
typedef void (*CFE_Assert_StatusCallback_t)(uint8 MessageType, const char *Prefix, const char *OutputMessage);
4849

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

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