Skip to content

Commit 8352d2f

Browse files
yammajammaskliper
authored andcommitted
Fix #374, Add idmap functional tests
1 parent c2bcebb commit 8352d2f

File tree

1 file changed

+278
-0
lines changed

1 file changed

+278
-0
lines changed
+278
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
#include "common_types.h"
6+
#include "osapi.h"
7+
#include "utassert.h"
8+
#include "uttest.h"
9+
#include "utbsp.h"
10+
11+
12+
/* *************************************** MAIN ************************************** */
13+
14+
typedef struct
15+
{
16+
uint32 TaskCount;
17+
uint32 QueueCount;
18+
uint32 CountSemCount;
19+
uint32 BinSemCount;
20+
uint32 MutexCount;
21+
uint32 TimeBaseCount;
22+
uint32 OtherCount;
23+
} Test_OS_ObjTypeCount_t;
24+
25+
static void ObjTypeCounter(uint32 object_id, void *arg)
26+
{
27+
Test_OS_ObjTypeCount_t *count = arg;
28+
29+
switch(OS_IdentifyObject(object_id))
30+
{
31+
case OS_OBJECT_TYPE_OS_TASK:
32+
++count->TaskCount;
33+
break;
34+
case OS_OBJECT_TYPE_OS_QUEUE:
35+
++count->QueueCount;
36+
break;
37+
case OS_OBJECT_TYPE_OS_COUNTSEM:
38+
++count->CountSemCount;
39+
break;
40+
case OS_OBJECT_TYPE_OS_BINSEM:
41+
++count->BinSemCount;
42+
break;
43+
case OS_OBJECT_TYPE_OS_MUTEX:
44+
++count->MutexCount;
45+
break;
46+
case OS_OBJECT_TYPE_OS_TIMEBASE:
47+
++count->TimeBaseCount;
48+
break;
49+
default:
50+
++count->OtherCount;
51+
break;
52+
}
53+
}
54+
55+
/*
56+
* A void test function that creates an object for testing
57+
*/
58+
void Test_Void_Fn(void)
59+
{
60+
uint32 bin_sem_id_my_task;
61+
OS_BinSemCreate( &bin_sem_id_my_task, "BinSemTaskMyTask", 1, 0);
62+
OS_TaskDelay(5);
63+
64+
} /* end Test_Void_Fn */
65+
66+
67+
/* *************************************** MAIN ************************************** */
68+
69+
void TestIdMapApi(void)
70+
{
71+
int32 expected;
72+
int32 actual;
73+
uint32 task_id;
74+
uint32 queue_id;
75+
uint32 count_sem_id;
76+
uint32 bin_sem_id;
77+
uint32 mutex_id1;
78+
uint32 mutex_id2;
79+
uint32 mutex_id3;
80+
uint32 time_base_id;
81+
uint32 TestArrayIndex;
82+
Test_OS_ObjTypeCount_t Count;
83+
84+
/*
85+
* Create all allowed objects
86+
*/
87+
OS_TaskCreate( &task_id, "Task", Test_Void_Fn, 0, 0, 0, 0);
88+
OS_QueueCreate( &queue_id, "Queue", 5, 5, 0);
89+
OS_CountSemCreate( &count_sem_id, "CountSem", 1, 0);
90+
OS_BinSemCreate( &bin_sem_id, "BinSem", 1, 0);
91+
OS_MutSemCreate( &mutex_id1, "Mutex1", 0);
92+
OS_MutSemCreate( &mutex_id2, "Mutex2", 0);
93+
OS_MutSemCreate( &mutex_id3, "Mutex3", 0);
94+
OS_TimeBaseCreate( &time_base_id, "TimeBase", 0);
95+
96+
/*
97+
* NOTE: The following objects were not created and tested:
98+
* OS_OBJECT_TYPE_OS_STREAM
99+
* OS_OBJECT_TYPE_OS_DIR
100+
* OS_OBJECT_TYPE_OS_TIMECB
101+
* OS_OBJECT_TYPE_OS_FILESYS
102+
* OS_OBJECT_TYPE_OS_CONSOLE
103+
* OS_OBJECT_TYPE_USER
104+
*/
105+
106+
/*
107+
* Test Case For:
108+
* int32 OS_IdentifyObject(void)
109+
*/
110+
111+
/*
112+
* Test with nominal values
113+
*/
114+
expected = OS_OBJECT_TYPE_OS_TASK;
115+
actual = OS_IdentifyObject(task_id);
116+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
117+
118+
expected = OS_OBJECT_TYPE_OS_QUEUE;
119+
actual = OS_IdentifyObject(queue_id);
120+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
121+
122+
expected = OS_OBJECT_TYPE_OS_COUNTSEM;
123+
actual = OS_IdentifyObject(count_sem_id);
124+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
125+
126+
expected = OS_OBJECT_TYPE_OS_BINSEM;
127+
actual = OS_IdentifyObject(bin_sem_id);
128+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
129+
130+
expected = OS_OBJECT_TYPE_OS_MUTEX;
131+
actual = OS_IdentifyObject(mutex_id1);
132+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
133+
134+
expected = OS_OBJECT_TYPE_OS_TIMEBASE;
135+
actual = OS_IdentifyObject(time_base_id);
136+
UtAssert_True(actual == expected, "OS_IdentifyObject() (%ld) == %ld", (long)actual, (long)expected);
137+
138+
/*
139+
* Test with extreme cases using min and max values
140+
* Note: There are no asserts, checks or expected values
141+
* here. The only check is that the function doesn't return
142+
* an error when called
143+
*/
144+
OS_IdentifyObject(0x00000);
145+
OS_IdentifyObject(0xFFFFFFFF);
146+
147+
/*
148+
* Test Case For:
149+
* int32 OS_ConvertToArrayIndex(void)
150+
*/
151+
expected = OS_SUCCESS;
152+
153+
/*
154+
* Check different id types and verify array indices
155+
* Each Object Type index is added to an array index of its own type
156+
* Each object type is checked once, and MUTEX is checked twice to
157+
* verify multiple indices
158+
*/
159+
160+
/*
161+
* Test with nominal values
162+
*/
163+
actual = OS_ConvertToArrayIndex(task_id, &TestArrayIndex);
164+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
165+
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);
166+
167+
actual = OS_ConvertToArrayIndex(queue_id, &TestArrayIndex);
168+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
169+
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);
170+
171+
actual = OS_ConvertToArrayIndex(count_sem_id, &TestArrayIndex);
172+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
173+
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);
174+
175+
actual = OS_ConvertToArrayIndex(bin_sem_id, &TestArrayIndex);
176+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
177+
UtAssert_True(TestArrayIndex == 2 , "TestArrayIndex(%lu) == 2", (long)TestArrayIndex);
178+
179+
actual = OS_ConvertToArrayIndex(mutex_id1, &TestArrayIndex);
180+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
181+
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);
182+
183+
actual = OS_ConvertToArrayIndex(mutex_id2, &TestArrayIndex);
184+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
185+
UtAssert_True(TestArrayIndex == 2 , "TestArrayIndex(%lu) == 2", (long)TestArrayIndex);
186+
187+
actual = OS_ConvertToArrayIndex(mutex_id3, &TestArrayIndex);
188+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
189+
UtAssert_True(TestArrayIndex == 3 , "TestArrayIndex(%lu) == 3", (long)TestArrayIndex);
190+
191+
actual = OS_ConvertToArrayIndex(time_base_id, &TestArrayIndex);
192+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
193+
UtAssert_True(TestArrayIndex == 1 , "TestArrayIndex(%lu) == 1", (long)TestArrayIndex);
194+
195+
/*
196+
* Test with extreme cases using invalid inputs and checking
197+
* for an error return code
198+
*/
199+
actual = OS_ConvertToArrayIndex(0x0000, &TestArrayIndex);
200+
expected = OS_ERR_INCORRECT_OBJ_TYPE;
201+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
202+
203+
actual = OS_ConvertToArrayIndex(0xFFFFFFFF, &TestArrayIndex);
204+
expected = OS_ERR_INCORRECT_OBJ_TYPE;
205+
UtAssert_True(actual == expected , "OS_ConvertToArrayIndex() (%ld) == %ld ", (long)actual, (long)expected );
206+
207+
/*
208+
* Test Case For:
209+
* void OS_ForEachObject (uint32 creator_id, OS_ArgCallback_t callback_ptr, void *callback_arg);
210+
*/
211+
memset(&Count, 0, sizeof(Count));
212+
213+
OS_ForEachObject (0, &ObjTypeCounter, &Count);
214+
215+
/* Verify Outputs */
216+
UtAssert_True(Count.TaskCount == 1, "OS_ForEachObject() TaskCount (%lu) == 1", (unsigned long)Count.TaskCount);
217+
UtAssert_True(Count.QueueCount == 1, "OS_ForEachObject() QueueCount (%lu) == 1", (unsigned long)Count.QueueCount);
218+
UtAssert_True(Count.CountSemCount == 1, "OS_ForEachObject() CountSemCount (%lu) == 1", (unsigned long)Count.CountSemCount);
219+
UtAssert_True(Count.BinSemCount == 2, "OS_ForEachObject() BinSemCount (%lu) == 2", (unsigned long)Count.BinSemCount);
220+
UtAssert_True(Count.MutexCount == 3, "OS_ForEachObject() MutexCount (%lu) == 3", (unsigned long)Count.MutexCount);
221+
UtAssert_True(Count.TimeBaseCount == 1, "OS_ForEachObject() TimeBaseCount (%lu) == 1", (unsigned long)Count.TimeBaseCount);
222+
223+
/*
224+
* Use current task as an input
225+
*/
226+
memset(&Count, 0, sizeof(Count));
227+
OS_ForEachObject (task_id, &ObjTypeCounter, &Count);
228+
229+
/* Verify Output */
230+
UtAssert_True(Count.BinSemCount == 1, "OS_ForEachObject() BinSemCount MyTask (%lu) == 1", (unsigned long)Count.BinSemCount);
231+
232+
/*
233+
* Delete all created objects, and verify that the count is now zero
234+
*/
235+
memset(&Count, 0, sizeof(Count));
236+
OS_DeleteAllObjects();
237+
OS_ForEachObject (0, &ObjTypeCounter, &Count);
238+
239+
/* Verify Outputs */
240+
UtAssert_True(Count.TaskCount == 0, "OS_ForEachObject() TaskCount After Delete (%lu) == 0", (unsigned long)Count.TaskCount);
241+
UtAssert_True(Count.QueueCount == 0, "OS_ForEachObject() QueueCount After Delete (%lu) == 0", (unsigned long)Count.QueueCount);
242+
UtAssert_True(Count.CountSemCount == 0, "OS_ForEachObject() CountSemCount After Delete (%lu) == 0", (unsigned long)Count.CountSemCount);
243+
UtAssert_True(Count.BinSemCount == 0, "OS_ForEachObject() BinSemCount After Delete (%lu) == 0", (unsigned long)Count.BinSemCount);
244+
UtAssert_True(Count.MutexCount == 0, "OS_ForEachObject() MutexCount After Delete (%lu) == 0", (unsigned long)Count.MutexCount);
245+
UtAssert_True(Count.TimeBaseCount == 0, "OS_ForEachObject() TimeBaseCount After Delete (%lu) == 0", (unsigned long)Count.TimeBaseCount);
246+
247+
/*
248+
* Pass an invalid input, and verify that object counts are not increased
249+
*/
250+
OS_ForEachObject (0xFFFFFFFF, &ObjTypeCounter, &Count);
251+
252+
/* Verify Outputs */
253+
UtAssert_True(Count.TaskCount == 0, "OS_ForEachObject() TaskCount Invalid Input (%lu) == 0", (unsigned long)Count.TaskCount);
254+
UtAssert_True(Count.QueueCount == 0, "OS_ForEachObject() QueueCount Invalid Input (%lu) == 0", (unsigned long)Count.QueueCount);
255+
UtAssert_True(Count.CountSemCount == 0, "OS_ForEachObject() CountSemCount Invalid Input (%lu) == 0", (unsigned long)Count.CountSemCount);
256+
UtAssert_True(Count.BinSemCount == 0, "OS_ForEachObject() BinSemCount Invalid Input (%lu) == 0", (unsigned long)Count.BinSemCount);
257+
UtAssert_True(Count.MutexCount == 0, "OS_ForEachObject() MutexCount Invalid Input (%lu) == 0", (unsigned long)Count.MutexCount);
258+
UtAssert_True(Count.TimeBaseCount == 0, "OS_ForEachObject() TimeBaseCount Invalid Input (%lu) == 0", (unsigned long)Count.TimeBaseCount);
259+
260+
261+
262+
263+
} /* end TestIdMapApi */
264+
265+
266+
void UtTest_Setup(void)
267+
{
268+
if (OS_API_Init() != OS_SUCCESS)
269+
{
270+
UtAssert_Abort("OS_API_Init() failed");
271+
}
272+
273+
/*
274+
* Register the test setup and check routines in UT assert
275+
*/
276+
UtTest_Add(TestIdMapApi, NULL, NULL, "TestIdMapApi");
277+
}
278+

0 commit comments

Comments
 (0)