Skip to content

Commit 24ea27c

Browse files
committed
ENH: Improve assertion message for .subview() oob
1 parent 013dcb1 commit 24ea27c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/dimension/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ pub fn do_sub<A, D: Dimension>(dims: &mut D, ptr: &mut *mut A, strides: &D,
208208
axis: usize, index: Ix) {
209209
let dim = dims.slice()[axis];
210210
let stride = strides.slice()[axis];
211-
assert!(index < dim);
211+
ndassert!(index < dim,
212+
concat!("subview: Index {} must be less than axis length {} ",
213+
"for array with shape {:?}"),
214+
index, dim, *dims);
212215
dims.slice_mut()[axis] = 1;
213216
let off = stride_offset(index, stride);
214217
unsafe {

tests/array.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ fn test_sub()
233233
assert_eq!(m, mat.subview(Axis(1), 1));
234234
}
235235

236+
#[should_panic]
237+
#[test]
238+
fn test_sub_oob_1() {
239+
let mat = RcArray::linspace(0., 15., 16).reshape((2, 4, 2));
240+
let s1 = mat.subview(Axis(0), 2);
241+
}
242+
236243

237244
#[test]
238245
fn test_select(){

0 commit comments

Comments
 (0)