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_MUTEX_ATTR 15
21
22 extern int __pthread_mutex_timedlock_time64(pthread_mutex_t *__restrict, const struct timespec *__restrict);
23
24 /**
25 * @tc.name : pthread_mutex_timedlock_0100
26 * @tc.desc : Provide correct parameters, test timeout and lock
27 * @tc.level : Level 0
28 */
pthread_mutex_timedlock_0100(void)29 void pthread_mutex_timedlock_0100(void)
30 {
31 struct timespec ts = {.tv_nsec = 0, .tv_sec = 1};
32 pthread_mutexattr_t mutex_attr;
33 pthread_mutex_t mutex;
34 pthread_mutexattr_init(&mutex_attr);
35 pthread_mutexattr_settype(&mutex_attr, TEST_MUTEX_ATTR);
36 pthread_mutex_init(&mutex, &mutex_attr);
37
38 int rev = pthread_mutex_timedlock(&mutex, &ts);
39 EXPECT_EQ("pthread_mutex_timedlock_0100", rev, 0);
40
41 pthread_mutex_unlock(&mutex);
42 pthread_mutex_destroy(&mutex);
43 pthread_mutexattr_destroy(&mutex_attr);
44 }
45
46 /**
47 * @tc.name : pthread_mutex_timedlock_0200
48 * @tc.desc : The lock attribute is PTHREAD_MUTEX_ERRORCHECK, and the test timeout is locked
49 * @tc.level : Level 0
50 */
pthread_mutex_timedlock_0200(void)51 void pthread_mutex_timedlock_0200(void)
52 {
53 struct timespec ts = {.tv_nsec = 0, .tv_sec = 1};
54 pthread_mutexattr_t mutex_attr;
55 pthread_mutex_t mutex;
56
57 pthread_mutexattr_init(&mutex_attr);
58 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
59 pthread_mutex_init(&mutex, &mutex_attr);
60 int rev = pthread_mutex_timedlock(&mutex, &ts);
61 EXPECT_EQ("pthread_mutex_timedlock_0200", rev, 0);
62
63 pthread_mutex_unlock(&mutex);
64 pthread_mutex_destroy(&mutex);
65 pthread_mutexattr_destroy(&mutex_attr);
66 }
67
68 /**
69 * @tc.name : pthread_mutex_timedlock_0300
70 * @tc.desc : In the locked state, the test times out and locks
71 * @tc.level : Level 2
72 */
pthread_mutex_timedlock_0300(void)73 void pthread_mutex_timedlock_0300(void)
74 {
75 struct timespec ts = {.tv_nsec = 0, .tv_sec = 1};
76 pthread_mutexattr_t mutex_attr;
77 pthread_mutex_t mutex;
78 pthread_mutexattr_init(&mutex_attr);
79 pthread_mutexattr_settype(&mutex_attr, TEST_MUTEX_ATTR);
80 pthread_mutex_init(&mutex, &mutex_attr);
81 int rev = pthread_mutex_lock(&mutex);
82 EXPECT_EQ("pthread_mutex_timedlock_0300", rev, 0);
83 rev = pthread_mutex_timedlock(&mutex, &ts);
84 EXPECT_EQ("pthread_mutex_timedlock_0300", rev, ETIMEDOUT);
85
86 pthread_mutex_unlock(&mutex);
87 pthread_mutex_destroy(&mutex);
88 pthread_mutexattr_destroy(&mutex_attr);
89 }
90
91 /**
92 * @tc.name : pthread_mutex_timedlock_time64_0100
93 * @tc.desc : Provide correct parameters, test timeout and lock
94 * @tc.level : Level 0
95 */
pthread_mutex_timedlock_time64_0100(void)96 void pthread_mutex_timedlock_time64_0100(void)
97 {
98 struct timespec ts = {.tv_nsec = 0, .tv_sec = 1};
99 pthread_mutexattr_t mutex_attr;
100 pthread_mutex_t mutex;
101 pthread_mutexattr_init(&mutex_attr);
102 pthread_mutexattr_settype(&mutex_attr, TEST_MUTEX_ATTR);
103 pthread_mutex_init(&mutex, &mutex_attr);
104
105 int rev = __pthread_mutex_timedlock_time64(&mutex, &ts);
106 EXPECT_EQ("pthread_mutex_timedlock_time64_0100", rev, 0);
107
108 pthread_mutex_unlock(&mutex);
109 pthread_mutex_destroy(&mutex);
110 pthread_mutexattr_destroy(&mutex_attr);
111 }
112
main(void)113 int main(void)
114 {
115 pthread_mutex_timedlock_0100();
116 pthread_mutex_timedlock_0200();
117 pthread_mutex_timedlock_0300();
118 pthread_mutex_timedlock_time64_0100();
119 return t_status;
120 }