miaopanel
1package system
2
3import "testing"
4
5func TestParseCPUStat(t *testing.T) {
6 // cpu user nice system idle iowait irq softirq steal guest guest_nice
7 data := "cpu 100 0 100 800 50 0 0 0 0 0\nintr 12345 0 0\n"
8 total, idle := parseCPUStat(data)
9 if total != 1050 {
10 t.Fatalf("total = %d, want 1050", total)
11 }
12 if idle != 850 {
13 t.Fatalf("idle = %d, want 850 (idle + iowait)", idle)
14 }
15 if total, idle := parseCPUStat("garbage"); total != 0 || idle != 0 {
16 t.Fatalf("malformed input should return zeros, got %d %d", total, idle)
17 }
18}
19
20func TestClampPercent(t *testing.T) {
21 cases := map[float64]float64{-5: 0, 0: 0, 42.5: 42.5, 100: 100, 120: 100}
22 for in, want := range cases {
23 if got := clampPercent(in); got != want {
24 t.Fatalf("clampPercent(%v) = %v, want %v", in, got, want)
25 }
26 }
27}