Skip to content

Commit

Permalink
Fix nasa#981, rework "unit-tests" to use macros
Browse files Browse the repository at this point in the history
Update most of the conditions in the "unit tests" to use the
support macros whenever possible.  The macros display the
function being called as well as the return value being checked
for, and therefore this makes it so the test log file can be
cross referenced back to the documentation to ensure the
documented return codes are being tested.

As a nice side effect this also removes quite a bit of
repetition in the test programs.
  • Loading branch information
jphickey committed May 12, 2021
1 parent 706f0de commit 8d17673
Show file tree
Hide file tree
Showing 17 changed files with 1,491 additions and 4,561 deletions.
49 changes: 33 additions & 16 deletions src/unit-tests/inc/ut_os_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,59 @@
*/
#define UT_OS_IO_BUFF_SIZE 128

static inline bool UtOsalRetVal(int32 Fn, int32 Exp, UtAssert_CaseType_t casetype, const char *File, uint32 Line,
const char *FnTxt, const char *ExpTxt, const char *Msg)
static inline bool UtOsalRetVal(int32 Fn, int32 Exp, bool NotImplAllowed, UtAssert_CaseType_t casetype,
const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt)
{
return UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg);
/* If "not implemented" is acceptable, override the casetype to be N/A */
if (NotImplAllowed && (Fn == OS_ERR_NOT_IMPLEMENTED || Fn == OS_ERR_OPERATION_NOT_SUPPORTED))
{
casetype = UTASSERT_CASETYPE_NA;
}

return UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d)", FnTxt, (int)Fn, ExpTxt, (int)Exp);
}

static inline bool UtManualInspectionWithStatus(int32 Fn, const char *File, uint32 Line, const char *FnTxt)
{
UtAssertEx(false, UTASSERT_CASETYPE_MIR, File, Line, "%s value=%d", FnTxt, (int)Fn);
return (Fn >= 0);
}

/* Only report errors */
static inline bool UtOsalCheck(int32 Fn, int32 Exp, UtAssert_CaseType_t casetype, const char *File, uint32 Line,
const char *FnTxt, const char *ExpTxt)
static inline bool UtOsalCheck(int32 Fn, UtAssert_CaseType_t casetype, const char *File, uint32 Line, const char *FnTxt)
{
return Fn == Exp
? true
: UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d)", FnTxt, (int)Fn, ExpTxt, (int)Exp);
/* Check non-negative to support APIs that return nonzero on success (e.g. read/write) */
if (Fn >= 0)
{
return true;
}

return UtAssertEx(false, casetype, File, Line, "%s (%d) >= %s", FnTxt, (int)Fn, "OS_SUCCESS");
}

static inline bool UtOsalImplemented(int32 Fn, const char *File, uint32 Line)
{
if (Fn == OS_ERR_NOT_IMPLEMENTED)
{
UtAssertEx(false, UTASSERT_CASETYPE_NA, File, Line, "API not implemented");
UtAssertEx(false, UTASSERT_CASETYPE_NA, File, Line, "API not implemented (%d)", (int)Fn);
return false;
}

return true;
}

#define UT_RETVAL(Fn, Exp) UtOsalRetVal(Fn, Exp, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, #Exp)
#define UT_NOMINAL(Fn) \
UtOsalRetVal(Fn, OS_SUCCESS, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS", "Nominal")
#define UT_RETVAL(Fn, Exp, Msg) UtOsalRetVal(Fn, Exp, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, #Exp, Msg)
#define UT_SETUP(Fn) UtOsalCheck(Fn, OS_SUCCESS, UTASSERT_CASETYPE_TSF, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
#define UT_TEARDOWN(Fn) UtOsalCheck(Fn, OS_SUCCESS, UTASSERT_CASETYPE_TTF, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
#define UT_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__)
UtOsalRetVal(Fn, OS_SUCCESS, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
#define UT_NOMINAL_OR_NOTIMPL(Fn) \
UtOsalRetVal(Fn, OS_SUCCESS, true, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS")

/*--------------------------------------------------------------------------------*/
#define UT_MIR_STATUS(Fn) UtManualInspectionWithStatus(Fn, __FILE__, __LINE__, #Fn)
#define UT_MIR_VOID(Fn) Fn, UtAssertEx(false, UTASSERT_CASETYPE_MIR, __FILE__, __LINE__, "%s", #Fn)

#define UT_OS_TEST_RESULT(descStr, caseType) UtAssertEx(false, caseType, __FILE__, __LINE__, "%s", descStr)
#define UT_SETUP(Fn) UtOsalCheck(Fn, UTASSERT_CASETYPE_TSF, __FILE__, __LINE__, #Fn)
#define UT_TEARDOWN(Fn) UtOsalCheck(Fn, UTASSERT_CASETYPE_TTF, __FILE__, __LINE__, #Fn)
#define UT_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__)

/*--------------------------------------------------------------------------------*/

Expand Down
68 changes: 17 additions & 51 deletions src/unit-tests/oscore-test/ut_oscore_binsem_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,15 @@ void UT_os_bin_sem_create_test()
osal_id_t sem_ids[OS_MAX_BIN_SEMAPHORES + 1];

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0)))
return;
UT_TEARDOWN(OS_BinSemDelete(sem_ids[0]));

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER, "null pointer arg 1");
UT_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER);

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER, "null pointer arg 2");
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER);

