Skip to content

Commit cd6482e

Browse files
committed
TEST: Add benchmarks and tests for iterator method rfold
This method is stable since Rust 1.27, and we can now implement it.
1 parent 39ccd4a commit cd6482e

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

benches/iter.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rawpointer::PointerExt;
1010
extern crate ndarray;
1111
use ndarray::prelude::*;
1212
use ndarray::{Zip, FoldWhile};
13+
use ndarray::Slice;
1314

1415
#[bench]
1516
fn iter_sum_2d_regular(bench: &mut Bencher)
@@ -342,3 +343,24 @@ fn indexed_iter_3d_dyn(bench: &mut Bencher) {
342343
}
343344
})
344345
}
346+
347+
#[bench]
348+
fn iter_sum_1d_strided_fold(bench: &mut Bencher)
349+
{
350+
let mut a = Array::<u64, _>::ones(10240);
351+
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
352+
bench.iter(|| {
353+
a.iter().fold(0, |acc, &x| acc + x)
354+
});
355+
}
356+
357+
#[bench]
358+
fn iter_sum_1d_strided_rfold(bench: &mut Bencher)
359+
{
360+
let mut a = Array::<u64, _>::ones(10240);
361+
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
362+
bench.iter(|| {
363+
a.iter().rfold(0, |acc, &x| acc + x)
364+
});
365+
}
366+

tests/iterators.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
extern crate ndarray;
33
extern crate itertools;
44

5-
use ndarray::{Array0, Array2};
6-
use ndarray::RcArray;
5+
use ndarray::prelude::*;
76
use ndarray::Ix;
87
use ndarray::{
9-
ArrayBase,
108
Data,
119
Dimension,
1210
aview1,
1311
arr2,
1412
arr3,
1513
Axis,
1614
indices,
15+
Slice,
1716
};
1817

1918
use itertools::assert_equal;
@@ -498,3 +497,44 @@ fn test_fold() {
498497
a += 1;
499498
assert_eq!(a.iter().fold(0, |acc, &x| acc + x), 1);
500499
}
500+
501+
#[test]
502+
fn test_rfold() {
503+
{
504+
let mut a = Array1::<i32>::default(256);
505+
a += 1;
506+
let mut iter = a.iter();
507+
iter.next();
508+
assert_eq!(iter.rfold(0, |acc, &x| acc + x), a.sum() - 1);
509+
}
510+
511+
// Test strided arrays
512+
{
513+
let mut a = Array1::<i32>::default(256);
514+
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
515+
a += 1;
516+
let mut iter = a.iter();
517+
iter.next();
518+
assert_eq!(iter.rfold(0, |acc, &x| acc + x), a.sum() - 1);
519+
}
520+
521+
{
522+
let mut a = Array1::<i32>::default(256);
523+
a.slice_axis_inplace(Axis(0), Slice::new(0, None, -2));
524+
a += 1;
525+
let mut iter = a.iter();
526+
iter.next();
527+
assert_eq!(iter.rfold(0, |acc, &x| acc + x), a.sum() - 1);
528+
}
529+
530+
// Test order
531+
{
532+
let mut a = Array1::from_iter(0..20);
533+
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
534+
let mut iter = a.iter();
535+
iter.next();
536+
let output = iter.rfold(Vec::new(),
537+
|mut acc, elt| { acc.push(*elt); acc });
538+
assert_eq!(Array1::from_vec(output), Array::from_iter((1..10).rev().map(|i| i * 2)));
539+
}
540+
}

0 commit comments

Comments
 (0)