• 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 <errno.h>
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include "test.h"
22 
23 #define Nanoseconds (1000000000)
24 static int count = 0;
25 
26 extern int __timer_gettime64(timer_t, struct itimerspec *);
27 
timerHandler(int sig)28 void timerHandler(int sig)
29 {
30     count++;
31     return;
32 }
33 
exception_handler(int sig)34 void exception_handler(int sig)
35 {
36     exit(t_status);
37 }
38 
39 /**
40  * @tc.name      : timer_gettime_0100
41  * @tc.desc      : Get the remaining time of the timer
42  * @tc.level     : Level 0
43  */
timer_gettime_0100(void)44 void timer_gettime_0100(void)
45 {
46     timer_t timerid;
47     struct sigevent sev;
48     struct itimerspec its;
49     struct itimerspec tmp;
50 
51     sev.sigev_notify = SIGEV_SIGNAL;
52     sev.sigev_signo = SIGUSR1;
53     sev.sigev_value.sival_ptr = &timerid;
54     signal(SIGUSR1, timerHandler);
55 
56     if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1) {
57         t_error("%s timer create failed", __func__);
58         return;
59     }
60 
61     its.it_value.tv_sec = 1;
62     its.it_value.tv_nsec = 0;
63     its.it_interval.tv_sec = 1;
64     its.it_interval.tv_nsec = 0;
65 
66     if (timer_settime(timerid, 0, &its, NULL) == -1) {
67         t_error("%s timer set time failed", __func__);
68         return;
69     }
70 
71     if (timer_gettime(timerid, &tmp) == -1 && errno == EINVAL) {
72         t_error("%s timer gettimer failed", __func__);
73     }
74 
75     if (tmp.it_value.tv_sec >= 1 || tmp.it_value.tv_nsec >= Nanoseconds) {
76         t_error("%s get time failed", __func__);
77     }
78 
79     while (count <= 0) {
80         sleep(1);
81     }
82 
83     if (timer_delete(timerid) != 0) {
84         t_error("%s timer_delete failed", __func__);
85     }
86 }
87 
88 /**
89  * @tc.name      : timer_gettime_0200
90  * @tc.desc      : The return value of the function when the parameter is abnormal
91  * @tc.level     : Level 2
92  */
timer_gettime_0200(void)93 void timer_gettime_0200(void)
94 {
95     int result = timer_gettime(NULL, NULL);
96     if (result != -1 && errno != EINVAL) {
97         t_error("%s failed result = %d", __func__, result);
98     }
99 }
100 
101 /**
102  * @tc.name      : timer_gettime64_0200
103  * @tc.desc      : The return value of the function when the parameter is abnormal
104  * @tc.level     : Level 2
105  */
timer_gettime64_0200(void)106 void timer_gettime64_0200(void)
107 {
108     int result = __timer_gettime64(NULL, NULL);
109     if (result != -1 && errno != EINVAL) {
110         t_error("%s failed result = %d", __func__, result);
111     }
112 }
113 
main(int argc,char * argv[])114 int main(int argc, char *argv[])
115 {
116     timer_gettime_0100();
117     timer_gettime_0200();
118     timer_gettime64_0200();
119     return t_status;
120 }