summaryrefslogtreecommitdiff
path: root/learn_4/src/main.rs
blob: 47408efbcf7e32db3fdea596843ae6a489ca9207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
struct History<T> {
    entries: Vec<T>,
    capacity: usize,
}

impl<T> History<T> {
    pub fn new(capacity: usize) -> Self {
        Self {
            entries: Vec::<T>::with_capacity(capacity),
            capacity,
        }
    }

    pub fn push(&mut self, value: T) {
        if self.capacity == 0 {
            return;
        }
        if self.entries.len() == self.capacity {
            self.entries.remove(0);
        }
        self.entries.push(value);
    }

    pub fn latest(&self) -> Option<&T> {
        self.entries.last()
    }

    pub fn pop(&mut self) -> Option<T> {
        self.entries.pop()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }
    
    pub fn clear(&mut self) {
        self.entries.clear()
    }
}

fn main() {
    ()
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn test_new() {
        let history = History::<u32>::new(4);
        assert_eq!(history.entries.len(), 0);
        assert_eq!(history.entries.capacity(), 4);
    }

    #[test]
    fn test_push() {
        let mut history = History::<u32>::new(2);
        assert_eq!(history.entries.len(), 0);
        history.push(11);
        assert_eq!(history.entries.len(), 1);
        assert_eq!(history.entries.as_slice(), &[11]);
        history.push(22);
        assert_eq!(history.entries.len(), 2);
        assert_eq!(history.entries.as_slice(), &[11, 22]);
        history.push(33);
        assert_eq!(history.entries.len(), 2);
        assert_eq!(history.entries.as_slice(), &[22, 33]);

        let mut history0 = History::<u32>::new(0);
        history0.push(11);
        assert_eq!(history0.entries.len(), 0);
        assert!(history0.pop().is_none());
    }

    #[test]
    fn test_latest() {
        let mut history = History::<u32>::new(2);
        assert_eq!(history.entries.len(), 0);
        history.push(11);
        assert_eq!(history.entries.len(), 1);
        assert!(history.latest().is_some_and(|x| *x == 11));
        history.push(22);
        assert_eq!(history.entries.len(), 2);
        assert!(history.latest().is_some_and(|x| *x == 22));
        history.push(33);
        assert_eq!(history.entries.len(), 2);
        assert!(history.latest().is_some_and(|x| *x == 33));
    }

    #[test]
    fn test_pop() {
        let mut history = History::<u32>::new(2);
        assert_eq!(history.entries.len(), 0);
        history.push(11);
        history.push(22);
        assert!(history.pop().is_some_and(|x| x == 22));
        assert!(history.pop().is_some_and(|x| x == 11));
        assert!(history.pop().is_none());
    }

    #[test]
    fn test_len() {
        let mut history = History::<u32>::new(2);
        assert_eq!(history.len(), 0);
        history.push(11);
        assert_eq!(history.len(), 1);
        history.push(22);
        assert_eq!(history.len(), 2);
    }
    
    #[test]
    fn test_clear() {
        let mut history = History::<u32>::new(2);
        assert_eq!(history.entries.len(), 0);
        history.push(11);
        history.push(22);
        history.clear();
        assert_eq!(history.len(), 0);
    }
}