1 /*
2  * Copyright (C) 2016 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 #include "common/libs/time/monotonic_time.h"
17 
18 #include <gtest/gtest.h>
19 #include <algorithm>
20 
21 using cuttlefish::time::TimeDifference;
22 
23 class MonotonicTimeTest : public ::testing::Test {
24  public:
MonotonicTimeTest()25   MonotonicTimeTest() {}
26 };
27 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd1)28 TEST_F(MonotonicTimeTest, TimeDifferenceAdd1) {
29   TimeDifference td1(1, 10, 1);
30   TimeDifference td2(0, 100, 1);
31   EXPECT_EQ((td1+td2).count(), (1)*1000000000 + 110);
32 }
33 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd2)34 TEST_F(MonotonicTimeTest, TimeDifferenceAdd2) {
35   TimeDifference td1(10, 1000, 1);
36   TimeDifference td2(100, 10000, 1);
37   EXPECT_EQ((td1+td2).count(), (110L)*1000000000L + 11000L);
38 }
39 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd3)40 TEST_F(MonotonicTimeTest, TimeDifferenceAdd3) {
41   int64_t scale = 1000;
42   TimeDifference td1(10, 1000, scale);
43   TimeDifference td2(100, 10000, scale);
44   EXPECT_EQ((td1+td2).count(), ((110L)*1000000000L + 11000L)/scale);
45 }
46 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd4)47 TEST_F(MonotonicTimeTest, TimeDifferenceAdd4) {
48   int64_t scale = 1;
49   TimeDifference td1(-10, 1000, scale);
50   TimeDifference td2(100, 10000, scale);
51   EXPECT_EQ((td1+td2).count(), ((90L)*1000000000L + 11000L)/scale);
52 }
53 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd5)54 TEST_F(MonotonicTimeTest, TimeDifferenceAdd5) {
55   int64_t scale1 = 1, scale2 = 1000;
56   TimeDifference td1(-10, 1000, scale1);
57   TimeDifference td2(100, 10000, scale2);
58   EXPECT_EQ((td1+td2).count(), ((90L)*1000000000L + 11000L)/std::min(scale1, scale2));
59 }
60 
TEST_F(MonotonicTimeTest,TimeDifferenceAdd6)61 TEST_F(MonotonicTimeTest, TimeDifferenceAdd6) {
62   int64_t scale1 = 1000, scale2 = 1000;
63   TimeDifference td1(0, 995, scale1);
64   TimeDifference td2(0, 10, scale2);
65   EXPECT_EQ((td1+td2).count(), (1005L)/std::min(scale1, scale2));
66 }
67 
TEST_F(MonotonicTimeTest,TimeDifferenceSub1)68 TEST_F(MonotonicTimeTest, TimeDifferenceSub1) {
69   int64_t scale = 1;
70   TimeDifference td1(10, 1000, scale);
71   TimeDifference td2(100, 10000, scale);
72   EXPECT_EQ((td2-td1).count(), ((90L)*1000000000L + 9000L)/scale);
73 }
74 
TEST_F(MonotonicTimeTest,TimeDifferenceSub2)75 TEST_F(MonotonicTimeTest, TimeDifferenceSub2) {
76   int64_t scale = 1;
77   TimeDifference td1(10, 1000, scale);
78   TimeDifference td2(100, 10000, scale);
79   EXPECT_EQ((td1-td2).count(), ((-91L)*1000000000L + 1000000000L - 9000L)/scale);
80 }
81 
TEST_F(MonotonicTimeTest,TimeDifferenceSub3)82 TEST_F(MonotonicTimeTest, TimeDifferenceSub3) {
83   int64_t scale1 = 1, scale2 = 1000;
84   TimeDifference td1(-10, 1000, scale1);
85   TimeDifference td2(100, 10000, scale2);
86   EXPECT_EQ((td1-td2).count(), ((-111L)*1000000000L + 1000000000L - 9000L)/std::min(scale1, scale2));
87 }
88 
TEST_F(MonotonicTimeTest,TimeDifferenceSub4)89 TEST_F(MonotonicTimeTest, TimeDifferenceSub4) {
90   int64_t scale1 = 1000, scale2 = 1000;
91   TimeDifference td1(0, 995, scale1);
92   TimeDifference td2(0, 10, scale2);
93   EXPECT_EQ((td1-td2).count(), (985L)/std::min(scale1, scale2));
94 }
95 
TEST_F(MonotonicTimeTest,TimeDifferenceComp1)96 TEST_F(MonotonicTimeTest, TimeDifferenceComp1) {
97   int64_t scale = 1;
98   TimeDifference td1(10, 10000, scale);
99   TimeDifference td2(100, 10, scale);
100   EXPECT_TRUE((td1 < td2));
101   EXPECT_FALSE(td2 < td1);
102 }
103 
TEST_F(MonotonicTimeTest,TimeDifferenceComp2)104 TEST_F(MonotonicTimeTest, TimeDifferenceComp2) {
105   int64_t scale = 1;
106   TimeDifference td1(100, 10000, scale);
107   TimeDifference td2(100, 10, scale);
108   EXPECT_TRUE((td2 < td1));
109   EXPECT_FALSE(td1 < td2);
110 }
111