@@ -294,6 +294,148 @@ fn tests_axis_windows_3d_zips_with_1d()
294294 assert_eq ! ( b, arr1( & [ 207 , 261 ] ) ) ;
295295}
296296
297+ /// Test verifies that non existent Axis results in panic
298+ #[ test]
299+ #[ should_panic]
300+ fn axis_windows_with_stride_outofbound ( )
301+ {
302+ let a = Array :: from_iter ( 10 ..37 )
303+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
304+ . unwrap ( ) ;
305+ a. axis_windows_with_stride ( Axis ( 4 ) , 2 , 2 ) ;
306+ }
307+
308+ /// Test verifies that zero sizes results in panic
309+ #[ test]
310+ #[ should_panic]
311+ fn axis_windows_with_stride_zero_size ( )
312+ {
313+ let a = Array :: from_iter ( 10 ..37 )
314+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
315+ . unwrap ( ) ;
316+ a. axis_windows_with_stride ( Axis ( 0 ) , 0 , 2 ) ;
317+ }
318+
319+ /// Test verifies that zero stride results in panic
320+ #[ test]
321+ #[ should_panic]
322+ fn axis_windows_with_stride_zero_stride ( )
323+ {
324+ let a = Array :: from_iter ( 10 ..37 )
325+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
326+ . unwrap ( ) ;
327+ a. axis_windows_with_stride ( Axis ( 0 ) , 2 , 0 ) ;
328+ }
329+
330+ /// Test verifies that over sized windows yield nothing
331+ #[ test]
332+ fn axis_windows_with_stride_oversized ( )
333+ {
334+ let a = Array :: from_iter ( 10 ..37 )
335+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
336+ . unwrap ( ) ;
337+ let mut iter = a. axis_windows_with_stride ( Axis ( 2 ) , 4 , 2 ) . into_iter ( ) ;
338+ assert_eq ! ( iter. next( ) , None ) ;
339+ }
340+
341+ /// Simple test for iterating 1d-arrays via `Axis Windows`.
342+ #[ test]
343+ fn test_axis_windows_with_stride_1d ( )
344+ {
345+ let a = Array :: from_iter ( 10 ..20 ) . into_shape_with_order ( 10 ) . unwrap ( ) ;
346+
347+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 0 ) , 5 , 2 ) , vec ! [
348+ arr1( & [ 10 , 11 , 12 , 13 , 14 ] ) ,
349+ arr1( & [ 12 , 13 , 14 , 15 , 16 ] ) ,
350+ arr1( & [ 14 , 15 , 16 , 17 , 18 ] ) ,
351+ ] ) ;
352+
353+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 0 ) , 5 , 3 ) , vec ! [
354+ arr1( & [ 10 , 11 , 12 , 13 , 14 ] ) ,
355+ arr1( & [ 13 , 14 , 15 , 16 , 17 ] ) ,
356+ ] ) ;
357+ }
358+
359+ /// Simple test for iterating 2d-arrays via `Axis Windows`.
360+ #[ test]
361+ fn test_axis_windows_with_stride_2d ( )
362+ {
363+ let a = Array :: from_iter ( 10 ..30 )
364+ . into_shape_with_order ( ( 5 , 4 ) )
365+ . unwrap ( ) ;
366+
367+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 0 ) , 2 , 1 ) , vec ! [
368+ arr2( & [ [ 10 , 11 , 12 , 13 ] , [ 14 , 15 , 16 , 17 ] ] ) ,
369+ arr2( & [ [ 14 , 15 , 16 , 17 ] , [ 18 , 19 , 20 , 21 ] ] ) ,
370+ arr2( & [ [ 18 , 19 , 20 , 21 ] , [ 22 , 23 , 24 , 25 ] ] ) ,
371+ arr2( & [ [ 22 , 23 , 24 , 25 ] , [ 26 , 27 , 28 , 29 ] ] ) ,
372+ ] ) ;
373+
374+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 0 ) , 2 , 2 ) , vec ! [
375+ arr2( & [ [ 10 , 11 , 12 , 13 ] , [ 14 , 15 , 16 , 17 ] ] ) ,
376+ arr2( & [ [ 18 , 19 , 20 , 21 ] , [ 22 , 23 , 24 , 25 ] ] ) ,
377+ ] ) ;
378+
379+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 0 ) , 2 , 3 ) , vec ! [
380+ arr2( & [ [ 10 , 11 , 12 , 13 ] , [ 14 , 15 , 16 , 17 ] ] ) ,
381+ arr2( & [ [ 22 , 23 , 24 , 25 ] , [ 26 , 27 , 28 , 29 ] ] ) ,
382+ ] ) ;
383+ }
384+
385+ /// Simple test for iterating 3d-arrays via `Axis Windows`.
386+ #[ test]
387+ fn test_axis_windows_with_stride_3d ( )
388+ {
389+ let a = Array :: from_iter ( 0 ..27 )
390+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
391+ . unwrap ( ) ;
392+
393+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 1 ) , 2 , 1 ) , vec ! [
394+ arr3( & [
395+ [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] ] ,
396+ [ [ 9 , 10 , 11 ] , [ 12 , 13 , 14 ] ] ,
397+ [ [ 18 , 19 , 20 ] , [ 21 , 22 , 23 ] ] ,
398+ ] ) ,
399+ arr3( & [
400+ [ [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] ,
401+ [ [ 12 , 13 , 14 ] , [ 15 , 16 , 17 ] ] ,
402+ [ [ 21 , 22 , 23 ] , [ 24 , 25 , 26 ] ] ,
403+ ] ) ,
404+ ] ) ;
405+
406+ itertools:: assert_equal ( a. axis_windows_with_stride ( Axis ( 1 ) , 2 , 2 ) , vec ! [
407+ arr3( & [
408+ [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] ] ,
409+ [ [ 9 , 10 , 11 ] , [ 12 , 13 , 14 ] ] ,
410+ [ [ 18 , 19 , 20 ] , [ 21 , 22 , 23 ] ] ,
411+ ] ) ,
412+ ] ) ;
413+ }
414+
415+ #[ test]
416+ fn tests_axis_windows_with_stride_3d_zips_with_1d ( )
417+ {
418+ let a = Array :: from_iter ( 0 ..27 )
419+ . into_shape_with_order ( ( 3 , 3 , 3 ) )
420+ . unwrap ( ) ;
421+ let mut b1 = Array :: zeros ( 2 ) ;
422+ let mut b2 = Array :: zeros ( 1 ) ;
423+
424+ Zip :: from ( b1. view_mut ( ) )
425+ . and ( a. axis_windows_with_stride ( Axis ( 1 ) , 2 , 1 ) )
426+ . for_each ( |b, a| {
427+ * b = a. sum ( ) ;
428+ } ) ;
429+ assert_eq ! ( b1, arr1( & [ 207 , 261 ] ) ) ;
430+
431+ Zip :: from ( b2. view_mut ( ) )
432+ . and ( a. axis_windows_with_stride ( Axis ( 1 ) , 2 , 2 ) )
433+ . for_each ( |b, a| {
434+ * b = a. sum ( ) ;
435+ } ) ;
436+ assert_eq ! ( b2, arr1( & [ 207 ] ) ) ;
437+ }
438+
297439#[ test]
298440fn test_window_neg_stride ( )
299441{
0 commit comments