@@ -1827,6 +1827,27 @@ fn map_memory_order() {
1827
1827
assert_eq ! ( amap. strides( ) , v. strides( ) ) ;
1828
1828
}
1829
1829
1830
+ #[ test]
1831
+ fn test_view_from_shape ( ) {
1832
+ let s = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ;
1833
+ let a = ArrayView :: from_shape ( ( 2 , 3 , 2 ) , & s) . unwrap ( ) ;
1834
+ let mut answer = Array :: from ( s. to_vec ( ) ) . into_shape ( ( 2 , 3 , 2 ) ) . unwrap ( ) ;
1835
+ assert_eq ! ( a, answer) ;
1836
+
1837
+ // custom strides (row major)
1838
+ let a = ArrayView :: from_shape ( ( 2 , 3 , 2 ) . strides ( ( 6 , 2 , 1 ) ) , & s) . unwrap ( ) ;
1839
+ assert_eq ! ( a, answer) ;
1840
+
1841
+ // custom strides (col major)
1842
+ let a = ArrayView :: from_shape ( ( 2 , 3 , 2 ) . strides ( ( 1 , 2 , 6 ) ) , & s) . unwrap ( ) ;
1843
+ assert_eq ! ( a, answer. t( ) ) ;
1844
+
1845
+ // negative strides
1846
+ let a = ArrayView :: from_shape ( ( 2 , 3 , 2 ) . strides ( ( 6 , ( -2isize ) as usize , 1 ) ) , & s) . unwrap ( ) ;
1847
+ answer. invert_axis ( Axis ( 1 ) ) ;
1848
+ assert_eq ! ( a, answer) ;
1849
+ }
1850
+
1830
1851
#[ test]
1831
1852
fn test_contiguous ( ) {
1832
1853
let c = arr3 ( & [ [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , [ [ 4 , 5 , 6 ] , [ 7 , 7 , 7 ] ] ] ) ;
@@ -1973,6 +1994,46 @@ fn test_view_from_shape_ptr() {
1973
1994
assert_eq ! ( view, aview2( & [ [ 0 , 0 , 2 ] , [ 3 , 4 , 6 ] ] ) ) ;
1974
1995
}
1975
1996
1997
+ #[ should_panic( expected = "Unsupported" ) ]
1998
+ #[ cfg( debug_assertions) ]
1999
+ #[ test]
2000
+ fn test_view_from_shape_ptr_deny_neg_strides ( ) {
2001
+ let data = [ 0 , 1 , 2 , 3 , 4 , 5 ] ;
2002
+ let _view = unsafe {
2003
+ ArrayView :: from_shape_ptr ( ( 2 , 3 ) . strides ( ( -3isize as usize , 1 ) ) , data. as_ptr ( ) )
2004
+ } ;
2005
+ }
2006
+
2007
+ #[ should_panic( expected = "Unsupported" ) ]
2008
+ #[ cfg( debug_assertions) ]
2009
+ #[ test]
2010
+ fn test_view_mut_from_shape_ptr_deny_neg_strides ( ) {
2011
+ let mut data = [ 0 , 1 , 2 , 3 , 4 , 5 ] ;
2012
+ let _view = unsafe {
2013
+ ArrayViewMut :: from_shape_ptr ( ( 2 , 3 ) . strides ( ( -3isize as usize , 1 ) ) , data. as_mut_ptr ( ) )
2014
+ } ;
2015
+ }
2016
+
2017
+ #[ should_panic( expected = "Unsupported" ) ]
2018
+ #[ cfg( debug_assertions) ]
2019
+ #[ test]
2020
+ fn test_raw_view_from_shape_ptr_deny_neg_strides ( ) {
2021
+ let data = [ 0 , 1 , 2 , 3 , 4 , 5 ] ;
2022
+ let _view = unsafe {
2023
+ RawArrayView :: from_shape_ptr ( ( 2 , 3 ) . strides ( ( -3isize as usize , 1 ) ) , data. as_ptr ( ) )
2024
+ } ;
2025
+ }
2026
+
2027
+ #[ should_panic( expected = "Unsupported" ) ]
2028
+ #[ cfg( debug_assertions) ]
2029
+ #[ test]
2030
+ fn test_raw_view_mut_from_shape_ptr_deny_neg_strides ( ) {
2031
+ let mut data = [ 0 , 1 , 2 , 3 , 4 , 5 ] ;
2032
+ let _view = unsafe {
2033
+ RawArrayViewMut :: from_shape_ptr ( ( 2 , 3 ) . strides ( ( -3isize as usize , 1 ) ) , data. as_mut_ptr ( ) )
2034
+ } ;
2035
+ }
2036
+
1976
2037
#[ test]
1977
2038
fn test_default ( ) {
1978
2039
let a = <Array < f32 , Ix2 > as Default >:: default ( ) ;
0 commit comments