1#[derive(Debug)]
2pub struct SoftIrq {
3 pub total_irqs: u64,
4 pub all: Vec<u64>
5}
6
7impl SoftIrq {
8 pub fn empty() -> Self { Self { total_irqs: 0, all: Vec::new() } }
9
10 pub fn from_vec(mut soft_irq_vals: Vec<u64>) -> Self {
11 if soft_irq_vals.len() == 0 { return Self::empty() }
12
13 let total_irqs = soft_irq_vals.remove(0);
14
15 Self {
16 total_irqs,
17 all: soft_irq_vals,
18 }
19 }
20}
21
22#[cfg(test)]
23mod tests {
24 use crate::soft_irq::SoftIrq;
25
26 #[test]
27 fn creates_interrupt_from_vec_of_numbers() {
28 let test_data: Vec<u64> = vec![2095397048, 59605681, 87064650, 75257, 115853410, 3389780, 0, 27371450, 1253180383, 6010986, 542845451];
29
30 let soft_irqs: SoftIrq = SoftIrq::from_vec(test_data);
31
32 assert_eq!(soft_irqs.total_irqs, 2095397048);
33 assert_eq!(soft_irqs.all.len(), 10);
34 assert_eq!(soft_irqs.all[0], 59605681);
35 }
36
37 #[test]
38 fn returns_only_total_if_vec_is_only_1_element() {
39 let test_data: Vec<u64> = vec![2095397048];
40
41 let soft_irqs: SoftIrq = SoftIrq::from_vec(test_data);
42
43 assert_eq!(soft_irqs.total_irqs, 2095397048);
44 assert_eq!(soft_irqs.all.len(), 0);
45 }
46
47 #[test]
48 fn returns_default_if_vec_is_empty() {
49 let soft_irqs: SoftIrq = SoftIrq::from_vec(Vec::new());
50
51 assert_eq!(soft_irqs.total_irqs, 0);
52 assert_eq!(soft_irqs.all.len(), 0);
53 }
54}