Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support peek_many and pop_many #12

Merged
merged 10 commits into from
Dec 16, 2023
Prev Previous commit
Next Next commit
fix: test peek many
  • Loading branch information
lulf committed Dec 15, 2023
commit ccf9fa2f5d01640fc1781d36f2920fe317c2d7c3
70 changes: 63 additions & 7 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ impl<'d, S: NorFlash> Peeker<'d, S> {
}
item::MaybeItem::Erased(_) => unreachable!("Item is already erased"),
item::MaybeItem::Present(item) => {
self.start_address = item.header.next_item_address::<S>(self.start_address);
let next_address = item.header.next_item_address::<S>(found_item_address);
let (header, data_buffer) = item.destruct();
self.start_address = next_address;
return Ok(Some(&mut data_buffer[..header.length as usize]));
}
}
Expand All @@ -275,12 +276,6 @@ impl<'d, S: NorFlash> Peeker<'d, S> {
}
}
}
//let page_count = flash_range.len() / S::ERASE_SIZE;
//flash_range
// .step_by(S::ERASE_SIZE)
// .enumerate()
// .map(move |(index, _)| (index + starting_page_index) % page_count)
//}
}

/// Peek at the oldest data.
Expand Down Expand Up @@ -560,6 +555,67 @@ mod tests {
}
}

#[test]
fn push_peek_pop_many() {
let mut flash = MockFlashBig::default();
let flash_range = 0x000..0x1000;
let mut data_buffer = [0; 1024];

for a in 0..100 {
for i in 0..20 {
let data = vec![i as u8; 50];
push(&mut flash, flash_range.clone(), &data, false).unwrap();
}

let mut peeker = peek_many(&mut flash, flash_range.clone());
for i in 0..20 {
let data = vec![i as u8; 50];
assert_eq!(
&peeker.next(&mut data_buffer).unwrap().unwrap()[..],
&data,
"At {i}"
);
}

for i in 0..5 {
let data = vec![i as u8; 50];
assert_eq!(
&pop(&mut flash, flash_range.clone(), &mut data_buffer)
.unwrap()
.unwrap()[..],
&data,
"At {i}"
);
}

for i in 20..25 {
let data = vec![i as u8; 50];
push(&mut flash, flash_range.clone(), &data, false).unwrap();
}

let mut peeker = peek_many(&mut flash, flash_range.clone());
for i in 5..25 {
let data = vec![i as u8; 50];
assert_eq!(
&peeker.next(&mut data_buffer).unwrap().unwrap()[..],
&data,
"At {i}"
);
}

for i in 5..25 {
let data = vec![i as u8; 50];
assert_eq!(
&pop(&mut flash, flash_range.clone(), &mut data_buffer)
.unwrap()
.unwrap()[..],
&data,
"At {i}"
);
}
}
}

#[test]
fn push_lots_then_pop_lots() {
let mut flash = MockFlashBig::default();
Expand Down