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 
17 #include "gtest/gtest.h"
18 
19 #include "chre/platform/slpi/smgr/platform_sensor_util.h"
20 
21 using chre::intervalToSmgrSamplingRate;
22 using chre::intervalToSmgrQ16ReportRate;
23 using chre::Milliseconds;
24 using chre::Nanoseconds;
25 using chre::Seconds;
26 
TEST(SmgrSamplingRateTest,Zero)27 TEST(SmgrSamplingRateTest, Zero) {
28   uint16_t rate = intervalToSmgrSamplingRate(Nanoseconds(0));
29   EXPECT_EQ(rate, 0);
30 }
31 
TEST(SmgrSamplingRateTest,FiftyHertz)32 TEST(SmgrSamplingRateTest, FiftyHertz) {
33   constexpr Nanoseconds kFiftyHertzInterval = Milliseconds(20);
34   uint16_t rate = intervalToSmgrSamplingRate(kFiftyHertzInterval);
35   EXPECT_EQ(rate, 50);
36 }
37 
TEST(SmgrSamplingRateTest,ZeroPointFiveHertz)38 TEST(SmgrSamplingRateTest, ZeroPointFiveHertz) {
39   constexpr Nanoseconds kZeroPointFiveHertzInterval = Seconds(2);
40   uint16_t rate = intervalToSmgrSamplingRate(kZeroPointFiveHertzInterval);
41   EXPECT_EQ(rate, 2000);
42 }
43 
TEST(SmgrSamplingRateTest,MinisculeRate)44 TEST(SmgrSamplingRateTest, MinisculeRate) {
45   constexpr Nanoseconds kOneNanoHzInterval = Seconds(1e9);
46   uint16_t rate = intervalToSmgrSamplingRate(kOneNanoHzInterval);
47   EXPECT_EQ(rate, INT16_MAX);
48 }
49 
TEST(SmgrSamplingRateTest,HugeRate)50 TEST(SmgrSamplingRateTest, HugeRate) {
51   constexpr Nanoseconds kTwentyMHzInterval = Nanoseconds(50);
52   uint16_t rate = intervalToSmgrSamplingRate(kTwentyMHzInterval);
53   EXPECT_EQ(rate, 1000);  // Should clamp to inversion point
54 }
55 
TEST(SmgrQ16ReportRateTest,Zero)56 TEST(SmgrQ16ReportRateTest, Zero) {
57   uint32_t rate = intervalToSmgrQ16ReportRate(Nanoseconds(0));
58   EXPECT_EQ(rate, INT32_MAX);
59 }
60 
TEST(SmgrQ16ReportRateTest,FiftyHertz)61 TEST(SmgrQ16ReportRateTest, FiftyHertz) {
62   constexpr Nanoseconds kTenHertzInterval = Milliseconds(20);
63   uint32_t rate = intervalToSmgrQ16ReportRate(kTenHertzInterval);
64   EXPECT_EQ(rate, 0x10000 * 50);
65 }
66 
TEST(SmgrQ16ReportRateTest,ZeroPointFiveHertz)67 TEST(SmgrQ16ReportRateTest, ZeroPointFiveHertz) {
68   constexpr Nanoseconds kZeroPointFiveHertzInterval = Seconds(2);
69   uint32_t rate = intervalToSmgrQ16ReportRate(kZeroPointFiveHertzInterval);
70   EXPECT_EQ(rate, 0x10000 / 2);
71 }
72 
TEST(SmgrQ16ReportRateTest,OneNanosecond)73 TEST(SmgrQ16ReportRateTest, OneNanosecond) {
74   uint32_t rate = intervalToSmgrQ16ReportRate(Nanoseconds(1));
75   EXPECT_EQ(rate, INT32_MAX);
76 }
77