diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-19 09:24:47 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-19 09:24:47 -0600 |
| commit | 2ee2eee69d461a1945dfb1fe82c7d0c3489712bb (patch) | |
| tree | c9ef4e946111368ffb2824c579bde52c96d03555 /learn_15/src/main.rs | |
| parent | 1c286d81e9faad5a128e58671e791f9f70bb943c (diff) | |
feat: learn_15
commit 873dc7fd9efb52d4257ce0e8abf8f0ec44e570ed
Author: Brad Clawsie <brad@b7j0c.org>
Date: Thu Nov 19 09:24:36 2026 -0600
feat: learn_15
Diffstat (limited to 'learn_15/src/main.rs')
| -rw-r--r-- | learn_15/src/main.rs | 134 |
1 files changed, 134 insertions, 0 deletions
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(), []); + } +} |
