Fix len miscalculation when head > tail

This commit is contained in:
Reece Stevens 2018-06-19 23:21:14 -05:00
parent 6ec77ef514
commit 0172082262
2 changed files with 18 additions and 1 deletions

View File

@ -335,7 +335,7 @@ macro_rules! impl_ {
let tail = self.tail.load_relaxed();
if head > tail {
head - tail
self.capacity() + 1 - head + tail
} else {
tail - head
}

View File

@ -155,3 +155,20 @@ fn unchecked() {
assert_eq!(rb.len(), N::to_usize() / 2);
}
#[test]
fn len_properly_wraps() {
type N = U3;
let mut rb: RingBuffer<u8, N> = RingBuffer::new();
rb.enqueue(1).unwrap();
assert_eq!(rb.len(), 1);
rb.dequeue();
assert_eq!(rb.len(), 0);
rb.enqueue(2).unwrap();
assert_eq!(rb.len(), 1);
rb.enqueue(3).unwrap();
assert_eq!(rb.len(), 2);
rb.enqueue(4).unwrap();
assert_eq!(rb.len(), 3);
}