Skip to content

Commit

Permalink
Test buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
BradenEverson committed Sep 9, 2024
1 parent 94bd429 commit 03edbe0
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions achiever/src/brain/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::ops::Deref;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// A DataBuffer with known position
pub struct DataBuffer<const BUFFER_SIZE: usize> {
/// The data
Expand All @@ -21,6 +21,16 @@ impl<const BUFFER_SIZE: usize> Default for DataBuffer<BUFFER_SIZE> {
}
}

impl<const BUFFER_SIZE: usize> AsRef<[u8]> for DataBuffer<BUFFER_SIZE> {
fn as_ref(&self) -> &[u8] {
if *self.marker == BUFFER_SIZE - 1 {
&self.data
} else {
&self.data[0..*self.marker]
}
}
}

impl<const BUFFER_SIZE: usize> DataBuffer<BUFFER_SIZE> {
/// Adds a buffer of bytes to the internal data buffer. If the length of the data to add is
/// greater than what's left in the buffer, we close early and return `None`
Expand All @@ -43,21 +53,24 @@ impl<const BUFFER_SIZE: usize> DataBuffer<BUFFER_SIZE> {
}
}

#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// A Buffer position marker
pub struct BufferMarker<const BUFFER_SIZE: usize>(usize);

impl<const BUFFER_SIZE: usize> BufferMarker<BUFFER_SIZE> {
/// Increment the buffer marker, returning None if the buffer is full
pub fn inc(&mut self) -> Option<usize> {
if self.0 + 1 >= BUFFER_SIZE {
self.0 = 0;
None
} else {
self.0 += 1;
Some(self.0)
}
}
/// Reset a buffer marker
pub fn rst(&mut self) {
self.0 = 0
}
}

impl<const BUFFER_SIZE: usize> Deref for BufferMarker<BUFFER_SIZE> {
Expand All @@ -66,3 +79,51 @@ impl<const BUFFER_SIZE: usize> Deref for BufferMarker<BUFFER_SIZE> {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::DataBuffer;

#[test]
fn data_buffer_can_be_fully_filled() {
let mut buf: DataBuffer<10> = DataBuffer::default();
let data = [10u8; 10];

buf.add_data(&data).expect("Fill buf all the way with data");
assert_eq!(buf.as_ref(), &data);
}

#[test]
fn data_buffer_cannot_be_overfilled() {
let mut buf: DataBuffer<10> = DataBuffer::default();
let data = [10u8; 11];

let overfill = buf.add_data(&data);
assert!(overfill.is_none())
}

#[test]
fn partially_filled_data_buffer() {
let mut buf: DataBuffer<100> = DataBuffer::default();
let small_chunk = b"Hello!";
buf.add_data(small_chunk).expect("Add to a buffer");
let word: &[u8] = buf.as_ref();

assert_eq!(word, small_chunk)
}

#[test]
fn partially_filled_data_buffer_stays_after_overfill() {
let mut buf: DataBuffer<10> = DataBuffer::default();
let small_chunk = b"Hello!";
buf.add_data(small_chunk).expect("Add to a buffer");
let small_over_chunk = b" world!";

let overfill = buf.add_data(small_over_chunk);
assert!(overfill.is_none());

let word: &[u8] = buf.as_ref();

assert_eq!(word, small_chunk)
}
}

0 comments on commit 03edbe0

Please sign in to comment.