• 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 <sys/time.h>
17 #include <stdlib.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21 
22 const int SUCCESS = 0;
23 
24 extern int __gettimeofday_time64(struct timeval *__restrict, void *__restrict);
25 
26 /**
27  * @tc.name      : gettimeofday_0100
28  * @tc.desc      : Each parameter is valid, and the current system time can be obtained.
29  * @tc.level     : Level 0
30  */
gettimeofday_0100(void)31 void gettimeofday_0100(void)
32 {
33     struct timeval tv;
34     char str[10];
35     system("date +%s > ./time.txt");
36     FILE *fptr = fopen("time.txt", "r");
37     fflush(fptr);
38     fread(str, sizeof(str), 1, fptr);
39     int sec = atoi(str);
40     int returnflag = gettimeofday(&tv, NULL);
41     EXPECT_EQ("gettimeofday_0100", returnflag, SUCCESS);
42     EXPECT_EQ("gettimeofday_0100", (long)sec, (long)tv.tv_sec);
43     remove("time.txt");
44 }
45 
46 /**
47  * @tc.name      : gettimeofday_0200
48  * @tc.desc      : Each parameter is invalid, and the current system time can be obtained.
49  * @tc.level     : Level 2
50  */
gettimeofday_0200(void)51 void gettimeofday_0200(void)
52 {
53     int returnflag = gettimeofday(NULL, NULL);
54     EXPECT_EQ("gettimeofday_0200", returnflag, SUCCESS);
55 }
56 
57 /**
58  * @tc.name      : gettimeofday_time64_0100
59  * @tc.desc      : Each parameter is valid, and the current system time can be obtained.
60  * @tc.level     : Level 0
61  */
gettimeofday_time64_0100(void)62 void gettimeofday_time64_0100(void)
63 {
64     struct timeval tv;
65     char str[10];
66     system("date +%s > ./time.txt");
67     FILE *fptr = fopen("time.txt", "r");
68     fflush(fptr);
69     fread(str, sizeof(str), 1, fptr);
70     int sec = atoi(str);
71     int returnflag = __gettimeofday_time64(&tv, NULL);
72     EXPECT_EQ("gettimeofday_time64_0100", returnflag, SUCCESS);
73     EXPECT_EQ("gettimeofday_time64_0100", (long)sec, (long)tv.tv_sec);
74     remove("time.txt");
75 }
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79     gettimeofday_0100();
80     gettimeofday_0200();
81     gettimeofday_time64_0100();
82     return t_status;
83 }