Skip to content

Commit 187cea9

Browse files
VasanthakumarVbluss
authored andcommitted
Add tests for axis_windows iterator
1 parent 155f941 commit 187cea9

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

tests/windows.rs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)]
77

88
use ndarray::prelude::*;
9-
use ndarray::Zip;
9+
use ndarray::{arr3, Zip};
1010

1111
// Edge Cases for Windows iterator:
1212
//
@@ -79,7 +79,6 @@ fn windows_iterator_2d() {
7979
/// Simple test for iterating 3d-arrays via `Windows`.
8080
#[test]
8181
fn windows_iterator_3d() {
82-
use ndarray::arr3;
8382
let a = Array::from_iter(10..37).into_shape((3, 3, 3)).unwrap();
8483
itertools::assert_equal(
8584
a.windows(Dim((2, 2, 2))),
@@ -117,6 +116,87 @@ fn test_window_zip() {
117116
}
118117
}
119118

119+
/// Test verifies that non existant Axis results in panic
120+
#[test]
121+
#[should_panic]
122+
fn axis_windows_outofbound() {
123+
let a = Array::from_iter(10..37).into_shape((3, 3, 3)).unwrap();
124+
a.axis_windows(Axis(4), 2);
125+
}
126+
127+
/// Test verifies that zero sizes results in panic
128+
#[test]
129+
#[should_panic]
130+
fn axis_windows_zero_size() {
131+
let a = Array::from_iter(10..37).into_shape((3, 3, 3)).unwrap();
132+
a.axis_windows(Axis(0), 0);
133+
}
134+
135+
/// Test verifies that over sized windows yield nothing
136+
#[test]
137+
fn axis_windows_oversized() {
138+
let a = Array::from_iter(10..37).into_shape((3, 3, 3)).unwrap();
139+
let mut iter = a.axis_windows(Axis(2), 4).into_iter();
140+
assert_eq!(iter.next(), None);
141+
}
142+
143+
/// Simple test for iterating 1d-arrays via `Axis Windows`.
144+
#[test]
145+
fn test_axis_windows_1d() {
146+
let a = Array::from_iter(10..20).into_shape(10).unwrap();
147+
148+
itertools::assert_equal(
149+
a.axis_windows(Axis(0), 5),
150+
vec![
151+
arr1(&[10, 11, 12, 13, 14]),
152+
arr1(&[11, 12, 13, 14, 15]),
153+
arr1(&[12, 13, 14, 15, 16]),
154+
arr1(&[13, 14, 15, 16, 17]),
155+
arr1(&[14, 15, 16, 17, 18]),
156+
arr1(&[15, 16, 17, 18, 19]),
157+
],
158+
);
159+
}
160+
161+
/// Simple test for iterating 2d-arrays via `Axis Windows`.
162+
#[test]
163+
fn test_axis_windows_2d() {
164+
let a = Array::from_iter(10..30).into_shape((5, 4)).unwrap();
165+
166+
itertools::assert_equal(
167+
a.axis_windows(Axis(0), 2),
168+
vec![
169+
arr2(&[[10, 11, 12, 13], [14, 15, 16, 17]]),
170+
arr2(&[[14, 15, 16, 17], [18, 19, 20, 21]]),
171+
arr2(&[[18, 19, 20, 21], [22, 23, 24, 25]]),
172+
arr2(&[[22, 23, 24, 25], [26, 27, 28, 29]]),
173+
],
174+
);
175+
}
176+
177+
/// Simple test for iterating 3d-arrays via `Axis Windows`.
178+
#[test]
179+
fn test_axis_windows_3d() {
180+
let a = Array::from_iter(0..27).into_shape((3, 3, 3)).unwrap();
181+
182+
itertools::assert_equal(
183+
a.axis_windows(Axis(1), 2),
184+
vec![
185+
arr3(&[
186+
[[0, 1, 2], [3, 4, 5]],
187+
[[9, 10, 11], [12, 13, 14]],
188+
[[18, 19, 20], [21, 22, 23]],
189+
]),
190+
arr3(&[
191+
[[3, 4, 5], [6, 7, 8]],
192+
[[12, 13, 14], [15, 16, 17]],
193+
[[21, 22, 23], [24, 25, 26]],
194+
]),
195+
],
196+
);
197+
}
198+
199+
120200
#[test]
121201
fn test_window_neg_stride() {
122202
let array = Array::from_iter(1..10).into_shape((3, 3)).unwrap();

0 commit comments

Comments
 (0)