1jdZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m ZdZejdjejdjkr*edd d d Zd eDZd Zd ZGddejZdZeZGddZGddZdS)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?bhilqBHILQefdFDlqLcPg|]#}ttj|$Stypenpdtype.0cs {/builddir/build/BUILD/cloudlinux-venv-1.0.11/venv/lib64/python3.11/site-packages/numpy/core/tests/test_casting_unittests.py rs(:::qbhqkk"":::c#KtD]u}|}tj|t|V|jdkr9|}tj|t|VvdS)N)id|) simple_dtypespytestparamstr byteorder newbyteorder) dtype_classdts rsimple_dtype_instancesr!s$// []]l2#b''****** <3  ""B,rc"gg... . . . //rc||tjkrdS|jdvr\|jdkrd}n<|jdkrd}n.|jdkrd}n |jdkrd }nt d ||jd kr|dz }|S|jd krd S|jdkrdS|jdkrdS|jdkrdSt d |)zHReturns the string length when casting the basic dtypes to strings. iu z!did not find expected length for ig0G`f r@)rbool_kinditemsizeAssertionErrorchar)rlengths rget_expected_stringlengthr:'s q zT >Q  FF ^q FF ^q FF ^q FF !LU!L!LMM M :   aKF  zSr s  v s  r s  v DUDD E EErc"eZdZdZdZdZdZdZdS)Castingrr%r'r&r(N)__name__ __module__ __qualname__noequivsafe same_kindunsafer rrr<r<Js' B E DI FFFrr<ctjdd}d|ddddD}tjtjtjtjdd}i}t||ddD]:\}}i||<t||dddD]\}}|||||<;|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 . . . . . . . . . . . . . . . . . . . . = = . #  cPg|]#}ttj|$Sr r rs rrz&_get_cancast_table..ns( 8 8 8Ad28A;; 8 8 8rrr').~=# r%) textwrapdedentstripsplitr<rDrCrBrAzip)tabledtypes convert_castcancastfrom_dtrowto_dtrs r_get_cancast_tablerZRs O   4UWWUU4[[5 69 8q!$Q$ 8 8 8F g.? GMLGFE!""I..66 FC1I.. 6 6HE1&21oGG U # # 6 NrceZdZdZejdddgejdgddZdZd S) TestChangesz: These test cases exercise some behaviour changes stringSUfloating)er1dr-chtj||sJtj||dsJdS)N100rcan_cast)selfr`r]s rtest_float_to_stringz TestChanges.test_float_to_strings@{8V,,,,,{8^^^4444444rctjddsJtjddsJtjddrJtjddrJtjddrJtjdddsJtjd dd sJtjd dd sJdS) NrbVS20V1U1zd,irCcastingV3r@V0rergs r test_to_voidzTestChanges.test_to_voids{3$$$$${5#&&&&&;sD)))));ud+++++;tT*****{5#{;;;;;;{4d333333{4d33333333rN) r=r>r?__doc__rmark parametrizerhrsr rrr\r\s [XSz22 [Z)=)=)=>>55?>325 4 4 4 4 4rr\c eZdZdZdZdZdZejde dZ ej dejde d Z ejde d Zejgd d d ejdddfeejd d ejd ddfddejdddfddejd ddfddejdddfd dejd ddfdd ejd ddfddejd ddfddejd d gdfdd ejdddfeejdd ejd ddfddejdddfddejd ddfddejdddfddejd ddfddejd ddfddejd ddfddejd d gd fgd!Zd"Zejd#e ejd$d%d&gd'Zejd$d%d&gejd(e d)Zejd(gd*ejd$d%d&gd+Zejd,d-d.gejd/d-d.gd0Zd1Zd2Z ejd3e d4Z!ejd3e d5Z"ejd6d7d8gd9Z#ejd:d;gejdd?gdd@gdAdBfejd=dd?gdd@gdAdBfejd=dd?gddCgdAd fgdDZ$ejdEdFdGdHdIe%d.s%EEE!&"4--EEErrur%?c,g|]}t|Sr boolrvs rrz(TestCasting.get_data..s222a$q''222rc,g|]}t|Sr rrs rrz(TestCasting.get_data..s...!d1gg...r) r6sizeremptyflags c_contiguousalignedrange enumerater5iinfomaxr8) rgdtype1dtype2r9arr1valuesr,valuearr2s rget_datazTestCasting.get_datas >V_??Y&/1FFY&/1Fxf---z&&&&z!!!!EEuV}}EEE!&))  HAuqyyV[C// 0 0 44q8DGG >{c!!226222<  ;#  ..v...Fxf---z&&&&z!!!!!&))  HAuqyyV[C// 0 0 44q8DGGT6!!rTc|r|jj}|jj}n?|rd|jjz}d|jjz}n|jjdz}|jjdz}t|dz|jjzdz}t|dz|jjzdz}tj|tj} tj|tj} |t|z| jksJ|t|z| jksJ|rot| dd|j|j |f} t| dd|j|j |f} nnt| dd|j|j |f} t| dd|j|j |f} || d<|sU|r |jjnd} | | d| }| d t|ksJ|r| j j sJ| j j sJn| j j rJ| j j rJ|r| j jsJ| j jsJn<| 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&rzNrH.r)rr6lenrzerosuint8nbytesrviewshapetobytescountrrr alignment)rgrrrcontigstride1stride2 max_size1 max_size2 from_bytesto_bytesnew1new2offsetbufs rget_data_variationzTestCasting.get_data_variations  .j)Gj)GG  .$*--G$*--GGj)A-Gj)A-GIIMDJ$77!; IIMDJ$77!; Xirx888 8IRX666T"j&77777T"ho5555  6j"o224:>>"j7*66Dhssm00<<"j7*66DDjn11$*=="j7*66Dhqrrl// ;;"j7*66DS  0,3:TZ((FV_W_-5577C99U##s3xx////  /:* * **:* * ** *z. . ..z. . ..  G:% % %%:% % %% %z) FTZ-AQ-F-F-FFz) FTZ-AQ-F-F-FFTzrfrom_DtctD]n}t||}||fD]3}||dfdd}||ksJ~||fD]}|||f\}\}} } t ||ksJt | |ksJ| H|t jksJt jt||ksJ|j | j ksJn1||kr|j | j ksJ|t||ksJ||ur ||usJ|| usJ֐5pdS)Nr%) rget_castingimplr_resolve_descriptorsr r<r@rA CAST_TABLEisnative) rgrto_DtcastrWdefaultrYrofrom_resto_resview_offs rtest_simple_cancastzTestCasting.test_simple_cancasts" 0 0E"7E22D#GIIwwyy'='='?'?@ 0 033WdODDQGJ%%''))))#egguuww';';'='=>00E 55w6FGG:G/h>>W4444<<50000+ ''*4444&} 70CE0JJJJJ'0FOCCCCC"e++#+#4#G#G#G#G&*W*=e*DDDDD%''&(2222$+0  0 0 0rzignore::numpy.ComplexWarningrWctD].}|jd}tt|t|}|||f\}\}}}||us||urdS|t jk}~~~|||\} } } || | f| | ksJ| | | dd\} } || | ft| | | | ksJ|j dkr |j dks|jsdS| | | dd\} } || | ft| | | | ksJ| | | dd\} } || | ft| | | | ksJ~ ~ ~0dS)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%)r!rrr rr<rBr_simple_strided_calltolistrrrr_supports_unaligned)rgrWrYrrorrrrBrrrarr1_oarr2_os rtest_simple_direct_castsz$TestCasting.test_simple_direct_casts#sh,--/ %/ %ELOE"4==$u++>>D484M4M% 5"5" 1G'hw&&&*=*=gl*D&'!%w!>!> D$  % %tTl 3 3 3;;==F****"44T4uMMNFF  % %vv&6 7 7 7 vt , , ,>>##t||~~5555"a''EOq,@,@0-A!44T4MMNFF  % %vv&6 7 7 7 vt , , ,>>##t||~~5555!44T4NNNFF  % %vv&6 7 7 7 vt , , ,>>##t||~~5555_/ %/ %rc"|}tjdtjdtjdtjdtjdtjdg}|D] }tt|t|}|||f\}\}}} ||usJ||usJ~~|t |t|zsJ| Jtjtj} ||| \} } } | |} tj d| d<|tjdkred | d <| | | ftj t5t| d dddn #1swxYwYdS| | | fd | D| ksJ|| | d d \}}| ||ft#|| || ksJdS)NM8M8[ms]M8[4D]m8m8[ms]m8[4D]NaT.rrHc,g|]}t|Sr )intrs rrz5TestCasting.test_numeric_to_times..s222qCFF222rTF)rrrr rrint64rr datetime64rrraises ValueErrorrrrrr)rgrrW time_dtypestime_dtrrorrrint64_dtrrrrrs rtest_numeric_to_timesz!TestCasting.test_numeric_to_timesas'))x~~rx'9'928H;M;Mx~~rx'9'928H;M;MO "% 6% 6G"4==$w--@@D484M4M'"5$5$ 1G'hw&&&&W$$$$&Z0g?? ? ??###x))H!%w!A!A D$99W%%D e,,DI"(4..((R))4,777]:..""RMMM"""""""""""""""  % %tTl 3 3 322DKKMM222f<<<<"44T4uMMNFF  % %vv&6 7 7 7 vt , , ,>>##t||~~55555K% 6% 6s4GG G )rWrYexpected_castingexpected_view_offnomdenomzM8[ns]Nrr%rrzM8[5ms]i@BzM8[7ms]rzM8[1M])rrH"!zm8[ns]rrzm8[5ms]zm8[7ms]rzm8[1M])rrrrrrctj|}|tj|}tjgd}|tjd|j}|jj|jksJtj||dsJt|}t||} | ||f\} \} } } | |usJ| |us|J| |ksJ| |ksJ|!||z|z| }d|d<n.tj |}||d<||}||}tj |}| tj krV|dks|dkrJtjt 5| ||fdddn #1swxYwYdSdD]t}dD]o}|||||\}}d|d<| ||ft'|d|dpudS) N)rr%li'ilrrr.rrTT)rrarrayastyperrisnatrr rr empty_liker<rDrrrrrr)rgrWrYrrrrrDTyperrorrr expected_outorig_arrorig_outrrarrouts rtest_time_to_timezTestCasting.test_time_to_times@(7##  HUOOEDDDEErx00==g>OPPQQ|%):::::x G,,Q/00000W ue,,040I0I% 1"1"-#(FX7""""%--/*****,,,,, ?"SLE177??L#LOO=00L %L ',,U33L;;w''=.. gn $ $%4--5D==z** @ @))8X*>??? @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ F# R RG& R R22 (GV==SC))3*555"388G#4#4l6G6G6P6PQQQQ  R R RsG44G8;G8c|jdkrdnd}|j|z|z}tj|j|j|S)Nr^r%r()r8r6rrr)rgr change_lengthfactr9s rstring_with_modified_lengthz'TestCasting.string_with_modified_lengthsLJ#%%qq14'-7x5?@EJ@@@AAArother_DT string_charr^r_ct|dkrdnd}ttj|}t||}|}t |}tj||}||df\} \} } } | j||zksJ| tjksJ| Jt| |sJdD]g} | dkr tj}n tj }| || }|||f\} \}} } | |usJ| |ksJ| Jht||}|||f\} }} | tj ksJ| Jt||}||df\} \}} } | tj ksJ| J|| usJdS)Nr^r%r(rHrr%r) r rrrr:rr6r<rB isinstancerCrrD)rgrrr string_DTrother_dtexpected_length string_dtsafety res_other_dtres_dtrrexpected_safetyrYrs rtest_string_cancastzTestCasting.test_string_cancasts8 3&&qqA+..// x338::3H==H >_>>?? 373L3L4 4"4"0&v/D"88888%%%%&),,,,,( $ $M!!"),")"344Y NNE,0,E,Eu%-'-' )FKQU????_,,,,####y(33"77H8MNN8''''y(33(,(A(A  ))% FX''''6!!!!!!rrc4ttj|}tt||}t|t|}||df\}\}}}||ur|j|jksJdS||d\} } tjt| |} | |d} tjt| | } | |d}tjt| |}|j rJ|j rJdD]B}| | | d|\}} | | | d|\}} | | |d|\}}| || f| || ft| | | | ||ft| ||jdkrd|d <| | |ft| |d|d <| ||ft| |DdS) 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). NrzrHr%)TFTbr.)r rrrrrrrrrrrcopyrrrr5)rgrrrr cast_backrrrrrstr_arrstring_dt_short str_arr_shortstring_dt_long str_arr_longr other_arrs r"test_simple_string_casts_roundtripz.TestCasting.test_simple_string_casts_roundtrips+..// tH~~y99#ItH~~>> *.*C*C4 +"+"' $L)a x ' '%)????? F==488&(3x== :::::9bIIXoFFF 99)QGGxH ^DDD ++++0000# 4 4F!%!8!8'4"1"1 Iw#66-,,..f > > A}"55,f66OA|  % %y'&: ; ; ;  % %y-&@ A A A w~~o>> N N N  % %y,&? @ @ @ w 5 5 5}##IcN  * *GY+? @ @ @ x 3 3 3IcN  * *L)+D E E E x 3 3 3 35 4 4r)S8zU8ctj|}|dkrdnd}|jdkrdnd}ttj|}t t||}|j|z}tj||}||df\} \} } } | j||zksJt| |sJd} |j|kr#|jrtj }d} n,tj }n|dkr tj }n tj }| | ksJ|| ksJdD]}|||}|||f\} \}} } | |usJ|dkr | | ksJn| J|tj kr | |ksJh|dkr| tjksJ|dkr | |ksJ|dkr| tj ksJdS)Nr^r%r(rr_r)rrr8r rr6rrrr<r@rArBrDrrC)rgrrrdivrrrrrrrrrrrrYrs rtest_string_to_string_cancastz)TestCasting.test_string_to_string_cancastPsl8H%%3&&qqA=C''aaQ+..// tH~~y99"+s2H >_>>?? 373L3L4 4"4"0&v/D"88888&),,,,,  =K ' '  0")*$%!!")- C  %lOO%nO,,,,,&((((' . .M44Y NNE,0,E,Eu%-'-' )FKQU????!!#444444''''.0000000""!222222!##00000""----# . .rorder1><order2cBtj|d}tj|d}tjdtjdd|}tjdtjdd|}|jdkr|jjrJ|jjrJd}||d<||fD]5}||d<|d|ksJ|d|ksJ6dS)NU30yrzr%uthis is a ünicode string‽r .) rrrrrrrrr) rgr rrrdata1data2elementdatas rtest_unicode_byteswapped_castz)TestCasting.test_unicode_byteswapped_casts+ V...))V...))28444QRR8==fEE28444QRR8==fEE  q {* * **{* * **0b EJJLL) / /DE#J9''''::<<#w..... / /rctjgddjjdksJtjgddjjdksJdS)NV5rzr^r#r_r+)rrrrr6rrs r test_void_to_string_special_casez,TestCasting.test_void_to_string_special_casesrx$'''..s339BaGGGGx$'''..s339BeKKKKKKrcxttjt}ttjt}t ||}t jtd5| tjddfddddS#1swxYwYdS)Nz+casting from object to the parametric DType)matchO) r rrobjectrrrr TypeErrorr)rg object_dtype other_dtypers r(test_object_to_parametric_internal_errorz4TestCasting.test_object_to_parametric_internal_errorsBHV,,-- 28C==)) |[99 ]9GIII = =  % %rx}}d&; < < < = = = = = = = = = = = = = = = = = =s8*B//B36B3rcttjt}t |t|}|tjd|f\}\}}}|t jksJ|J||usJ|tjddf\}\}}}|t jksJ|J||dksJdS)NrrK) r rrrrrr<rDrrgrr rrrrrs r!test_object_and_simple_resolutionz-TestCasting.test_object_and_simple_resolutionsBHV,,-- |T%[[99(,(A(A#&)()(% FX''''(,(A(A#%)')'% FX''''++C00000000rc$ttjt}t t||}||df\}\}}}|t jksJ|J|tjdusJdS)Nr)r rrrrrr<rBr$s r test_simple_to_object_resolutionz,TestCasting.test_simple_to_object_resolutionsBHV,,-- tE{{L99(,(A(A ))% FX%%%%#&&&&&&rror@rDctjdg}|dk}tjd|||ksJtj|d||ksJdS)N)foozC; ?DD AllIntegerAllFloatctjgdtjt}||}gd}t ||dS)N)rr&irz)rr%r%)rrint8rrrr)rgrnonstandard_boolsresr+s rtest_nonstandard_bool_to_otherz*TestCasting.test_nonstandard_bool_to_other)s_ HZZZrw???DDTJJ&&u--993)))))rr)*r=r>r?rrrrrurvrrfilterwarningsr!rrr<r@rrrrrArBrDrCrrrrr rrr"r%r'r,r7dictr= typecodesrDrKr rrrxrxsR D'"'"'"R9999v [Y 6600760B [ >?? [Y(>(>(@(@AA:%:%BA@?:%x [Y 66,6,676,6\ [   gj!Q 2c("(8$$113344d-q!-HglAq! 4gndAq 9GJ1a 8'"3T1e D',eQ ? 7#4dAq A'"3T4999;gj!Q 2c("(8$$113344d-q!-HglAq! 4gndAq 9GJ1a 8'"3T1e D',eQ ? 7#4dAq A'.$888:/ ;<<:.R.R;<<:.R`BBB  [Z77 []S#J77,","8787,"\ []S#J77 [Z)?)?)A)ABB:4:4CB87:4x [Z)=)=)=>> []S#J771.1.87?>1.f [XSz22 [XSz22//3232/(LLL=== [W&<&<&>&>??11@?1" [W&<&<&>&>?? ' '@? ' [Yx(899EE:9E [g~6bh#st $%q6++,,-.0bh#st $%q6++,,-.0bh#st $%q6++,,-13 4 5 5(( 5 5(  [A   #$$cUSEA3??? F Tuqc : : :C C$$c3Z#saVLLL   #          "=D   @ ( (A  @ ( [Wbl5&9::BB;:B" [W L &j)A ACC**CC***rrx)rtrrNenumr}r@numpyrnumpy.lib.stride_tricksr numpy.testingrnumpy.core._multiarray_umathrrrrr6replacer!r:IntEnumr<rZrr\rxr rrrVs  ......,,,,,,LLLLLL#  28C==XRXc]]333!))#r22::3CCM::M::: /// F F FFdl(((T  ! ! 444444446X *X *X *X *X *X *X *X *X *X *r