Skip to content

Commit 560a7c4

Browse files
committedDec 28, 2024·
Improve iter docs and add iter to fuzz
1 parent ae5cc12 commit 560a7c4

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed
 

‎example/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎fuzz/fuzz_targets/map.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum Op {
3636
Fetch(u8),
3737
Remove(u8),
3838
RemoveAll,
39+
Iter,
3940
}
4041

4142
#[derive(Arbitrary, Debug, Clone)]
@@ -263,7 +264,6 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
263264
}
264265
}
265266
}
266-
267267
Err(Error::Corrupted {
268268
backtrace: _backtrace,
269269
}) => {
@@ -274,6 +274,36 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
274274
Err(e) => panic!("{e:?}"),
275275
}
276276
}
277+
Op::Iter => {
278+
let mut iter = block_on(sequential_storage::map::fetch_all_items::<u8, _, _>(
279+
&mut flash,
280+
FLASH_RANGE,
281+
&mut cache,
282+
&mut buf.0,
283+
))
284+
.unwrap();
285+
286+
let mut seen_items = HashMap::new();
287+
288+
loop {
289+
match block_on(iter.next::<u8, &[u8]>(&mut buf.0)) {
290+
Ok(None) => break,
291+
Ok(Some((key, val))) => {
292+
seen_items.insert(key, val.to_vec());
293+
}
294+
Err(Error::Corrupted {
295+
backtrace: _backtrace,
296+
}) => {
297+
#[cfg(fuzzing_repro)]
298+
eprintln!("Corrupted when removing! Originated from:\n{_backtrace:#}");
299+
panic!("Corrupted!");
300+
}
301+
Err(e) => panic!("{e:?}"),
302+
}
303+
}
304+
305+
assert_eq!(seen_items, map);
306+
}
277307
}
278308
}
279309
}

‎src/map.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ async fn remove_item_inner<K: Key, S: MultiwriteNorFlash>(
682682
/// # use sequential_storage::cache::NoCache;
683683
/// # use mock_flash::MockFlashBase;
684684
/// # use futures::executor::block_on;
685+
/// # use std::collections::HashMap;
685686
/// # type Flash = MockFlashBase<10, 1, 4096>;
686687
/// # mod mock_flash {
687688
/// # include!("mock_flash.rs");
@@ -706,15 +707,18 @@ async fn remove_item_inner<K: Key, S: MultiwriteNorFlash>(
706707
/// .await
707708
/// .unwrap();
708709
///
710+
/// let mut all_items = HashMap::new();
711+
///
709712
/// // Iterate through all items, suppose the Key and Value types are u8, u32
710-
/// while let Ok(Some((key, value))) = iterator
713+
/// while let Some((key, value)) = iterator
711714
/// .next::<u8, u32>(&mut data_buffer)
712715
/// .await
716+
/// .unwrap()
713717
/// {
714718
/// // Do somethinmg with the item.
715719
/// // Please note that for the same key there might be multiple items returned,
716720
/// // the last one is the current active one.
717-
/// println!("{key}:{value}");
721+
/// all_items.insert(key, value);
718722
/// }
719723
/// # })
720724
/// ```
@@ -811,6 +815,7 @@ impl<S: NorFlash, CI: CacheImpl> MapItemIter<'_, '_, S, CI> {
811815
/// # use sequential_storage::cache::NoCache;
812816
/// # use mock_flash::MockFlashBase;
813817
/// # use futures::executor::block_on;
818+
/// # use std::collections::HashMap;
814819
/// # type Flash = MockFlashBase<10, 1, 4096>;
815820
/// # mod mock_flash {
816821
/// # include!("mock_flash.rs");
@@ -835,14 +840,18 @@ impl<S: NorFlash, CI: CacheImpl> MapItemIter<'_, '_, S, CI> {
835840
/// .await
836841
/// .unwrap();
837842
///
843+
/// let mut all_items = HashMap::new();
844+
///
838845
/// // Iterate through all items, suppose the Key and Value types are u8, u32
839-
/// while let Ok(Some((key, value))) = iterator
846+
/// while let Some((key, value)) = iterator
840847
/// .next::<u8, u32>(&mut data_buffer)
841848
/// .await
849+
/// .unwrap()
842850
/// {
843851
/// // Do somethinmg with the item.
844852
/// // Please note that for the same key there might be multiple items returned,
845853
/// // the last one is the current active one.
854+
/// all_items.insert(key, value);
846855
/// }
847856
/// # })
848857
/// ```
@@ -1837,7 +1846,6 @@ mod tests {
18371846
}
18381847
}
18391848

1840-
// Check the
18411849
assert_eq!(last_value_length, 9);
18421850
assert_eq!(
18431851
&last_value_buffer[..last_value_length],

0 commit comments

Comments
 (0)
Please sign in to comment.