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