1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "base/clock.h"
31 
32 #include "base/clock_mock.h"
33 #include "testing/base/public/gunit.h"
34 
35 namespace mozc {
36 namespace {
37 
38 // 2020-12-23 13:24:35 (Wed) UTC
39 // 123456 [usec]
40 const uint64 kTestSeconds = 1608729875uLL;
41 const uint32 kTestMicroSeconds = 123456u;
42 
TEST(ClockTest,TimeTestWithMock)43 TEST(ClockTest, TimeTestWithMock) {
44   ClockMock clock_mock(kTestSeconds, kTestMicroSeconds);
45   Clock::SetClockForUnitTest(&clock_mock);
46 
47   // GetTime
48   {
49     EXPECT_EQ(kTestSeconds, Clock::GetTime());
50   }
51 
52   // GetTimeOfDay
53   {
54     uint64 current_sec;
55     uint32 current_usec;
56     Clock::GetTimeOfDay(&current_sec, &current_usec);
57     EXPECT_EQ(kTestSeconds, current_sec);
58     EXPECT_EQ(kTestMicroSeconds, current_usec);
59   }
60 
61   // GetCurrentTm
62   // 2020-12-23 13:24:35 (Wed)
63   {
64     tm current_tm;
65     Clock::GetCurrentTm(&current_tm);
66     EXPECT_EQ(120, current_tm.tm_year);
67     EXPECT_EQ(11,  current_tm.tm_mon);
68     EXPECT_EQ(23,  current_tm.tm_mday);
69     EXPECT_EQ(13,  current_tm.tm_hour);
70     EXPECT_EQ(24,  current_tm.tm_min);
71     EXPECT_EQ(35,  current_tm.tm_sec);
72     EXPECT_EQ(3,   current_tm.tm_wday);
73   }
74 
75   // GetTmWithoutOffsetSecond
76   // 2024/02/23 23:11:15 (Fri)
77   {
78     const int offset_seconds = 100000000;
79     tm offset_tm;
80     Clock::GetTmWithOffsetSecond(&offset_tm, offset_seconds);
81     EXPECT_EQ(124, offset_tm.tm_year);
82     EXPECT_EQ(1,   offset_tm.tm_mon);
83     EXPECT_EQ(23,  offset_tm.tm_mday);
84     EXPECT_EQ(23,  offset_tm.tm_hour);
85     EXPECT_EQ(11,  offset_tm.tm_min);
86     EXPECT_EQ(15,  offset_tm.tm_sec);
87     EXPECT_EQ(5,   offset_tm.tm_wday);
88   }
89 
90   // GetFrequency / GetTicks
91   {
92     const uint64 kFrequency = 12345;
93     const uint64 kTicks = 54321;
94     clock_mock.SetFrequency(kFrequency);
95     EXPECT_EQ(kFrequency, Clock::GetFrequency());
96     clock_mock.SetTicks(kTicks);
97     EXPECT_EQ(kTicks, Clock::GetTicks());
98   }
99 
100   // Restore the default clock
101   Clock::SetClockForUnitTest(nullptr);
102 
103   // GetFrequency / GetTicks without ClockMock
104   {
105     EXPECT_NE(0, Clock::GetFrequency());
106     EXPECT_NE(0, Clock::GetTicks());
107   }
108 }
109 
TEST(ClockTest,TimeTestWithoutMock)110 TEST(ClockTest, TimeTestWithoutMock) {
111   uint64 get_time_of_day_sec, get_time_sec;
112   uint32 get_time_of_day_usec;
113 
114   Clock::GetTimeOfDay(&get_time_of_day_sec, &get_time_of_day_usec);
115   get_time_sec = Clock::GetTime();
116 
117   // hmm, unstable test.
118   const int margin = 1;
119   EXPECT_NEAR(get_time_of_day_sec, get_time_sec, margin)
120       << ": This test have possibilities to fail "
121       << "when system is busy and slow.";
122 }
123 
124 }  // namespace
125 }  // namespace mozc
126