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_mux.h"
34 #include "los_config.h"
35 #include "los_sem.h"
36
37
TaskFuncC(VOID)38 static VOID TaskFuncC(VOID)
39 {
40 UINT32 ret;
41 g_testCount++;
42
43 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
44
45 ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
46 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
47
48 ret = LOS_MuxPost(g_mutexTest1);
49 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
50
51 // 5, Here, assert that priority is equal to 5.
52 ICUNIT_ASSERT_EQUAL_VOID(g_losTask.runTask->priority, 5, g_losTask.runTask->priority);
53 // 3, Here, assert that g_testCount is equal to 3.
54 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 3, g_testCount);
55
56 g_testCount++;
57 }
58
TaskFuncB(VOID)59 static VOID TaskFuncB(VOID)
60 {
61 UINT32 ret;
62 g_testCount++;
63
64 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
65
66 ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
67 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
68
69 ret = LOS_MuxPost(g_mutexTest1);
70 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
71
72 // 8, Here, assert that priority is equal to 8.
73 ICUNIT_ASSERT_EQUAL_VOID(g_losTask.runTask->priority, 8, g_losTask.runTask->priority);
74
75 // 4, Here, assert that g_testCount is equal to 4.
76 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 4, g_testCount);
77
78 g_testCount++;
79 }
80
TaskFuncA(VOID)81 static VOID TaskFuncA(VOID)
82 {
83 UINT32 ret;
84 TSK_INIT_PARAM_S task1 = {0};
85 TSK_INIT_PARAM_S task2 = {0};
86 g_testCount++;
87
88 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 1, g_testCount); // 1, Here, assert that g_testCount is equal to 1.
89
90 ret = LOS_MuxPend(g_mutexTest1, 0);
91 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
92
93 task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskFuncB;
94 // 8, Set the priority according to the task purpose,a smaller number means a higher priority.
95 task1.usTaskPrio = 8;
96 task1.pcName = "TaskB";
97 task1.uwStackSize = TASK_STACK_SIZE_TEST;
98 task1.uwResved = LOS_TASK_STATUS_DETACHED;
99
100 ret = LOS_TaskCreate(&g_testTaskID02, &task1);
101 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
102
103 task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskFuncC;
104 // 5, Set the priority according to the task purpose,a smaller number means a higher priority.
105 task2.usTaskPrio = 5;
106 task2.pcName = "TaskC";
107 task2.uwStackSize = TASK_STACK_SIZE_TEST;
108 task2.uwResved = LOS_TASK_STATUS_DETACHED;
109
110 ret = LOS_TaskCreate(&g_testTaskID03, &task2);
111 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
112
113 LOS_TaskDelay(5); // 5, set delay time.
114
115 ret = LOS_MuxPost(g_mutexTest1);
116 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
117
118 // 10, Here, assert that priority is equal to 10.
119 ICUNIT_ASSERT_EQUAL_VOID(g_losTask.runTask->priority, 10, g_losTask.runTask->priority);
120 // 5, Here, assert that g_testCount is equal to 5.
121 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 5, g_testCount);
122 }
123
Testcase(VOID)124 static UINT32 Testcase(VOID)
125 {
126 UINT32 ret;
127 TSK_INIT_PARAM_S task = {0};
128 g_testCount = 0;
129
130 ret = LOS_MuxCreate(&g_mutexTest1);
131 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
132
133 task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskFuncA;
134 // 10, Set the priority according to the task purpose,a smaller number means a higher priority.
135 task.usTaskPrio = 10;
136 task.pcName = "TaskA";
137 task.uwStackSize = TASK_STACK_SIZE_TEST;
138 task.uwResved = LOS_TASK_STATUS_DETACHED;
139
140 ret = LOS_TaskCreate(&g_testTaskID01, &task);
141 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
142
143 ICUNIT_ASSERT_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
144
145 LOS_TaskDelay(10); // 10, set delay time.
146
147 ret = LOS_MuxDelete(g_mutexTest1);
148 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
149
150 // 25, Here, assert that priority is equal to 25.
151 ICUNIT_ASSERT_EQUAL(g_losTask.runTask->priority, 25, g_losTask.runTask->priority);
152
153 ICUNIT_ASSERT_EQUAL(g_testCount, 5, g_testCount); // 5, Here, assert that g_testCount is equal to 5.
154 return LOS_OK;
155 }
156
ItLosMux033(void)157 VOID ItLosMux033(void)
158 {
159 TEST_ADD_CASE("ItLosMux033", Testcase, TEST_LOS, TEST_MUX, TEST_LEVEL1, TEST_FUNCTION);
160 }
161
162