summaryrefslogtreecommitdiff
path: root/cmds/incident_helper/tests/CpuFreqParser_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cmds/incident_helper/tests/CpuFreqParser_test.cpp')
-rw-r--r--cmds/incident_helper/tests/CpuFreqParser_test.cpp130
1 files changed, 130 insertions, 0 deletions
diff --git a/cmds/incident_helper/tests/CpuFreqParser_test.cpp b/cmds/incident_helper/tests/CpuFreqParser_test.cpp
new file mode 100644
index 000000000000..1c2f9e59308d
--- /dev/null
+++ b/cmds/incident_helper/tests/CpuFreqParser_test.cpp
@@ -0,0 +1,130 @@
+/*
+ * 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.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);
+ }
+
+ string getSerializedString(::google::protobuf::Message& message) {
+ string expectedStr;
+ message.SerializeToFileDescriptor(tf.fd);
+ ReadFileToString(tf.path, &expectedStr);
+ return expectedStr;
+ }
+
+protected:
+ TemporaryFile tf;
+
+ const string kTestPath = GetExecutableDirectory();
+ const string kTestDataPath = kTestPath + "/testdata/";
+};
+
+TEST_F(CpuFreqParserTest, Success) {
+ const string testFile = kTestDataPath + "cpufreq.txt";
+ CpuFreqParser parser;
+ CpuFreq expected;
+
+ long jiffyHz = sysconf(_SC_CLK_TCK);
+ expected.set_jiffy_hz(jiffyHz);
+
+ CpuFreqStats::TimeInState* state;
+
+ CpuFreqStats* 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);
+
+ CpuFreqStats* 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);
+
+ CpuFreqStats* 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);
+
+ CpuFreqStats* 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(), getSerializedString(expected));
+ close(fd);
+}