summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore7
-rw-r--r--learn_15/Cargo.lock7
-rw-r--r--learn_15/Cargo.toml6
-rw-r--r--learn_15/src/main.rs134
4 files changed, 154 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 36700d7..84b84b9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -29,3 +29,10 @@ rustc-ice-*.txt
# already existing elements were commented out
#/target
+
+
+# Added by cargo
+#
+# already existing elements were commented out
+
+#/target
diff --git a/learn_15/Cargo.lock b/learn_15/Cargo.lock
new file mode 100644
index 0000000..1471d91
--- /dev/null
+++ b/learn_15/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_15"
+version = "0.1.0"
diff --git a/learn_15/Cargo.toml b/learn_15/Cargo.toml
new file mode 100644
index 0000000..37435e5
--- /dev/null
+++ b/learn_15/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "learn_15"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/learn_15/src/main.rs b/learn_15/src/main.rs
new file mode 100644
index 0000000..8ceb994
--- /dev/null
+++ b/learn_15/src/main.rs
@@ -0,0 +1,134 @@
+#[derive(Debug, PartialEq)]
+struct LogEntry {
+ timestamp: u64,
+ message: String,
+}
+
+struct LogBuffer {
+ entries: Vec<LogEntry>,
+ capacity: usize,
+}
+
+impl LogBuffer {
+ fn new(capacity: usize) -> Self {
+ Self {
+ // panics if capacity > isize::MAX
+ entries: Vec::with_capacity(capacity),
+ capacity,
+ }
+ }
+
+ fn push(&mut self, entry: LogEntry) -> bool {
+ if self.entries.len() == self.capacity {
+ return false;
+ }
+ self.entries.push(entry);
+ true
+ }
+
+ fn entries(&self) -> &[LogEntry] {
+ &self.entries[..]
+ }
+
+ fn drain(&mut self) -> Vec<LogEntry> {
+ let entries = std::mem::take(&mut self.entries);
+ self.entries.reserve_exact(self.capacity);
+ entries
+ }
+
+ fn reset(&mut self) {
+ let _ = self.drain();
+ return
+ }
+}
+
+impl Default for LogBuffer {
+ fn default() -> Self {
+ const CAPACITY: usize = 100;
+ Self {
+ entries: Vec::with_capacity(CAPACITY),
+ capacity: CAPACITY,
+ }
+ }
+}
+
+fn main() {
+ ()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::*;
+
+ #[test]
+ fn test_new() {
+ const CAPACITY: usize = 100;
+ let lb: LogBuffer = LogBuffer::new(CAPACITY);
+ assert_eq!(lb.capacity, CAPACITY);
+ assert!(lb.entries.is_empty());
+ assert_eq!(lb.entries.capacity(), lb.capacity);
+ }
+
+ #[test]
+ fn test_default() {
+ const CAPACITY: usize = 100;
+ let lb: LogBuffer = Default::default();
+ assert_eq!(lb.capacity, CAPACITY);
+ assert_eq!(lb.entries.capacity(), CAPACITY);
+ }
+
+ #[test]
+ fn test_push() {
+ let mut lb: LogBuffer = LogBuffer::new(1);
+ assert!(lb.push(LogEntry{timestamp:0, message:"a".to_string()}));
+ assert_eq!(lb.entries.len(), 1);
+ assert!(!lb.push(LogEntry{timestamp:0, message:"a".to_string()}));
+ assert_eq!(lb.entries.len(), 1);
+ }
+
+ #[test]
+ fn test_entries() {
+ const CAPACITY: usize = 2;
+ let mut lb: LogBuffer = LogBuffer::new(CAPACITY);
+ assert!(lb.push(LogEntry{timestamp:0, message: "a".to_string()}));
+ assert!(lb.push(LogEntry{timestamp:1, message: "b".to_string()}));
+ assert_eq!(lb.entries.len(), 2);
+ assert_eq!(lb.entries(), [
+ LogEntry{timestamp:0, message: "a".to_string()},
+ LogEntry{timestamp:1, message: "b".to_string()}
+ ]);
+ }
+
+ #[test]
+ fn test_drain() {
+ const CAPACITY: usize = 2;
+ let mut lb: LogBuffer = LogBuffer::new(CAPACITY);
+ assert!(lb.push(LogEntry{timestamp:0, message:"a".to_string()}));
+ assert_eq!(lb.entries.len(), 1);
+ assert!(lb.push(LogEntry{timestamp:1, message:"b".to_string()}));
+ assert_eq!(lb.entries.len(), 2);
+ assert_eq!(lb.drain(), vec![
+ LogEntry{timestamp:0, message: "a".to_string()},
+ LogEntry{timestamp:1, message: "b".to_string()}
+ ]);
+ assert!(lb.entries.is_empty());
+ assert_eq!(lb.entries.capacity(), lb.capacity);
+ }
+
+ #[test]
+ fn test_reset() {
+ const CAPACITY: usize = 2;
+ let mut lb: LogBuffer = LogBuffer::new(CAPACITY);
+ assert!(lb.push(LogEntry{timestamp:0, message: "a".to_string()}));
+ assert!(lb.push(LogEntry{timestamp:1, message: "b".to_string()}));
+ assert_eq!(lb.entries.len(), 2);
+ assert_eq!(lb.entries(), [
+ LogEntry{timestamp:0, message: "a".to_string()},
+ LogEntry{timestamp:1, message: "b".to_string()}
+ ]);
+ lb.reset();
+ assert_eq!(lb.entries.capacity(), CAPACITY);
+ assert_eq!(lb.entries.len(), 0);
+ assert_eq!(lb.entries(), []);
+ }
+}