Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #397 - strawman for discussion #400

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/unit-tests/inc/ut_os_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@
*/
#define UT_OS_IO_BUFF_SIZE 128

static inline bool UtOsalRetVal(int32 Fn, int32 Exp, const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt, const char *Msg)
{
return UtAssertEx(Fn == Exp, UtAssert_GetContext(), File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg);
}

/* Only report errors */
static inline bool UtOsalCheck(int32 Fn, int32 Exp, const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt, const char *Msg)
{
return Fn == Exp ? true :
UtAssertEx(Fn == Exp, UtAssert_GetContext(), File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg);
}

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");
return false;
}

return true;
}


#define UT_OS_SUCCESS(Fn, Msg) UtOsalRetVal(Fn, OS_SUCCESS, __FILE__, __LINE__, #Fn, "OS_SUCCESS", Msg)
#define UT_OS_RETVAL(Fn, Exp, Msg) UtOsalRetVal(Fn, Exp, __FILE__, __LINE__, #Fn, #Exp, Msg)
#define UT_OS_CHECK(Fn, Msg) UtOsalCheck(Fn, OS_SUCCESS, __FILE__, __LINE__, #Fn, "OS_SUCCESS", Msg)
#define UT_OS_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__)

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

Expand Down
170 changes: 14 additions & 156 deletions src/unit-tests/oscore-test/ut_oscore_binsem_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,84 +34,6 @@
** Local function definitions
**--------------------------------------------------------------------------------*/

/* Test code template for testing a single OSAL API with multiple test cases */

#if 0
void UT_os_sample_test()
{
/* Must declare these variables for each function. They can be renamed.
* They're referenced in the macros used to track test cases and their results. */
int32 idx = 0;
const char* testDesc;

/*-----------------------------------------------------*
* For each test case,
* 1. Assign testDesc a brief description of the test
* 2. Setup the test environment, if necessary
* 3. Run the test
* 4. Log result by calling UT_OS_SET_TEST_RESULT_MACRO
* 4. Reset the test environment, if neccessary
*
* NOTE: "Not implemented" is always checked first but not
* being included as a test case.
* "Nominal" test case is always the last test case.
*-----------------------------------------------------*/

/*-----------------------------------------------------*/
testDesc = "API not implemented";

/* TODO: Setup the test environment, if necessary */

if (OS_xxx() == OS_ERR_NOT_IMPLEMENTED)
{
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_NA);
goto UT_os_sample_test_exit_tag;
}

/* TODO: Reset the test environment here, if necessary */

/*-----------------------------------------------------*/
testDesc = "#1 Null-pointer-arg";

/* TODO: Setup the test environment here, if necessary */

if (OS_xxx(NULL,...) == OS_INVALID_POINTER)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);

/* TODO: Reset the test environment here, if necessary */

/*-----------------------------------------------------*/
testDesc = "#2 Name-too-long-arg";

/* TODO: Setup the test environment here, if necessary */

if (OS_xxx(aVeryLoooooongName) == OS_ERR_NAME_TOO_LONG)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);

/* TODO: Reset the test environment here, if necessary */

/*-----------------------------------------------------*/
testDesc = "#3 Nominal";

/* TODO: Setup the test environment here, if necessary */

if (OS_xxx(...) != OS_SUCCESS)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);

/* TODO: Reset the test environment here, if necessary */

UT_os_sample_test_exit_tag:
return;

}
#endif

/*--------------------------------------------------------------------------------*
** Syntax: OS_BinSemCreate
** Purpose: Creates a binary semaphore
Expand All @@ -126,128 +48,64 @@ void UT_os_sample_test()
void UT_os_bin_sem_create_test()
{
int i;
int32 res = 0;
const char* testDesc;
uint32 test_setup_invalid = 0;
char sem_name[UT_OS_NAME_BUFF_SIZE];
char long_sem_name[UT_OS_NAME_BUFF_SIZE];
uint32 sem_ids[OS_MAX_BIN_SEMAPHORES+1];

/*-----------------------------------------------------*/
testDesc = "API not implemented";

