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 <limits.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <stdlib.h>
20 #include "functionalext.h"
21
22 #define THREAD_STACK_LEN 0x4001
23 #define BUFFEIZE 0x3000
24
25 /**
26 * @tc.name : pthread_attr_setstack_0100
27 * @tc.desc : Create a stack memory size of 4k, get the size value through the
28 * pthread_attr_getstack function, whether the setting is successful
29 * @tc.level : Level 0
30 */
pthread_attr_setstack_0100(void)31 void pthread_attr_setstack_0100(void)
32 {
33 pthread_t thread;
34 pthread_attr_t attr;
35 void *stackAddr = NULL;
36 void *getstackaddr = NULL;
37
38 size_t getstackSize = 0;
39 int ret = 0;
40 void *p = NULL;
41
42 int pageSize = getpagesize();
43 if (pageSize == 0) {
44 t_error("page Size is 0\n");
45 return;
46 }
47 pthread_attr_init(&attr);
48 ret = posix_memalign(&stackAddr, pageSize, THREAD_STACK_LEN);
49 if (ret != 0) {
50 EXPECT_EQ("pthread_attr_setstack_0100", ret, 0);
51 return;
52 }
53 ret = pthread_attr_setstack(&attr, stackAddr, THREAD_STACK_LEN);
54 if (ret != 0) {
55 EXPECT_EQ("pthread_attr_setstack_0100", ret, 0);
56 return;
57 }
58
59 pthread_attr_getstack(&attr, &getstackaddr, &getstackSize);
60 EXPECT_EQ("pthread_attr_setstack_0100", getstackSize, THREAD_STACK_LEN);
61
62 pthread_attr_destroy(&attr);
63 free(stackAddr);
64 stackAddr = NULL;
65 }
66
67 /**
68 * @tc.name : pthread_attr_setstack_0200
69 * @tc.desc : Determine whether the stack maximum function returns an abnormal value
70 * @tc.level : Level 2
71 */
pthread_attr_setstack_0200(void)72 void pthread_attr_setstack_0200(void)
73 {
74 pthread_attr_t attr;
75 void *stackAddr = NULL;
76 int ret = 0;
77 int MINSIZE = PTHREAD_STACK_MIN - 1;
78
79 pthread_attr_init(&attr);
80
81 ret = pthread_attr_setstack(&attr, stackAddr, ERREXPECT);
82 EXPECT_EQ("pthread_attr_setstack_0200", ret, EINVAL);
83
84 ret = pthread_attr_setstack(&attr, stackAddr, MINSIZE);
85 EXPECT_EQ("pthread_attr_setstack_0200", ret, EINVAL);
86
87 pthread_attr_destroy(&attr);
88 }
89
main(void)90 int main(void)
91 {
92 pthread_attr_setstack_0100();
93 pthread_attr_setstack_0200();
94
95 return t_status;
96 }