summaryrefslogtreecommitdiff
path: root/cmds/incident_helper/tests/CpuFreqParser_test.cpp
blob: 0839a7e45eddb14fd192266d14d72126690234e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CpuFreqParser.h"

#include "frameworks/base/core/proto/android/os/cpufreq.pb.h"

#include <android-base/file.h>
#include <android-base/test_utils.h>
#include <gmock/gmock.h>
#include <google/protobuf/message_lite.h>
#include <gtest/gtest.h>
#include <string.h>
#include <fcntl.h>

using namespace android::base;
using namespace android::os;
using namespace std;
using ::testing::StrEq;
using ::testing::Test;
using ::testing::internal::CaptureStderr;
using ::testing::internal::CaptureStdout;
using ::testing::internal::GetCapturedStderr;
using ::testing::internal::GetCapturedStdout;

class CpuFreqParserTest : public Test {
public:
    virtual void SetUp() override {
        ASSERT_TRUE(tf.fd != -1);
    }

protected:
    TemporaryFile tf;

    const string kTestPath = GetExecutableDirectory();
    const string kTestDataPath = kTestPath + "/testdata/";
};

TEST_F(CpuFreqParserTest, Success) {
    const string testFile = kTestDataPath + "cpufreq.txt";
    CpuFreqParser parser;
    CpuFreqProto expected;

    long jiffyHz = sysconf(_SC_CLK_TCK);
    expected.set_jiffy_hz(jiffyHz);

    CpuFreqProto::Stats::TimeInState* state;

    CpuFreqProto::Stats* cpu0 = expected.add_cpu_freqs();
    cpu0->set_cpu_name("cpu0");
    state = cpu0->add_times();
    state->set_state_khz(307200);
    state->set_time_jiffy(23860761);
    state = cpu0->add_times();
    state->set_state_khz(384000);
    state->set_time_jiffy(83124);
    state = cpu0->add_times();
    state->set_state_khz(768000);
    state->set_time_jiffy(22652);

    CpuFreqProto::Stats* cpu1 = expected.add_cpu_freqs();
    cpu1->set_cpu_name("cpu1");
    state = cpu1->add_times();
    state->set_state_khz(307200);
    state->set_time_jiffy(23860761);
    state = cpu1->add_times();
    state->set_state_khz(384000);
    state->set_time_jiffy(83124);
    state = cpu1->add_times();
    state->set_state_khz(768000);
    state->set_time_jiffy(22652);

    CpuFreqProto::Stats* cpu2 = expected.add_cpu_freqs();
    cpu2->set_cpu_name("cpu2");
    state = cpu2->add_times();
    state->set_state_khz(307200);
    state->set_time_jiffy(23890935);
    state = cpu2->add_times();
    state->set_state_khz(384000);
    state->set_time_jiffy(29383);
    state = cpu2->add_times();
    state->set_state_khz(748800);
    state->set_time_jiffy(10547);
    state = cpu2->add_times();
    state->set_state_khz(825600);
    state->set_time_jiffy(13173);

    CpuFreqProto::Stats* cpu3 = expected.add_cpu_freqs();
    cpu3->set_cpu_name("cpu3");
    state = cpu3->add_times();
    state->set_state_khz(307200);
    state->set_time_jiffy(23890935);
    state = cpu3->add_times();
    state->set_state_khz(384000);
    state->set_time_jiffy(29383);
    state = cpu3->add_times();
    state->set_state_khz(748800);
    state->set_time_jiffy(10547);
    state = cpu3->add_times();
    state->set_state_khz(825600);
    state->set_time_jiffy(13173);

    int fd = open(testFile.c_str(), O_RDONLY);
    ASSERT_TRUE(fd != -1);

    CaptureStdout();
    ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
    EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
    close(fd);
}