Skip to content

Commit

Permalink
Fix nasa#374, created rigorous idmap-api-test functional tests and re…
Browse files Browse the repository at this point in the history
…moved the ReadMe
  • Loading branch information
yammajamma committed May 6, 2020
1 parent 2fa5e28 commit 8fbea56
Showing 1 changed file with 216 additions and 37 deletions.
253 changes: 216 additions & 37 deletions src/tests/idmap-api-test/idmap-api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,256 @@

/* *************************************** MAIN ************************************** */

uint32 ObjectFoundCount = 0;
typedef struct
{
uint32 TaskCount;
uint32 QueueCount;
uint32 CountSemCount;
uint32 BinSemCount;
uint32 MutexCount;
uint32 TimeBaseCount;
uint32 OtherCount;
} Test_OS_ObjTypeCount_t;

void OS_Test_ForEachObject(uint32 object_id, void *arg)
static void ObjTypeCounter(uint32 object_id, void *arg)
{
uint32 *ObjectCount;
ObjectCount = (uint32 *)arg;
++(*ObjectCount);
Test_OS_ObjTypeCount_t *count = arg;

switch(OS_IdentifyObject(object_id))
{
case OS_OBJECT_TYPE_OS_FILESYS:
ObjectFoundCount++;
case OS_OBJECT_TYPE_OS_TASK:
++count->TaskCount;
break;
case OS_OBJECT_TYPE_OS_QUEUE:
++count->QueueCount;
break;
case OS_OBJECT_TYPE_OS_CONSOLE:
ObjectFoundCount++;
case OS_OBJECT_TYPE_OS_COUNTSEM:
++count->CountSemCount;
break;
case OS_OBJECT_TYPE_OS_BINSEM:
ObjectFoundCount++;
++count->BinSemCount;
break;
case OS_OBJECT_TYPE_OS_MUTEX:
++count->MutexCount;
break;
case OS_OBJECT_TYPE_OS_TIMEBASE:
++count->TimeBaseCount;
break;
default:
++count->OtherCount;
break;
}
} /* end OS_Test_ForEachObject */
}

/*
* A void test function that creates an object for testing
*/
void Test_Void_Fn(void)
{
uint32 bin_sem_id_my_task;
OS_BinSemCreate( &bin_sem_id_my_task, "BinSemTaskMyTask", 1, 0);
OS_TaskDelay(5);

} /* end Test_Void_Fn */


/* *************************************** MAIN ************************************** */

void TestIdMapApi(void)
{
int32 expected;
int32 actual;
uint32 task_id;
uint32 queue_id;
uint32 count_sem_id;
uint32 bin_sem_id;
uint32 mutex_id1;
uint32 mutex_id2;
uint32 mutex_id3;
uint32 time_base_id;
uint32 TestArrayIndex;
Test_OS_ObjTypeCount_t Count;

/*
* Create all allowed objects
*/
OS_TaskCreate( &task_id, "Task", Test_Void_Fn, 0, 0, 0, 0);
OS_QueueCreate( &queue_id, "Queue", 5, 5, 0);
OS_CountSemCreate( &count_sem_id, "CountSem", 1, 0);
OS_BinSemCreate( &bin_sem_id, "BinSem", 1, 0);
OS_MutSemCreate( &mutex_id1, "Mutex1", 0);
OS_MutSemCreate( &mutex_id2, "Mutex2", 0);
OS_MutSemCreate( &mutex_id3, "Mutex3", 0);
OS_TimeBaseCreate( &time_base_id, "TimeBase", 0);

/*
* NOTE: The following objects were not created and tested:
* OS_OBJECT_TYPE_OS_STREAM
* OS_OBJECT_TYPE_OS_DIR
* OS_OBJECT_TYPE_OS_TIMECB
* OS_OBJECT_TYPE_OS_FILESYS
* OS_OBJECT_TYPE_OS_CONSOLE
* OS_OBJECT_TYPE_USER
*/

/*
* Test Case For:
* int32 OS_IdentifyObject(void)
*/
int32 expected = 99999999 >> OS_OBJECT_TYPE_SHIFT;
int32 actual = OS_IdentifyObject(99999999);
uint32 TestArrayIndex;

UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
/*
* Test with nominal values
*/
expected = OS_OBJECT_TYPE_OS_TASK;
actual = OS_IdentifyObject(task_id);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

expected = OS_OBJECT_TYPE_OS_QUEUE;
actual = OS_IdentifyObject(queue_id);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

expected = OS_OBJECT_TYPE_OS_COUNTSEM;
actual = OS_IdentifyObject(count_sem_id);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

expected = OS_OBJECT_TYPE_OS_BINSEM;
actual = OS_IdentifyObject(bin_sem_id);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

expected = OS_OBJECT_TYPE_OS_MUTEX;
actual = OS_IdentifyObject(mutex_id1);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

expected = OS_OBJECT_TYPE_OS_TIMEBASE;
actual = OS_IdentifyObject(time_base_id);
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);

