Home | History | Annotate | Download | only in cputimeinstate
      1 
      2 #include <unordered_map>
      3 #include <vector>
      4 
      5 #include <gtest/gtest.h>
      6 
      7 #include <cputimeinstate.h>
      8 
      9 namespace android {
     10 namespace bpf {
     11 
     12 using std::vector;
     13 
     14 TEST(TimeInStateTest, SingleUid) {
     15     vector<vector<uint64_t>> times;
     16     ASSERT_TRUE(getUidCpuFreqTimes(0, &times));
     17     EXPECT_FALSE(times.empty());
     18 }
     19 
     20 TEST(TimeInStateTest, AllUid) {
     21     vector<size_t> sizes;
     22     std::unordered_map<uint32_t, vector<vector<uint64_t>>> map;
     23     ASSERT_TRUE(getUidsCpuFreqTimes(&map));
     24 
     25     ASSERT_FALSE(map.empty());
     26 
     27     auto firstEntry = map.begin()->second;
     28     for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size());
     29 
     30     for (const auto &vec : map) {
     31         ASSERT_EQ(vec.second.size(), sizes.size());
     32         for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]);
     33     }
     34 }
     35 
     36 TEST(TimeInStateTest, RemoveUid) {
     37     vector<vector<uint64_t>> times, times2;
     38     ASSERT_TRUE(getUidCpuFreqTimes(0, &times));
     39     ASSERT_FALSE(times.empty());
     40 
     41     uint64_t sum = 0;
     42     for (size_t i = 0; i < times.size(); ++i) {
     43         for (auto x : times[i]) sum += x;
     44     }
     45     ASSERT_GT(sum, (uint64_t)0);
     46 
     47     ASSERT_TRUE(clearUidCpuFreqTimes(0));
     48 
     49     ASSERT_TRUE(getUidCpuFreqTimes(0, &times2));
     50     ASSERT_EQ(times2.size(), times.size());
     51     for (size_t i = 0; i < times.size(); ++i) {
     52         ASSERT_EQ(times2[i].size(), times[i].size());
     53         for (size_t j = 0; j < times[i].size(); ++j) ASSERT_LE(times2[i][j], times[i][j]);
     54     }
     55 }
     56 
     57 } // namespace bpf
     58 } // namespace android
     59