1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <pthread.h>
17570af302Sopenharmony_ci#include <stdlib.h>
18570af302Sopenharmony_ci#include <unistd.h>
19570af302Sopenharmony_ci#include "functionalext.h"
20570af302Sopenharmony_ci
21570af302Sopenharmony_ciint32_t COUNT = 0;
22570af302Sopenharmony_ciconst int32_t INIT_LEN = 0;
23570af302Sopenharmony_ciconst int32_t INCREASE_LEN = 1;
24570af302Sopenharmony_ciconst int32_t PTHREADSTACK = 0x4001;
25570af302Sopenharmony_ciconst char *URL = "http://c.test.com";
26570af302Sopenharmony_ci
27570af302Sopenharmony_civoid *threadfuncA(void *arg)
28570af302Sopenharmony_ci{
29570af302Sopenharmony_ci    COUNT++;
30570af302Sopenharmony_ci    return arg;
31570af302Sopenharmony_ci}
32570af302Sopenharmony_ci
33570af302Sopenharmony_civoid *threadfunc(void *arg)
34570af302Sopenharmony_ci{
35570af302Sopenharmony_ci    return arg;
36570af302Sopenharmony_ci}
37570af302Sopenharmony_ci
38570af302Sopenharmony_ci/**
39570af302Sopenharmony_ci * @tc.name:      pthread_create_0100
40570af302Sopenharmony_ci * @tc.desc:      Verify pthread create process sucess when attrp and arg is NULL.
41570af302Sopenharmony_ci * @tc.level:     level 0.
42570af302Sopenharmony_ci */
43570af302Sopenharmony_civoid pthread_create_0100(void)
44570af302Sopenharmony_ci{
45570af302Sopenharmony_ci    pthread_t ph;
46570af302Sopenharmony_ci    int32_t ret = pthread_create(&ph, NULL, threadfuncA, NULL);
47570af302Sopenharmony_ci    pthread_join(ph, NULL);
48570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0100", ret, INIT_LEN);
49570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0100", COUNT, INCREASE_LEN);
50570af302Sopenharmony_ci}
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci/**
53570af302Sopenharmony_ci * @tc.name:      pthread_create_0200
54570af302Sopenharmony_ci * @tc.desc:      Verify pthread create process sucess when attrp is NULL.
55570af302Sopenharmony_ci * @tc.level:     level 0.
56570af302Sopenharmony_ci */
57570af302Sopenharmony_civoid pthread_create_0200(void)
58570af302Sopenharmony_ci{
59570af302Sopenharmony_ci    pthread_t ph;
60570af302Sopenharmony_ci    int32_t ret = pthread_create(&ph, NULL, threadfunc, (char *)(URL));
61570af302Sopenharmony_ci    pthread_join(ph, NULL);
62570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0200", ret, INIT_LEN);
63570af302Sopenharmony_ci}
64570af302Sopenharmony_ci
65570af302Sopenharmony_ci/**
66570af302Sopenharmony_ci * @tc.name:      pthread_create_0300
67570af302Sopenharmony_ci * @tc.desc:      Verify pthread create process sucess when attrp is NULL.
68570af302Sopenharmony_ci * @tc.level:     level 1.
69570af302Sopenharmony_ci */
70570af302Sopenharmony_civoid pthread_create_0300(void)
71570af302Sopenharmony_ci{
72570af302Sopenharmony_ci    pthread_t ph;
73570af302Sopenharmony_ci    pthread_attr_t atrr;
74570af302Sopenharmony_ci
75570af302Sopenharmony_ci    pthread_attr_init(&atrr);
76570af302Sopenharmony_ci    int32_t ret = pthread_create(&ph, &atrr, threadfunc, (char *)(URL));
77570af302Sopenharmony_ci    pthread_join(ph, NULL);
78570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0300", ret, INIT_LEN);
79570af302Sopenharmony_ci}
80570af302Sopenharmony_ci
81570af302Sopenharmony_ci/**
82570af302Sopenharmony_ci * @tc.name:      pthread_create_0400
83570af302Sopenharmony_ci * @tc.desc:      Verify pthread create process sucess when stackAddr is null.
84570af302Sopenharmony_ci * @tc.level:     level 1
85570af302Sopenharmony_ci */
86570af302Sopenharmony_civoid pthread_create_0400(void)
87570af302Sopenharmony_ci{
88570af302Sopenharmony_ci    int32_t ret;
89570af302Sopenharmony_ci    pthread_t ph;
90570af302Sopenharmony_ci    void *stackAddr = NULL;
91570af302Sopenharmony_ci    pthread_attr_t attr;
92570af302Sopenharmony_ci    int32_t pageSize = getpagesize();
93570af302Sopenharmony_ci    pthread_attr_init(&attr);
94570af302Sopenharmony_ci    ret = posix_memalign(&stackAddr, pageSize, PTHREADSTACK);
95570af302Sopenharmony_ci    if (ret) {
96570af302Sopenharmony_ci        EXPECT_NE("pthread_create_0400", ret, 0);
97570af302Sopenharmony_ci        return;
98570af302Sopenharmony_ci    }
99570af302Sopenharmony_ci    ret = pthread_attr_setstack(&attr, stackAddr, PTHREADSTACK);
100570af302Sopenharmony_ci    if (ret) {
101570af302Sopenharmony_ci        EXPECT_NE("pthread_create_0400", ret, 0);
102570af302Sopenharmony_ci        return;
103570af302Sopenharmony_ci    }
104570af302Sopenharmony_ci    ret = pthread_create(&ph, &attr, threadfunc, (char *)(URL));
105570af302Sopenharmony_ci    pthread_join(ph, NULL);
106570af302Sopenharmony_ci    pthread_attr_destroy(&attr);
107570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0400", ret, INIT_LEN);
108570af302Sopenharmony_ci}
109570af302Sopenharmony_ci
110570af302Sopenharmony_ci/**
111570af302Sopenharmony_ci * @tc.name:      pthread_create_0500
112570af302Sopenharmony_ci * @tc.desc:      Verify pthread_create success when pthread is detached.
113570af302Sopenharmony_ci * @tc.level:     levlel 1
114570af302Sopenharmony_ci */
115570af302Sopenharmony_civoid pthread_create_0500(void)
116570af302Sopenharmony_ci{
117570af302Sopenharmony_ci    pthread_t ph;
118570af302Sopenharmony_ci    pthread_attr_t attr;
119570af302Sopenharmony_ci
120570af302Sopenharmony_ci    pthread_attr_init(&attr);
121570af302Sopenharmony_ci    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
122570af302Sopenharmony_ci    int32_t ret = pthread_create(&ph, &attr, threadfunc, (char *)(URL));
123570af302Sopenharmony_ci    EXPECT_EQ("pthread_create_0500", ret, INIT_LEN);
124570af302Sopenharmony_ci}
125570af302Sopenharmony_ci
126570af302Sopenharmony_ciint main(void)
127570af302Sopenharmony_ci{
128570af302Sopenharmony_ci    pthread_create_0100();
129570af302Sopenharmony_ci    pthread_create_0200();
130570af302Sopenharmony_ci    pthread_create_0300();
131570af302Sopenharmony_ci    pthread_create_0400();
132570af302Sopenharmony_ci    pthread_create_0500();
133570af302Sopenharmony_ci    return t_status;
134570af302Sopenharmony_ci}
135