@@ -7923,6 +7923,44 @@ def test_uintalignment_and_alignment():
7923
7923
dst = np .zeros ((2 ,2 ), dtype = 'c8' )
7924
7924
dst [:,1 ] = src [:,1 ] # assert in lowlevel_strided_loops fails?
7925
7925
7926
+ class TestAlignment (object ):
7927
+ # adapted from scipy._lib.tests.test__util.test__aligned_zeros
7928
+ # Checks that unusual memory alignments don't trip up numpy.
7929
+ # In particular, check RELAXED_STRIDES don't trip alignment assertions in
7930
+ # NDEBUG mode for size-0 arrays (gh-12503)
7931
+
7932
+ def check (self , shape , dtype , order , align ):
7933
+ err_msg = repr ((shape , dtype , order , align ))
7934
+ x = _aligned_zeros (shape , dtype , order , align = align )
7935
+ if align is None :
7936
+ align = np .dtype (dtype ).alignment
7937
+ assert_equal (x .__array_interface__ ['data' ][0 ] % align , 0 )
7938
+ if hasattr (shape , '__len__' ):
7939
+ assert_equal (x .shape , shape , err_msg )
7940
+ else :
7941
+ assert_equal (x .shape , (shape ,), err_msg )
7942
+ assert_equal (x .dtype , dtype )
7943
+ if order == "C" :
7944
+ assert_ (x .flags .c_contiguous , err_msg )
7945
+ elif order == "F" :
7946
+ if x .size > 0 :
7947
+ assert_ (x .flags .f_contiguous , err_msg )
7948
+ elif order is None :
7949
+ assert_ (x .flags .c_contiguous , err_msg )
7950
+ else :
7951
+ raise ValueError ()
7952
+
7953
+ def test_various_alignments (self ):
7954
+ for align in [1 , 2 , 3 , 4 , 8 , 16 , 32 , 64 , None ]:
7955
+ for n in [0 , 1 , 3 , 11 ]:
7956
+ for order in ["C" , "F" , None ]:
7957
+ for dtype in np .typecodes ["All" ]:
7958
+ if dtype == 'O' :
7959
+ # object dtype can't be misaligned
7960
+ continue
7961
+ for shape in [n , (1 , 2 , 3 , n )]:
7962
+ self .check (shape , np .dtype (dtype ), order , align )
7963
+
7926
7964
def test_getfield ():
7927
7965
a = np .arange (32 , dtype = 'uint16' )
7928
7966
if sys .byteorder == 'little' :
0 commit comments