• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern int __pthread_rwlock_timedwrlock_time64(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
21 
22 /**
23  * @tc.name      : pthread_rwlock_timedwrlock_0100
24  * @tc.desc      : Provide correct parameters, test timeout and rwlock
25  * @tc.level     : Level 0
26  */
pthread_rwlock_timedwrlock_0100(void)27 void pthread_rwlock_timedwrlock_0100(void)
28 {
29     struct timespec ts = {.tv_nsec = 0, .tv_sec = 0};
30     pthread_rwlock_t rwlock;
31     pthread_rwlock_init(&rwlock, NULL);
32     clock_gettime(CLOCK_REALTIME, &ts);
33     struct tm *tmp = localtime(&ts.tv_sec);
34     int rev = -1;
35     if (tmp) {
36         ts.tv_sec += 1;
37         rev = pthread_rwlock_timedwrlock(&rwlock, &ts);
38     }
39     EXPECT_EQ("pthread_rwlock_timedwrlock_0100", rev, 0);
40     if (rev == 0) {
41         pthread_rwlock_unlock(&rwlock);
42     }
43     pthread_rwlock_destroy(&rwlock);
44 }
45 
46 /**
47  * @tc.name      : pthread_rwlock_timedwrlock_0200
48  * @tc.desc      : In the locked (read lock) state, the test times out and rwlock
49  * @tc.level     : Level 2
50  */
pthread_rwlock_timedwrlock_0200(void)51 void pthread_rwlock_timedwrlock_0200(void)
52 {
53     struct timespec ts = {.tv_nsec = 0, .tv_sec = 0};
54     pthread_rwlock_t rwlock;
55     pthread_rwlock_init(&rwlock, NULL);
56     int rev = pthread_rwlock_timedrdlock(&rwlock, NULL);
57     EXPECT_EQ("pthread_rwlock_timedwrlock_0200", rev, 0);
58     clock_gettime(CLOCK_REALTIME, &ts);
59     struct tm *tmp = localtime(&ts.tv_sec);
60     rev = -1;
61     if (tmp) {
62         ts.tv_sec += 1;
63         rev = pthread_rwlock_timedwrlock(&rwlock, &ts);
64     }
65     EXPECT_EQ("pthread_rwlock_timedwrlock_0200", rev, ETIMEDOUT);
66     pthread_rwlock_unlock(&rwlock);
67     pthread_rwlock_destroy(&rwlock);
68 }
69 
70 /**
71  * @tc.name      : pthread_rwlock_timedwrlock_0300
72  * @tc.desc      : In the locked (write lock) state, the test times out and rwlock
73  * @tc.level     : Level 2
74  */
pthread_rwlock_timedwrlock_0300(void)75 void pthread_rwlock_timedwrlock_0300(void)
76 {
77     struct timespec ts = {.tv_nsec = 0, .tv_sec = 0};
78     pthread_rwlock_t rwlock;
79     pthread_rwlock_init(&rwlock, NULL);
80     int rev = pthread_rwlock_timedwrlock(&rwlock, NULL);
81     EXPECT_EQ("pthread_rwlock_timedwrlock_0300", rev, 0);
82     clock_gettime(CLOCK_REALTIME, &ts);
83     struct tm *tmp = localtime(&ts.tv_sec);
84     rev = -1;
85     if (tmp) {
86         ts.tv_sec += 1;
87         rev = pthread_rwlock_timedwrlock(&rwlock, &ts);
88     }
89     EXPECT_EQ("pthread_rwlock_timedwrlock_0300", rev, ETIMEDOUT);
90     pthread_rwlock_unlock(&rwlock);
91     pthread_rwlock_destroy(&rwlock);
92 }
93 
94 /**
95  * @tc.name      : pthread_rwlock_timedwrlock_time64_0100
96  * @tc.desc      : Provide correct parameters, test timeout and rwlock
97  * @tc.level     : Level 0
98  */
pthread_rwlock_timedwrlock_time64_0100(void)99 void pthread_rwlock_timedwrlock_time64_0100(void)
100 {
101     struct timespec ts = {.tv_nsec = 0, .tv_sec = 0};
102     pthread_rwlock_t rwlock;
103     pthread_rwlock_init(&rwlock, NULL);
104     clock_gettime(CLOCK_REALTIME, &ts);
105     struct tm *tmp = localtime(&ts.tv_sec);
106     int rev = -1;
107     if (tmp) {
108         ts.tv_sec += 1;
109         rev = __pthread_rwlock_timedwrlock_time64(&rwlock, &ts);
110     }
111     EXPECT_EQ("pthread_rwlock_timedwrlock_time64_0100", rev, 0);
112     if (rev == 0) {
113         pthread_rwlock_unlock(&rwlock);
114     }
115     pthread_rwlock_destroy(&rwlock);
116 }
117 
main(void)118 int main(void)
119 {
120     pthread_rwlock_timedwrlock_0100();
121     pthread_rwlock_timedwrlock_0200();
122     pthread_rwlock_timedwrlock_0300();
123     pthread_rwlock_timedwrlock_time64_0100();
124     return t_status;
125 }