diff --git a/fsw/cfe-core/unit-test/sb_UT.c b/fsw/cfe-core/unit-test/sb_UT.c index d72cdc48b..bdcad05df 100644 --- a/fsw/cfe-core/unit-test/sb_UT.c +++ b/fsw/cfe-core/unit-test/sb_UT.c @@ -55,20 +55,34 @@ static inline bool CheckTrue(bool Exp, const char *Msg, const char *Filename, co return Exp; } -static inline bool CheckEq(const int32 ExpVal, const int32 ActVal, const char *Msg, +static inline bool CheckEq_i32(const int32 ExpVal, const int32 ActVal, const char *Msg, const char *Filename, const int Linenum) { snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "%s (exp=%d, act=%d)", Msg, ExpVal, ActVal); return CheckTrue(ExpVal == ActVal, cMsg, Filename, Linenum); } -static inline bool CheckNe(const int32 ExpVal, const int32 ActVal, const char *Msg, +static inline bool CheckNe_i32(const int32 ExpVal, const int32 ActVal, const char *Msg, const char *Filename, const int Linenum) { snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "%s (exp!=%d, act=%d)", Msg, ExpVal, ActVal); return CheckTrue(ExpVal != ActVal, cMsg, Filename, Linenum); } +static inline bool CheckEq_uint64(const uint64 ExpVal, const uint64 ActVal, const char *Msg, + const char *Filename, const int Linenum) +{ + snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "%s (exp=0x%lx, act=0x%lx)", Msg, ExpVal, ActVal); + return CheckTrue(ExpVal == ActVal, cMsg, Filename, Linenum); +} + +static inline bool CheckNe_uint64(const uint64 ExpVal, const uint64 ActVal, const char *Msg, + const char *Filename, const int Linenum) +{ + snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "%s (exp!=0x%lx, act=0x%lx)", Msg, ExpVal, ActVal); + return CheckTrue(ExpVal != ActVal, cMsg, Filename, Linenum); +} + static inline bool CheckEventSent(int32 EID, const char *Filename, const int Linenum, const char *EIDName) { bool t = UT_EventIsInHistory(EID); @@ -92,9 +106,11 @@ static inline bool CheckEventNotSent(int32 EID, const char *Filename, const int } #define UT_CHECK_TRUE(EXP, MSG) (CheckTrue((EXP), (MSG), __FILE__, __LINE__)) -#define UT_CHECK_EQ(EXP, ACT, MSG) (CheckEq((EXP), (ACT), (MSG), __FILE__, __LINE__)) -#define UT_CHECK_NE(EXP, ACT, MSG) (CheckNe((EXP), (ACT), (MSG), __FILE__, __LINE__)) -#define UT_CHECK_EVENT_CNT(EXP) (CheckEq((EXP), UT_GetNumEventsSent(), "EventSent", __FILE__, __LINE__)) +#define UT_CHECK_EQ(EXP, ACT, MSG) (CheckEq_i32((int32)(EXP), (int32)(ACT), (MSG), __FILE__, __LINE__)) +#define UT_CHECK_NE(EXP, ACT, MSG) (CheckNe_i32((int32)(EXP), (int32)(ACT), (MSG), __FILE__, __LINE__)) +#define UT_CHECK_EQ_UI64(EXP, ACT, MSG) (CheckEq_uint64((uint64)(EXP), (uint64)(ACT), (MSG), __FILE__, __LINE__)) +#define UT_CHECK_NE_UI64(EXP, ACT, MSG) (CheckNe_uint64((uint64)(EXP), (uint64)(ACT), (MSG), __FILE__, __LINE__)) +#define UT_CHECK_EVENT_CNT(EXP) (CheckEq_i32((EXP), UT_GetNumEventsSent(), "EventSent", __FILE__, __LINE__)) #define UT_CHECK_EVENT_NOTSENT(EID) (CheckEventNotSent((EID), __FILE__, __LINE__, #EID)) #define UT_CHECK_EVENT_SENT(EID) (CheckEventSent((EID), __FILE__, __LINE__, #EID)) @@ -3592,8 +3608,6 @@ void Test_SendMsg_ZeroCopyGetPtr(void) { CFE_SB_ZeroCopyHandle_t ZeroCpyBufHndl; uint16 MsgSize = 10; - cpuaddr ExpRtn; - cpuaddr ActRtn; UT_START("ZeroCopyGetPtr"); @@ -3603,67 +3617,22 @@ void Test_SendMsg_ZeroCopyGetPtr(void) * allocation failed) */ UT_SetDeferredRetcode(UT_KEY(CFE_ES_GetPoolBuf), 1, CFE_ES_ERR_MEM_BLOCK_SIZE); - ActRtn = (cpuaddr) CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl); - ExpRtn = (cpuaddr) NULL; - - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected return in ZeroCopyGetPtr test (buffer " - "descriptor), exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } + UT_CHECK_EQ_UI64(CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl), NULL, "ZeroCopyGetPtr test (buf descriptor)"); /* Have GetPoolBuf stub return error on its second call (actual buffer * allocation failed) */ UT_SetDeferredRetcode(UT_KEY(CFE_ES_GetPoolBuf), 2, CFE_ES_ERR_MEM_BLOCK_SIZE); - ActRtn = (cpuaddr) CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl); - ExpRtn = (cpuaddr) NULL; - - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected return in ZeroCopyGetPtr test, (buffer " - "allocation) exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } - + UT_CHECK_EQ_UI64(CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl), NULL, "ZeroCopyGetPtr test (buf allocation)"); /* Have GetPoolBuf stub return error on its second call (null buffer * returned and error returning the memory to the buffer) */ UT_SetDeferredRetcode(UT_KEY(CFE_ES_GetPoolBuf), 2, -1); UT_SetDeferredRetcode(UT_KEY(CFE_ES_PutPoolBuf), 1, 0); - ActRtn = (cpuaddr) CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl); - ExpRtn = (cpuaddr) NULL; + UT_CHECK_EQ_UI64(CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl), NULL, "ZeroCopyGetPtr test (null buf, put buf)"); - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected return in ZeroCopyGetPtr test, (null buffer, put " - "buffer) exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } - - ExpRtn = 0; - ActRtn = UT_GetNumEventsSent(); - - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected rtn from UT_GetNumEventsSent (get pool buffer), " - "exp=%ld, act=%ld", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } + UT_CHECK_EVENT_CNT(0); /* Increase the peak memory and buffers in use above the expected values in * order to exercise branch paths @@ -3672,57 +3641,14 @@ void Test_SendMsg_ZeroCopyGetPtr(void) sizeof(CFE_SB_BufferD_t)) * 5; CFE_SB.StatTlmMsg.Payload.PeakSBBuffersInUse = CFE_SB.StatTlmMsg.Payload.SBBuffersInUse + 2; - ActRtn = (cpuaddr) CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl); - ExpRtn = (cpuaddr) NULL; - if (ActRtn == ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected return in ZeroCopyGetPtr test, (branch paths) " - "exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } + UT_CHECK_NE_UI64(NULL, CFE_SB_ZeroCopyGetPtr(MsgSize, &ZeroCpyBufHndl), "ZeroCopyGetPtr test (branch paths)"); - ExpRtn = (MsgSize + sizeof(CFE_SB_BufferD_t)) * 5; - ActRtn = CFE_SB.StatTlmMsg.Payload.PeakMemInUse; + UT_CHECK_EQ_UI64((MsgSize + sizeof(CFE_SB_BufferD_t)) * 5, CFE_SB.StatTlmMsg.Payload.PeakMemInUse, "ZeroCopyGetPtr test (memory in use)"); - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected branch path taken in ZeroCopyGetPtr test " - "(memory in use), exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } - - ExpRtn = CFE_SB.StatTlmMsg.Payload.SBBuffersInUse + 1; - ActRtn = CFE_SB.StatTlmMsg.Payload.PeakSBBuffersInUse; - - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected branch path taken in ZeroCopyGetPtr test " - "(buffers in use), exp=0x%lx, act=0x%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } - - ExpRtn = 0; - ActRtn = (cpuaddr)UT_GetNumEventsSent(); + UT_CHECK_EQ_UI64(CFE_SB.StatTlmMsg.Payload.SBBuffersInUse + 1, CFE_SB.StatTlmMsg.Payload.PeakSBBuffersInUse, "branch path taken in ZeroCopyGetPtr test (buffers in use)"); - if (ActRtn != ExpRtn) - { - snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, - "Unexpected rtn from UT_GetNumEventsSent (branch paths), " - "exp=%lx, act=%lx", - (unsigned long) ExpRtn, (unsigned long) ActRtn); - UT_Text(cMsg); - TestStat = CFE_FAIL; - } + UT_CHECK_EQ_UI64(0, (cpuaddr)UT_GetNumEventsSent(), "retn from UI_GetNumEventsSent (branch paths)"); UT_Report(__FILE__, __LINE__, TestStat, "Test_SendMsg_API", "ZeroCopyGetPtr test"); @@ -3746,15 +3672,8 @@ void Test_SendMsg_ZeroCopySend(void) SB_ResetUnitTest(); CFE_SB_CreatePipe(&PipeId, PipeDepth, "ZeroCpyTestPipe"); CFE_SB_Subscribe(MsgId, PipeId); - ZeroCpyMsgPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), - &ZeroCpyBufHndl); - - if (ZeroCpyMsgPtr == NULL) - { - UT_Text("Unexpected NULL pointer returned from ZeroCopyGetPtr"); - TestStat = CFE_FAIL; - } - else + UT_CHECK_NE_UI64(NULL, ZeroCpyMsgPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), &ZeroCpyBufHndl), "ZeroCopyGetPtr()"); + if (TestStat == CFE_PASS) { CFE_SB_InitMsg(ZeroCpyMsgPtr, MsgId, sizeof(SB_UT_Test_Tlm_t), true); CCSDS_WR_SEQ(ZeroCpyMsgPtr->Hdr, 22); @@ -3769,12 +3688,7 @@ void Test_SendMsg_ZeroCopySend(void) UT_CHECK_EQ(CFE_SUCCESS, CFE_SB_RcvMsg(&PtrToMsg, PipeId, CFE_SB_PEND_FOREVER),"rcv"); - if (PtrToMsg == NULL) - { - UT_Text("Unexpected NULL return from rcv in zero copy send test"); - TestStat = CFE_FAIL; - } - else if (CCSDS_RD_SEQ(PtrToMsg->Hdr) != 1) + if (!UT_CHECK_NE_UI64(NULL, PtrToMsg, "return from rcv in ZeroCopySend()") && CCSDS_RD_SEQ(PtrToMsg->Hdr) != 1) { snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "Unexpected sequence count for send in sequence count test, " @@ -3813,15 +3727,7 @@ void Test_SendMsg_ZeroCopyPass(void) SB_ResetUnitTest(); CFE_SB_CreatePipe(&PipeId, PipeDepth, "ZeroCpyPassTestPipe"); CFE_SB_Subscribe(MsgId, PipeId); - ZeroCpyMsgPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), - &ZeroCpyBufHndl); - - if (ZeroCpyMsgPtr == NULL) - { - UT_Text("Unexpected NULL pointer returned from ZeroCopyGetPtr"); - TestStat = CFE_FAIL; - } - else + if(!UT_CHECK_NE_UI64(NULL, ZeroCpyMsgPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), &ZeroCpyBufHndl), "ZeroCopyGetPtr")) { CFE_SB_InitMsg(ZeroCpyMsgPtr, MsgId, sizeof(SB_UT_Test_Tlm_t), true); CCSDS_WR_SEQ(ZeroCpyMsgPtr->Hdr, Seq); @@ -3836,12 +3742,7 @@ void Test_SendMsg_ZeroCopyPass(void) UT_CHECK_EQ(CFE_SUCCESS, CFE_SB_RcvMsg(&PtrToMsg, PipeId, CFE_SB_PEND_FOREVER), "recv in zero copy pass"); - if (PtrToMsg == NULL) - { - UT_Text("Unexpected NULL return from receive in zero copy pass test"); - TestStat = CFE_FAIL; - } - else if (CCSDS_RD_SEQ(PtrToMsg->Hdr) != Seq) + if (!UT_CHECK_NE_UI64(NULL, PtrToMsg, "return from rcv in zero copy pass test") && CCSDS_RD_SEQ(PtrToMsg->Hdr) != Seq) { snprintf(cMsg, UT_MAX_MESSAGE_LENGTH, "Unexpected sequence count for send in sequence count test, exp=%d, act=%d", @@ -4003,14 +3904,7 @@ void Test_SendMsg_InvalidMsgId_ZeroCopy(void) UT_START("Invalid Message ID (ZeroCopy)"); SB_ResetUnitTest(); - TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), - (CFE_SB_ZeroCopyHandle_t *) &TlmPkt); - if (TlmPktPtr == NULL) - { - UT_Text("Unexpected NULL pointer returned from ZeroCopyGetPtr"); - TestStat = CFE_FAIL; - } - else + if (!UT_CHECK_NE_UI64(NULL, TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), (CFE_SB_ZeroCopyHandle_t *) &TlmPkt), "ZeroCopyGetPtr")) { CFE_SB_InitMsg(TlmPktPtr, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID + 1, sizeof(SB_UT_Test_Tlm_t), true); @@ -4040,14 +3934,7 @@ void Test_SendMsg_MaxMsgSizePlusOne_ZeroCopy(void) UT_START("Maximum Message Size Plus One (ZeroCopy)"); SB_ResetUnitTest(); - TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), - (CFE_SB_ZeroCopyHandle_t *) &TlmPkt); - if (TlmPktPtr == NULL) - { - UT_Text("Unexpected NULL pointer returned from ZeroCopyGetPtr"); - TestStat = CFE_FAIL; - } - else + if (!UT_CHECK_NE_UI64(NULL, TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), (CFE_SB_ZeroCopyHandle_t *) &TlmPkt), "ZeroCopyGetPtr")) { CFE_SB_InitMsg(TlmPktPtr, MsgId, CFE_MISSION_SB_MAX_SB_MSG_SIZE + 1, false); UT_CHECK_EQ(CFE_SB_MSG_TOO_BIG, CFE_SB_SendMsgFull(TlmPktPtr, CFE_SB_INCREMENT_TLM, CFE_SB_SEND_ZEROCOPY),"max msg sz +1"); @@ -4075,14 +3962,7 @@ void Test_SendMsg_NoSubscribers_ZeroCopy(void) UT_START("No Subscribers (ZeroCopy)"); SB_ResetUnitTest(); - TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), - (CFE_SB_ZeroCopyHandle_t *) &TlmPkt); - if (TlmPktPtr == NULL) - { - UT_Text("Unexpected NULL pointer returned from ZeroCopyGetPtr"); - TestStat = CFE_FAIL; - } - else + if (!UT_CHECK_NE_UI64(NULL, TlmPktPtr = CFE_SB_ZeroCopyGetPtr(sizeof(SB_UT_Test_Tlm_t), (CFE_SB_ZeroCopyHandle_t *) &TlmPkt), "ZeroCopyGetPtr")) { CFE_SB_InitMsg(TlmPktPtr, MsgId, sizeof(SB_UT_Test_Tlm_t), true); UT_CHECK_EQ(CFE_SUCCESS, CFE_SB_SendMsgFull(TlmPktPtr, CFE_SB_INCREMENT_TLM, CFE_SB_SEND_ZEROCOPY), "no sub"); @@ -4412,11 +4292,7 @@ void Test_CleanupApp_API(void) CFE_SB.PipeTbl[1].InUse = CFE_SB_IN_USE; CFE_SB.PipeTbl[1].AppId = 1; - if (CFE_SB.ZeroCopyTail == NULL) - { - UT_Text("Unexpected ZeroCopyTail UT_GetNumEventsSent, exp = not NULL"); - TestStat = CFE_FAIL; - } + UT_CHECK_NE_UI64(NULL, CFE_SB.ZeroCopyTail, "ZeroCopyTail UT_GetNumEventsSent"); /* Attempt with a bad application ID first in order to get full branch path * coverage in CFE_SB_ZeroCopyReleaseAppId @@ -4426,11 +4302,7 @@ void Test_CleanupApp_API(void) /* Attempt again with a valid application ID */ CFE_SB_CleanUpApp(0); - if (CFE_SB.ZeroCopyTail != NULL) - { - UT_Text("Unexpected ZeroCopyTail UT_GetNumEventsSent, exp = NULL"); - TestStat = CFE_FAIL; - } + UT_CHECK_NE_UI64(NULL, CFE_SB.ZeroCopyTail, "ZeroCopyTail UT_GetNumEventsSent"); UT_CHECK_EVENT_CNT(3);