package net.hyperic.sigar.test;

import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarNotImplementedException;
import net.hyperic.sigar.Cpu;

public class TestCpu extends SigarTestCase {

    public TestCpu(String name) {
        super(name);
    }

    private void checkCpu(Cpu cpu) {
        traceln("User..." + cpu.getUser());
        assertTrue(cpu.getUser() >= 0);

        traceln("Sys...." + cpu.getSys());
        assertTrue(cpu.getSys() >= 0);

        traceln("Idle..." + cpu.getIdle());
        assertTrue(cpu.getIdle() >= 0);

        traceln("Wait..." + cpu.getWait());
        assertTrue(cpu.getWait() >= 0);

        traceln("Total.." + cpu.getTotal());
        assertTrue(cpu.getTotal() > 0);
    }

    public void testCreate() throws Exception {
        Sigar sigar = getSigar();
        Cpu cpu = sigar.getCpu();

        traceln("getCpu:");
        checkCpu(cpu);

        try {
            Cpu[] cpuList = sigar.getCpuList();

            for (int i=0; i<cpuList.length; i++) {
                traceln("Cpu " + i + ":");
                checkCpu(cpuList[i]);
            }
        } catch (SigarNotImplementedException e) {
            //ok
        }
    }
}

@@ -142,16 +142,50 @@
 gar.Cpu;
+%0Aimport net.hyperic.sigar.CpuPerc;
 %0A%0Apublic
@@ -1214,11 +1214,1182 @@
  %7D%0A    %7D
+%0A%0A    private static void printCpu(String prefix, CpuPerc cpu) %7B%0A        System.out.println(prefix +%0A                           CpuPerc.format(cpu.getUser()) + %22%5Ct%22 +%0A                           CpuPerc.format(cpu.getSys()) + %22%5Ct%22 +%0A                           CpuPerc.format(cpu.getWait()) + %22%5Ct%22 +%0A                           CpuPerc.format(cpu.getNice()) + %22%5Ct%22 +%0A                           CpuPerc.format(cpu.getIdle()) + %22%5Ct%22 +%0A                           CpuPerc.format(cpu.getCombined()));%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        final String HEADER =%0A            %22   User%5CtSys%5CtWait%5CtNice%5CtIdle%5CtTotal%22;%0A        int interval = 1;%0A        if (args.length %3E 0) %7B%0A            interval = Integer.parseInt(args%5B0%5D);%0A        %7D%0A        int sleep = 1000 * 60 * interval;%0A%0A        Sigar sigar = new Sigar();%0A%0A        while (true) %7B%0A            System.out.println(HEADER);%0A%0A            printCpu(%22   %22, sigar.getCpuPerc());%0A%0A            CpuPerc%5B%5D cpuList = sigar.getCpuPercList();%0A%0A            for (int i=0; i%3CcpuList.length; i++) %7B%0A                printCpu(i+%22: %22, cpuList%5Bi%5D);%0A            %7D%0A            Thread.sleep(sleep);%0A        %7D%0A    %7D
 %0A%7D%0A

add main method to test CpuPerc