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 <unistd.h>
19 #include "functionalext.h"
20
21 int32_t COUNT = 0;
22 const int32_t INIT_LEN = 0;
23 const int32_t INCREASE_LEN = 1;
24 const int32_t PTHREADSTACK = 0x4001;
25 const char *URL = "http://c.test.com";
26
threadfuncA(void * arg)27 void *threadfuncA(void *arg)
28 {
29 COUNT++;
30 return arg;
31 }
32
threadfunc(void * arg)33 void *threadfunc(void *arg)
34 {
35 return arg;
36 }
37
38 /**
39 * @tc.name: pthread_create_0100
40 * @tc.desc: Verify pthread create process sucess when attrp and arg is NULL.
41 * @tc.level: level 0.
42 */
pthread_create_0100(void)43 void pthread_create_0100(void)
44 {
45 pthread_t ph;
46 int32_t ret = pthread_create(&ph, NULL, threadfuncA, NULL);
47 pthread_join(ph, NULL);
48 EXPECT_EQ("pthread_create_0100", ret, INIT_LEN);
49 EXPECT_EQ("pthread_create_0100", COUNT, INCREASE_LEN);
50 }
51
52 /**
53 * @tc.name: pthread_create_0200
54 * @tc.desc: Verify pthread create process sucess when attrp is NULL.
55 * @tc.level: level 0.
56 */
pthread_create_0200(void)57 void pthread_create_0200(void)
58 {
59 pthread_t ph;
60 int32_t ret = pthread_create(&ph, NULL, threadfunc, (char *)(URL));
61 pthread_join(ph, NULL);
62 EXPECT_EQ("pthread_create_0200", ret, INIT_LEN);
63 }
64
65 /**
66 * @tc.name: pthread_create_0300
67 * @tc.desc: Verify pthread create process sucess when attrp is NULL.
68 * @tc.level: level 1.
69 */
pthread_create_0300(void)70 void pthread_create_0300(void)
71 {
72 pthread_t ph;
73 pthread_attr_t atrr;
74
75 pthread_attr_init(&atrr);
76 int32_t ret = pthread_create(&ph, &atrr, threadfunc, (char *)(URL));
77 pthread_join(ph, NULL);
78 EXPECT_EQ("pthread_create_0300", ret, INIT_LEN);
79 }
80
81 /**
82 * @tc.name: pthread_create_0400
83 * @tc.desc: Verify pthread create process sucess when stackAddr is null.
84 * @tc.level: level 1
85 */
pthread_create_0400(void)86 void pthread_create_0400(void)
87 {
88 int32_t ret;
89 pthread_t ph;
90 void *stackAddr = NULL;
91 pthread_attr_t attr;
92 int32_t pageSize = getpagesize();
93 pthread_attr_init(&attr);
94 ret = posix_memalign(&stackAddr, pageSize, PTHREADSTACK);
95 if (ret) {
96 EXPECT_NE("pthread_create_0400", ret, 0);
97 return;
98 }
99 ret = pthread_attr_setstack(&attr, stackAddr, PTHREADSTACK);
100 if (ret) {
101 EXPECT_NE("pthread_create_0400", ret, 0);
102 return;
103 }
104 ret = pthread_create(&ph, &attr, threadfunc, (char *)(URL));
105 pthread_join(ph, NULL);
106 pthread_attr_destroy(&attr);
107 EXPECT_EQ("pthread_create_0400", ret, INIT_LEN);
108 }
109
110 /**
111 * @tc.name: pthread_create_0500
112 * @tc.desc: Verify pthread_create success when pthread is detached.
113 * @tc.level: levlel 1
114 */
pthread_create_0500(void)115 void pthread_create_0500(void)
116 {
117 pthread_t ph;
118 pthread_attr_t attr;
119
120 pthread_attr_init(&attr);
121 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
122 int32_t ret = pthread_create(&ph, &attr, threadfunc, (char *)(URL));
123 EXPECT_EQ("pthread_create_0500", ret, INIT_LEN);
124 }
125
main(void)126 int main(void)
127 {
128 pthread_create_0100();
129 pthread_create_0200();
130 pthread_create_0300();
131 pthread_create_0400();
132 pthread_create_0500();
133 return t_status;
134 }
135