1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "osTest.h"
33 #include "It_los_sem.h"
34
35
TaskF01(void)36 static VOID TaskF01(void)
37 {
38 UINT32 ret;
39
40 g_testCount++;
41
42 ret = LOS_SemPend(g_usSemID, LOS_WAIT_FOREVER);
43 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
44
45 ICUNIT_GOTO_EQUAL(g_testCount, 6, g_testCount, EXIT); // 6, Here, assert that g_testCount is equal to 6.
46
47 g_testCount++;
48
49 EXIT:
50 LOS_TaskDelete(g_testTaskID01);
51 }
52
TaskF02(void)53 static VOID TaskF02(void)
54 {
55 UINT32 ret;
56
57 g_testCount++;
58
59 ret = LOS_SemPend(g_usSemID, LOS_WAIT_FOREVER);
60 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
61
62 ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); // 5, Here, assert that g_testCount is equal to 5.
63
64 g_testCount++;
65
66 EXIT:
67 LOS_TaskDelete(g_testTaskID02);
68 }
69
TaskF03(void)70 static VOID TaskF03(void)
71 {
72 UINT32 ret;
73
74 g_testCount++;
75
76 ret = LOS_SemPend(g_usSemID, LOS_WAIT_FOREVER);
77 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
78
79 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 4, Here, assert that g_testCount is equal to 4.
80
81 g_testCount++;
82
83 EXIT:
84 LOS_TaskDelete(g_testTaskID03);
85 }
86
HwiF01(void)87 static VOID HwiF01(void)
88 {
89 UINT32 ret;
90 TestHwiClear(HWI_NUM_TEST);
91
92 ret = LOS_SemPost(g_usSemID);
93 ICUNIT_TRACK_EQUAL(ret, LOS_OK, ret);
94
95 ret = LOS_SemPost(g_usSemID);
96 ICUNIT_TRACK_EQUAL(ret, LOS_OK, ret);
97
98 ret = LOS_SemPost(g_usSemID);
99 ICUNIT_TRACK_EQUAL(ret, LOS_OK, ret);
100
101 g_testCount++;
102 }
103
Testcase(VOID)104 static UINT32 Testcase(VOID)
105 {
106 UINT32 ret;
107 TSK_INIT_PARAM_S task = { 0 };
108
109 g_testCount = 0;
110
111 ret = LOS_SemCreate(0, &g_usSemID);
112 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
113
114 task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
115 task.pcName = "Sem33_1";
116 task.uwStackSize = TASK_STACK_SIZE_TEST;
117 task.usTaskPrio = TASK_PRIO_TEST - 1;
118
119 ret = LOS_TaskCreate(&g_testTaskID01, &task);
120 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
121
122 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT1);
123
124 task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
125 task.pcName = "Sem33_2";
126 task.usTaskPrio = TASK_PRIO_TEST - 2; // 2, set new task priority, it is higher than the current task.
127
128 ret = LOS_TaskCreate(&g_testTaskID02, &task);
129 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT1);
130
131 task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF03;
132 task.pcName = "Sem33_3";
133 task.usTaskPrio = TASK_PRIO_TEST - 3; // 3, set new task priority, it is higher than the current task.
134
135 ret = LOS_TaskCreate(&g_testTaskID03, &task);
136 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT1);
137
138 ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT2); // 3, Here, assert that g_testCount is equal to 3.
139
140 ret = LOS_HwiCreate(HWI_NUM_TEST, 1, 0, HwiF01, 0);
141 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT2);
142
143 TestHwiTrigger(HWI_NUM_TEST);
144
145 LOS_TaskDelay(1);
146 TestHwiDelete(HWI_NUM_TEST);
147 ICUNIT_GOTO_EQUAL(g_testCount, 7, g_testCount, EXIT2); // 7, Here, assert that g_testCount is equal to 7.
148
149 ret = LOS_SemDelete(g_usSemID);
150 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
151
152 return LOS_OK;
153
154 EXIT2:
155 LOS_TaskDelete(g_testTaskID02);
156 EXIT1:
157 LOS_TaskDelete(g_testTaskID01);
158 EXIT:
159 ret = LOS_SemDelete(g_usSemID);
160 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
161
162 return LOS_OK;
163 }
164
ItLosSem033(void)165 VOID ItLosSem033(void)
166 {
167 TEST_ADD_CASE("ItLosSem033", Testcase, TEST_LOS, TEST_SEM, TEST_LEVEL2, TEST_FUNCTION);
168 }
169
170