Skip to content

Commit 996d86e

Browse files
Test DoubleEndedIterator specializations
Similar to `test_specializations` but for `DoubleEndedIterator::{rfold,nth_back}`. The other difference is that in the macro, I advance the iterator alternatively from both fronts 8 times instead of one front 5 times. Note that we don't have any `rfold/nth_back` specialization yet but it will come as I intend to do `rfold` specializations alongside `fold` ones.
1 parent 3d75598 commit 996d86e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/specializations.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,44 @@ where
8686
}
8787
}
8888

89+
fn test_double_ended_specializations<I>(it: &I)
90+
where
91+
I::Item: Eq + Debug + Clone,
92+
I: DoubleEndedIterator + Clone,
93+
{
94+
macro_rules! check_specialized {
95+
($src:expr, |$it:pat| $closure:expr) => {
96+
// Many iterators special-case the first elements, so we test specializations for iterators that have already been advanced.
97+
let mut src = $src.clone();
98+
for step in 0..8 {
99+
let $it = src.clone();
100+
let v1 = $closure;
101+
let $it = Unspecialized(src.clone());
102+
let v2 = $closure;
103+
assert_eq!(v1, v2);
104+
if step % 2 == 0 {
105+
src.next();
106+
} else {
107+
src.next_back();
108+
}
109+
}
110+
}
111+
}
112+
check_specialized!(it, |i| {
113+
let mut parameters_from_rfold = vec![];
114+
let rfold_result = i.rfold(vec![], |mut acc, v: I::Item| {
115+
parameters_from_rfold.push((acc.clone(), v.clone()));
116+
acc.push(v);
117+
acc
118+
});
119+
(parameters_from_rfold, rfold_result)
120+
});
121+
let size = it.clone().count();
122+
for n in 0..size + 2 {
123+
check_specialized!(it, |mut i| i.nth_back(n));
124+
}
125+
}
126+
89127
quickcheck! {
90128
fn interleave(v: Vec<u8>, w: Vec<u8>) -> () {
91129
test_specializations(&v.iter().interleave(w.iter()));

0 commit comments

Comments
 (0)