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 #include "It_posix_pthread.h"
32
pthread_f01(void * argument)33 static VOID *pthread_f01(void *argument)
34 {
35 UINT32 temp = 2;
36 UINT32 ret;
37 VOID *result;
38
39 ICUNIT_GOTO_EQUAL(g_testCount, 0, g_testCount, EXIT);
40 g_testCount++;
41
42 result = pthread_getspecific(g_key);
43 ICUNIT_GOTO_EQUAL(result, NULL, result, EXIT);
44
45 ret = pthread_setspecific(g_key, (void *)&temp);
46 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
47
48 LosTaskDelay(5);
49
50 ret = *((UINT32 *)pthread_getspecific(g_key));
51 ICUNIT_GOTO_EQUAL(ret, 2, ret, EXIT);
52
53 g_testCount++;
54 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT);
55 EXIT:
56 return static_cast<void *>(9); // 9: return value for testing
57 }
58
pthread_f02(void * argument)59 static VOID *pthread_f02(void *argument)
60 {
61 UINT32 temp = 9;
62 UINT32 ret;
63 VOID *result;
64
65 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
66 g_testCount++;
67
68 result = pthread_getspecific(g_key);
69 ICUNIT_GOTO_EQUAL(result, NULL, result, EXIT);
70
71 ret = pthread_setspecific(g_key, (void *)&temp);
72 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
73
74 LosTaskDelay(10);
75
76 ret = *((UINT32 *)pthread_getspecific(g_key));
77 ICUNIT_GOTO_EQUAL(ret, 9, ret, EXIT);
78
79 g_testCount++;
80 ICUNIT_GOTO_EQUAL(g_testCount, 6, g_testCount, EXIT);
81 EXIT:
82 return static_cast<void *>(8); // 8: return value for testing
83 }
84
PthreadF03(void * argument)85 static VOID *PthreadF03(void *argument)
86 {
87 char temp[10] = "abcde";
88 char *temp1;
89 UINT32 ret;
90 VOID *result;
91
92 ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT);
93 g_testCount++;
94
95 result = pthread_getspecific(g_key);
96 ICUNIT_GOTO_EQUAL(result, NULL, result, EXIT);
97
98 ret = pthread_setspecific(g_key, (void *)temp);
99 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
100
101 LosTaskDelay(20);
102
103 temp1 = (char *)pthread_getspecific(g_key);
104 ICUNIT_GOTO_STRING_EQUAL(temp1, "abcde", temp1, EXIT);
105
106 g_testCount++;
107 ICUNIT_GOTO_EQUAL(g_testCount, 8, g_testCount, EXIT);
108
109 EXIT:
110 return static_cast<void *>(7); // 7: return value for testing
111 }
112
PthreadKeyF01(void * threadLog)113 static void PthreadKeyF01(void *threadLog)
114 {
115 g_testCount++;
116 }
117
Testcase(VOID)118 static UINT32 Testcase(VOID)
119 {
120 pthread_t newTh, newTh2, newTh3;
121 UINT32 ret;
122 UINTPTR uwtemp = 1;
123
124 g_testCount = 0;
125
126 ret = pthread_key_create(&g_key, PthreadKeyF01);
127 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
128
129 ret = pthread_create(&newTh, NULL, pthread_f01, NULL);
130 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131
132 ret = pthread_create(&newTh2, NULL, pthread_f02, NULL);
133 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
134
135 ret = pthread_create(&newTh3, NULL, PthreadF03, NULL);
136 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
137
138 LosTaskDelay(30);
139 ICUNIT_ASSERT_EQUAL(g_testCount, 9, g_testCount);
140
141 ret = pthread_join(newTh, (void **)&uwtemp);
142 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143
144 ret = pthread_join(newTh2, (void **)&uwtemp);
145 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
146
147 ret = pthread_join(newTh3, (void **)&uwtemp);
148 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
149
150 ret = pthread_key_delete(g_key);
151 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
152
153 return PTHREAD_NO_ERROR;
154 }
155
ItPosixPthread052(VOID)156 VOID ItPosixPthread052(VOID) // IT_Layer_ModuleORFeature_No
157 {
158 TEST_ADD_CASE("IT_POSIX_PTHREAD_052", Testcase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
159 }
160