res = OS_BinSemCreate(&sem_ids[0], "Good", 1, 0 );
if (res == OS_ERR_NOT_IMPLEMENTED)
{
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_NA);
goto UT_os_bin_sem_create_test_exit_tag;
}

/* Clean up */
if (!UT_OS_IMPL(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0))) return;
OS_BinSemDelete(sem_ids[0]);

/*-----------------------------------------------------*/
testDesc = "#1 Null-pointer-arg-1";

res = OS_BinSemCreate(NULL, "BinSem1", 1, 0);
if (res == OS_INVALID_POINTER)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);
UT_OS_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER, "#1 Null-pointer-arg-1");

/*-----------------------------------------------------*/
testDesc = "#2 Null-pointer-arg-2";

res = OS_BinSemCreate(&sem_ids[0], NULL, 1, 0);
if (res == OS_INVALID_POINTER)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);
UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER, "#2 Null-pointer-arg-2");

/*-----------------------------------------------------*/
testDesc = "#3 Name-too-long";

memset(long_sem_name, 'X', sizeof(long_sem_name));
long_sem_name[sizeof(long_sem_name)-1] = '\0';
res = OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0);
if (res == OS_ERR_NAME_TOO_LONG)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);
UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG, "#3 Name-too-long");

/*-----------------------------------------------------*/
testDesc = "#4 No-free-IDs";

/* Setup */
for ( i = 0; i< OS_MAX_BIN_SEMAPHORES; i++ )
{
memset(sem_name, '\0', sizeof(sem_name));
UT_os_sprintf(sem_name, "BINSEM%d", i);

res = OS_BinSemCreate(&sem_ids[i], sem_name, 1, 0);
if ( res != OS_SUCCESS )
if(!UT_OS_CHECK(OS_BinSemCreate(&sem_ids[i], sem_name, 1, 0), "BinSemCreate"))
{
testDesc = "#4 No-free-IDs - Bin Sem Create failed";
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_TSF);
test_setup_invalid = 1;
break;
break;
}
}

if ( test_setup_invalid == 0 )
if ( i == OS_MAX_BIN_SEMAPHORES )
{
res = OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0);
if (res == OS_ERR_NO_FREE_IDS)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);

UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS, "#4 No-free-IDs");
}

/* Reset test environment */
OS_DeleteAllObjects();

/*-----------------------------------------------------*/
testDesc = "#5 Duplicate-name";

/* Setup */
res = OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0);
if ( res != OS_SUCCESS )
if(UT_OS_CHECK(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), "BinSemCreate"))
{
testDesc = "#5 Duplicate-name - Bin Sem Create failed";
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_TSF);
}
else
{
res = OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0);
if (res == OS_ERR_NAME_TAKEN)
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);
UT_OS_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN, "#5 Duplicate-name");

/* Reset test environment */
res = OS_BinSemDelete(sem_ids[0]);
UT_OS_CHECK(OS_BinSemDelete(sem_ids[0]), "BinSemDelete");
}

/*-----------------------------------------------------*/
testDesc = "#6 OS-call-failure";

UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_INFO);
UT_OS_TEST_RESULT( "#6 OS-call-failure", UTASSERT_CASETYPE_INFO);

/*-----------------------------------------------------*/
testDesc = "#7 Nominal";

res = OS_BinSemCreate(&sem_ids[0], "Good", 1, 0);
if ( res == OS_SUCCESS )
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_PASS);
else
UT_OS_TEST_RESULT( testDesc, UTASSERT_CASETYPE_FAILURE);
UT_OS_SUCCESS(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0), "#7 Nominal");

/* Reset test environment */
res = OS_BinSemDelete(sem_ids[0]);

UT_os_bin_sem_create_test_exit_tag:
return;
UT_OS_CHECK(OS_BinSemDelete(sem_ids[0]), "BinSemDelete");
}

/*--------------------------------------------------------------------------------*
Expand Down