1 /*
2  * Copyright (C) 2017 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 "chre/util/time.h"
20 
21 using chre::Seconds;
22 using chre::Milliseconds;
23 using chre::Microseconds;
24 using chre::Nanoseconds;
25 using chre::kOneSecondInMilliseconds;
26 using chre::kOneSecondInNanoseconds;
27 using chre::kOneMillisecondInMicroseconds;
28 using chre::kOneMillisecondInNanoseconds;
29 using chre::kOneMicrosecondInNanoseconds;
30 
31 // Tests for Time constants
TEST(Time,CheckTimeConversionConstants)32 TEST(Time, CheckTimeConversionConstants) {
33   EXPECT_EQ(kOneSecondInNanoseconds, 1e9);
34   EXPECT_EQ(kOneMillisecondInNanoseconds, 1e6);
35   EXPECT_EQ(kOneMicrosecondInNanoseconds, 1e3);
36   EXPECT_EQ(kOneMillisecondInMicroseconds, 1e3);
37 }
38 
39 // Tests for Seconds
TEST(Time,ConvertSecToNanosec)40 TEST(Time, ConvertSecToNanosec) {
41   Seconds t(5);
42   EXPECT_EQ(t.toRawNanoseconds(), 5 * kOneSecondInNanoseconds);
43 }
44 
TEST(Time,ConvertSecToNanosecOverflowIsUint64Max)45 TEST(Time, ConvertSecToNanosecOverflowIsUint64Max) {
46   Seconds t(UINT64_MAX / kOneSecondInNanoseconds + 1);
47   EXPECT_EQ(t.toRawNanoseconds(), UINT64_MAX);
48 }
49 
TEST(Time,ConvertSecToMillisec)50 TEST(Time, ConvertSecToMillisec) {
51   Seconds t(5);
52   EXPECT_EQ(t.getMilliseconds(), 5 * kOneSecondInMilliseconds);
53 }
54 
TEST(Time,ConvertSecToMillisecOverflowIsUint64Max)55 TEST(Time, ConvertSecToMillisecOverflowIsUint64Max) {
56   Seconds t(UINT64_MAX / kOneSecondInMilliseconds + 1);
57   EXPECT_EQ(t.getMilliseconds(), UINT64_MAX);
58 }
59 
60 // Tests for Milliseconds
TEST(Time,DefaultMillisecIsZero)61 TEST(Time, DefaultMillisecIsZero) {
62   Milliseconds t;
63   EXPECT_EQ(t.getMilliseconds(), 0);
64 }
65 
TEST(Time,GetInitialMillisec)66 TEST(Time, GetInitialMillisec) {
67   Milliseconds t(5);
68   EXPECT_EQ(t.getMilliseconds(), 5);
69 }
70 
TEST(Time,InitializeMillisecFromNanosec)71 TEST(Time, InitializeMillisecFromNanosec) {
72   Nanoseconds tNano(5 * kOneMillisecondInNanoseconds);
73   Milliseconds tMilli(tNano);
74   EXPECT_EQ(tMilli.getMilliseconds(), 5);
75 }
76 
TEST(Time,ConcertMillisecToMicrosec)77 TEST(Time, ConcertMillisecToMicrosec) {
78   Milliseconds t(5);
79   EXPECT_EQ(t.getMicroseconds(), 5 * kOneMillisecondInMicroseconds);
80 }
81 
TEST(Time,ConvertMillisecToMicrosecOverflowIsUint64Max)82 TEST(Time, ConvertMillisecToMicrosecOverflowIsUint64Max) {
83   Milliseconds t(UINT64_MAX / kOneMillisecondInMicroseconds + 1);
84   EXPECT_EQ(t.getMicroseconds(), UINT64_MAX);
85 }
86 
TEST(Time,ConvertMillisecToNanosec)87 TEST(Time, ConvertMillisecToNanosec) {
88   Milliseconds t(5);
89   EXPECT_EQ(t.toRawNanoseconds(), 5 * kOneMillisecondInNanoseconds);
90 }
91 
TEST(Time,ConvertMillisecToNanosecOverflowIsUint64Max)92 TEST(Time, ConvertMillisecToNanosecOverflowIsUint64Max) {
93   Milliseconds t(UINT64_MAX / kOneMillisecondInNanoseconds + 1);
94   EXPECT_EQ(t.toRawNanoseconds(), UINT64_MAX);
95 }
96 
TEST(Time,TestMillisecEquals)97 TEST(Time, TestMillisecEquals) {
98   Milliseconds t1(5), t2(5), t3(6);
99   EXPECT_TRUE(t1 == t2);
100   EXPECT_FALSE(t1 == t3);
101 }
102 
103 // Tests for Microseconds
TEST(Time,GetInitialMicrosec)104 TEST(Time, GetInitialMicrosec) {
105   Microseconds t(5);
106   EXPECT_EQ(t.getMicroseconds(), 5);
107 }
108 
TEST(Time,InitializeMicrosecFromNanosec)109 TEST(Time, InitializeMicrosecFromNanosec) {
110   Nanoseconds tNano(5 * kOneMicrosecondInNanoseconds);
111   Microseconds tMicro(tNano);
112   EXPECT_EQ(tMicro.getMicroseconds(), 5);
113 }
114 
TEST(Time,ConvertMicrosecToNanosec)115 TEST(Time, ConvertMicrosecToNanosec) {
116   Microseconds t(5);
117   EXPECT_EQ(t.toRawNanoseconds(), 5 * kOneMicrosecondInNanoseconds);
118 }
119 
TEST(Time,ConvertMicrosecToNanosecOverflowIsUint64Max)120 TEST(Time, ConvertMicrosecToNanosecOverflowIsUint64Max) {
121   Milliseconds t(UINT64_MAX / kOneMicrosecondInNanoseconds + 1);
122   EXPECT_EQ(t.toRawNanoseconds(), UINT64_MAX);
123 }
124 
TEST(Time,ConvertMicrosecToMillisec)125 TEST(Time, ConvertMicrosecToMillisec) {
126   Microseconds t(5120);
127   EXPECT_EQ(t.getMilliseconds(), 5);
128 }
129 
130 // Tests for Nanoseconds
TEST(Time,DefaultNanosecIsZero)131 TEST(Time, DefaultNanosecIsZero) {
132   Nanoseconds t;
133   EXPECT_EQ(t.toRawNanoseconds(), 0);
134 }
135 
TEST(Time,GetInitialNanosec)136 TEST(Time, GetInitialNanosec) {
137   Nanoseconds t(5);
138   EXPECT_EQ(t.toRawNanoseconds(), 5);
139 }
140 
TEST(Time,InitializeNanosecFromSec)141 TEST(Time, InitializeNanosecFromSec) {
142   Seconds tSec(5);
143   Nanoseconds tNano(tSec);
144   EXPECT_EQ(tNano.toRawNanoseconds(), 5 * kOneSecondInNanoseconds);
145 }
146 
TEST(Time,InitializeNanosecFromMillisec)147 TEST(Time, InitializeNanosecFromMillisec) {
148   Milliseconds tMilli(5);
149   Nanoseconds tNano(tMilli);
150   EXPECT_EQ(tNano.toRawNanoseconds(), 5 * kOneMillisecondInNanoseconds);
151 }
152 
TEST(Time,InitializeNanosecFromMicrosec)153 TEST(Time, InitializeNanosecFromMicrosec) {
154   Microseconds tMicro(5);
155   Nanoseconds tNano(tMicro);
156   EXPECT_EQ(tNano.toRawNanoseconds(), 5 * kOneMicrosecondInNanoseconds);
157 }
158 
TEST(Time,NanosecEquals)159 TEST(Time, NanosecEquals) {
160   Nanoseconds t1(5), t2(5), t3(6);
161   EXPECT_TRUE(t1 == t2);
162   EXPECT_FALSE(t1 == t3);
163 }
164 
TEST(Time,NanosecNotEquals)165 TEST(Time, NanosecNotEquals) {
166   Nanoseconds t1(5), t2(5), t3(6);
167   EXPECT_FALSE(t1 != t2);
168   EXPECT_TRUE(t1 != t3);
169 }
170 
TEST(Time,AddSecToNanosec)171 TEST(Time, AddSecToNanosec) {
172   Nanoseconds t = Seconds(5) + Nanoseconds(6);
173   EXPECT_EQ(t.toRawNanoseconds(), 5 * kOneSecondInNanoseconds + 6);
174 }
175 
TEST(Time,AddSecToNanosecOverflow)176 TEST(Time, AddSecToNanosecOverflow) {
177   Nanoseconds t = Seconds(5) + Nanoseconds(UINT64_MAX);
178   EXPECT_EQ(t.toRawNanoseconds(), 5 * kOneSecondInNanoseconds - 1);
179 }
180 
TEST(Time,AddNanosecToNanosec)181 TEST(Time, AddNanosecToNanosec) {
182   Nanoseconds t = Nanoseconds(6) + Nanoseconds(5);
183   EXPECT_EQ(t.toRawNanoseconds(), 11);
184 }
185 
TEST(Time,AddNanosecToNanosecOverflow)186 TEST(Time, AddNanosecToNanosecOverflow) {
187   Nanoseconds t = Nanoseconds(6) + Nanoseconds(UINT64_MAX);
188   EXPECT_EQ(t.toRawNanoseconds(), 5);
189 }
190 
TEST(Time,SubtractNanosecFromNanosec)191 TEST(Time, SubtractNanosecFromNanosec) {
192   Nanoseconds t = Nanoseconds(11) - Nanoseconds(5);
193   EXPECT_EQ(t.toRawNanoseconds(), 6);
194 }
195 
TEST(Time,SubtractNanosecFromNanosecOverflow)196 TEST(Time, SubtractNanosecFromNanosecOverflow) {
197   Nanoseconds t = Nanoseconds(5) - Nanoseconds(11);
198   EXPECT_EQ(t.toRawNanoseconds(), UINT64_MAX - 5);
199 }
200 
TEST(Time,NanosecGreaterThanEqual)201 TEST(Time, NanosecGreaterThanEqual) {
202   Nanoseconds t1(5), t2(5), t3(6);
203   EXPECT_TRUE(t2 >= t1);
204   EXPECT_TRUE(t3 >= t1);
205   EXPECT_FALSE(t1 >= t3);
206 }
207 
TEST(Time,NanosecLessThanEqual)208 TEST(Time, NanosecLessThanEqual) {
209   Nanoseconds t1(5), t2(5), t3(6);
210   EXPECT_TRUE(t2 <= t1);
211   EXPECT_TRUE(t1 <= t3);
212   EXPECT_FALSE(t3 <= t1);
213 }
214 
TEST(Time,NanosecLessThan)215 TEST(Time, NanosecLessThan) {
216   Nanoseconds t1(5), t2(5), t3(6);
217   EXPECT_FALSE(t2 < t1);
218   EXPECT_FALSE(t3 < t1);
219   EXPECT_TRUE(t1 < t3);
220 }
221 
TEST(Time,NanosecGreaterThan)222 TEST(Time, NanosecGreaterThan) {
223   Nanoseconds t1(5), t2(5), t3(6);
224   EXPECT_FALSE(t2 > t1);
225   EXPECT_TRUE(t3 > t1);
226   EXPECT_FALSE(t1 > t3);
227 }
228