### Question:
CpuUsage { @VisibleForTesting static float parseCpuRateOfAppByShell(String line, int cpuIndex, Float cpuTotal) { if (line.startsWith(String.valueOf(android.os.Process.myPid()))) { if (cpuIndex == -1) { throw new IllegalStateException("parseCpuRateOfAppByShell but cpuIndex == -1:" + line); } String[] param = line.split("\\s+"); if (param.length <= cpuIndex) { throw new IllegalStateException("parseCpuRateOfAppByShell but param.length <= cpuIndex:" + line); } String cpu = param[cpuIndex]; if (cpu.endsWith("%")) { cpu = cpu.substring(0, cpu.lastIndexOf("%")); } if (cpuTotal == null || cpuTotal <= 0) { throw new IllegalStateException("parseCpuRateOfAppByShell but cpuTotal == null || cpuTotal <= 0:" + line); } try { return Float.parseFloat(cpu) / cpuTotal; } catch (Throwable e) { throw new IllegalStateException("parseCpuRateOfAppByShell but " + e + ":" + line); } } return -1; }  static CpuInfo getCpuInfo();  }### Answer:
@Test public void parseCpuRateOfAppByShell() { String line2 = "800%cpu 0%user 0%nice 0%sys 800%idle 0%iow 0%irq 0%sirq 0%host"; String line3 = "[7m PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS \u001B[0m"; String line4 = "0 u0_a591 10 -10 5.4G 131M 80M S 16.0 1.7 0:02.98 cn.hikyson.andr+"; String line5 = "0wfwf fweffwef fwef6.064564f3402.98 cn.hikyson.andr+"; int index = CpuUsage.parseCPUIndex(line3); Map<String, Float> tempCpuDevice = CpuUsage.parseCpuRateOfDeviceAndTotalByShell(line2); try { CpuUsage.parseCpuRateOfAppByShell(line5, index, tempCpuDevice.get("cpu")); Assert.fail(); } catch (IllegalStateException ignore) { } try { CpuUsage.parseCpuRateOfAppByShell(line4, -1, tempCpuDevice.get("cpu")); Assert.fail(); } catch (IllegalStateException ignore) { } try { CpuUsage.parseCpuRateOfAppByShell(line4, index, tempCpuDevice.get("unknown")); Assert.fail(); } catch (IllegalStateException ignore) { } float appCpu0 = CpuUsage.parseCpuRateOfAppByShell(line4, index, tempCpuDevice.get("cpu")); Assert.assertEquals((float) Float.parseFloat("16.0") / 800, appCpu0, 0.1); }