View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux.proc;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.greaterThanOrEqualTo;
9   
10  import org.junit.jupiter.api.Test;
11  import org.junit.jupiter.api.condition.EnabledOnOs;
12  import org.junit.jupiter.api.condition.OS;
13  
14  import oshi.SystemInfo;
15  import oshi.hardware.CentralProcessor;
16  import oshi.hardware.HardwareAbstractionLayer;
17  
18  @EnabledOnOs(OS.LINUX)
19  class CpuStatTest {
20  
21      @Test
22      void testSystemCpuLoadTicks() {
23          long[] systemCpuLoadTicks = CpuStat.getSystemCpuLoadTicks();
24          for (long systemCpuTick : systemCpuLoadTicks) {
25              assertThat("CPU tick should be greater than or equal to 0", systemCpuTick, greaterThanOrEqualTo(0L));
26          }
27      }
28  
29      @Test
30      void testGetProcessorCpuLoadTicks() {
31          SystemInfo si = new SystemInfo();
32          HardwareAbstractionLayer hal = si.getHardware();
33          CentralProcessor processor = hal.getProcessor();
34          int logicalProcessorCount = processor.getLogicalProcessorCount();
35          long[][] processorCpuLoadTicks = CpuStat.getProcessorCpuLoadTicks(logicalProcessorCount);
36          for (long[] cpuTicks : processorCpuLoadTicks) {
37              for (long cpuTick : cpuTicks) {
38                  assertThat("CPU tick should be greater than or equal to 0", cpuTick, greaterThanOrEqualTo(0L));
39              }
40          }
41      }
42  
43      @Test
44      void testGetContextSwitches() {
45          assertThat("Context switches should be greater than or equal to -1", CpuStat.getContextSwitches(),
46                  greaterThanOrEqualTo(-1L));
47      }
48  
49      @Test
50      void testGetInterrupts() {
51          assertThat("Interrupts should be greater than or equal to -1", CpuStat.getInterrupts(),
52                  greaterThanOrEqualTo(-1L));
53      }
54  
55      @Test
56      void testGetBootTime() {
57          assertThat("Boot time should be greater than or equal to 0", CpuStat.getBootTime(), greaterThanOrEqualTo(0L));
58      }
59  }