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 <signal.h>
17 #include <stdlib.h>
18 #include <threads.h>
19 #include "test.h"
20
21 extern int __thrd_sleep_time64(const struct timespec *, struct timespec *);
22
23 static int count = 0;
24
exception_handler(int sig)25 void exception_handler(int sig)
26 {
27 exit(t_status);
28 }
29
threadfunc(void * arg)30 int threadfunc(void *arg)
31 {
32 count++;
33
34 struct timespec const *tl = (struct timespec const *)arg;
35 if (thrd_sleep(tl, NULL) != 0) {
36 t_error("%s thrd_sleep failed", __func__);
37 }
38
39 thrd_exit(thrd_success);
40 }
41
42 /**
43 * @tc.name : thrd_sleep_0100
44 * @tc.desc : Thread waits for 1s
45 * @tc.level : Level 0
46 */
thrd_sleep_0100(void)47 void thrd_sleep_0100(void)
48 {
49 int result;
50 thrd_t id;
51 struct timespec ts = {.tv_sec = 1};
52
53 result = thrd_create(&id, threadfunc, (void *)(&ts));
54 if (result != thrd_success) {
55 t_error("%s thrd_create failed", __func__);
56 }
57
58 result = thrd_join(id, NULL);
59 if (result != thrd_success) {
60 t_error("%s thrd_join failed", __func__);
61 }
62
63 if (count != 1) {
64 t_error("%s failed, count is %d", __func__, count);
65 }
66 count = 0;
67 }
68
69 /**
70 * @tc.name : thrd_sleep_0200
71 * @tc.desc : Negative value
72 * @tc.level : Level 1
73 */
thrd_sleep_0200(void)74 void thrd_sleep_0200(void)
75 {
76 int result = thrd_sleep(&(struct timespec){.tv_sec = -1}, NULL);
77 if (result != -2) {
78 t_error("%s negative value", __func__);
79 }
80 }
81
82 /**
83 * @tc.name : thrd_sleep_0300
84 * @tc.desc : Invalid parameter test
85 * @tc.level : Level 2
86 */
thrd_sleep_0300(void)87 void thrd_sleep_0300(void)
88 {
89 signal(SIGSEGV, exception_handler);
90
91 thrd_sleep(NULL, NULL);
92 }
93
94 /**
95 * @tc.name : thrd_sleep_time64_0200
96 * @tc.desc : Negative value
97 * @tc.level : Level 1
98 */
thrd_sleep_time64_0200(void)99 void thrd_sleep_time64_0200(void)
100 {
101 int result = __thrd_sleep_time64(&(struct timespec){.tv_sec = -1}, NULL);
102 if (result != -2) {
103 t_error("%s negative value", __func__);
104 }
105 }
106
main(int argc,char * argv[])107 int main(int argc, char *argv[])
108 {
109 thrd_sleep_0100();
110 thrd_sleep_0200();
111 thrd_sleep_0300();
112 thrd_sleep_time64_0200();
113 return t_status;
114 }