get with negative index does not work when the buffer is not full
weiyinteo opened this issue · comments
It appears the rb.get(-n) does not work when rb.len() < rb.capacity().
Here is the code to reproduce:
#[test]
fn test_ring_buffer() {
let capacity = 8;
let mut rb: AllocRingBuffer<i16> = AllocRingBuffer::with_capacity(capacity as usize);
for i in 0..capacity + 2 {
rb.push(i);
let valid = rb.get(-1) == Some(&i);
if !valid {
println!(
"\npushed {}, len={}, pos at -1 should be {} but {}",
i,
rb.len(),
i,
rb.get(-1).unwrap(),
);
for j in -(i as isize + 1)..i as isize + 1 {
println!("{}: {}", j, rb.get(j).unwrap());
}
}
}
}
And the output is
pushed 2, len=3, pos at -1 should be 2 but 0
-3: 1
-2: 2
-1: 0
0: 0
1: 1
2: 2
pushed 4, len=5, pos at -1 should be 4 but 0
-5: 1
-4: 2
-3: 3
-2: 4
-1: 0
0: 0
1: 1
2: 2
3: 3
4: 4
pushed 5, len=6, pos at -1 should be 5 but 3
-6: 4
-5: 5
-4: 0
-3: 1
-2: 2
-1: 3
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
pushed 6, len=7, pos at -1 should be 6 but 1
-7: 2
-6: 3
-5: 4
-4: 5
-3: 6
-2: 0
-1: 1
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6