@@ -990,8 +990,8 @@ fn map1() {
990990}
991991
992992#[ test]
993- fn as_slice_memory_order ( ) {
994- // test that mutation breaks sharing
993+ fn as_slice_memory_order_mut_arcarray ( ) {
994+ // Test that mutation breaks sharing for `ArcArray`.
995995 let a = rcarr2 ( & [ [ 1. , 2. ] , [ 3. , 4.0f32 ] ] ) ;
996996 let mut b = a. clone ( ) ;
997997 for elt in b. as_slice_memory_order_mut ( ) . unwrap ( ) {
@@ -1000,6 +1000,38 @@ fn as_slice_memory_order() {
10001000 assert ! ( a != b, "{:?} != {:?}" , a, b) ;
10011001}
10021002
1003+ #[ test]
1004+ fn as_slice_memory_order_mut_cowarray ( ) {
1005+ // Test that mutation breaks sharing for `CowArray`.
1006+ let a = arr2 ( & [ [ 1. , 2. ] , [ 3. , 4.0f32 ] ] ) ;
1007+ let mut b = CowArray :: from ( a. view ( ) ) ;
1008+ for elt in b. as_slice_memory_order_mut ( ) . unwrap ( ) {
1009+ * elt = 0. ;
1010+ }
1011+ assert ! ( a != b, "{:?} != {:?}" , a, b) ;
1012+ }
1013+
1014+ #[ test]
1015+ fn as_slice_memory_order_mut_contiguous_arcarray ( ) {
1016+ // Test that unsharing preserves the strides in the contiguous case for `ArcArray`.
1017+ let a = rcarr2 ( & [ [ 0 , 5 ] , [ 1 , 6 ] , [ 2 , 7 ] , [ 3 , 8 ] , [ 4 , 9 ] ] ) . reversed_axes ( ) ;
1018+ let mut b = a. clone ( ) . slice_move ( s ! [ .., ..2 ] ) ;
1019+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1020+ b. as_slice_memory_order_mut ( ) . unwrap ( ) ;
1021+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1022+ }
1023+
1024+ #[ test]
1025+ fn as_slice_memory_order_mut_contiguous_cowarray ( ) {
1026+ // Test that unsharing preserves the strides in the contiguous case for `CowArray`.
1027+ let a = arr2 ( & [ [ 0 , 5 ] , [ 1 , 6 ] , [ 2 , 7 ] , [ 3 , 8 ] , [ 4 , 9 ] ] ) . reversed_axes ( ) ;
1028+ let mut b = CowArray :: from ( a. slice ( s ! [ .., ..2 ] ) ) ;
1029+ assert ! ( b. is_view( ) ) ;
1030+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1031+ b. as_slice_memory_order_mut ( ) . unwrap ( ) ;
1032+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1033+ }
1034+
10031035#[ test]
10041036fn array0_into_scalar ( ) {
10051037 // With this kind of setup, the `Array`'s pointer is not the same as the
@@ -1788,6 +1820,33 @@ fn map_memory_order() {
17881820 assert_eq ! ( amap. strides( ) , v. strides( ) ) ;
17891821}
17901822
1823+ #[ test]
1824+ fn map_mut_with_unsharing ( ) {
1825+ // Fortran-layout `ArcArray`.
1826+ let a = rcarr2 ( & [ [ 0 , 5 ] , [ 1 , 6 ] , [ 2 , 7 ] , [ 3 , 8 ] , [ 4 , 9 ] ] ) . reversed_axes ( ) ;
1827+ assert_eq ! ( a. shape( ) , & [ 2 , 5 ] ) ;
1828+ assert_eq ! ( a. strides( ) , & [ 1 , 2 ] ) ;
1829+ assert_eq ! (
1830+ a. as_slice_memory_order( ) ,
1831+ Some ( & [ 0 , 5 , 1 , 6 , 2 , 7 , 3 , 8 , 4 , 9 ] [ ..] )
1832+ ) ;
1833+
1834+ // Shared reference of a portion of `a`.
1835+ let mut b = a. clone ( ) . slice_move ( s ! [ .., ..2 ] ) ;
1836+ assert_eq ! ( b. shape( ) , & [ 2 , 2 ] ) ;
1837+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1838+ assert_eq ! ( b. as_slice_memory_order( ) , Some ( & [ 0 , 5 , 1 , 6 ] [ ..] ) ) ;
1839+ assert_eq ! ( b, array![ [ 0 , 1 ] , [ 5 , 6 ] ] ) ;
1840+
1841+ // `.map_mut()` unshares the data. Earlier versions of `ndarray` failed
1842+ // this assertion. See #1018.
1843+ assert_eq ! ( b. map_mut( |& mut x| x + 10 ) , array![ [ 10 , 11 ] , [ 15 , 16 ] ] ) ;
1844+
1845+ // The strides should be preserved.
1846+ assert_eq ! ( b. shape( ) , & [ 2 , 2 ] ) ;
1847+ assert_eq ! ( b. strides( ) , & [ 1 , 2 ] ) ;
1848+ }
1849+
17911850#[ test]
17921851fn test_view_from_shape ( ) {
17931852 let s = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ;
0 commit comments