• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "It_los_mux.h"
33 
34 #ifdef __cplusplus
35 #if __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38 #endif /* __cplusplus */
39 
TaskDFunc(VOID)40 static VOID TaskDFunc(VOID)
41 {
42     UINT32 ret;
43     g_testCount++;
44 
45     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 6, g_testCount); // 6, here assert the result.
46 
47     g_testCount++;
48     ret = LOS_MuxLock(&g_mutexTest3, LOS_WAIT_FOREVER);
49     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
50 
51     ret = LOS_MuxUnlock(&g_mutexTest3);
52     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
53 
54     g_testCount++;
55     UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
56     ICUNIT_ASSERT_EQUAL_VOID(priority, 2, priority); // 2, here assert the result.
57     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 9, g_testCount);                             // 9, here assert the result.
58 
59     g_testCount++;
60 }
61 
TaskCFunc(VOID)62 static VOID TaskCFunc(VOID)
63 {
64     UINT32 ret;
65     g_testCount++;
66 
67     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 4, g_testCount); // 4, here assert the result.
68 
69     g_testCount++;
70     ret = LOS_MuxLock(&g_mutexTest2, LOS_WAIT_FOREVER);
71     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
72 
73     ret = LOS_MuxUnlock(&g_mutexTest2);
74     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
75     g_testCount++;
76 
77     UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
78     ICUNIT_ASSERT_EQUAL_VOID(priority, 3, priority); // 3, here assert the result.
79     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 11, g_testCount);                            // 11, here assert the result.
80 
81     g_testCount++;
82 }
83 
TaskBFunc(VOID)84 static VOID TaskBFunc(VOID)
85 {
86     UINT32 ret;
87 
88     g_testCount++;
89 
90     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 2, g_testCount); // 2, here assert the result.
91 
92     g_testCount++;
93     ret = LOS_MuxLock(&g_mutexTest1, LOS_WAIT_FOREVER);
94     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
95 
96     ret = LOS_MuxUnlock(&g_mutexTest1);
97     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
98 
99     g_testCount++;
100 
101     UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
102     ICUNIT_ASSERT_EQUAL_VOID(priority, 5, priority); // 5, here assert the result.
103     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 13, g_testCount);                            // 13, here assert the result.
104     g_testCount++;
105 }
106 
TaskAFunc(VOID)107 static VOID TaskAFunc(VOID)
108 {
109     UINT32 ret;
110     TSK_INIT_PARAM_S task1 = { 0 };
111     TSK_INIT_PARAM_S task2 = { 0 };
112     TSK_INIT_PARAM_S task3 = { 0 };
113 
114     g_testCount++;
115 
116     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 1, g_testCount);
117     ret = LOS_MuxLock(&g_mutexTest1, 0);
118     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
119 
120     task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskBFunc;
121     task1.usTaskPrio = 5; // 5, set reasonable priority.
122     task1.pcName = "TaskB";
123     task1.uwStackSize = TASK_STACK_SIZE_TEST;
124     task1.uwResved = LOS_TASK_STATUS_DETACHED;
125 #ifdef LOSCFG_KERNEL_SMP
126     task1.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
127 #endif
128     ret = LOS_TaskCreate(&g_testTaskID02, &task1);
129     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
130 
131     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 3, g_testCount); // 3, here assert the result.
132 
133     ret = LosMuxCreate(&g_mutexTest2);
134     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
135     ret = LOS_MuxLock(&g_mutexTest2, 0);
136     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
137 
138     task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskCFunc;
139     task2.usTaskPrio = 3; // 3, set reasonable priority.
140     task2.pcName = "TaskC";
141     task2.uwStackSize = TASK_STACK_SIZE_TEST;
142     task2.uwResved = LOS_TASK_STATUS_DETACHED;
143 #ifdef LOSCFG_KERNEL_SMP
144     task2.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
145 #endif
146     ret = LOS_TaskCreate(&g_testTaskID03, &task2);
147     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
148 
149     ret = LosMuxCreate(&g_mutexTest3);
150     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
151     ret = LOS_MuxLock(&g_mutexTest3, 0);
152     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
153 
154     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 5, g_testCount); // 5, here assert the result.
155     task3.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskDFunc;
156     task3.usTaskPrio = 2; // 2, set reasonable priority.
157     task3.pcName = "TaskD";
158     task3.uwStackSize = TASK_STACK_SIZE_TEST;
159     task3.uwResved = LOS_TASK_STATUS_DETACHED;
160 #ifdef LOSCFG_KERNEL_SMP
161     task3.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
162 #endif
163     ret = LOS_TaskCreate(&g_testTaskID04, &task3);
164     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
165     g_testCount++;
166     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 8, g_testCount); // 8, here assert the result.
167 
168     ret = LOS_MuxUnlock(&g_mutexTest3);
169     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
170 
171     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 10, g_testCount); // 10, here assert the result.
172 
173     UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
174     ICUNIT_ASSERT_EQUAL_VOID(priority, 3, priority); // 3, here assert the result.
175 
176     ret = LOS_MuxUnlock(&g_mutexTest2);
177     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
178 
179     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 12, g_testCount); // 12, here assert the result.
180     priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
181     ICUNIT_ASSERT_EQUAL_VOID(priority, 5, priority); // 5, here assert the result.
182 
183     ret = LOS_MuxUnlock(&g_mutexTest1);
184     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
185 
186     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 14, g_testCount); // 14, here assert the result.
187 
188     ret = LOS_MuxDestroy(&g_mutexTest1);
189     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
190 
191     ret = LOS_MuxDestroy(&g_mutexTest2);
192     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
193 
194     ret = LOS_MuxDestroy(&g_mutexTest3);
195     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
196 
197     ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 14, g_testCount);                             // 14, here assert the result.
198     priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
199     ICUNIT_ASSERT_EQUAL_VOID(priority, 10, priority); // 10, here assert the result.
200 }
201 
202 
Testcase(VOID)203 static UINT32 Testcase(VOID)
204 {
205     UINT32 ret;
206     TSK_INIT_PARAM_S task = { 0 };
207 
208     g_testCount = 0;
209 
210     UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
211     ICUNIT_ASSERT_EQUAL(priority, 25, priority); // 25, here assert the result.
212 
213     ret = LosMuxCreate(&g_mutexTest1);
214     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
215 
216     task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskAFunc;
217     task.usTaskPrio = 10; // 10, set reasonable priority.
218     task.pcName = "TaskA";
219     task.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
220     task.uwResved = LOS_TASK_STATUS_DETACHED;
221 #ifdef LOSCFG_KERNEL_SMP
222     task.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
223 #endif
224     ret = LOS_TaskCreate(&g_testTaskID01, &task);
225     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
226 
227     ICUNIT_ASSERT_EQUAL(g_testCount, 14, g_testCount);  // 14, here assert the result.
228     priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
229     ICUNIT_ASSERT_EQUAL(priority, 25, priority); // 25, here assert the result.
230     return LOS_OK;
231 }
232 
ItLosMux026(void)233 VOID ItLosMux026(void)
234 {
235     TEST_ADD_CASE("ItLosMux026", Testcase, TEST_LOS, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
236 }
237 
238 #ifdef __cplusplus
239 #if __cplusplus
240 }
241 #endif /* __cplusplus */
242 #endif /* __cplusplus */
243