Skip to content

Commit

Permalink
Add test for offset computation in empty array
Browse files Browse the repository at this point in the history
The first of the added tests failed because of bug #998, and the fix is
verified by the test. The second and third testcases did not have an
error, but were added for completeness.

The first testcase is by SparrowLii, from the initial mention of the
bug.
  • Loading branch information
bluss committed May 13, 2021
1 parent d155d84 commit b2873db
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/array-construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use defmac::defmac;
use ndarray::prelude::*;
use ndarray::arr3;
use ndarray::Zip;

#[test]
Expand Down Expand Up @@ -164,6 +165,43 @@ fn test_ones() {
assert_eq!(a, b);
}

#[test]
fn test_from_shape_empty_with_neg_stride() {
// Issue #998, negative strides for an axis where it doesn't matter.
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
let v = s[..12].to_vec();
let v_ptr = v.as_ptr();
let a = Array::from_shape_vec((2, 0, 2).strides((1, -4isize as usize, 2)), v).unwrap();
assert_eq!(a, arr3(&[[[0; 2]; 0]; 2]));
assert_eq!(a.as_ptr(), v_ptr);
}

#[test]
fn test_from_shape_with_neg_stride() {
// Issue #998, negative strides for an axis where it doesn't matter.
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
let v = s[..12].to_vec();
let v_ptr = v.as_ptr();
let a = Array::from_shape_vec((2, 1, 2).strides((1, -4isize as usize, 2)), v).unwrap();
assert_eq!(a, arr3(&[[[0, 2]],
[[1, 3]]]));
assert_eq!(a.as_ptr(), v_ptr);
}

#[test]
fn test_from_shape_2_2_2_with_neg_stride() {
// Issue #998, negative strides for an axis where it doesn't matter.
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
let v = s[..12].to_vec();
let v_ptr = v.as_ptr();
let a = Array::from_shape_vec((2, 2, 2).strides((1, -4isize as usize, 2)), v).unwrap();
assert_eq!(a, arr3(&[[[4, 6],
[0, 2]],
[[5, 7],
[1, 3]]]));
assert_eq!(a.as_ptr(), v_ptr.wrapping_add(4));
}

#[should_panic]
#[test]
fn deny_wraparound_zeros() {
Expand Down

0 comments on commit b2873db

Please sign in to comment.