Skip to content

Commit 632aa3f

Browse files
committed
Add test for offset computation in empty array
The first of the added tests failed because of bug rust-ndarray#998, and the fix is verified by the test. The second and third testcases did not have an error, but were added for completeness.
1 parent d155d84 commit 632aa3f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/array-construct.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use defmac::defmac;
99
use ndarray::prelude::*;
10+
use ndarray::arr3;
1011
use ndarray::Zip;
1112

1213
#[test]
@@ -164,6 +165,43 @@ fn test_ones() {
164165
assert_eq!(a, b);
165166
}
166167

168+
#[test]
169+
fn test_from_shape_empty_with_neg_stride() {
170+
// Issue #998, negative strides for an axis where it doesn't matter.
171+
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
172+
let v = s[..12].to_vec();
173+
let v_ptr = v.as_ptr();
174+
let a = Array::from_shape_vec((2, 0, 2).strides((1, -4isize as usize, 2)), v).unwrap();
175+
assert_eq!(a, arr3(&[[[0; 2]; 0]; 2]));
176+
assert_eq!(a.as_ptr(), v_ptr);
177+
}
178+
179+
#[test]
180+
fn test_from_shape_with_neg_stride() {
181+
// Issue #998, negative strides for an axis where it doesn't matter.
182+
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
183+
let v = s[..12].to_vec();
184+
let v_ptr = v.as_ptr();
185+
let a = Array::from_shape_vec((2, 1, 2).strides((1, -4isize as usize, 2)), v).unwrap();
186+
assert_eq!(a, arr3(&[[[0, 2]],
187+
[[1, 3]]]));
188+
assert_eq!(a.as_ptr(), v_ptr);
189+
}
190+
191+
#[test]
192+
fn test_from_shape_2_2_2_with_neg_stride() {
193+
// Issue #998, negative strides for an axis where it doesn't matter.
194+
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
195+
let v = s[..12].to_vec();
196+
let v_ptr = v.as_ptr();
197+
let a = Array::from_shape_vec((2, 2, 2).strides((1, -4isize as usize, 2)), v).unwrap();
198+
assert_eq!(a, arr3(&[[[4, 6],
199+
[0, 2]],
200+
[[5, 7],
201+
[1, 3]]]));
202+
assert_eq!(a.as_ptr(), v_ptr.wrapping_add(4));
203+
}
204+
167205
#[should_panic]
168206
#[test]
169207
fn deny_wraparound_zeros() {

0 commit comments

Comments
 (0)