• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <pthread.h>
17 #include <stdlib.h>
18 #include "functionalext.h"
19 
20 #define TEST_STACK_SIZE 4096
21 
22 /**
23  * @tc.name      : pthread_attr_getscope_0100
24  * @tc.desc      : Get thread scope
25  * @tc.level     : Level 0
26  */
pthread_attr_getscope_0100(void)27 void pthread_attr_getscope_0100(void)
28 {
29     int scope = 0;
30     pthread_attr_t attr;
31     pthread_attr_init(&attr);
32     int rev = pthread_attr_getscope(&attr, &scope);
33     EXPECT_EQ("pthread_attr_getscope_0100", rev, 0);
34     EXPECT_EQ("pthread_attr_getscope_0100", scope, PTHREAD_SCOPE_SYSTEM);
35     pthread_attr_destroy(&attr);
36 }
37 
38 /**
39  * @tc.name      : pthread_attr_getguardsize_0100
40  * @tc.desc      : Get the guard value of the thread property
41  * @tc.level     : Level 0
42  */
pthread_attr_getguardsize_0100(void)43 void pthread_attr_getguardsize_0100(void)
44 {
45     size_t size = 0;
46     pthread_attr_t attr;
47     pthread_attr_init(&attr);
48     int rev = pthread_attr_getguardsize(&attr, &size);
49     EXPECT_EQ("pthread_attr_getguardsize_0100", rev, 0);
50     pthread_attr_destroy(&attr);
51 }
52 
53 /**
54  * @tc.name      : pthread_attr_getinheritsched_0100
55  * @tc.desc      : Verify that the preset properties can be obtained through get inheritsched in
56  *                 a single-threaded environment
57  * @tc.level     : Level 0
58  */
pthread_attr_getinheritsched_0100(void)59 void pthread_attr_getinheritsched_0100(void)
60 {
61     pthread_attr_t attr;
62     int result = -1;
63 
64     int ret = pthread_attr_init(&attr);
65     EXPECT_EQ("pthread_attr_getinheritsched_0100", ret, CMPFLAG);
66     ret = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
67     EXPECT_EQ("pthread_attr_getinheritsched_0100", ret, CMPFLAG);
68 
69     ret = pthread_attr_getinheritsched(&attr, &result);
70     EXPECT_EQ("pthread_attr_getinheritsched_0100", ret, CMPFLAG);
71     EXPECT_EQ("pthread_attr_getinheritsched_0100", result, PTHREAD_INHERIT_SCHED);
72 
73     ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
74     EXPECT_EQ("pthread_attr_getinheritsched_0100", ret, CMPFLAG);
75 
76     ret = pthread_attr_getinheritsched(&attr, &result);
77     EXPECT_EQ("pthread_attr_getinheritsched_0100", ret, CMPFLAG);
78     EXPECT_EQ("pthread_attr_getinheritsched_0100", result, PTHREAD_EXPLICIT_SCHED);
79 
80     pthread_attr_destroy(&attr);
81 }
82 
83 /**
84  * @tc.name      : pthread_attr_getinheritsched_0200
85  * @tc.desc      : Verify that the return value is EINVAL when setting an exception value in a single-threaded
86  * environment
87  * @tc.level     : Level 0
88  */
pthread_attr_getinheritsched_0200(void)89 void pthread_attr_getinheritsched_0200(void)
90 {
91     pthread_attr_t attr;
92     int inheritsched = 2;
93     int result = -1;
94 
95     int ret = pthread_attr_init(&attr);
96     EXPECT_EQ("pthread_attr_getinheritsched_0200", ret, CMPFLAG);
97     ret = pthread_attr_setinheritsched(&attr, inheritsched);
98     EXPECT_EQ("pthread_attr_getinheritsched_0200", ret, EINVAL);
99 
100     ret = pthread_attr_getinheritsched(&attr, &result);
101     EXPECT_EQ("pthread_attr_getinheritsched_0200", ret, CMPFLAG);
102     pthread_attr_destroy(&attr);
103 }
104 
105 /**
106  * @tc.name      : pthread_barrierattr_getpshared_0100
107  * @tc.desc      : Correct parameter setting barrier variable property value
108  * @tc.level     : Level 0
109  */
pthread_barrierattr_getpshared_0100(void)110 void pthread_barrierattr_getpshared_0100(void)
111 {
112     pthread_barrierattr_t attr;
113     int result = -1;
114     int share = 0;
115 
116     int ret = pthread_barrierattr_init(&attr);
117     EXPECT_EQ("pthread_barrierattr_getpshared_0100", ret, CMPFLAG);
118 
119     ret = pthread_barrierattr_setpshared(&attr, share);
120     EXPECT_EQ("pthread_barrierattr_getpshared_0100", ret, CMPFLAG);
121 
122     ret = pthread_barrierattr_getpshared(&attr, &result);
123     EXPECT_EQ("pthread_barrierattr_getpshared_0100", ret, CMPFLAG);
124     EXPECT_EQ("pthread_barrierattr_getpshared_0100", result, share);
125 
126     share = 1;
127     ret = pthread_barrierattr_setpshared(&attr, share);
128 
129     ret = pthread_barrierattr_getpshared(&attr, &result);
130     EXPECT_EQ("pthread_barrierattr_getpshared_0100", ret, CMPFLAG);
131     EXPECT_EQ("pthread_barrierattr_getpshared_0100", result, share);
132 
133     pthread_barrierattr_destroy(&attr);
134 }
135 
136 /**
137  * @tc.name      : pthread_condattr_getclock_0100
138  * @tc.desc      : Correct parameter setting condition variable property value
139  * @tc.level     : Level 0
140  */
pthread_condattr_getclock_0100(void)141 void pthread_condattr_getclock_0100(void)
142 {
143     pthread_condattr_t attr;
144     clockid_t result = -1;
145 
146     int ret = pthread_condattr_init(&attr);
147     EXPECT_EQ("pthread_condattr_getclock_0100", ret, CMPFLAG);
148 
149     ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC_RAW);
150     EXPECT_EQ("pthread_condattr_getclock_0100", ret, CMPFLAG);
151 
152     ret = pthread_condattr_getclock(&attr, &result);
153     EXPECT_EQ("pthread_condattr_getclock_0200", ret, CMPFLAG);
154     EXPECT_EQ("pthread_condattr_getclock_0100", result, CLOCK_MONOTONIC_RAW);
155 
156     pthread_condattr_destroy(&attr);
157 }
158 
159 /**
160  * @tc.name      : pthread_condattr_getpshared_0100
161  * @tc.desc      : Correct parameter setting condition variable property value
162  * @tc.level     : Level 0
163  */
pthread_condattr_getpshared_0100(void)164 void pthread_condattr_getpshared_0100(void)
165 {
166     pthread_condattr_t attr;
167     int result = -1;
168     int share = 0;
169     int ret = pthread_condattr_init(&attr);
170 
171     EXPECT_EQ("pthread_condattr_getpshared_0100", ret, CMPFLAG);
172 
173     ret = pthread_condattr_setpshared(&attr, share);
174     EXPECT_EQ("pthread_condattr_getpshared_0100", ret, CMPFLAG);
175 
176     ret = pthread_condattr_getpshared(&attr, &result);
177     EXPECT_EQ("pthread_condattr_getpshared_0100", ret, CMPFLAG);
178     EXPECT_EQ("pthread_condattr_getpshared_0100", result, share);
179 
180     share = 1;
181     ret = pthread_condattr_setpshared(&attr, share);
182 
183     ret = pthread_condattr_getpshared(&attr, &result);
184     EXPECT_EQ("pthread_condattr_getpshared_0100", ret, CMPFLAG);
185     EXPECT_EQ("pthread_condattr_getpshared_0100", result, share);
186 
187     pthread_condattr_destroy(&attr);
188 }
189 
190 /**
191  * @tc.name      : pthread_mutexattr_getprotocol_0100
192  * @tc.desc      : Correct parameter setting condition variable property value
193  * @tc.level     : Level 0
194  */
pthread_mutexattr_getprotocol_0100(void)195 void pthread_mutexattr_getprotocol_0100(void)
196 {
197     pthread_mutexattr_t attr;
198     int result = -1;
199 
200     int ret = pthread_mutexattr_init(&attr);
201     EXPECT_EQ("pthread_mutexattr_getprotocol_0100", ret, CMPFLAG);
202 
203     ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE);
204     EXPECT_EQ("pthread_mutexattr_getprotocol_0100", ret, CMPFLAG);
205 
206     ret = pthread_mutexattr_getprotocol(&attr, &result);
207     EXPECT_EQ("pthread_mutexattr_getprotocol_0100", ret, CMPFLAG);
208     EXPECT_EQ("pthread_mutexattr_getprotocol_0100", result, PTHREAD_PRIO_NONE);
209 
210     pthread_mutexattr_destroy(&attr);
211 }
212 
213 /**
214  * @tc.name      : pthread_mutexattr_gettype_0100
215  * @tc.desc      : Correct parameter setting condition variable property value
216  * @tc.level     : Level 0
217  */
pthread_mutexattr_gettype_0100(void)218 void pthread_mutexattr_gettype_0100(void)
219 {
220     pthread_mutexattr_t attr;
221     int result = -1;
222 
223     int ret = pthread_mutexattr_init(&attr);
224     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
225 
226     ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
227     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
228 
229     ret = pthread_mutexattr_gettype(&attr, &result);
230     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
231     EXPECT_EQ("pthread_mutexattr_gettype_0100", result, PTHREAD_MUTEX_NORMAL);
232 
233     ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
234     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
235 
236     ret = pthread_mutexattr_gettype(&attr, &result);
237     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
238     EXPECT_EQ("pthread_mutexattr_gettype_0100", result, PTHREAD_MUTEX_ERRORCHECK);
239 
240     ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
241     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
242 
243     ret = pthread_mutexattr_gettype(&attr, &result);
244     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
245     EXPECT_EQ("pthread_mutexattr_gettype_0100", result, PTHREAD_MUTEX_RECURSIVE);
246 
247     ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
248     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
249 
250     ret = pthread_mutexattr_gettype(&attr, &result);
251     EXPECT_EQ("pthread_mutexattr_gettype_0100", ret, CMPFLAG);
252     EXPECT_EQ("pthread_mutexattr_gettype_0100", result, PTHREAD_MUTEX_DEFAULT);
253 
254     pthread_mutexattr_destroy(&attr);
255 }
256 
257 /**
258  * @tc.name      : pthread_mutexattr_getpshared_0100
259  * @tc.desc      : Correct parameter setting mutex variable property value
260  * @tc.level     : Level 0
261  */
pthread_mutexattr_getpshared_0100(void)262 void pthread_mutexattr_getpshared_0100(void)
263 {
264     pthread_mutexattr_t attr;
265     int result = -1;
266     int share = 0;
267     int ret = pthread_mutexattr_init(&attr);
268 
269     EXPECT_EQ("pthread_mutexattr_getpshared_0100", ret, CMPFLAG);
270 
271     ret = pthread_mutexattr_setpshared(&attr, share);
272     EXPECT_EQ("pthread_mutexattr_getpshared_0100", ret, CMPFLAG);
273 
274     ret = pthread_mutexattr_getpshared(&attr, &result);
275     EXPECT_EQ("pthread_mutexattr_getpshared_0100", ret, CMPFLAG);
276     EXPECT_EQ("pthread_mutexattr_getpshared_0100", result, share);
277 
278     share = 1;
279     ret = pthread_mutexattr_setpshared(&attr, share);
280 
281     ret = pthread_mutexattr_getpshared(&attr, &result);
282     EXPECT_EQ("pthread_mutexattr_getpshared_0100", ret, CMPFLAG);
283     EXPECT_EQ("pthread_mutexattr_getpshared_0100", result, share);
284 
285     pthread_mutexattr_destroy(&attr);
286 }
287 
288 /**
289  * @tc.name      : pthread_rwlockattr_getpshared_0100
290  * @tc.desc      : Correct parameter setting rwlock variable property value
291  * @tc.level     : Level 0
292  */
pthread_rwlockattr_getpshared_0100(void)293 void pthread_rwlockattr_getpshared_0100(void)
294 {
295     pthread_rwlockattr_t attr;
296     int result = -1;
297     int share = 0;
298     int ret = pthread_rwlockattr_init(&attr);
299 
300     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", ret, CMPFLAG);
301 
302     ret = pthread_rwlockattr_setpshared(&attr, share);
303     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", ret, CMPFLAG);
304 
305     ret = pthread_rwlockattr_getpshared(&attr, &result);
306     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", ret, CMPFLAG);
307     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", result, share);
308 
309     share = 1;
310     ret = pthread_rwlockattr_setpshared(&attr, share);
311 
312     ret = pthread_rwlockattr_getpshared(&attr, &result);
313     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", ret, CMPFLAG);
314     EXPECT_EQ("pthread_rwlockattr_getpshared_0100", result, share);
315 
316     pthread_rwlockattr_destroy(&attr);
317 }
318 
319 /**
320  * @tc.name      : pthread_attr_getdetachstate_0100
321  * @tc.desc      : Get detached state from thread attr
322  * @tc.level     : Level 0
323  */
pthread_attr_getdetachstate_0100(void)324 void pthread_attr_getdetachstate_0100(void)
325 {
326     pthread_attr_t attr;
327     int state = 0;
328     pthread_attr_init(&attr);
329     int ret = pthread_attr_getdetachstate(&attr, &state);
330     EXPECT_EQ("pthread_attr_getdetachstate_0100", ret, CMPFLAG);
331     EXPECT_EQ("pthread_attr_getdetachstate_0100", state, CMPFLAG);
332     pthread_attr_destroy(&attr);
333 }
334 
335 /**
336  * @tc.name      : pthread_attr_getschedpolicy_0100
337  * @tc.desc      : Get detached state from thread attr
338  * @tc.level     : Level 0
339  */
pthread_attr_getschedpolicy_0100(void)340 void pthread_attr_getschedpolicy_0100(void)
341 {
342     pthread_attr_t attr;
343     int setpolicy = 1;
344     int getpolicy = 0;
345     pthread_attr_init(&attr);
346     int ret = pthread_attr_setschedpolicy(&attr, setpolicy);
347     EXPECT_EQ("pthread_attr_getschedpolicy_0100", ret, CMPFLAG);
348 
349     ret = pthread_attr_getschedpolicy(&attr, &getpolicy);
350     EXPECT_EQ("pthread_attr_getschedpolicy_0100", ret, CMPFLAG);
351     EXPECT_EQ("pthread_attr_getschedpolicy_0100", getpolicy, setpolicy);
352     pthread_attr_destroy(&attr);
353 }
354 
355 /**
356  * @tc.name      : pthread_attr_getschedparam_0100
357  * @tc.desc      : Get scheduling parameter attributes in thread attributes object
358  * @tc.level     : Level 0
359  */
pthread_attr_getschedparam_0100(void)360 void pthread_attr_getschedparam_0100(void)
361 {
362     pthread_attr_t attr;
363     struct sched_param setparam;
364     struct sched_param getparam;
365 
366     memset(&setparam, 0x0, sizeof(setparam));
367     memset(&getparam, 0x0, sizeof(getparam));
368     setparam.sched_priority = 1;
369 
370     pthread_attr_init(&attr);
371     int ret = pthread_attr_setschedparam(&attr, &setparam);
372     EXPECT_EQ("pthread_attr_getschedparam_0100", ret, CMPFLAG);
373 
374     ret = pthread_attr_getschedparam(&attr, &getparam);
375     EXPECT_EQ("pthread_attr_getschedparam_0100", ret, CMPFLAG);
376     EXPECT_EQ("pthread_attr_getschedparam_0100", getparam.sched_priority, 1);
377     pthread_attr_destroy(&attr);
378 }
379 
380 /**
381  * @tc.name      : pthread_attr_getstacksize_0100
382  * @tc.desc      : Get stack size attribute in thread attributes object
383  * @tc.level     : Level 0
384  */
pthread_attr_getstacksize_0100(void)385 void pthread_attr_getstacksize_0100(void)
386 {
387     size_t size;
388     pthread_attr_t attr;
389     pthread_attr_init(&attr);
390 
391     int ret = pthread_attr_setstacksize(&attr, TEST_STACK_SIZE);
392     EXPECT_EQ("pthread_attr_getstacksize_0100", ret, CMPFLAG);
393 
394     ret = pthread_attr_getstacksize(&attr, &size);
395     EXPECT_EQ("pthread_attr_getstacksize_0100", ret, CMPFLAG);
396     EXPECT_EQ("pthread_attr_getstacksize_0100", size, TEST_STACK_SIZE);
397     pthread_attr_destroy(&attr);
398 }
399 
main(void)400 int main(void)
401 {
402     pthread_attr_getscope_0100();
403     pthread_attr_getguardsize_0100();
404     pthread_attr_getinheritsched_0100();
405     pthread_attr_getinheritsched_0200();
406     pthread_barrierattr_getpshared_0100();
407     pthread_condattr_getclock_0100();
408     pthread_condattr_getpshared_0100();
409     pthread_mutexattr_getprotocol_0100();
410     pthread_mutexattr_gettype_0100();
411     pthread_mutexattr_getpshared_0100();
412     pthread_rwlockattr_getpshared_0100();
413     pthread_attr_getdetachstate_0100();
414     pthread_attr_getschedpolicy_0100();
415     pthread_attr_getschedparam_0100();
416     pthread_attr_getstacksize_0100();
417     return t_status;
418 }