/*
* Test with extreme cases using min and max values
* Note: There are no asserts, checks or expected values
* here. The only check is that the function doesn't return
* an error when called
*/
OS_IdentifyObject(0x00000);
OS_IdentifyObject(0xFFFFFFFF);

/*
* Test Case For:
* int32 OS_ConvertToArrayIndex(void)
*/
expected = OS_SUCCESS;
actual = OS_ConvertToArrayIndex(0x0004FFFF, &TestArrayIndex);

UtAssert_True(actual == expected && TestArrayIndex == 15 , "OS_ConvertToArrayIndex() (%ld) == %ld TestArrayIndex(%lu) == 15", (long)actual, (long)expected, (long)TestArrayIndex);

/*
* Check different id types and verify array indices
* Each Object Type index is added to an array index of its own type
* Each object type is checked once, and MUTEX is checked twice to
* verify multiple indices
*/

/*
* Test with nominal values
*/
actual = OS_ConvertToArrayIndex(task_id, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(queue_id, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(count_sem_id, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(bin_sem_id, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 2 , "TestArrayIndex(%lu) == 2", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(mutex_id1, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(mutex_id2, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 2 , "TestArrayIndex(%lu) == 2", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(mutex_id3, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 3 , "TestArrayIndex(%lu) == 3", (long)TestArrayIndex);

actual = OS_ConvertToArrayIndex(time_base_id, &TestArrayIndex);
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);

/*
* Test with extreme cases using invalid inputs and checking
* for an error return code
*/
actual = OS_ConvertToArrayIndex(0x0000, &TestArrayIndex);
expected = OS_ERR_INCORRECT_OBJ_TYPE;
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );

actual = OS_ConvertToArrayIndex(0xFFFFFFFF, &TestArrayIndex);
expected = OS_ERR_INCORRECT_OBJ_TYPE;
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );

/*
* Test Case For:
* void OS_ForEachObject (uint32 creator_id, OS_ArgCallback_t callback_ptr, void *callback_arg);
*/
uint32 ObjectCount;
uint32 TryCount;
memset(&Count, 0, sizeof(Count));

OS_ForEachObject (0, &ObjTypeCounter, &Count);

/* Verify Outputs */
UtAssert_True(Count.TaskCount == 1, "OS_ForEachObject() TaskCount (%lu) == 1", (unsigned long)Count.TaskCount);
UtAssert_True(Count.QueueCount == 1, "OS_ForEachObject() QueueCount (%lu) == 1", (unsigned long)Count.QueueCount);
UtAssert_True(Count.CountSemCount == 1, "OS_ForEachObject() CountSemCount (%lu) == 1", (unsigned long)Count.CountSemCount);
UtAssert_True(Count.BinSemCount == 2, "OS_ForEachObject() BinSemCount (%lu) == 2", (unsigned long)Count.BinSemCount);
UtAssert_True(Count.MutexCount == 3, "OS_ForEachObject() MutexCount (%lu) == 3", (unsigned long)Count.MutexCount);
UtAssert_True(Count.TimeBaseCount == 1, "OS_ForEachObject() TimeBaseCount (%lu) == 1", (unsigned long)Count.TimeBaseCount);

/*
* Use current task as an input
*/
memset(&Count, 0, sizeof(Count));
OS_ForEachObject (task_id, &ObjTypeCounter, &Count);

/* Verify Output */
UtAssert_True(Count.BinSemCount == 1, "OS_ForEachObject() BinSemCount MyTask (%lu) == 1", (unsigned long)Count.BinSemCount);

/*
* Note - this is done in a loop because some objects depend on other objects
* and you will not be able to delete the object until the ref count becomes zero.
* Delete all created objects, and verify that the count is now zero
*/
TryCount = 0;
while(true)
{
ObjectCount = 0;
++TryCount;
OS_ForEachObject(0, OS_Test_ForEachObject, &ObjectCount);
if (ObjectCount == 0 || TryCount > 4)
{
break;
}
OS_TaskDelay(5);
}
while (ObjectCount > 0 && TryCount < 5);
memset(&Count, 0, sizeof(Count));
OS_DeleteAllObjects();
OS_ForEachObject (0, &ObjTypeCounter, &Count);

/* Verify Outputs */
UtAssert_True(Count.TaskCount == 0, "OS_ForEachObject() TaskCount After Delete (%lu) == 0", (unsigned long)Count.TaskCount);
UtAssert_True(Count.QueueCount == 0, "OS_ForEachObject() QueueCount After Delete (%lu) == 0", (unsigned long)Count.QueueCount);
UtAssert_True(Count.CountSemCount == 0, "OS_ForEachObject() CountSemCount After Delete (%lu) == 0", (unsigned long)Count.CountSemCount);
UtAssert_True(Count.BinSemCount == 0, "OS_ForEachObject() BinSemCount After Delete (%lu) == 0", (unsigned long)Count.BinSemCount);
UtAssert_True(Count.MutexCount == 0, "OS_ForEachObject() MutexCount After Delete (%lu) == 0", (unsigned long)Count.MutexCount);
UtAssert_True(Count.TimeBaseCount == 0, "OS_ForEachObject() TimeBaseCount After Delete (%lu) == 0", (unsigned long)Count.TimeBaseCount);

/*
* Pass an invalid input, and verify that object counts are not increased
*/
OS_ForEachObject (0xFFFFFFFF, &ObjTypeCounter, &Count);

/* Verify Outputs */
UtAssert_True(Count.TaskCount == 0, "OS_ForEachObject() TaskCount Invalid Input (%lu) == 0", (unsigned long)Count.TaskCount);
UtAssert_True(Count.QueueCount == 0, "OS_ForEachObject() QueueCount Invalid Input (%lu) == 0", (unsigned long)Count.QueueCount);
UtAssert_True(Count.CountSemCount == 0, "OS_ForEachObject() CountSemCount Invalid Input (%lu) == 0", (unsigned long)Count.CountSemCount);
UtAssert_True(Count.BinSemCount == 0, "OS_ForEachObject() BinSemCount Invalid Input (%lu) == 0", (unsigned long)Count.BinSemCount);
UtAssert_True(Count.MutexCount == 0, "OS_ForEachObject() MutexCount Invalid Input (%lu) == 0", (unsigned long)Count.MutexCount);
UtAssert_True(Count.TimeBaseCount == 0, "OS_ForEachObject() TimeBaseCount Invalid Input (%lu) == 0", (unsigned long)Count.TimeBaseCount);



UtAssert_True(ObjectFoundCount == 10, "OS_ForEachObject() ObjectFoundCount");
}

} /* end TestIdMapApi */


void UtTest_Setup(void)
Expand Down

0 comments on commit 8fbea56

Please sign in to comment.