/*-----------------------------------------------------*/
memset(long_sem_name, 'X', sizeof(long_sem_name));
long_sem_name[sizeof(long_sem_name) - 1] = '\0';
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG, "name too long");
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG);

/*-----------------------------------------------------*/
/* Setup */
Expand All @@ -103,8 +98,7 @@ void UT_os_bin_sem_create_test()

if (i == OS_MAX_BIN_SEMAPHORES) /* setup was successful */
{
UT_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS,
"no free ids");
UT_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS);
}

/* Reset test environment */
Expand All @@ -113,7 +107,7 @@ void UT_os_bin_sem_create_test()
/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0)))
{
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN, "duplicate name");
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN);

/* Reset test environment */
UT_TEARDOWN(OS_BinSemDelete(sem_ids[0]));
Expand All @@ -139,11 +133,7 @@ void UT_os_bin_sem_delete_test()
osal_id_t bin_sem_id;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemDelete(OS_OBJECT_ID_UNDEFINED)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemDelete(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
UT_RETVAL(OS_BinSemDelete(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "DeleteTest", 1, 0)))
Expand All @@ -166,11 +156,7 @@ void UT_os_bin_sem_flush_test()
osal_id_t bin_sem_id;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemFlush(OS_OBJECT_ID_UNDEFINED)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemFlush(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
UT_RETVAL(OS_BinSemFlush(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);

/*----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "FlushTest", 1, 0)))
Expand All @@ -194,11 +180,7 @@ void UT_os_bin_sem_give_test()
osal_id_t bin_sem_id;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemGive(OS_OBJECT_ID_UNDEFINED)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemGive(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
UT_RETVAL(OS_BinSemGive(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GiveTest", 1, 0)))
Expand All @@ -223,11 +205,7 @@ void UT_os_bin_sem_take_test()
osal_id_t bin_sem_id;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemTake(OS_OBJECT_ID_UNDEFINED)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemTake(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
UT_RETVAL(OS_BinSemTake(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "TakeTest", 1, 0)))
Expand All @@ -251,16 +229,12 @@ void UT_os_bin_sem_timed_wait_test()
osal_id_t bin_sem_id;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemTimedWait(OS_OBJECT_ID_UNDEFINED, 1000)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemTimedWait(UT_OBJID_INCORRECT, 1000), OS_ERR_INVALID_ID, "invalid id arg");
UT_RETVAL(OS_BinSemTimedWait(UT_OBJID_INCORRECT, 1000), OS_ERR_INVALID_ID);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "TimedWait", 1, 0)) && UT_SETUP(OS_BinSemTake(bin_sem_id)))
{
UT_RETVAL(OS_BinSemTimedWait(bin_sem_id, 1000), OS_SEM_TIMEOUT, "semtake timed out");
UT_RETVAL(OS_BinSemTimedWait(bin_sem_id, 1000), OS_SEM_TIMEOUT);
UT_TEARDOWN(OS_BinSemDelete(bin_sem_id));
}

Expand Down Expand Up @@ -288,22 +262,18 @@ void UT_os_bin_sem_get_id_by_name_test()
char long_sem_name[UT_OS_NAME_BUFF_SIZE];

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemGetIdByName(NULL, "InvalidName")))
return;
UT_RETVAL(OS_BinSemGetIdByName(NULL, "InvalidName"), OS_INVALID_POINTER);

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemGetIdByName(NULL, "InvalidName"), OS_INVALID_POINTER, "invalid ptr arg 1");

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, NULL), OS_INVALID_POINTER, "invalid ptr arg 2");
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, NULL), OS_INVALID_POINTER);

/*-----------------------------------------------------*/
memset(long_sem_name, 'Y', sizeof(long_sem_name));
long_sem_name[sizeof(long_sem_name) - 1] = '\0';
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, long_sem_name), OS_ERR_NAME_TOO_LONG, "name too long");
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, long_sem_name), OS_ERR_NAME_TOO_LONG);

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, "NameNotFound"), OS_ERR_NAME_NOT_FOUND, "name not found");
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, "NameNotFound"), OS_ERR_NAME_NOT_FOUND);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GetIDByName", 1, 0)))
Expand All @@ -328,16 +298,12 @@ void UT_os_bin_sem_get_info_test()
OS_bin_sem_prop_t bin_sem_prop;

/*-----------------------------------------------------*/
if (!UT_IMPL(OS_BinSemGetInfo(OS_OBJECT_ID_UNDEFINED, &bin_sem_prop)))
return;

/*-----------------------------------------------------*/
UT_RETVAL(OS_BinSemGetInfo(UT_OBJID_INCORRECT, &bin_sem_prop), OS_ERR_INVALID_ID, "invalid id");
UT_RETVAL(OS_BinSemGetInfo(UT_OBJID_INCORRECT, &bin_sem_prop), OS_ERR_INVALID_ID);

/*-----------------------------------------------------*/
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GetInfo", 1, 0)))
{
UT_RETVAL(OS_BinSemGetInfo(bin_sem_id, NULL), OS_INVALID_POINTER, "invalid ptr");
UT_RETVAL(OS_BinSemGetInfo(bin_sem_id, NULL), OS_INVALID_POINTER);
UT_TEARDOWN(OS_BinSemDelete(bin_sem_id));
}

Expand Down
Loading

0 comments on commit 8d17673

Please sign in to comment.