package system import "testing" func TestParseCPUStat(t *testing.T) { // cpu user nice system idle iowait irq softirq steal guest guest_nice data := "cpu 100 0 100 800 50 0 0 0 0 0\nintr 12345 0 0\n" total, idle := parseCPUStat(data) if total != 1050 { t.Fatalf("total = %d, want 1050", total) } if idle != 850 { t.Fatalf("idle = %d, want 850 (idle + iowait)", idle) } if total, idle := parseCPUStat("garbage"); total != 0 || idle != 0 { t.Fatalf("malformed input should return zeros, got %d %d", total, idle) } } func TestClampPercent(t *testing.T) { cases := map[float64]float64{-5: 0, 0: 0, 42.5: 42.5, 100: 100, 120: 100} for in, want := range cases { if got := clampPercent(in); got != want { t.Fatalf("clampPercent(%v) = %v, want %v", in, got, want) } } }