Skip to content

Commit 0951795

Browse files
Add peek_nth_next_if quickcheck test
1 parent 44b4d71 commit 0951795

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/quick.rs

+24
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,30 @@ quickcheck! {
782782
assert_eq!(it.next(), None);
783783
assert_eq!(it.next(), None);
784784
}
785+
786+
fn peek_nth_next_if(a: Vec<u8>) -> () {
787+
let mut it = peek_nth(a.clone());
788+
for (idx, mut value) in a.iter().copied().enumerate() {
789+
let should_be_none = it.next_if(|x| x != &value);
790+
assert_eq!(should_be_none, None);
791+
if value % 5 == 0 {
792+
// Sometimes, peek up to 3 further.
793+
let n = value as usize % 3;
794+
let nth = it.peek_nth(n);
795+
assert_eq!(nth, a.get(idx + n));
796+
} else if value % 5 == 1 {
797+
// Sometimes, peek next element mutably.
798+
if let Some(v) = it.peek_mut() {
799+
*v = v.wrapping_sub(1);
800+
let should_be_none = it.next_if_eq(&value);
801+
assert_eq!(should_be_none, None);
802+
value = value.wrapping_sub(1);
803+
}
804+
}
805+
let eq = it.next_if_eq(&value);
806+
assert_eq!(eq, Some(value));
807+
}
808+
}
785809
}
786810

787811
quickcheck! {

0 commit comments

Comments
 (0)