Perform same len fix for iterators

This commit is contained in:
Reece Stevens 2018-06-19 23:29:14 -05:00
parent 0172082262
commit 84ce6ec159
2 changed files with 19 additions and 1 deletions

View File

@ -207,7 +207,7 @@ where
let tail = self.tail.load_relaxed().into();
if head > tail {
head - tail
self.capacity().into() + 1 - head + tail
} else {
tail - head
}

View File

@ -172,3 +172,21 @@ fn len_properly_wraps() {
rb.enqueue(4).unwrap();
assert_eq!(rb.len(), 3);
}
#[test]
fn iterator_properly_wraps() {
type N = U3;
let mut rb: RingBuffer<u8, N> = RingBuffer::new();
rb.enqueue(1).unwrap();
rb.dequeue();
rb.enqueue(2).unwrap();
rb.enqueue(3).unwrap();
rb.enqueue(4).unwrap();
let expected = [2, 3, 4];
let mut actual = [0, 0, 0];
for (idx, el) in rb.iter().enumerate() {
actual[idx] = *el;
}
assert_eq!(expected, actual)
}