Skip to content

Commit 160201f

Browse files
committed
add basic test for stride constructor
1 parent 5e03afb commit 160201f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/dimension.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ pub fn stride_is_positive(stride: Ix) -> bool
2020
/// Check whether the given dimension and strides are memory safe
2121
/// to index the provided slice.
2222
///
23-
/// To be safe, no stride may be negative, and the
23+
/// To be safe, no stride may be negative, and the offset corresponding
24+
/// to the last element of each dimension should be smaller than the length
25+
/// of the slice. Also, the strides should not allow a same element to be
26+
/// referenced by two different index.
2427
pub fn can_index_slice<A, D: Dimension>(data: &[A],
2528
dim: &D,
2629
strides: &D

tests/dimension.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use ndarray::{
44
Array,
55
RemoveAxis,
66
arr2,
7+
can_index_slice
78
};
89

910
#[test]
@@ -36,3 +37,14 @@ fn dyn_dimension()
3637
assert_eq!(z.shape(), &dim[..]);
3738
}
3839

40+
#[test]
41+
fn slice_indexing_uncommon_strides()
42+
{
43+
let v: Vec<_> = (0..12).collect();
44+
let dim = (2, 3, 2);
45+
let strides = (1, 2, 6);
46+
assert!(can_index_slice(&v, &dim, &strides));
47+
48+
let strides = (2, 4, 12);
49+
assert!(!can_index_slice(&v, &dim, &strides));
50+
}

0 commit comments

Comments
 (0)