1 /**
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16 #include "ohos_types.h"
17 #include <securec.h>
18 #include "hctest.h"
19 #include "los_config.h"
20 #include "cmsis_os2.h"
21 #include "kernel_test.h"
22
23 osEventFlagsId_t g_eventId;
24 UINT16 g_cmsisTestEventCount;
25 #define EVENT_MASK_HEX_1 0x01
26 #define EVENT_MASK_HEX_2 0x02
27 #define EVENT_MASK_HEX_4 0x04
28 #define EVENT_MASK_HEX_10 0x10
29 #define EVENT_MASK_HEX_11 0x11
30 #define TIMEOUT_NUM_3 3
31 #define TIMEOUT_NUM_10 10
32 #define INVALID_FLAG_OPTION 0x00000004U
33
34 /**
35 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
36 * @param : subsystem name is utils
37 * @param : module name is utilsFile
38 * @param : test suit name is CmsisEventFuncTestSuite
39 */
40 LITE_TEST_SUIT(Cmsis, Cmsisevent, CmsisEventFuncTestSuite);
41
42 /**
43 * @tc.setup : setup for all testcases
44 * @return : setup result, TRUE is success, FALSE is fail
45 */
CmsisEventFuncTestSuiteSetUp(void)46 static BOOL CmsisEventFuncTestSuiteSetUp(void)
47 {
48 return TRUE;
49 }
50
51 /**
52 * @tc.teardown : teardown for all testcases
53 * @return : teardown result, TRUE is success, FALSE is fail
54 */
CmsisEventFuncTestSuiteTearDown(void)55 static BOOL CmsisEventFuncTestSuiteTearDown(void)
56 {
57 printf("+-------------------------------------------+\n");
58 return TRUE;
59 }
60
CmsisEventFlagsWaitFunc001(void const *argument)61 static void CmsisEventFlagsWaitFunc001(void const *argument)
62 {
63 (void)argument;
64 UINT32 uwRet;
65 g_cmsisTestEventCount++;
66 uwRet = osEventFlagsWait(g_eventId, EVENT_MASK_HEX_11, (osFlagsWaitAll | osFlagsNoClear), osWaitForever);
67 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_11, uwRet);
68 g_cmsisTestEventCount++;
69 osThreadExit();
70 }
71
CmsisEventFlagsSetFunc002(void const *argument)72 static void CmsisEventFlagsSetFunc002(void const *argument)
73 {
74 (void)argument;
75 UINT32 uwRet;
76 g_cmsisTestEventCount++;
77 uwRet = osEventFlagsWait(g_eventId, EVENT_MASK_HEX_11, (osFlagsWaitAll | osFlagsNoClear), TIMEOUT_NUM_3);
78 TEST_ASSERT_EQUAL_INT(osErrorTimeout, uwRet);
79 g_cmsisTestEventCount++;
80
81 uwRet = osEventFlagsWait(g_eventId, EVENT_MASK_HEX_4, (osFlagsWaitAll | osFlagsNoClear), osWaitForever);
82 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_4, uwRet);
83 g_cmsisTestEventCount++;
84 osThreadExit();
85 }
86
CmsisEventFlagsClearFunc001(void const *argument)87 static void CmsisEventFlagsClearFunc001(void const *argument)
88 {
89 (void)argument;
90 UINT32 uwRet;
91 g_cmsisTestEventCount++;
92 uwRet = osEventFlagsWait(g_eventId, EVENT_MASK_HEX_1, osFlagsWaitAll, osWaitForever);
93 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_1, uwRet);
94 g_cmsisTestEventCount++;
95 osThreadExit();
96 }
97
98 /**
99 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0100
100 * @tc.name : event operation for creat
101 * @tc.desc : [C- SOFTWARE -0200]
102 */
103 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsNew001, Function | MediumTest | Level1)
104 {
105 g_eventId = osEventFlagsNew(NULL);
106 TEST_ASSERT_NOT_NULL(g_eventId);
107 (void)osEventFlagsDelete(g_eventId);
108 };
109
110 /**
111 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0200
112 * @tc.name : event operation for delete
113 * @tc.desc : [C- SOFTWARE -0200]
114 */
115 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsDelete001, Function | MediumTest | Level1)
116 {
117 UINT32 uwRet;
118 g_eventId = osEventFlagsNew(NULL);
119 TEST_ASSERT_NOT_NULL(g_eventId);
120
121 uwRet = osEventFlagsDelete(g_eventId);
122 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
123 };
124
125 /**
126 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0300
127 * @tc.name : event delete operation with EventFlagsId = NULL
128 * @tc.desc : [C- SOFTWARE -0200]
129 */
130 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsDelete002, Function | MediumTest | Level1)
131 {
132 UINT32 uwRet;
133
134 uwRet = osEventFlagsDelete(NULL);
135 TEST_ASSERT_EQUAL_INT(osErrorParameter, uwRet);
136 };
137
138 /**
139 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0400
140 * @tc.name : event operation for flags set
141 * @tc.desc : [C- SOFTWARE -0200]
142 */
143 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsSet001, Function | MediumTest | Level1)
144 {
145 UINT32 uwRet;
146 g_eventId = osEventFlagsNew(NULL);
147 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_10);
148 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
149 uwRet = osEventFlagsDelete(g_eventId);
150 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
151
152 };
153
154 /**
155 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0500
156 * @tc.name : event reliability test for flags set
157 * @tc.desc : [C- SOFTWARE -0200]
158 */
159 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsSet002, Function | MediumTest | Level1)
160 {
161 UINT32 uwRet;
162 osThreadId_t id;
163 osThreadAttr_t attr;
164 attr.name = "test";
165 attr.attr_bits = 0U;
166 attr.cb_mem = NULL;
167 attr.cb_size = 0U;
168 attr.stack_mem = NULL;
169 attr.stack_size = TEST_TASK_STACK_SIZE;
170 attr.priority = osPriorityAboveNormal;
171
172 g_cmsisTestEventCount = 0;
173 g_eventId = osEventFlagsNew(NULL);
174 TEST_ASSERT_NOT_NULL(g_eventId);
175 id = osThreadNew((osThreadFunc_t)CmsisEventFlagsSetFunc002, NULL, &attr);
176 TEST_ASSERT_NOT_NULL(id);
177
178 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_1, g_cmsisTestEventCount);
179 g_cmsisTestEventCount++;
180 uwRet=osEventFlagsSet(g_eventId, EVENT_MASK_HEX_2);
181 osDelay(DELAY_TICKS_5);
182 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_3, g_cmsisTestEventCount);
183
184 g_cmsisTestEventCount++;
185 uwRet=osEventFlagsSet(g_eventId, EVENT_MASK_HEX_11);
186 osDelay(DELAY_TICKS_5);
187
188 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_4, g_cmsisTestEventCount);
189 uwRet=osEventFlagsSet(g_eventId, EVENT_MASK_HEX_4);
190 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_5, g_cmsisTestEventCount);
191 uwRet = osEventFlagsDelete(g_eventId);
192 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
193
194 };
195
196 /**
197 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0600
198 * @tc.name : event flags set operation with EventFlagsId = NULL
199 * @tc.desc : [C- SOFTWARE -0200]
200 */
201 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsSet003, Function | MediumTest | Level1)
202 {
203 UINT32 uwRet;
204 uwRet = osEventFlagsSet(NULL, EVENT_MASK_HEX_10);
205 TEST_ASSERT_EQUAL_INT(osFlagsErrorParameter, uwRet);
206
207 };
208
209 /**
210 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0700
211 * @tc.name : event operation for wait
212 * @tc.desc : [C- SOFTWARE -0200]
213 */
214 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsWait001, Function | MediumTest | Level1)
215 {
216 UINT32 uwRet;
217 osThreadId_t id;
218 osThreadAttr_t attr;
219 attr.name = "test";
220 attr.attr_bits = 0U;
221 attr.cb_mem = NULL;
222 attr.cb_size = 0U;
223 attr.stack_mem = NULL;
224 attr.stack_size = TEST_TASK_STACK_SIZE;
225 attr.priority = osPriorityAboveNormal;
226
227 g_cmsisTestEventCount = 0;
228 g_eventId = osEventFlagsNew(NULL);
229 TEST_ASSERT_NOT_NULL(g_eventId);
230 id = osThreadNew((osThreadFunc_t)CmsisEventFlagsWaitFunc001, NULL, &attr);
231 TEST_ASSERT_NOT_NULL(id);
232
233 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_10);
234 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
235 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_1, g_cmsisTestEventCount);
236
237 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_1);
238 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_11, uwRet);
239 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_2, g_cmsisTestEventCount);
240
241 uwRet = osEventFlagsDelete(g_eventId);
242 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
243 };
244
245 /**
246 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0800
247 * @tc.name : event operation for invalid option
248 * @tc.desc : [C- SOFTWARE -0200]
249 */
250 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsWait002, Function | MediumTest | Level1)
251 {
252 UINT32 uwRet;
253 g_eventId = osEventFlagsNew(NULL);
254 TEST_ASSERT_NOT_NULL(g_eventId);
255
256 uwRet = osEventFlagsWait(g_eventId, EVENT_MASK_HEX_11, INVALID_FLAG_OPTION, osWaitForever);
257 TEST_ASSERT_EQUAL_INT(osFlagsErrorParameter, uwRet);
258
259 uwRet = osEventFlagsDelete(g_eventId);
260 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
261 };
262
263 /**
264 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_0900
265 * @tc.name : event wait operation with EventFlagsId = NULL
266 * @tc.desc : [C- SOFTWARE -0200]
267 */
268 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsWait003, Function | MediumTest | Level1)
269 {
270 UINT32 uwRet;
271 uwRet = osEventFlagsWait(NULL, EVENT_MASK_HEX_11, (osFlagsWaitAll | osFlagsNoClear), osWaitForever);
272 TEST_ASSERT_EQUAL_INT(osFlagsErrorParameter, uwRet);
273
274 };
275
276 /**
277 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_1000
278 * @tc.name : event operation for flags get
279 * @tc.desc : [C- SOFTWARE -0200]
280 */
281 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsGet001, Function | MediumTest | Level1)
282 {
283 UINT32 uwRet;
284 g_eventId = osEventFlagsNew(NULL);
285 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_10);
286 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
287 uwRet = 0;
288 uwRet = osEventFlagsGet(g_eventId);
289 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
290
291 uwRet = osEventFlagsDelete(g_eventId);
292 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
293
294 };
295
296 /**
297 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_1100
298 * @tc.name : event flags get operation with EventFlagsId = NULL
299 * @tc.desc : [C- SOFTWARE -0200]
300 */
301 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsGet002, Function | MediumTest | Level1)
302 {
303 UINT32 uwRet;
304 uwRet = osEventFlagsGet(NULL);
305 TEST_ASSERT_EQUAL_INT(osFlagsErrorParameter, uwRet);
306 };
307
308 /**
309 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_1200
310 * @tc.name : event operation for flags clear
311 * @tc.desc : [C- SOFTWARE -0200]
312 */
313 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsClear001, Function | MediumTest | Level1)
314 {
315 UINT32 uwRet;
316 osThreadId_t id;
317
318 osThreadAttr_t attr;
319 attr.name = "test";
320 attr.attr_bits = 0U;
321 attr.cb_mem = NULL;
322 attr.cb_size = 0U;
323 attr.stack_mem = NULL;
324 attr.stack_size = TEST_TASK_STACK_SIZE;
325 attr.priority = osPriorityAboveNormal;
326
327 g_cmsisTestEventCount = 0;
328 g_eventId = osEventFlagsNew(NULL);
329 id = osThreadNew((osThreadFunc_t)CmsisEventFlagsClearFunc001, NULL, &attr);
330 TEST_ASSERT_NOT_NULL(id);
331 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_10);
332 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
333 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_1, g_cmsisTestEventCount);
334
335 uwRet = osEventFlagsClear(g_eventId, 0xffff);
336 TEST_ASSERT_EQUAL_INT(EVENT_MASK_HEX_10, uwRet);
337 uwRet = osEventFlagsGet(g_eventId);
338 TEST_ASSERT_EQUAL_INT(0, uwRet);
339 uwRet = osEventFlagsSet(g_eventId, EVENT_MASK_HEX_1);
340 // after the event is triggered, the flag is reset to be zero
341 TEST_ASSERT_EQUAL_INT(0, uwRet);
342 TEST_ASSERT_EQUAL_INT(TESTCOUNT_NUM_2, g_cmsisTestEventCount);
343 uwRet = osEventFlagsDelete(g_eventId);
344 TEST_ASSERT_EQUAL_INT(osOK, uwRet);
345 };
346
347 /**
348 * @tc.number : SUB_KERNEL_CMSIS_EVENT_OPERATION_1300
349 * @tc.name : event flags clear operation with EventFlagsId = NULL
350 * @tc.desc : [C- SOFTWARE -0200]
351 */
352 LITE_TEST_CASE(CmsisEventFuncTestSuite, testOsEventFlagsClear002, Function | MediumTest | Level1)
353 {
354 UINT32 uwRet;
355 uwRet = osEventFlagsClear(NULL, 0xffff);
356 TEST_ASSERT_EQUAL_INT(osFlagsErrorParameter, uwRet);
357 };
358
359 RUN_TEST_SUITE(CmsisEventFuncTestSuite);
360