Skip to content

Commit 1304f9d

Browse files
committed
Fix uniqueness in last_mut()
Last mut did not ensure the array was unique before calling uget_mut. The required properties were protected by a debug assertion, but a clear bug in release mode. Adding tests that would have caught this.
1 parent 6f77377 commit 1304f9d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/impl_methods.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ where
406406
if self.is_empty() {
407407
None
408408
} else {
409+
self.ensure_unique();
409410
let mut index = self.raw_dim();
410411
for ax in 0..index.ndim() {
411412
index[ax] -= 1;
@@ -3081,6 +3082,7 @@ mod tests
30813082
{
30823083
use super::*;
30833084
use crate::arr3;
3085+
use defmac::defmac;
30843086

30853087
#[test]
30863088
fn test_flatten()
@@ -3107,4 +3109,45 @@ mod tests
31073109
let flattened = array.into_flat();
31083110
assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
31093111
}
3112+
3113+
#[test]
3114+
fn test_first_last()
3115+
{
3116+
let first = 2;
3117+
let last = 3;
3118+
3119+
defmac!(assert_first mut array => {
3120+
assert_eq!(array.first().copied(), Some(first));
3121+
assert_eq!(array.first_mut().copied(), Some(first));
3122+
});
3123+
defmac!(assert_last mut array => {
3124+
assert_eq!(array.last().copied(), Some(last));
3125+
assert_eq!(array.last_mut().copied(), Some(last));
3126+
});
3127+
3128+
let base = Array::from_vec(vec![first, last]);
3129+
let a = base.clone();
3130+
assert_first!(a);
3131+
3132+
let a = base.clone();
3133+
assert_last!(a);
3134+
3135+
let a = CowArray::from(base.view());
3136+
assert_first!(a);
3137+
let a = CowArray::from(base.view());
3138+
assert_last!(a);
3139+
3140+
let a = CowArray::from(base.clone());
3141+
assert_first!(a);
3142+
let a = CowArray::from(base.clone());
3143+
assert_last!(a);
3144+
3145+
let a = ArcArray::from(base.clone());
3146+
let _a2 = a.clone();
3147+
assert_last!(a);
3148+
3149+
let a = ArcArray::from(base.clone());
3150+
let _a2 = a.clone();
3151+
assert_first!(a);
3152+
}
31103153
}

0 commit comments

Comments
 (0)