1 /*
2  * Copyright (C) 2024 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "aidl/PhysicalQuantityTypes.h"
21 
22 using std::chrono_literals::operator""ms;
23 using std::chrono_literals::operator""s;
24 using std::chrono_literals::operator""min;
25 using testing::Eq;
26 using testing::Gt;
27 using testing::Lt;
28 
29 namespace aidl {
30 namespace google {
31 namespace hardware {
32 namespace power {
33 namespace impl {
34 namespace pixel {
35 
36 // compile time tests
37 static_assert(Cycles(10) * 2 == Cycles(20));
38 static_assert(100_hz + 200_hz == Frequency(300));
39 static_assert(Cycles(100) / 1s == Frequency(100));
40 
TEST(PhysicalQuantityTypeTest,type_check_basic_cycles)41 TEST(PhysicalQuantityTypeTest, type_check_basic_cycles) {
42     Cycles a(0);
43     Cycles b(-1);
44     Cycles c(8);
45     Cycles d(11);
46     Cycles e(8);
47     EXPECT_THAT(a, Eq(a));
48     EXPECT_THAT(e, Eq(c));
49     EXPECT_THAT(c, Eq(e));
50     EXPECT_THAT(b, Lt(a));
51     EXPECT_THAT(a, Gt(b));
52     EXPECT_THAT(a + b, Eq(b));
53     EXPECT_THAT(b + c, Eq(Cycles(7)));
54     EXPECT_THAT(c - b, Eq(Cycles(9)));
55     EXPECT_THAT(c * 8, Eq(Cycles(64)));
56     EXPECT_THAT(3 * c, Eq(Cycles(24)));
57     EXPECT_THAT(c / 2, Eq(Cycles(4)));
58 }
59 
TEST(PhysicalQuantityTypeTest,type_check_basic_frequency)60 TEST(PhysicalQuantityTypeTest, type_check_basic_frequency) {
61     Frequency a(1000);
62     Frequency b(1111);
63     EXPECT_THAT(a, Eq(a));
64     EXPECT_THAT(a + Frequency(111), Eq(b));
65     EXPECT_THAT(b, Gt(a));
66     EXPECT_THAT(a, Lt(b));
67 }
68 
TEST(PhysicalQuantityTypeTest,freq_cycles_time_conversions)69 TEST(PhysicalQuantityTypeTest, freq_cycles_time_conversions) {
70     EXPECT_THAT(Cycles(1000) / 2s, Eq(500_hz));
71     EXPECT_THAT(Cycles(1000) / 500ms, Eq(2000_hz));
72 
73     EXPECT_THAT(1000_hz * 12ms, Eq(Cycles(12)));
74     EXPECT_THAT(6min * 500_hz, Eq(Cycles(180000)));
75     EXPECT_THAT(1000_hz * 2min, Eq(Cycles(120000)));
76 }
77 
78 }  // namespace pixel
79 }  // namespace impl
80 }  // namespace power
81 }  // namespace hardware
82 }  // namespace google
83 }  // namespace aidl
84