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