mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-28 13:00:26 +00:00
Fix len miscalculation when head > tail
This commit is contained in:
parent
6ec77ef514
commit
0172082262
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user