1 /*
2  * Copyright (C) 2020 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 "src/traced/probes/common/cpu_freq_info_for_testing.h"
18 
19 #include "test/gtest_and_gmock.h"
20 
21 using ::testing::ElementsAre;
22 
23 namespace perfetto {
24 namespace {
25 
26 class CpuFreqInfoTest : public ::testing::Test {
27  protected:
GetCpuFreqInfo()28   std::unique_ptr<CpuFreqInfo> GetCpuFreqInfo() {
29     return std::unique_ptr<CpuFreqInfo>(
30         cpu_freq_info_for_testing.GetInstance());
31   }
32 
33   CpuFreqInfoForTesting cpu_freq_info_for_testing;
34 };
35 
FreqsToVector(CpuFreqInfo::Range range)36 std::vector<uint32_t> FreqsToVector(CpuFreqInfo::Range range) {
37   std::vector<uint32_t> freqs;
38   for (auto it = range.first; it != range.second; it++)
39     freqs.push_back(*it);
40   return freqs;
41 }
42 
TEST_F(CpuFreqInfoTest,CpuFreqInfo)43 TEST_F(CpuFreqInfoTest, CpuFreqInfo) {
44   auto cpu_freq_info = GetCpuFreqInfo();
45 
46   EXPECT_THAT(FreqsToVector(cpu_freq_info->GetFreqs(0u)),
47               ElementsAre(300000, 576000, 748800, 998400, 1209600, 1324800,
48                           1516800, 1612800, 1708800));
49   EXPECT_THAT(FreqsToVector(cpu_freq_info->GetFreqs(1u)),
50               ElementsAre(300000, 652800, 825600, 979200, 1132800, 1363200,
51                           1536000, 1747200, 1843200, 1996800, 2803200));
52   EXPECT_THAT(FreqsToVector(cpu_freq_info->GetFreqs(2u)), ElementsAre());
53   EXPECT_THAT(FreqsToVector(cpu_freq_info->GetFreqs(100u)), ElementsAre());
54 
55   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(0u, 300000u), 1u);
56   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(0u, 748800u), 3u);
57   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(1u, 300000u), 10u);
58   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(1u, 1996800u), 19u);
59   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(1u, 2803200), 20u);
60   EXPECT_EQ(cpu_freq_info->GetCpuFreqIndex(1u, 5u), 0u);
61 }
62 
63 }  // namespace
64 }  // namespace perfetto
65