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