1/* 2 * Copyright (C) 2015 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 <gtest/gtest.h> 18 19#include "collectors/cpu_usage_collector.h" 20#include "metrics/metrics_library_mock.h" 21 22 23TEST(CpuUsageTest, ParseProcStat) { 24 MetricsLibraryMock metrics_lib_mock; 25 CpuUsageCollector collector(&metrics_lib_mock); 26 std::vector<std::string> invalid_contents = { 27 "", 28 // First line does not start with cpu. 29 "spu 17191 11 36579 151118 289 0 2 0 0 0\n" 30 "cpu0 1564 2 866 48650 68 0 2 0 0 0\n" 31 "cpu1 14299 0 35116 1844 81 0 0 0 0 0\n", 32 // One of the field is not a number. 33 "cpu a17191 11 36579 151118 289 0 2 0 0 0", 34 // To many numbers in the first line. 35 "cpu 17191 11 36579 151118 289 0 2 0 0 0 102" 36 }; 37 38 uint64_t user, nice, system; 39 for (int i = 0; i < invalid_contents.size(); i++) { 40 ASSERT_FALSE(collector.ParseProcStat(invalid_contents[i], &user, &nice, 41 &system)); 42 } 43 44 ASSERT_TRUE(collector.ParseProcStat( 45 std::string("cpu 17191 11 36579 151118 289 0 2 0 0 0"), 46 &user, &nice, &system)); 47 ASSERT_EQ(17191, user); 48 ASSERT_EQ(11, nice); 49 ASSERT_EQ(36579, system); 50} 51