Skip to content

Commit

Permalink
Fix nasa#1743, update coverage test to use UtAssert macros
Browse files Browse the repository at this point in the history
A number of CFE coverage-specific macros and generic assert functions
were moved into OSAL UtAssert library so it can be used in a wider
variety of tests.

This removes the CFE coverage-specific version, and changes all
references to use the equivalent UtAssert macro.
  • Loading branch information
jphickey committed Aug 2, 2021
1 parent cc8c9a1 commit 961d4eb
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 651 deletions.
232 changes: 18 additions & 214 deletions modules/core_private/ut-stubs/inc/ut_support.h

Large diffs are not rendered by default.

271 changes: 2 additions & 269 deletions modules/core_private/ut-stubs/src/ut_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,93 +632,6 @@ uint32 UT_PrintfIsInHistory(const char *MsgToSearchFor)
return UT_GetMessageCount(MsgToSearchFor, &UT_PrintfBuffer, UT_StrCmpFormatStr);
}

void UT_AddSubTest(void (*Test)(void), void (*Setup)(void), void (*Teardown)(void), const char *GroupName,
const char *TestName)
{
char CompleteTestName[128];
const char *GroupPtr;
const char *TestPtr;

/* Remove any common prefix between the two names.
* They are often function names that all start with "Test_XXX"
* and this repetitive information just becomes clutter.
*/
GroupPtr = GroupName;
TestPtr = TestName;
while (*GroupPtr != 0 && *GroupPtr == *TestPtr)
{
++GroupPtr;
++TestPtr;
}

/*
* Only break at an underscore(_) to avoid weird effects
*/
while (TestPtr > TestName && *TestPtr != '_')
{
--TestPtr;
}
if (*TestPtr == '_')
{
++TestPtr;
}

/*
* Remove a remaining "Test_" prefix on the group name.
* Again just to remove common repetitive content
*/
GroupPtr = GroupName;
if (strncmp(GroupPtr, "Test_", 5) == 0)
{
GroupPtr += 5;
}

(void)snprintf(CompleteTestName, sizeof(CompleteTestName), "%s.%s", GroupPtr, TestPtr);

/*
* Note that UtTest_Add() does not copy the string - it retains the passed-in pointer.
* Therefore it must be duplicated.
*
* This isn't an issue on Linux as the memory is freed when the process exits, but
* might be an issue on VxWorks.
*/

UtTest_Add(Test, Setup, Teardown, strdup(CompleteTestName));
}

const char *CFE_UtAssert_GetOpText(CFE_UtAssert_Compare_t CompareType)
{
const char *OpText;

switch (CompareType)
{
case CFE_UtAssert_Compare_EQ: /* actual equals reference value */
case CFE_UtAssert_Compare_BOOL: /* actual and reference are logical boolean values */
OpText = "==";
break;
case CFE_UtAssert_Compare_NEQ: /* actual does not non equal reference value */
OpText = "!=";
break;
case CFE_UtAssert_Compare_LT: /* actual less than reference (exclusive) */
OpText = "<";
break;
case CFE_UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
OpText = ">";
break;
case CFE_UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
OpText = "<=";
break;
case CFE_UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
OpText = ">=";
break;
default: /* should never happen */
OpText = "??";
break;
}

return OpText;
}

bool CFE_UtAssert_SuccessCheck_Impl(CFE_Status_t Status, UtAssert_CaseType_t CaseType, const char *File, uint32 Line,
const char *Text)
{
Expand All @@ -732,93 +645,6 @@ bool CFE_UtAssert_SuccessCheck_Impl(CFE_Status_t Status, UtAssert_CaseType_t Cas
return Result;
}

bool CFE_UtAssert_GenericUnsignedCompare_Impl(uint32 ActualValue, CFE_UtAssert_Compare_t CompareType,
uint32 ReferenceValue, const char *File, uint32 Line, const char *Desc,
const char *ActualText, const char *ReferenceText)
{
bool Result;

switch (CompareType)
{
case CFE_UtAssert_Compare_EQ: /* actual equals reference value */
Result = (ActualValue == ReferenceValue);
break;
case CFE_UtAssert_Compare_NEQ: /* actual does not non equal reference value */
Result = (ActualValue != ReferenceValue);
break;
case CFE_UtAssert_Compare_LT: /* actual less than reference (exclusive) */
Result = (ActualValue < ReferenceValue);
break;
case CFE_UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
Result = (ActualValue > ReferenceValue);
break;
case CFE_UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
Result = (ActualValue <= ReferenceValue);
break;
case CFE_UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
Result = (ActualValue >= ReferenceValue);
break;
case CFE_UtAssert_Compare_BOOL: /* actual and reference are logical boolean values */
Result = ActualValue;
if (!ReferenceValue)
{
/* Invert the result if reference is false */
Result = !Result;
}
break;
default: /* should never happen */
Result = false;
break;
}

return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, "%s%s (0x%lx) %s %s (0x%lx)", Desc, ActualText,
(unsigned long)ActualValue, CFE_UtAssert_GetOpText(CompareType), ReferenceText,
(unsigned long)ReferenceValue);
}

