Skip to content

Commit 4f5c305

Browse files
committed
MAINT: Add tricky copy on write test
Test cow with an array that changes strides when it is unsharing itself.
1 parent 325bcf3 commit 4f5c305

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/array.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,41 @@ fn test_cow()
219219
assert_eq!(before[3], 1);
220220
}
221221

222+
#[test]
223+
fn test_cow_shrink()
224+
{
225+
// A test for clone-on-write in the case that
226+
// mutation shrinks the array and gives it different strides
227+
//
228+
let mut mat = RcArray::zeros((2, 3));
229+
//mat.islice(s![.., ..;2]);
230+
mat[[0, 0]] = 1;
231+
let n = mat.clone();
232+
mat[[0, 1]] = 2;
233+
mat[[0, 2]] = 3;
234+
mat[[1, 0]] = 4;
235+
mat[[1, 1]] = 5;
236+
mat[[1, 2]] = 6;
237+
assert_eq!(mat[[0, 0]], 1);
238+
assert_eq!(mat[[0, 1]], 2);
239+
assert_eq!(n[[0, 0]], 1);
240+
assert_eq!(n[[0, 1]], 0);
241+
assert_eq!(n.get((0, 1)), Some(&0));
242+
// small has non-C strides this way
243+
let mut small = mat.reshape(6);
244+
small.islice(s![4..;-1]);
245+
assert_eq!(small[0], 6);
246+
assert_eq!(small[1], 5);
247+
let before = small.clone();
248+
// mutation
249+
// small gets back C strides in CoW.
250+
small[1] = 9;
251+
assert_eq!(small[0], 6);
252+
assert_eq!(small[1], 9);
253+
assert_eq!(before[0], 6);
254+
assert_eq!(before[1], 5);
255+
}
256+
222257
#[test]
223258
fn test_sub()
224259
{

0 commit comments

Comments
 (0)