1
+
2
+ extern crate ndarray;
3
+ extern crate itertools;
4
+
5
+ use ndarray:: prelude:: * ;
6
+
7
+ // Edge Cases for Windows iterator:
8
+ //
9
+ // - window size is 0
10
+ // - what is the behaviour of the standard for this situation?
11
+ // "Panics if size is 0."
12
+ // - window size of 1
13
+ // - should a warning be printed?
14
+ // - what is the behaviour of the standard for this situation?
15
+ // => No overlapping for size-1-windows but normal behaviour besides that.
16
+ // - window size bigger than actual array size
17
+ // - ragged windows or panic?
18
+ // - what is the behaviour of the standard for this situation?
19
+ // "If the slice is shorter than size, the iterator returns no values."
20
+
21
+ /// Test that verifies the `Windows` iterator panics on window sizes equal to zero.
22
+ #[ test]
23
+ #[ should_panic]
24
+ fn windows_iterator_zero_size ( ) {
25
+ let a = Array :: from_iter ( 10 ..37 )
26
+ . into_shape ( ( 3 , 3 , 3 ) )
27
+ . unwrap ( ) ;
28
+ a. windows ( Dim ( ( 0 , 0 , 0 ) ) ) ;
29
+ }
30
+
31
+ /// Test that verifites that no windows are yielded on oversized window sizes.
32
+ #[ test]
33
+ fn windows_iterator_oversized ( ) {
34
+ let a = Array :: from_iter ( 10 ..37 )
35
+ . into_shape ( ( 3 , 3 , 3 ) )
36
+ . unwrap ( ) ;
37
+ let mut iter = a. windows ( Dim ( ( 4 , 3 , 2 ) ) ) ; // (4,3,2) doesn't fit into (3,3,3) => oversized!
38
+ assert_eq ! ( iter. next( ) , None ) ;
39
+ }
40
+
41
+ /// Simple test for iterating 1d-arrays via `Windows`.
42
+ #[ test]
43
+ fn windows_iterator_1d ( ) {
44
+ let a = Array :: from_iter ( 10 ..20 ) . into_shape ( 10 ) . unwrap ( ) ;
45
+ itertools:: assert_equal (
46
+ a. windows ( Dim ( 4 ) ) ,
47
+ vec ! [
48
+ arr1( & [ 10 , 11 , 12 , 13 ] ) ,
49
+ arr1( & [ 11 , 12 , 13 , 14 ] ) ,
50
+ arr1( & [ 12 , 13 , 14 , 15 ] ) ,
51
+ arr1( & [ 13 , 14 , 15 , 16 ] ) ,
52
+ arr1( & [ 14 , 15 , 16 , 17 ] ) ,
53
+ arr1( & [ 15 , 16 , 17 , 18 ] ) ,
54
+ arr1( & [ 16 , 17 , 18 , 19 ] )
55
+ ] ) ;
56
+ }
57
+
58
+ /// Simple test for iterating 2d-arrays via `Windows`.
59
+ #[ test]
60
+ fn windows_iterator_2d ( ) {
61
+ let a = Array :: from_iter ( 10 ..30 ) . into_shape ( ( 5 , 4 ) ) . unwrap ( ) ;
62
+ itertools:: assert_equal (
63
+ a. windows ( Dim ( ( 3 , 2 ) ) ) ,
64
+ vec ! [
65
+ arr2( & [ [ 10 , 11 ] , [ 14 , 15 ] , [ 18 , 19 ] ] ) ,
66
+ arr2( & [ [ 11 , 12 ] , [ 15 , 16 ] , [ 19 , 20 ] ] ) ,
67
+ arr2( & [ [ 12 , 13 ] , [ 16 , 17 ] , [ 20 , 21 ] ] ) ,
68
+
69
+ arr2( & [ [ 14 , 15 ] , [ 18 , 19 ] , [ 22 , 23 ] ] ) ,
70
+ arr2( & [ [ 15 , 16 ] , [ 19 , 20 ] , [ 23 , 24 ] ] ) ,
71
+ arr2( & [ [ 16 , 17 ] , [ 20 , 21 ] , [ 24 , 25 ] ] ) ,
72
+
73
+ arr2( & [ [ 18 , 19 ] , [ 22 , 23 ] , [ 26 , 27 ] ] ) ,
74
+ arr2( & [ [ 19 , 20 ] , [ 23 , 24 ] , [ 27 , 28 ] ] ) ,
75
+ arr2( & [ [ 20 , 21 ] , [ 24 , 25 ] , [ 28 , 29 ] ] )
76
+ ] ) ;
77
+ }
78
+
79
+ /// Simple test for iterating 3d-arrays via `Windows`.
80
+ #[ test]
81
+ fn windows_iterator_3d ( ) {
82
+ use ndarray:: arr3;
83
+ let a = Array :: from_iter ( 10 ..37 ) . into_shape ( ( 3 , 3 , 3 ) ) . unwrap ( ) ;
84
+ itertools:: assert_equal (
85
+ a. windows ( Dim ( ( 2 , 2 , 2 ) ) ) ,
86
+ vec ! [
87
+ arr3( & [ [ [ 10 , 11 ] , [ 13 , 14 ] ] , [ [ 19 , 20 ] , [ 22 , 23 ] ] ] ) ,
88
+ arr3( & [ [ [ 11 , 12 ] , [ 14 , 15 ] ] , [ [ 20 , 21 ] , [ 23 , 24 ] ] ] ) ,
89
+
90
+ arr3( & [ [ [ 13 , 14 ] , [ 16 , 17 ] ] , [ [ 22 , 23 ] , [ 25 , 26 ] ] ] ) ,
91
+ arr3( & [ [ [ 14 , 15 ] , [ 17 , 18 ] ] , [ [ 23 , 24 ] , [ 26 , 27 ] ] ] ) ,
92
+
93
+ arr3( & [ [ [ 19 , 20 ] , [ 22 , 23 ] ] , [ [ 28 , 29 ] , [ 31 , 32 ] ] ] ) ,
94
+ arr3( & [ [ [ 20 , 21 ] , [ 23 , 24 ] ] , [ [ 29 , 30 ] , [ 32 , 33 ] ] ] ) ,
95
+
96
+ arr3( & [ [ [ 22 , 23 ] , [ 25 , 26 ] ] , [ [ 31 , 32 ] , [ 34 , 35 ] ] ] ) ,
97
+ arr3( & [ [ [ 23 , 24 ] , [ 26 , 27 ] ] , [ [ 32 , 33 ] , [ 35 , 36 ] ] ] ) ,
98
+ ] ) ;
99
+ }
0 commit comments