mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-26 03:21:03 +00:00
gstreamer: bufferlist: Fix remove() range end being off by one
The end index was being calculated the same way as the start one, which is incorrect. It should be +1'd when range is inclusive and left as-is if it's exclusive, not the other way around. Fixed and added a simple test to verify correctness. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1494>
This commit is contained in:
parent
efc87fd239
commit
db446f3f46
1 changed files with 59 additions and 27 deletions
|
@ -64,8 +64,8 @@ impl BufferListRef {
|
||||||
assert!(start_idx < n);
|
assert!(start_idx < n);
|
||||||
|
|
||||||
let end_idx = match range.end_bound() {
|
let end_idx = match range.end_bound() {
|
||||||
std::ops::Bound::Included(idx) => *idx,
|
std::ops::Bound::Included(idx) => idx.checked_add(1).unwrap(),
|
||||||
std::ops::Bound::Excluded(idx) => idx.checked_add(1).unwrap(),
|
std::ops::Bound::Excluded(idx) => *idx,
|
||||||
std::ops::Bound::Unbounded => n,
|
std::ops::Bound::Unbounded => n,
|
||||||
};
|
};
|
||||||
assert!(end_idx <= n);
|
assert!(end_idx <= n);
|
||||||
|
@ -405,21 +405,29 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::ClockTime;
|
use crate::ClockTime;
|
||||||
|
|
||||||
#[test]
|
fn make_buffer_list(size: usize) -> BufferList {
|
||||||
fn test_foreach() {
|
skip_assert_initialized!();
|
||||||
crate::init().unwrap();
|
|
||||||
|
|
||||||
let mut buffer_list = BufferList::new();
|
let mut buffer_list = BufferList::new();
|
||||||
{
|
{
|
||||||
let buffer_list = buffer_list.get_mut().unwrap();
|
let buffer_list = buffer_list.get_mut().unwrap();
|
||||||
|
for i in 0..size {
|
||||||
let mut buffer = Buffer::new();
|
let mut buffer = Buffer::new();
|
||||||
buffer.get_mut().unwrap().set_pts(ClockTime::ZERO);
|
buffer
|
||||||
buffer_list.add(buffer);
|
.get_mut()
|
||||||
|
.unwrap()
|
||||||
let mut buffer = Buffer::new();
|
.set_pts(ClockTime::SECOND * i as u64);
|
||||||
buffer.get_mut().unwrap().set_pts(ClockTime::SECOND);
|
|
||||||
buffer_list.add(buffer);
|
buffer_list.add(buffer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
buffer_list
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_foreach() {
|
||||||
|
crate::init().unwrap();
|
||||||
|
|
||||||
|
let buffer_list = make_buffer_list(2);
|
||||||
|
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
buffer_list.foreach(|buffer, idx| {
|
buffer_list.foreach(|buffer, idx| {
|
||||||
|
@ -437,21 +445,7 @@ mod tests {
|
||||||
fn test_foreach_mut() {
|
fn test_foreach_mut() {
|
||||||
crate::init().unwrap();
|
crate::init().unwrap();
|
||||||
|
|
||||||
let mut buffer_list = BufferList::new();
|
let mut buffer_list = make_buffer_list(3);
|
||||||
{
|
|
||||||
let buffer_list = buffer_list.get_mut().unwrap();
|
|
||||||
let mut buffer = Buffer::new();
|
|
||||||
buffer.get_mut().unwrap().set_pts(ClockTime::ZERO);
|
|
||||||
buffer_list.add(buffer);
|
|
||||||
|
|
||||||
let mut buffer = Buffer::new();
|
|
||||||
buffer.get_mut().unwrap().set_pts(ClockTime::SECOND);
|
|
||||||
buffer_list.add(buffer);
|
|
||||||
|
|
||||||
let mut buffer = Buffer::new();
|
|
||||||
buffer.get_mut().unwrap().set_pts(2 * ClockTime::SECOND);
|
|
||||||
buffer_list.add(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
buffer_list.get_mut().unwrap().foreach_mut(|buffer, idx| {
|
buffer_list.get_mut().unwrap().foreach_mut(|buffer, idx| {
|
||||||
|
@ -515,4 +509,42 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(res, &[1, 2, 4, 5, 7, 8]);
|
assert_eq!(res, &[1, 2, 4, 5, 7, 8]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_remove() {
|
||||||
|
crate::init().unwrap();
|
||||||
|
|
||||||
|
let mut buffer_list = make_buffer_list(10);
|
||||||
|
|
||||||
|
buffer_list.make_mut().remove(0..2);
|
||||||
|
|
||||||
|
let buffers_left = buffer_list
|
||||||
|
.iter()
|
||||||
|
.map(|buf| buf.pts().unwrap() / ClockTime::SECOND)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(buffers_left, &[2, 3, 4, 5, 6, 7, 8, 9]);
|
||||||
|
|
||||||
|
buffer_list.make_mut().remove(0..=2);
|
||||||
|
|
||||||
|
let buffers_left = buffer_list
|
||||||
|
.iter()
|
||||||
|
.map(|buf| buf.pts().unwrap() / ClockTime::SECOND)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(buffers_left, &[5, 6, 7, 8, 9]);
|
||||||
|
|
||||||
|
buffer_list.make_mut().remove(2..);
|
||||||
|
|
||||||
|
let buffers_left = buffer_list
|
||||||
|
.iter()
|
||||||
|
.map(|buf| buf.pts().unwrap() / ClockTime::SECOND)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(buffers_left, &[5, 6]);
|
||||||
|
|
||||||
|
buffer_list.make_mut().remove(..);
|
||||||
|
|
||||||
|
assert!(buffer_list.is_empty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue