summaryrefslogtreecommitdiff
path: root/learn_4
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-11-13 14:19:54 -0600
committerBrad Clawsie <brad@b7j0c.org>2026-11-13 14:19:54 -0600
commit96e9806883d34fe60c433e84f201a751dee40a7a (patch)
treeccb9fc31ce3fa58752efdac5423a559b6e90eab8 /learn_4
parent576d9db1b5b12c934a5542b7ff8290a5f0b7ab56 (diff)
feat: learn_4
commit 39854e833cb588df73bbefc75727e7c920bb370b Author: Brad Clawsie <brad@b7j0c.org> Date: Fri Nov 13 14:19:32 2026 -0600 feat: learn_4
Diffstat (limited to 'learn_4')
-rw-r--r--learn_4/Cargo.lock7
-rw-r--r--learn_4/Cargo.toml6
-rw-r--r--learn_4/src/main.rs113
3 files changed, 126 insertions, 0 deletions
diff --git a/learn_4/Cargo.lock b/learn_4/Cargo.lock
new file mode 100644
index 0000000..439caa7
--- /dev/null
+++ b/learn_4/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "learn_4"
+version = "0.1.0"
diff --git a/learn_4/Cargo.toml b/learn_4/Cargo.toml
new file mode 100644
index 0000000..26944f6
--- /dev/null
+++ b/learn_4/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "learn_4"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/learn_4/src/main.rs b/learn_4/src/main.rs
new file mode 100644
index 0000000..4361b52
--- /dev/null
+++ b/learn_4/src/main.rs
@@ -0,0 +1,113 @@
+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.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]);
+ }
+
+ #[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);
+ }
+}