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
122
123
124
125
126
|
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::*;
#[derive (Debug, PartialEq)]
struct Record {
id: u32,
}
#[test]
fn test_new() {
let history = History::<Record>::new(4);
assert_eq!(history.entries.len(), 0);
assert_eq!(history.entries.capacity(), 4);
}
#[test]
fn test_push() {
let mut history = History::<Record>::new(2);
assert_eq!(history.entries.len(), 0);
history.push(Record{id: 11});
assert_eq!(history.entries.len(), 1);
assert_eq!(history.entries.as_slice(), &[Record{id: 11}]);
history.push(Record{id: 22});
assert_eq!(history.entries.len(), 2);
assert_eq!(history.entries.as_slice(), &[Record{id: 11}, Record{id:22}]);
history.push(Record{id: 33});
assert_eq!(history.entries.len(), 2);
assert_eq!(history.entries.as_slice(), &[Record{id:22}, Record{id: 33}]);
let mut history0 = History::<Record>::new(0);
history0.push(Record{id: 11});
assert_eq!(history0.entries.len(), 0);
assert!(history0.pop().is_none());
}
#[test]
fn test_latest() {
let mut history = History::<Record>::new(2);
assert_eq!(history.entries.len(), 0);
history.push(Record { id: 11 });
assert_eq!(history.entries.len(), 1);
assert!(history.latest().is_some_and(|x| *x == Record { id: 11 }));
history.push(Record { id: 22 });
assert_eq!(history.entries.len(), 2);
assert!(history.latest().is_some_and(|x| *x == Record { id: 22 }));
history.push(Record { id: 33 });
assert_eq!(history.entries.len(), 2);
assert!(history.latest().is_some_and(|x| *x == Record { id: 33 }));
}
#[test]
fn test_pop() {
let mut history = History::<Record>::new(2);
assert_eq!(history.entries.len(), 0);
history.push(Record { id: 11 });
history.push(Record { id: 22 });
assert!(history.pop().is_some_and(|x| x == Record { id: 22 }));
assert!(history.pop().is_some_and(|x| x == Record { id: 11 }));
assert!(history.pop().is_none());
}
#[test]
fn test_len() {
let mut history = History::<Record>::new(2);
assert_eq!(history.len(), 0);
history.push(Record { id: 11 });
assert_eq!(history.len(), 1);
history.push(Record { id: 22 });
assert_eq!(history.len(), 2);
}
#[test]
fn test_clear() {
let mut history = History::<Record>::new(2);
assert_eq!(history.entries.len(), 0);
history.push(Record { id: 11 });
history.push(Record { id: 22 });
history.clear();
assert_eq!(history.len(), 0);
}
}
|