Skip to content

Commit

Permalink
index with usizes and fix main
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Sep 15, 2023
1 parent 2711146 commit f7aaf5b
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
buffer.push(5);

// The last item we pushed is 5
assert_eq!(buffer.get(-1), Some(&5));
assert_eq!(buffer.back(), Some(&5));

// Second entry is now 42.
buffer.push(42);
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_coverage]
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBuffer};

Expand Down Expand Up @@ -40,8 +41,8 @@ fn benchmark_push_dequeue<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new
rb.push(2);
black_box(());

assert_eq!(black_box(rb.get(-1)), Some(&2));
assert_eq!(black_box(rb.get(-2)), Some(&1));
assert_eq!(black_box(rb.get_signed(-1)), Some(&2));
assert_eq!(black_box(rb.get_signed(-2)), Some(&1));
}

rb
Expand All @@ -55,7 +56,7 @@ fn benchmark_various<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new: F)
for i in 0..100_000 {
rb.push(i);
black_box(());
black_box(rb.get(-1));
black_box(rb.back());
}

rb
Expand Down
136 changes: 107 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub use with_const_generics::ConstGenericRingBuffer;

/// Used internally. Computes the bitmask used to properly wrap the ringbuffers.
#[inline]
const fn mask(cap: usize, index: usize) -> usize {
const fn mask_and(cap: usize, index: usize) -> usize {
debug_assert!(cap.is_power_of_two());
index & (cap - 1)
}

Expand Down Expand Up @@ -63,7 +64,7 @@ mod tests {
fn test_neg_index(mut b: impl RingBuffer<usize>) {
for i in 0..capacity + 2 {
b.push(i);
assert_eq!(b.get(-1), Some(&i));
assert_eq!(b.get_signed(-1), Some(&i));
}
}

Expand Down Expand Up @@ -215,6 +216,61 @@ mod tests {
test_iter(ConstGenericRingBuffer::<i32, 8>::new());
}


#[test]
fn run_test_forward_iter_non_power_of_two() {
fn test_iter(mut b: impl RingBuffer<i32>) {
b.push(1);
b.push(2);
b.push(3);
b.push(4);
b.push(5);
b.push(6);
b.push(7);

let mut iter = b.iter();
assert_eq!(&1, iter.next().unwrap());
assert_eq!(&2, iter.next().unwrap());
assert_eq!(&3, iter.next().unwrap());
assert_eq!(&4, iter.next().unwrap());
assert_eq!(&5, iter.next().unwrap());
assert_eq!(&6, iter.next().unwrap());
assert_eq!(&7, iter.next().unwrap());
assert_eq!(None, iter.next());
}

test_iter(AllocRingBuffer::new(7));
test_iter(GrowableAllocRingBuffer::with_capacity(7));
test_iter(ConstGenericRingBuffer::<i32, 7>::new());
}

#[test]
fn run_test_iter_non_power_of_two() {
fn test_iter(mut b: impl RingBuffer<i32>) {
b.push(1);
b.push(2);
b.push(3);
b.push(4);
b.push(5);
b.push(6);
b.push(7);

let mut iter = b.iter();
assert_eq!(&1, iter.next().unwrap());
assert_eq!(&7, iter.next_back().unwrap());
assert_eq!(&2, iter.next().unwrap());
assert_eq!(&3, iter.next().unwrap());
assert_eq!(&6, iter.next_back().unwrap());
assert_eq!(&5, iter.next_back().unwrap());
assert_eq!(&4, iter.next().unwrap());
assert_eq!(None, iter.next());
}

test_iter(AllocRingBuffer::new(7));
test_iter(GrowableAllocRingBuffer::with_capacity(7));
test_iter(ConstGenericRingBuffer::<i32, 7>::new());
}

#[test]
fn run_test_iter_ref() {
fn test_iter<B>(mut b: B)
Expand Down Expand Up @@ -509,6 +565,33 @@ mod tests {
test_index(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn run_test_get() {
fn test_index(mut b: impl RingBuffer<i32>) {
b.push(0);
b.push(1);
b.push(2);
b.push(3);
b.push(4);
b.push(5);
b.push(6);
b.push(7);

assert_eq!(b.get(0), Some(&0));
assert_eq!(b.get(1), Some(&1));
assert_eq!(b.get(2), Some(&2));
assert_eq!(b.get(3), Some(&3));
assert_eq!(b.get(4), Some(&4));
assert_eq!(b.get(5), Some(&5));
assert_eq!(b.get(6), Some(&6));
assert_eq!(b.get(7), Some(&7));
}

test_index(AllocRingBuffer::new(8));
test_index(GrowableAllocRingBuffer::with_capacity(8));
test_index(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn run_test_index_mut() {
fn test_index_mut(mut b: impl RingBuffer<i32>) {
Expand Down Expand Up @@ -557,11 +640,6 @@ mod tests {
b.push(0);
b.push(1);

// [0, ...]
// ^
// [0, 1, ...]
// ^
// get[(index + 0) % len] = 0 (wrap to 0 because len == 2)
// get[(index + 1) % len] = 1
assert_eq!(b.get(0).unwrap(), &0);
assert_eq!(b.get(1).unwrap(), &1);
Expand Down Expand Up @@ -729,12 +807,12 @@ mod tests {
// ^
// get[(index + -1) % len] = 1
// get[(index + -2) % len] = 0 (wrap to 1 because len == 2)
assert_eq!(b.get(-1).unwrap(), &1);
assert_eq!(b.get(-2).unwrap(), &0);
assert_eq!(b.get_signed(-1).unwrap(), &1);
assert_eq!(b.get_signed(-2).unwrap(), &0);

// Wraps around
assert_eq!(b.get(-3).unwrap(), &1);
assert_eq!(b.get(-4).unwrap(), &0);
assert_eq!(b.get_signed(-3).unwrap(), &1);
assert_eq!(b.get_signed(-4).unwrap(), &0);
}

test_get_relative_negative(AllocRingBuffer::new(8));
Expand Down Expand Up @@ -980,11 +1058,11 @@ mod tests {
fn test_large_negative_index(mut b: impl RingBuffer<i32>) {
b.push(1);
b.push(2);
assert_eq!(b.get(1), Some(&2));
assert_eq!(b.get(0), Some(&1));
assert_eq!(b.get(-1), Some(&2));
assert_eq!(b.get(-2), Some(&1));
assert_eq!(b.get(-3), Some(&2));
assert_eq!(b.get_signed(1), Some(&2));
assert_eq!(b.get_signed(0), Some(&1));
assert_eq!(b.get_signed(-1), Some(&2));
assert_eq!(b.get_signed(-2), Some(&1));
assert_eq!(b.get_signed(-3), Some(&2));
}

test_large_negative_index(AllocRingBuffer::new(2));
Expand All @@ -997,11 +1075,11 @@ mod tests {
fn test_large_negative_index(mut b: impl RingBuffer<i32>) {
b.push(1);
b.push(2);
assert_eq!(b.get_mut(1), Some(&mut 2));
assert_eq!(b.get_mut(0), Some(&mut 1));
assert_eq!(b.get_mut(-1), Some(&mut 2));
assert_eq!(b.get_mut(-2), Some(&mut 1));
assert_eq!(b.get_mut(-3), Some(&mut 2));
assert_eq!(b.get_mut_signed(1), Some(&mut 2));
assert_eq!(b.get_mut_signed(0), Some(&mut 1));
assert_eq!(b.get_mut_signed(-1), Some(&mut 2));
assert_eq!(b.get_mut_signed(-2), Some(&mut 1));
assert_eq!(b.get_mut_signed(-3), Some(&mut 2));
}

test_large_negative_index(AllocRingBuffer::new(2));
Expand Down Expand Up @@ -1076,9 +1154,9 @@ mod tests {
b.push(1);
b.push(2);

assert_eq!(b.get(-1), Some(&2));
assert_eq!(b.get(-2), Some(&1));
assert_eq!(b.get(-3), Some(&2));
assert_eq!(b.get_signed(-1), Some(&2));
assert_eq!(b.get_signed(-2), Some(&1));
assert_eq!(b.get_signed(-3), Some(&2));
}

test_push_dequeue_push_full_get(AllocRingBuffer::new(2));
Expand Down Expand Up @@ -1109,9 +1187,9 @@ mod tests {
b.push(1);
b.push(2);

assert_eq!(b.get(-1), Some(&2));
assert_eq!(b.get(-2), Some(&1));
assert_eq!(b.get(-3), Some(&0))
assert_eq!(b.get_signed(-1), Some(&2));
assert_eq!(b.get_signed(-2), Some(&1));
assert_eq!(b.get_signed(-3), Some(&0))
}

#[test]
Expand All @@ -1135,8 +1213,8 @@ mod tests {
rb.push(1);
rb.push(2);

assert_eq!(rb.get(-1), Some(&2));
assert_eq!(rb.get(-2), Some(&1));
assert_eq!(rb.get_signed(-1), Some(&2));
assert_eq!(rb.get_signed(-2), Some(&1));
}
}

Expand Down
Loading

1 comment on commit f7aaf5b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.