Skip to content

Commit 490c9b7

Browse files
authored
Merge pull request #1790 from jphickey/fix-1784-cfeassert-macros
Fix #1784, add CFE assert macros to functional test
2 parents eb9c523 + c2d0ee7 commit 490c9b7

9 files changed

+166
-31
lines changed

modules/cfe_assert/inc/cfe_assert.h

+131
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,118 @@
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 Asserts the nominal execution of the function being tested.
59+
**
60+
** \par Description
61+
** The core of each unit test is the execution of the function being tested.
62+
** This function and macro should be used to test the nominal execution of the
63+
** function; the expectation is that it will return CFE_SUCCESS or an
64+
** unspecified positive value.
65+
**
66+
** \par Assumptions, External Events, and Notes:
67+
** None
68+
**
69+
******************************************************************************/
70+
#define CFE_UtAssert_STATUS_OK(FN) \
71+
CFE_UtAssert_StatusCheck(FN, true, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #FN)
72+
73+
/*****************************************************************************/
74+
/**
75+
** \brief Asserts the off-nominal execution of the function being tested.
76+
**
77+
** \par Description
78+
** The core of each unit test is the execution of the function being tested.
79+
** This function and macro should be used to test the generic off-nominal execution
80+
** of the function; the expectation is that it will return an unspecified negative
81+
** value.
82+
**
83+
** \par Assumptions, External Events, and Notes:
84+
** This should be used in cases where a specific error for a particular condition
85+
** is not known/documented. Whenever a specific error is indicated by the documentation,
86+
** tests should check for that error instead of using this.
87+
**
88+
******************************************************************************/
89+
#define CFE_UtAssert_STATUS_ERROR(FN) \
90+
CFE_UtAssert_StatusCheck(FN, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #FN)
91+
92+
/*****************************************************************************/
93+
/**
94+
** \brief Macro to check CFE resource ID for equality
95+
**
96+
** \par Description
97+
** A macro that checks two resource ID values for equality.
98+
**
99+
** \par Assumptions, External Events, and Notes:
100+
** The generic #UtAssert_UINT32_EQ check should not be used, as ID values
101+
** and integers may not be interchangable with strict type checking.
102+
**
103+
******************************************************************************/
104+
#define CFE_UtAssert_RESOURCEID_EQ(id1, id2) \
105+
UtAssert_GenericUnsignedCompare(CFE_RESOURCEID_TO_ULONG(id1), UtAssert_Compare_EQ, CFE_RESOURCEID_TO_ULONG(id2), \
106+
UtAssert_Radix_HEX, __FILE__, __LINE__, "Resource ID Check: ", #id1, #id2)
107+
108+
/*****************************************************************************/
109+
/**
110+
** \brief Check if a Resource ID is Undefined
111+
**
112+
** \par Description
113+
** A macro that checks if resource ID value is undefined.
114+
**
115+
** \par Assumptions, External Events, and Notes:
116+
** This utilizes the "TEST_DEFINED" macro provided by the resourceid module, as the
117+
** set of undefined IDs is more than the single value of CFE_RESOURCEID_UNDEFINED.
118+
**
119+
******************************************************************************/
120+
#define CFE_UtAssert_RESOURCEID_UNDEFINED(id) \
121+
UtAssert_True(!CFE_RESOURCEID_TEST_DEFINED(id), "%s (0x%lx) not defined", #id, CFE_RESOURCEID_TO_ULONG(id))
122+
123+
/*****************************************************************************/
124+
/**
125+
** \brief Macro to check CFE memory size/offset for equality
126+
**
127+
** \par Description
128+
** A macro that checks two memory offset/size values for equality.
129+
**
130+
** \par Assumptions, External Events, and Notes:
131+
** This is a simple unsigned comparison which logs the values as hexadecimal
132+
**
133+
******************************************************************************/
134+
#define CFE_UtAssert_MEMOFFSET_EQ(off1, off2) \
135+
UtAssert_GenericUnsignedCompare(off1, UtAssert_Compare_EQ, off2, UtAssert_Radix_HEX, __FILE__, __LINE__, \
136+
"Offset Check: ", #off1, #off2)
137+
138+
/*****************************************************************************/
139+
/**
140+
** \brief Macro to check CFE message ID for equality
141+
**
142+
** \par Description
143+
** A macro that checks two message ID values for equality.
144+
**
145+
** \par Assumptions, External Events, and Notes:
146+
** The generic #UtAssert_UINT32_EQ check should not be used, as CFE_SB_MsgId_t values
147+
** and integers may not be interchangable with strict type checking.
148+
**
149+
******************************************************************************/
150+
#define CFE_UtAssert_MSGID_EQ(mid1, mid2) \
151+
UtAssert_GenericUnsignedCompare(CFE_SB_MsgIdToValue(mid1), UtAssert_Compare_EQ, CFE_SB_MsgIdToValue(mid2), \
152+
UtAssert_Radix_HEX, __FILE__, __LINE__, "MsgId Check: ", #mid1, #mid2)
153+
49154
/*************************************************************************
50155
** Exported Functions
51156
*************************************************************************/
@@ -145,4 +250,30 @@ int32 CFE_Assert_OpenLogFile(const char *Filename);
145250
*/
146251
void CFE_Assert_CloseLogFile(void);
147252

253+
/*****************************************************************************/
254+
/**
255+
** \brief Helper function for nominal CFE calls
256+
**
257+
** \par Description
258+
** This helper function wraps the normal UtAssert function, intended for verifying
259+
** CFE API calls that are expected to return successfully.
260+
**
261+
** Note that this checks for a logical "success", which includes the specific #CFE_SUCCESS
262+
** status code, as well as all other status codes that represent a successful completion
263+
** of the function objectives (i.e. such as a nonzero size, from functions that return a
264+
** size).
265+
**
266+
** This can also be used to report with an alternative pass/fail marker by passing the CaseType
267+
** parameter appropriately.
268+
**
269+
** \par Assumptions, External Events, and Notes:
270+
** Note this will accept any non-negative value as logical "success", so it
271+
** also works with functions that return a size or other non-error status.
272+
**
273+
** \returns Test pass status, returns true if status was successful, false if it failed.
274+
**
275+
******************************************************************************/
276+
bool CFE_UtAssert_StatusCheck(CFE_Status_t Status, bool ExpectSuccess, UtAssert_CaseType_t CaseType, const char *File,
277+
uint32 Line, const char *Text);
278+
148279
#endif /* CFE_ASSERT_H */

modules/cfe_assert/src/cfe_assert_runner.c

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

72+
bool CFE_UtAssert_StatusCheck(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+
const char *MatchText;
77+
78+
if (ExpectSuccess)
79+
{
80+
MatchText = "OK";
81+
}
82+
else
83+
{
84+
/* expecting non-success; result should be inverted */
85+
Result = !Result;
86+
MatchText = "ERROR";
87+
}
88+
89+
return UtAssertEx(Result, CaseType, File, Line, "%s (0x%lx) is %s", Text, (unsigned long)Status, MatchText);
90+
}
91+
7292
void CFE_Assert_StatusReport(uint8 MessageType, const char *Prefix, const char *OutputMessage)
7393
{
7494
uint16 EventType;

modules/cfe_testcase/src/cfe_test.h

+1-17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include "uttest.h"
4545
#include "utassert.h"
46+
#include "cfe_assert.h"
4647

4748
typedef struct
4849
{
@@ -69,23 +70,6 @@ extern CFE_FT_Global_t CFE_FT_Global;
6970
*/
7071
#define CFE_ASSERT_LOG_FILE_NAME "/cf/cfe_test.log"
7172

72-
/* Compare two Resource IDs */
73-
#define cFE_FTAssert_ResourceID_EQ(actual, expect) \
74-
UtAssert_True(CFE_RESOURCEID_TEST_EQUAL(actual, expect), "%s (%lu) == %s (%lu)", #actual, \
75-
CFE_RESOURCEID_TO_ULONG(actual), #expect, CFE_RESOURCEID_TO_ULONG(expect))
76-
77-
/* Check if a Resource ID is Undefined */
78-
#define cFE_FTAssert_ResourceID_Undefined(id) \
79-
UtAssert_True(!CFE_RESOURCEID_TEST_DEFINED(id), "%s (%lu) not defined", #id, CFE_RESOURCEID_TO_ULONG(id))
80-
81-
/* Assert a return code is not equal to cfe_success */
82-
#define cFE_FTAssert_NOT_CFE_SUCCESS(actual) \
83-
do \
84-
{ \
85-
int32 rcact = (int32)(actual); \
86-
UtAssert_True(rcact < CFE_SUCCESS, "%s == (%ld) ", #actual, (long)rcact); \
87-
} while (0)
88-
8973
bool TimeInRange(CFE_TIME_SysTime_t Time, CFE_TIME_SysTime_t Target, OS_time_t difference);
9074

9175
void CFE_TestMain(void);

modules/cfe_testcase/src/es_cds_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void TestCDSName(void)
8282
UtAssert_INT32_EQ(CFE_ES_GetCDSBlockName(CDSNameBuf, CDSHandlePtr, sizeof(CDSNameBuf)), CFE_SUCCESS);
8383
UtAssert_StrCmp(CDSNameBuf, CDSName, "CFE_ES_GetCDSBlockName() = %s", CDSNameBuf);
8484
UtAssert_INT32_EQ(CFE_ES_GetCDSBlockIDByName(&IdByName, CDSNameBuf), CFE_SUCCESS);
85-
cFE_FTAssert_ResourceID_EQ(CDSHandlePtr, IdByName);
85+
CFE_UtAssert_RESOURCEID_EQ(CDSHandlePtr, IdByName);
8686

8787
UtAssert_INT32_EQ(CFE_ES_GetCDSBlockName(NULL, CDSHandlePtr, sizeof(CDSNameBuf)), CFE_ES_BAD_ARGUMENT);
8888
UtAssert_INT32_EQ(CFE_ES_GetCDSBlockName(CDSNameBuf, CFE_ES_CDS_BAD_HANDLE, sizeof(CDSNameBuf)),

modules/cfe_testcase/src/es_counter_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void TestCounterCreateDelete(void)
8484

8585
/* Confirm conversion To/From Name */
8686
UtAssert_INT32_EQ(CFE_ES_GetGenCounterIDByName(&CheckId, CounterName), CFE_SUCCESS);
87-
cFE_FTAssert_ResourceID_EQ(CheckId, TestId);
87+
CFE_UtAssert_RESOURCEID_EQ(CheckId, TestId);
8888
UtAssert_INT32_EQ(CFE_ES_GetGenCounterName(CheckName, TestId, sizeof(CheckName)), CFE_SUCCESS);
8989
UtAssert_STRINGBUF_EQ(CheckName, sizeof(CheckName), CounterName, sizeof(CounterName));
9090

modules/cfe_testcase/src/es_info_test.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void TestAppInfo(void)
5252

5353
UtAssert_INT32_EQ(CFE_ES_GetAppIDByName(&AppIdByName, TEST_EXPECTED_APP_NAME), CFE_SUCCESS);
5454
UtAssert_INT32_EQ(CFE_ES_GetAppID(&TestAppId), CFE_SUCCESS);
55-
cFE_FTAssert_ResourceID_EQ(TestAppId, AppIdByName);
55+
CFE_UtAssert_RESOURCEID_EQ(TestAppId, AppIdByName);
5656
UtAssert_INT32_EQ(CFE_ES_GetAppName(AppNameBuf, TestAppId, sizeof(AppNameBuf)), CFE_SUCCESS);
5757
UtAssert_StrCmp(AppNameBuf, TEST_EXPECTED_APP_NAME, "CFE_ES_GetAppName() = %s", AppNameBuf);
5858

@@ -122,7 +122,7 @@ void TestAppInfo(void)
122122
UtAssert_True(ESAppInfo.NumOfChildTasks > 0, "ES App Info -> Child Tasks = %d", (int)ESAppInfo.NumOfChildTasks);
123123

124124
UtAssert_INT32_EQ(CFE_ES_GetAppIDByName(&AppIdByName, INVALID_APP_NAME), CFE_ES_ERR_NAME_NOT_FOUND);
125-
cFE_FTAssert_ResourceID_Undefined(AppIdByName);
125+
CFE_UtAssert_RESOURCEID_UNDEFINED(AppIdByName);
126126
UtAssert_INT32_EQ(CFE_ES_GetAppID(NULL), CFE_ES_BAD_ARGUMENT);
127127
UtAssert_INT32_EQ(CFE_ES_GetAppIDByName(NULL, TEST_EXPECTED_APP_NAME), CFE_ES_BAD_ARGUMENT);
128128
UtAssert_INT32_EQ(CFE_ES_GetAppName(AppNameBuf, CFE_ES_APPID_UNDEFINED, sizeof(AppNameBuf)),
@@ -146,15 +146,15 @@ void TestTaskInfo(void)
146146

147147
UtAssert_INT32_EQ(CFE_ES_GetTaskInfo(&TaskInfo, AppInfo.MainTaskId), CFE_SUCCESS);
148148
UtAssert_INT32_EQ(CFE_ES_GetTaskID(&TaskId), CFE_SUCCESS);
149-
cFE_FTAssert_ResourceID_EQ(TaskId, AppInfo.MainTaskId);
149+
CFE_UtAssert_RESOURCEID_EQ(TaskId, AppInfo.MainTaskId);
150150

151151
UtAssert_StrCmp(TaskInfo.AppName, AppInfo.Name, "TaskInfo.AppName (%s) = AppInfo.name (%s)", TaskInfo.AppName,
152152
AppInfo.Name);
153153
UtAssert_StrCmp(TaskInfo.TaskName, AppInfo.MainTaskName, "TaskInfo.TaskName (%s) = AppInfo.MainTaskName (%s)",
154154
TaskInfo.TaskName, AppInfo.MainTaskName);
155155

156-
cFE_FTAssert_ResourceID_EQ(TaskInfo.TaskId, AppInfo.MainTaskId);
157-
cFE_FTAssert_ResourceID_EQ(TaskInfo.AppId, AppId);
156+
CFE_UtAssert_RESOURCEID_EQ(TaskInfo.TaskId, AppInfo.MainTaskId);
157+
CFE_UtAssert_RESOURCEID_EQ(TaskInfo.AppId, AppId);
158158
UtAssert_INT32_EQ(TaskInfo.ExecutionCounter, AppInfo.ExecutionCounter);
159159

160160
UtAssert_INT32_EQ(CFE_ES_GetTaskInfo(&TaskInfo, CFE_ES_TASKID_UNDEFINED), CFE_ES_ERR_RESOURCEID_NOT_VALID);
@@ -205,7 +205,7 @@ void TestLibInfo(void)
205205

206206
UtAssert_INT32_EQ(LibInfo.ExceptionAction, 0);
207207
UtAssert_True(LibInfo.Priority == 0, "Lib Info -> Priority = %d", (int)LibInfo.Priority);
208-
cFE_FTAssert_ResourceID_Undefined(LibInfo.MainTaskId);
208+
CFE_UtAssert_RESOURCEID_UNDEFINED(LibInfo.MainTaskId);
209209
UtAssert_True(LibInfo.ExecutionCounter == 0, "Lib Info -> ExecutionCounter = %d", (int)LibInfo.ExecutionCounter);
210210
UtAssert_True(strlen(LibInfo.MainTaskName) == 0, "Lib Info -> Task Name = %s", LibInfo.MainTaskName);
211211
UtAssert_True(LibInfo.NumOfChildTasks == 0, "Lib Info -> Child Tasks = %d", (int)LibInfo.NumOfChildTasks);

modules/cfe_testcase/src/es_task_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ void TestChildTaskName(void)
108108
CFE_SUCCESS);
109109

110110
UtAssert_INT32_EQ(CFE_ES_GetTaskIDByName(&TaskIdByName, TaskName), CFE_SUCCESS);
111-
cFE_FTAssert_ResourceID_EQ(TaskIdByName, TaskId);
111+
CFE_UtAssert_RESOURCEID_EQ(TaskIdByName, TaskId);
112112

113113
UtAssert_INT32_EQ(CFE_ES_GetTaskName(TaskNameBuf, TaskId, sizeof(TaskNameBuf)), CFE_SUCCESS);
114114
UtAssert_StrCmp(TaskNameBuf, TaskName, "CFE_ES_GetTaskName() = %s", TaskNameBuf);
115115

116116
UtAssert_INT32_EQ(CFE_ES_GetTaskIDByName(NULL, TaskName), CFE_ES_BAD_ARGUMENT);
117117
UtAssert_INT32_EQ(CFE_ES_GetTaskIDByName(&TaskIdByName, INVALID_TASK_NAME), CFE_ES_ERR_NAME_NOT_FOUND);
118-
cFE_FTAssert_ResourceID_Undefined(TaskIdByName);
118+
CFE_UtAssert_RESOURCEID_UNDEFINED(TaskIdByName);
119119

120120
UtAssert_INT32_EQ(CFE_ES_GetTaskName(NULL, TaskId, sizeof(TaskNameBuf)), CFE_ES_BAD_ARGUMENT);
121121
UtAssert_INT32_EQ(CFE_ES_GetTaskName(TaskNameBuf, CFE_ES_TASKID_UNDEFINED, sizeof(TaskNameBuf)),

modules/cfe_testcase/src/fs_header_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void TestCreateHeader(void)
5959
UtAssert_INT32_EQ(OS_lseek(fd, 0, OS_SEEK_CUR), sizeof(CFE_FS_Header_t));
6060

6161
UtAssert_INT32_EQ(CFE_FS_WriteHeader(fd, NULL), CFE_FS_BAD_ARGUMENT);
62-
cFE_FTAssert_NOT_CFE_SUCCESS(CFE_FS_WriteHeader(OS_OBJECT_ID_UNDEFINED, &Header));
62+
CFE_UtAssert_STATUS_ERROR(CFE_FS_WriteHeader(OS_OBJECT_ID_UNDEFINED, &Header));
6363

6464
UtAssert_VOIDCALL(CFE_FS_InitHeader(NULL, TestDescription, CFE_FS_SubType_ES_ERLOG));
6565
UtAssert_VOIDCALL(CFE_FS_InitHeader(&HeaderFail, NULL, CFE_FS_SubType_ES_ERLOG));
@@ -88,7 +88,7 @@ void TestReadHeader(void)
8888
UtAssert_StrCmp(TestDescription, ReadHeader.Description, "ReadHeader.Description = %s", ReadHeader.Description);
8989

9090
UtAssert_INT32_EQ(CFE_FS_ReadHeader(NULL, fd), CFE_FS_BAD_ARGUMENT);
91-
cFE_FTAssert_NOT_CFE_SUCCESS(CFE_FS_ReadHeader(&ReadHeader, OS_OBJECT_ID_UNDEFINED));
91+
CFE_UtAssert_STATUS_ERROR(CFE_FS_ReadHeader(&ReadHeader, OS_OBJECT_ID_UNDEFINED));
9292

9393
OS_close(fd);
9494
OS_remove(OS_TEST_HEADER_FILENAME);
@@ -114,7 +114,7 @@ void TestTimeStamp(void)
114114
UtAssert_UINT32_EQ(0xFFFFFFFF, ReadHeader.TimeSeconds);
115115
UtAssert_UINT32_EQ(0xFFFFFFFF, ReadHeader.TimeSubSeconds);
116116

117-
cFE_FTAssert_NOT_CFE_SUCCESS(CFE_FS_SetTimestamp(OS_OBJECT_ID_UNDEFINED, NewTimestamp));
117+
CFE_UtAssert_STATUS_ERROR(CFE_FS_SetTimestamp(OS_OBJECT_ID_UNDEFINED, NewTimestamp));
118118

119119
OS_close(fd);
120120
OS_remove(OS_TEST_HEADER_FILENAME);

modules/cfe_testcase/src/sb_pipe_mang_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void TestPipeName(void)
112112
UtAssert_StrCmp(PipeNameBuf, PipeName, "CFE_SB_GetPipeName() = %s", PipeNameBuf);
113113

114114
UtAssert_INT32_EQ(CFE_SB_GetPipeIdByName(&PipeIdBuff, PipeName), CFE_SUCCESS);
115-
cFE_FTAssert_ResourceID_EQ(PipeId, PipeIdBuff);
115+
CFE_UtAssert_RESOURCEID_EQ(PipeId, PipeIdBuff);
116116

117117
UtAssert_INT32_EQ(CFE_SB_GetPipeName(NULL, sizeof(PipeNameBuf), PipeId), CFE_SB_BAD_ARGUMENT);
118118
UtAssert_INT32_EQ(CFE_SB_GetPipeName(PipeNameBuf, 0, PipeId), CFE_SB_BAD_ARGUMENT);

0 commit comments

Comments
 (0)