o 6al@sdZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m ZdZedjedjkrDedd d d Zd d eDZd dZddZGdddejZddZeZGdddZGdddZdS)z The tests exercise the casting machinery in a more low-level manner. The reason is mostly to test a new implementation of the casting machinery. Unlike most tests in NumPy, these are closer to unit-tests rather than integration tests. N) as_strided)assert_array_equal)_get_castingimplz?bhilqBHILQefdFDlqLcCg|] }tt|qStypenpdtype.0cr r I/usr/lib/python3/dist-packages/numpy/core/tests/test_casting_unittests.py rccsPtD]"}|}tj|t|dV|jdkr%|}tj|t|dVqdS)N)id|) simple_dtypespytestparamstr byteorder newbyteorder) dtype_classdtr r rsimple_dtype_instancess rcCs|tjkrdS|jdvr>|jdkrd}n|jdkrd}n|jdkr$d}n|jdkr,d }ntd ||jd kr<|d7}|S|jd krEd S|jdkrLdS|jdkrSdS|jdkrZdStd |)zHReturns the string length when casting the basic dtypes to strings. iu z!did not find expected length for ig0G`f r@)r bool_kinditemsizeAssertionErrorchar)rlengthr r rget_expected_stringlength's0           r7c@s$eZdZdZdZdZdZdZdZdS)Castingrr"r$r#r%iN) __name__ __module__ __qualname__noequivsafe same_kindunsafe cast_is_viewr r r rr8Jsr8cCstdd}dd|ddddD}tjtjtjtjdd}i}t ||d dD]\}}i||<t ||dddD] \}}|||||<qAq/|S) Na X ? b h i l q B H I L Q e f d g F D G S U V O M m ? # = = = = = = = = = = = = = = = = = = = = = . = b . # = = = = . . . . . = = = = = = = = = = = . = h . ~ # = = = . . . . . ~ = = = = = = = = = = . = i . ~ ~ # = = . . . . . ~ ~ = = ~ = = = = = = . = l . ~ ~ ~ # # . . . . . ~ ~ = = ~ = = = = = = . = q . ~ ~ ~ # # . . . . . ~ ~ = = ~ = = = = = = . = B . ~ = = = = # = = = = = = = = = = = = = = = . = H . ~ ~ = = = ~ # = = = ~ = = = = = = = = = = . = I . ~ ~ ~ = = ~ ~ # = = ~ ~ = = ~ = = = = = = . = L . ~ ~ ~ ~ ~ ~ ~ ~ # # ~ ~ = = ~ = = = = = = . ~ Q . ~ ~ ~ ~ ~ ~ ~ ~ # # ~ ~ = = ~ = = = = = = . ~ e . . . . . . . . . . . # = = = = = = = = = = . . f . . . . . . . . . . . ~ # = = = = = = = = = . . d . . . . . . . . . . . ~ ~ # = ~ = = = = = = . . g . . . . . . . . . . . ~ ~ ~ # ~ ~ = = = = = . . F . . . . . . . . . . . . . . . # = = = = = = . . D . . . . . . . . . . . . . . . ~ # = = = = = . . G . . . . . . . . . . . . . . . ~ ~ # = = = = . . S . . . . . . . . . . . . . . . . . . # = = = . . U . . . . . . . . . . . . . . . . . . . # = = . . V . . . . . . . . . . . . . . . . . . . . # = . . O . . . . . . . . . . . . . . . . . . . . = # . . M . . . . . . . . . . . . . . . . . . . . = = # . m . . . . . . . . . . . . . . . . . . . . = = . #  cSr r r rr r rrorz&_get_cancast_table..rr$).~=# r") textwrapdedentstripsplitr8r@r?r>r=zip)tabledtypes convert_castcancastfrom_dtrowto_dtrr r r_get_cancast_tableSs rUc@sDeZdZdZejdddgejdgdddZd d Zd S) TestChangesz; These test cases excercise some behaviour changes stringSUfloating)er.dr*cCs*t||sJt||dsJdS)N100r can_cast)selfrZrWr r rtest_float_to_stringsz TestChanges.test_float_to_stringcCstddsJtddsJtddrJtddr Jtddr(Jtjdddds2Jtjd dd dsZ!ejd?d@dAgdBdCZ"ejdDej#dEdFdGZ$dS)I TestCastingic Cs |dus |j|jkr|j|j}n|j|j}tj||d}|jjs$J|jjs*Jddt|D}t|D]\}}|||<q7|durT|j dkrPdd|D}||fS|j dkr`dd|D}tj||d}|jjsmJ|jjssJt|D]\}}|||<qw|||fS)NrcSsg|]}tddqS)i)random randrange)r_r r rrsz(TestCasting.get_data..?cSg|]}t|qSr boolrvr r rrcSrvr rwryr r rrr{) r3sizer emptyflags c_contiguousalignedrange enumerater5) r`dtype1dtype2r6arr1valuesr)valuearr2r r rget_datas*          zTestCasting.get_dataTcCs,|r |jj}|jj}n|rd|jj}d|jj}n |jjd}|jjd}t|d|jjd}t|d|jjd}tj|tjd} tj|tjd} |t|| jksYJ|t|| jksdJ|rt| dd|j|j |f} t| dd|j|j |f} n t| dd|j|j |f} t| dd|j|j |f} || d<|s|r|jjnd} | | d| }| d t|ksJ|r| j j sJ| j j sJn | j j rJ| j j rJ|r| j jsJ| j jsJ| | fS| j jr| jjdksJ| j jr| jjdksJ| | fS) z Returns a copy of arr1 that may be non-contiguous or unaligned, and a matching array for arr2 (although not a copy). r$r"r#rpNrC.r)rr3lenr zerosuint8nbytesrviewshapetobytescountr~rr alignment)r`rrrcontigstride1stride2 max_size1 max_size2 from_bytesto_bytesnew1new2offsetbufr r rget_data_variationsX         zTestCasting.get_data_variationfrom_Dtc Cs2tD]}t||}||fD]}||dfdd}||ks%J~||fD]f}|||f\}\}} t||ksCJt| |ksKJ|tj@rn|tjtjBksZJtjt ||kseJ|j | j ksmJn||krz|j | j kszJ|t ||ksJ||ur||usJ|| usJq.qqdS)Nr") rget_castingimplr_resolve_descriptorsr r8rAr<r= CAST_TABLEisnative) r`rto_DtcastrRdefaultrTrgfrom_resto_resr r rtest_simple_cancasts4     zTestCasting.test_simple_cancastzignore::numpy.ComplexWarningrRc CstD]}|jd}tt|t|}|||f\}\}}||us&||ur)dS|tj@tjk}~~~|||\}} } | || f| | ksMJ| || dd\} } | | | ft | | | | ksmJ|jdkrw|jdksz|js}dS| || dd\} } | | | ft | | | | ksJ| || dd\} } | | | ft | | | | ksJ~ ~ ~qdS)a  This test checks numeric direct casts for dtypes supported also by the struct module (plus complex). It tries to be test a wide range of inputs, but skips over possibly undefined behaviour (e.g. int rollover). Longdouble and CLongdouble are tested, but only using double precision. If this test creates issues, it should possibly just be simplified or even removed (checking whether unaligned/non-contiguous casts give the same results is useful, though). rNTFr")rrrr rr8rAr>r_simple_strided_calltolistrrrr_supports_unaligned) r`rRrTrrgrrr>rrrarr1_oarr2_or r rtest_simple_direct_castss>     z$TestCasting.test_simple_direct_castsc Cs|}tdtdtdtdtdtdg}|D]}tt|t|}|||f\}\}}||us;J||usAJ~~|t|t|@sOJttj} ||| \} } } | |} t d| d<|tdkrd | d <| | | ft t t| d WddS1swYdS| | | fd d | D| ksJ|| | d d\} }| | |ft|| || ksJqdS)NM8M8[ms]M8[4D]m8m8[ms]m8[4D]NaT.rrCcSrvr )intryr r rrr{z5TestCasting.test_numeric_to_times..TF)r rrr rrint64rr datetime64rrraises ValueErrorrrrrr)r`rrR time_dtypestime_dtrrgrrint64_dtrrrrrr r rtest_numeric_to_times\sB       z!TestCasting.test_numeric_to_times)rRrTexpected_castingnomdenomzM8[ns]Nr"rrzM8[5ms]i@BzM8[7ms]rzM8[1M])rrC"!zm8[ns]rrzm8[5ms]zm8[7ms]rzm8[1M])rrrrrrcCst|}|durt|}tgd}|td|j}|jj|jks*Jt||ds6Jt|}t ||}| ||f\} \} } | |usPJ| |usZ|dusZJ| |ks`J|durr|||| } d| d<nt |} || d<| |} ||} t | }| t j kr|dks|dkrtt|| |fWddS1swYdSdD]'}dD]"}|| |||\}}d|d<|||ft|d| dqqdS) N)rr"li'ilrrr.rrTT)r rarrayastyperrisnatrr rr empty_liker8r@rrrrrr)r`rRrTrrrrDTyperrgrr expected_outorig_arrorig_outrrarroutr r rtest_time_to_timesL            zTestCasting.test_time_to_timecCs:|jdkrdnd}|j||}t|j|j|S)NrXr"r%)r5r3r rr)r`r change_lengthfactr6r r rstring_with_modified_lengthsz'TestCasting.string_with_modified_lengthother_DT string_charrXrYcCsN|dkrdnd}tt|}t||}|}t|}t||}||df\} \} } | j||ks8J| tjks?Jt | |sFJdD]*} | dkrRtj} ntj } | || }|||f\} \}} | |uslJ| | ksrJqHt||}|||f\} }| tj ksJt||}||df\} \}} | tj ksJ|| usJdS)NrXr"r%rCrr"r) r r rrr7rr3r8r> isinstancer?rr@)r`rrr string_DTrother_dtexpected_length string_dtsafety res_other_dtres_dtrexpected_safetyrTrtr r rtest_string_cancasts2     zTestCasting.test_string_cancastrcCstt|}tt||}t|t|}||df\}\}}||ur.|j|jks,JdS||d\} } tjt| |d} | |d} tjt| | d} | |d}tjt| |d}|j rbJ|j rgJdD]j}| | | d|\}} | | | d|\}} | | |d|\}}| || f| || ft| | | | ||ft| ||jdkrqid|d <| | |ft| |d|d <| ||ft| |qidS) ah Tests casts from and to string by checking the roundtripping property. The test also covers some string to string casts (but not all). If this test creates issues, it should possibly just be simplified or even removed (checking whether unaligned/non-contiguous casts give the same results is useful, though). NrprCr")TFTbr.)r r rrrrrrrrrrcopyrrrr2)r`rrrr cast_backrtrrrrstr_arrstring_dt_short str_arr_shortstring_dt_long str_arr_longr other_arrr r r"test_simple_string_casts_roundtripsN          z.TestCasting.test_simple_string_casts_roundtrip)S8zU8cCszt|}|dkr dnd}|jdkrdnd}tt|}tt||}|j|}t||}||df\} \} } | j||ksFJt| |sMJ|j|kr`|jr\t j t j B} nt j } n |dkrht j } nt j} | | ksqJdD]G} ||| }|||f\} \}} | |usJ| t jkr| | ksJqs| dkr| t jksJqs| dkr| | ksJqs| dkr| t j ksJqsdS)NrXr"r%rYrr)r rr5r rr3rrrr8r<rAr=r>r@rr?)r`rrrdivrrrrrrrrrrTrtr r rtest_string_to_string_cancast:s@       z)TestCasting.test_string_to_string_cancastorder1><order2c Cst|d}t|d}tjdtjddd|}tjdtjddd|}|jdkr?|jjr9J|jjr?Jd}||d<||fD]}||d<|d|ksYJ|d|kscJqKdS)NU30yrpr"uthis is a ünicode string‽r .) r rr}rrrr~rr) r`rrrrdata1data2elementdatar r rtest_unicode_byteswapped_castfs   z)TestCasting.test_unicode_byteswapped_castcCsDtjgdddjjdksJtjgdddjjdks JdS)NV5rprXr rYr()r rrrr3rjr r r test_void_to_string_special_case|s $z,TestCasting.test_void_to_string_special_casecCsnttt}ttt}t||}tjtdd| tddfWddS1s0wYdS)Nz+casting from object to the parametric DType)matchO) r r robjectrrrr TypeErrorr)r` object_dtype other_dtyperr r r(test_object_to_parametric_internal_errors "z4TestCasting.test_object_to_parametric_internal_errorrgr<r@cCsHtdg}|dk}tjd||d|ksJtj|d|d|ks"JdS)N)foozr@r?rrrrrrrrr  typecodesr r r r rrosv # ;  !  < -   ,  ' ; *  ro)rlrrIenum itertoolsrrnumpyr numpy.lib.stride_tricksr numpy.testingrnumpy.core._multiarray_umathrrrrr3replacerr7IntEnumr8rUrrVror r r rs*    # *