1 /* 2 * Copyright (C) 2013 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 package com.android.loganalysis.parser; 17 18 import com.android.loganalysis.item.TopItem; 19 import com.android.loganalysis.util.ArrayUtil; 20 21 import junit.framework.TestCase; 22 23 import java.util.Arrays; 24 import java.util.List; 25 26 /** 27 * Unit tests for {@link ProcrankParser} 28 */ 29 public class TopParserTest extends TestCase { 30 31 /** 32 * Test that the output of the top command is parsed. 33 */ testTopParser()34 public void testTopParser() { 35 List<String> inputBlock = Arrays.asList( 36 "User 20%, System 20%, IOW 5%, IRQ 3%", 37 "User 150 + Nice 50 + Sys 200 + Idle 510 + IOW 60 + IRQ 5 + SIRQ 25 = 1000", 38 "", 39 " PID TID PR CPU% S VSS RSS PCY UID Thread Proc", 40 " 4474 4474 0 2% R 1420K 768K shell top top"); 41 42 TopItem item = new TopParser().parse(inputBlock); 43 44 assertEquals(150, item.getUser()); 45 assertEquals(50, item.getNice()); 46 assertEquals(200, item.getSystem()); 47 assertEquals(510, item.getIdle()); 48 assertEquals(60, item.getIow()); 49 assertEquals(5, item.getIrq()); 50 assertEquals(25, item.getSirq()); 51 assertEquals(1000, item.getTotal()); 52 assertEquals(ArrayUtil.join("\n", inputBlock), item.getText()); 53 } 54 55 /** 56 * Test that the last output is stored. 57 */ testLastTop()58 public void testLastTop() { 59 List<String> inputBlock = Arrays.asList( 60 "User 0 + Nice 0 + Sys 0 + Idle 1000 + IOW 0 + IRQ 0 + SIRQ 0 = 1000", 61 "User 0 + Nice 0 + Sys 0 + Idle 1000 + IOW 0 + IRQ 0 + SIRQ 0 = 1000", 62 "User 150 + Nice 50 + Sys 200 + Idle 510 + IOW 60 + IRQ 5 + SIRQ 25 = 1000"); 63 64 TopItem item = new TopParser().parse(inputBlock); 65 66 assertEquals(150, item.getUser()); 67 assertEquals(50, item.getNice()); 68 assertEquals(200, item.getSystem()); 69 assertEquals(510, item.getIdle()); 70 assertEquals(60, item.getIow()); 71 assertEquals(5, item.getIrq()); 72 assertEquals(25, item.getSirq()); 73 assertEquals(1000, item.getTotal()); 74 } 75 76 /** 77 * Test that an empty input returns {@code null}. 78 */ testEmptyInput()79 public void testEmptyInput() { 80 TopItem item = new TopParser().parse(Arrays.asList("")); 81 assertNull(item); 82 } 83 } 84