1 /*
2  * Copyright (c) 2021, 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 #pragma once
18 
19 #include "UidProcStatsCollector.h"
20 
21 #include <gmock/gmock.h>
22 
23 namespace android {
24 namespace automotive {
25 namespace watchdog {
26 
27 MATCHER(CpuCyclesByTidEq, "") {
28     const auto& actual = std::get<0>(arg);
29     const auto& expected = std::get<1>(arg);
30     return actual.first == expected.first && actual.second == expected.second;
31 }
32 
33 MATCHER_P(ProcessStatsEq, expected, "") {
34     const auto& actual = arg;
35     return ::testing::Value(actual.comm, ::testing::Eq(expected.comm)) &&
36             ::testing::Value(actual.startTimeMillis, ::testing::Eq(expected.startTimeMillis)) &&
37             ::testing::Value(actual.cpuTimeMillis, ::testing::Eq(expected.cpuTimeMillis)) &&
38             ::testing::Value(actual.totalCpuCycles, ::testing::Eq(expected.totalCpuCycles)) &&
39             ::testing::Value(actual.totalMajorFaults, ::testing::Eq(expected.totalMajorFaults)) &&
40             ::testing::Value(actual.totalTasksCount, ::testing::Eq(expected.totalTasksCount)) &&
41             ::testing::Value(actual.ioBlockedTasksCount,
42                              ::testing::Eq(expected.ioBlockedTasksCount)) &&
43             ::testing::Value(actual.cpuCyclesByTid,
44                              ::testing::UnorderedPointwise(CpuCyclesByTidEq(),
45                                                            expected.cpuCyclesByTid)) &&
46             ::testing::Value(actual.rssKb, ::testing::Eq(expected.rssKb)) &&
47             ::testing::Value(actual.pssKb, ::testing::Eq(expected.pssKb)) &&
48             ::testing::Value(actual.ussKb, ::testing::Eq(expected.ussKb)) &&
49             ::testing::Value(actual.swapPssKb, ::testing::Eq(expected.swapPssKb));
50 }
51 
52 MATCHER(ProcessStatsByPidEq, "") {
53     const auto& actual = std::get<0>(arg);
54     const auto& expected = std::get<1>(arg);
55     return actual.first == expected.first &&
56             ::testing::Value(actual.second, ProcessStatsEq(expected.second));
57 }
58 
59 MATCHER_P(UidProcStatsEq, expected, "") {
60     const auto& actual = arg;
61     return ::testing::Value(actual.cpuTimeMillis, ::testing::Eq(expected.cpuTimeMillis)) &&
62             ::testing::Value(actual.cpuCycles, ::testing::Eq(expected.cpuCycles)) &&
63             ::testing::Value(actual.totalMajorFaults, ::testing::Eq(expected.totalMajorFaults)) &&
64             ::testing::Value(actual.totalTasksCount, ::testing::Eq(expected.totalTasksCount)) &&
65             ::testing::Value(actual.ioBlockedTasksCount,
66                              ::testing::Eq(expected.ioBlockedTasksCount)) &&
67             ::testing::Value(actual.totalRssKb, ::testing::Eq(expected.totalRssKb)) &&
68             ::testing::Value(actual.totalPssKb, ::testing::Eq(expected.totalPssKb)) &&
69             ::testing::Value(actual.processStatsByPid,
70                              ::testing::UnorderedPointwise(ProcessStatsByPidEq(),
71                                                            expected.processStatsByPid));
72 }
73 
74 }  // namespace watchdog
75 }  // namespace automotive
76 }  // namespace android
77