Skip to content

Commit 107f51f

Browse files
committed
FIX: in remove_axis(axis, index) specify we need index < len_of(axis)
Removing the index-past-the end does not make sense (it's an ok split index, so the previous code passed through without action).
1 parent c568124 commit 107f51f

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/impl_methods.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,13 +2446,21 @@ where
24462446

24472447
/// Remove the `index`th elements along `axis` and shift down elements from higher indexes.
24482448
///
2449+
/// Note that this "removes" the elements by swapping them around to the end of the axis and
2450+
/// shortening the length of the axis; the elements are not deinitialized or dropped by this,
2451+
/// just moved out of view (this only matters for elements with ownership semantics). It's
2452+
/// similar to slicing an owned array in place.
2453+
///
24492454
/// Decreases the length of `axis` by one.
24502455
///
2451-
/// ***Panics** if `axis` or `index` is out of bounds.
2456+
/// ***Panics*** if `axis` is out of bounds<br>
2457+
/// ***Panics*** if not `index < self.len_of(axis)`.
24522458
pub fn remove_index(&mut self, axis: Axis, index: usize)
24532459
where
24542460
S: DataOwned + DataMut,
24552461
{
2462+
assert!(index < self.len_of(axis), "index {} must be less than length of Axis({})",
2463+
index, axis.index());
24562464
let (_, mut tail) = self.view_mut().split_at(axis, index);
24572465
// shift elements to the front
24582466
// use swapping to keep all elements initialized (as required by owned storage)

tests/array.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,3 +2439,37 @@ fn test_remove_index() {
24392439
[],
24402440
[]]);
24412441
}
2442+
2443+
#[should_panic(expected="must be less")]
2444+
#[test]
2445+
fn test_remove_index_oob1() {
2446+
let mut a = arr2(&[[1, 2, 3],
2447+
[4, 5, 6],
2448+
[7, 8, 9],
2449+
[10,11,12]]);
2450+
a.remove_index(Axis(0), 4);
2451+
}
2452+
2453+
#[should_panic(expected="must be less")]
2454+
#[test]
2455+
fn test_remove_index_oob2() {
2456+
let mut a = array![[10], [4], [1]];
2457+
a.remove_index(Axis(1), 0);
2458+
assert_eq!(a.shape(), &[3, 0]);
2459+
assert_eq!(a,
2460+
array![[],
2461+
[],
2462+
[]]);
2463+
a.remove_index(Axis(0), 1); // ok
2464+
assert_eq!(a,
2465+
array![[],
2466+
[]]);
2467+
a.remove_index(Axis(1), 0); // oob
2468+
}
2469+
2470+
#[should_panic(expected="index out of bounds")]
2471+
#[test]
2472+
fn test_remove_index_oob3() {
2473+
let mut a = array![[10], [4], [1]];
2474+
a.remove_index(Axis(2), 0);
2475+
}

0 commit comments

Comments
 (0)