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_posix_pthread.h"
33
34 #ifdef __cplusplus
35 #if __cplusplus
36 extern "C" {
37 #endif /* __cpluscplus */
38 #endif /* __cpluscplus */
39
PthreadF01(void * argument)40 static VOID *PthreadF01(void *argument)
41 {
42 g_testCount++;
43
44 return (void *)9; // 9, here set value about the return status.
45 }
46
PthreadF02(void * argument)47 static VOID *PthreadF02(void *argument)
48 {
49 UINT32 ret;
50 UINTPTR temp = 1;
51
52 TestExtraTaskDelay(2); // 2, delay for Timing control.
53
54 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
55 g_testCount++;
56
57 ret = pthread_join(g_newTh2, (void *)&temp);
58 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
59 ICUNIT_GOTO_EQUAL(temp, 7, temp, EXIT); // 7, here assert the result.
60
61 ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, here assert the result.
62 g_testCount++;
63 EXIT:
64 return (void *)8; // 8, here set value about the return status.
65 }
66
PthreadF03(void * argument)67 static VOID *PthreadF03(void *argument)
68 {
69 UINT32 ret;
70 UINTPTR temp = 1;
71
72 TestExtraTaskDelay(4); // 4, delay for Timing control.
73
74 ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT); // 2, here assert the result.
75 g_testCount++;
76
77 ret = pthread_join(g_newTh, (void *)&temp);
78 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
79 ICUNIT_GOTO_EQUAL(temp, 9, temp, EXIT); // 9, here assert the result.
80
81 EXIT:
82 return (void *)7; // 7, here set value about the return status.
83 }
Testcase(VOID)84 static UINT32 Testcase(VOID)
85 {
86 pthread_t newTh;
87 pthread_attr_t attr;
88 UINT32 ret;
89 UINTPTR temp = 1;
90
91 g_testCount = 0;
92
93 ret = PosixPthreadInit(&attr, 4); // 4, test for param of the function.
94 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95
96 LOS_TaskLock();
97
98 ret = pthread_create(&g_newTh, &attr, PthreadF01, NULL);
99 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
100
101 ret = pthread_create(&newTh, &attr, PthreadF02, NULL);
102 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
103
104 ret = pthread_create(&g_newTh2, &attr, PthreadF03, NULL);
105 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
106
107 LOS_TaskUnlock();
108
109 TestExtraTaskDelay(6); // 6, delay for Timing control.
110
111 ICUNIT_ASSERT_EQUAL(g_testCount, 4, g_testCount); // 4, here assert the result.
112
113 ret = pthread_join(newTh, (void *)&temp);
114 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
115 ICUNIT_ASSERT_EQUAL(temp, 8, temp); // 8, here assert the result.
116
117 ret = pthread_join(newTh, (void *)&temp);
118 ICUNIT_ASSERT_EQUAL(ret, ESRCH, ret);
119 return PTHREAD_NO_ERROR;
120 }
121
ItPosixPthread032(VOID)122 VOID ItPosixPthread032(VOID) // IT_Layer_ModuleORFeature_No
123 {
124 TEST_ADD_CASE("ItPosixPthread032", Testcase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
125 }
126
127 #ifdef __cplusplus
128 #if __cplusplus
129 }
130 #endif /* __cpluscplus */
131 #endif /* __cpluscplus */