1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <sys/time.h>
20 #include <time.h>
21
TEST(Time,Time)22 TEST(Time, Time) {
23 time_t t = time(nullptr);
24 EXPECT_NE(t, -1);
25 EXPECT_NE(t, 0);
26 time_t t1 = time(&t);
27 EXPECT_NE(t1, -1);
28 EXPECT_NE(t1, 0);
29 EXPECT_LE(t1 - t, 1);
30 }
31
TEST(Time,Localtime)32 TEST(Time, Localtime) {
33 time_t time = 123;
34 struct tm time_info;
35 ASSERT_EQ(localtime_r(&time, &time_info), &time_info);
36 ASSERT_EQ(mktime(&time_info), 123);
37 }
38
TEST(Time,Gmtime)39 TEST(Time, Gmtime) {
40 time_t time = 123;
41 struct tm time_info;
42 memset(&time_info, 0, sizeof(time_info));
43 ASSERT_EQ(gmtime_r(&time, &time_info), &time_info);
44 EXPECT_EQ(time_info.tm_year, 70);
45 EXPECT_EQ(time_info.tm_gmtoff, 0);
46 }
47
TEST(Time,Ctime)48 TEST(Time, Ctime) {
49 time_t time = 123;
50 char buf[30];
51 buf[0] = 0;
52 ASSERT_EQ(ctime_r(&time, buf), buf);
53 ASSERT_NE(buf[0], 0);
54 }
55
TEST(Time,ClockGetres)56 TEST(Time, ClockGetres) {
57 struct timespec res;
58 ASSERT_EQ(clock_getres(CLOCK_REALTIME, &res), 0);
59 ASSERT_TRUE(res.tv_sec != 0 || res.tv_nsec != 0);
60 }
61
TEST(Time,ClockGettime)62 TEST(Time, ClockGettime) {
63 struct timespec res;
64 ASSERT_EQ(clock_gettime(CLOCK_REALTIME, &res), 0);
65 ASSERT_TRUE(res.tv_sec != 0 || res.tv_nsec != 0);
66 }
67
TEST(Time,Gettimeofday)68 TEST(Time, Gettimeofday) {
69 struct timeval tv;
70 struct timezone tz;
71 tv.tv_sec = -1;
72 tv.tv_usec = -1;
73 ASSERT_EQ(0, gettimeofday(&tv, &tz));
74 EXPECT_NE(tv.tv_sec, -1);
75 EXPECT_GE(tv.tv_usec, 0);
76 EXPECT_LT(tv.tv_usec, 1000000);
77 }
78