bool CFE_UtAssert_GenericSignedCompare_Impl(int32 ActualValue, CFE_UtAssert_Compare_t CompareType, int32 ReferenceValue,
const char *File, uint32 Line, const char *Desc, const char *ActualText,
const char *ReferenceText)
{
bool Result;

switch (CompareType)
{
case CFE_UtAssert_Compare_EQ: /* actual equals reference value */
Result = (ActualValue == ReferenceValue);
break;
case CFE_UtAssert_Compare_NEQ: /* actual does not non equal reference value */
Result = (ActualValue != ReferenceValue);
break;
case CFE_UtAssert_Compare_LT: /* actual less than reference (exclusive) */
Result = (ActualValue < ReferenceValue);
break;
case CFE_UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
Result = (ActualValue > ReferenceValue);
break;
case CFE_UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
Result = (ActualValue <= ReferenceValue);
break;
case CFE_UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
Result = (ActualValue >= ReferenceValue);
break;
case CFE_UtAssert_Compare_BOOL: /* actual and reference are logical boolean values */
Result = ActualValue;
if (!ReferenceValue)
{
/* Invert the result if reference is false */
Result = !Result;
}
break;
default: /* should never happen */
Result = false;
break;
}

return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, "%s%s (%ld) %s %s (%ld)", Desc, ActualText,
(long)ActualValue, CFE_UtAssert_GetOpText(CompareType), ReferenceText, (long)ReferenceValue);
}

bool CFE_UtAssert_MessageCheck_Impl(bool Status, const char *File, uint32 Line, const char *Desc,
const char *FormatString)
{
Expand Down Expand Up @@ -851,99 +677,6 @@ bool CFE_UtAssert_MessageCheck_Impl(bool Status, const char *File, uint32 Line,
ScrubbedFormat[FormatLen] = '\'';
ScrubbedFormat[FormatLen + 1] = 0;

return CFE_UtAssert_GenericSignedCompare_Impl(Status, CFE_UtAssert_Compare_GT, 0, File, Line, Desc, ScrubbedFormat,
"");
}

bool CFE_UtAssert_StringBufCheck_Impl(const char *String1, size_t String1Max, const char *String2, size_t String2Max,
const char *File, uint32 Line)
{
char ScrubbedString1[256];
char ScrubbedString2[256];
const char *EndPtr1;
const char *EndPtr2;
size_t FormatLen1;
size_t FormatLen2;
bool Result;

/* Locate the actual end of both strings */
if (String1 == NULL)
{
EndPtr1 = NULL;
}
else
{
EndPtr1 = memchr(String1, 0, String1Max);
}

if (EndPtr1 != NULL)
{
FormatLen1 = EndPtr1 - String1;
}
else
{
FormatLen1 = String1Max;
}

if (String2 == NULL)
{
EndPtr2 = NULL;
}
else
{
EndPtr2 = memchr(String2, 0, String2Max);
}

if (EndPtr2 != NULL)
{
FormatLen2 = EndPtr2 - String2;
}
else
{
FormatLen2 = String2Max;
}

if (FormatLen1 != FormatLen2)
{
/* This means the strings have different termination/length, and therefore must not be equal (content doesn't
* matter) */
Result = false;
}
else if (FormatLen1 == 0)
{
/* Two empty strings are considered equal */
Result = true;
}
else
{
/* If the effective lengths are the same, use memcmp to check content */
Result = (memcmp(String1, String2, FormatLen1) == 0);
}

/* Now make "safe" copies of the strings */
/* Check for a newline within the string, and if present, end the string there instead */
if (FormatLen1 > 0)
{
EndPtr1 = memchr(String1, '\n', FormatLen1);
if (EndPtr1 != NULL)
{
FormatLen1 = EndPtr1 - String1;
}
memcpy(ScrubbedString1, String1, FormatLen1);
}
ScrubbedString1[FormatLen1] = 0;

if (FormatLen2 > 0)
{
EndPtr2 = memchr(String2, '\n', FormatLen2);
if (EndPtr2 != NULL)
{
FormatLen2 = EndPtr2 - String2;
}
memcpy(ScrubbedString2, String2, FormatLen2);
}
ScrubbedString2[FormatLen2] = 0;

return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, "String: \'%s\' == \'%s\'", ScrubbedString1,
ScrubbedString2);
return UtAssert_GenericSignedCompare(Status, UtAssert_Compare_GT, 0, UtAssert_Radix_DECIMAL, File, Line, Desc,
ScrubbedFormat, "");
}
Loading

0 comments on commit 961d4eb

Please sign in to comment.