a :jgƒ@s|dZddlZddlmZmZmZmZmZmZm Z ddl m Z ddl m Z mZmZmZddlZGdddZGdd d ZdS) z-Test functions for 1D array set operations. N)ediff1d intersect1dsetxor1dunion1d setdiff1duniqueisin) AxisError)assert_array_equal assert_equal assert_raisesassert_raises_regexc @s@eZdZddZddZddZddZd d Zej d e j gd e j d de jdfe j gd e j d e j gde jd ddfe j gde jd e je jdfgddZej de j gd e jd dde j gde jd fe j gd e jd e j dge jd de j gde jd fe j gd e jd dde j gde jd fe j gd e j d dde j gde j d fgd d!Zej d"gd#d$d%Zej d"gd#d&d%Zd'd(Zej d"gd#d)d*Zd+d,Zej d"gd#d-d.Zej d"dd/gd0d1Zd2d3Zej d4e je jfe je jfe je jfe je jfe je jfe je jfe je j fgej d"gd#d5d6Z ej d7e j d8d9ge jd e j d:d;ge j d gej d"gd#dd?Z"d@dAZ#dBdCZ$dDdEZ%dFdGZ&dHdIZ'dJdKZ(dLdMZ)dNdOZ*dPdQZ+dRdSZ,dTdUZ-dS)V TestSetOpscCstgd}tgd}tgd}t||dd}t||tgd}tgd}tgd}t||}t||tgtggdS)Nrrr)rrrT assume_unique)rrrrr)rrrrrrr)nparrayrr )selfabeccZedr J/usr/local/lib/python3.9/site-packages/numpy/lib/tests/test_arraysetops.pytest_intersect1ds   zTestSetOps.test_intersect1dcCsLGddd}|}t||}t||tgdgd}t|gddS)Nc@seZdZdddZdS)z4TestSetOps.test_intersect1d_array_like..TestNcSs tdS)Nr)rarange)rdtypecopyr r r! __array__&sz>TestSetOps.test_intersect1d_array_like..Test.__array__)NN)__name__ __module__ __qualname__r&r r r r!Test%sr*rrr)rr )rr*rresr r r!test_intersect1d_array_like#s   z&TestSetOps.test_intersect1d_array_likec Cstgd}tgd}t||ddd\}}}tgd}t||t|||t|||tgd}tgd}t||dd\}}}tgd}t||t|||t|||tgd gd g}tgd gd g}t||ddd\}}}t||j}t||j} tgd } t| ||t| || tgdgdg}tgdgdg}t||dd\}}}t||j}t||j} tgd} t| ||t| || dS)Nrrrr)rrrT)rreturn_indices)rrr)rrrrrrr)rrrrrrr)r0)rrrr/)rr1r)rrrr/) r1 )rr/rr1)rrrr/r/)rrr1rr)rrrr)r3r4r1r)rrr1)rrrr Z unravel_indexshape) rrrri1i2eeZefZui1Zui2Zear r r!test_intersect1d_indices/s<  z#TestSetOps.test_intersect1d_indicescCstgd}tgd}tgd}t||}t||tgd}tgd}tgd}t||}t||tgd}tgd}tgd}t||}t||tgtggdS) Nrr)rrrr+)r/rr)rrrrrr/)rr1rr)r/rrr1)rrrr rrrrrr r r! test_setxor1dVs       zTestSetOps.test_setxor1dcCs,tg}tdg}tddg}tgt|tdgt|ddtdgt|ddtddgt|dddtgt|tdgt|tgdt|d d dtgd t|d d gd dgdtdd gt|d dtgdt|d dgdtd dgt|d dtgdt|d d gddS)Nrrr)to_begin)to_end)r=r>)rrr5rr5)rr/rrr1rr/r1)rrr1)rr/r)rrr r)rZ zero_elemZone_elemZtwo_elemr r r! test_ediff1dns"  zTestSetOps.test_ediff1dzary, prepend, append, expectedr+r$Nr>)rrrr=)?g@g"@cCsFd|}tt|t|||dWdn1s80YdS)Nz dtype of `{}` must be compatiblearyr>r=)formatr TypeErrorr)rrDprependappendexpectedmsgr r r!!test_ediff1d_forbidden_type_castss   z,TestSetOps.test_ediff1d_forbidden_type_castszary,prepend,append,expectedii)rrrrr)rrrr)rrrrr)rrrrLcCs.tj|||d}t|||j|jks*JdS)NrC)rrr r$)rrDrGrHrIactualr r r!test_ediff1d_scalar_handlings  z'TestSetOps.test_ediff1d_scalar_handlingkind)Nsorttablec sdd}tj|tgdhdfdd}tdgd}tgd gd gd g}|||d d g}d}|||td}||||||||||d|||d|dddkrg} || |||| || | ttjtjfD]} dkr| tjkrq| tjtjhvr:tjgd | d} n| thvrTtgd} tjg| d} || | || | || | qdS)NcSst|}||vS)N)rasarrayflattentolist)rrr r r! _isin_slowsz(TestSetOps.test_isin.._isin_slowr)Zotypesexcludedcs&t||d}||}t||dS)NrO)rr )rrxyZ isin_slowrOr r!assert_isin_equals z/TestSetOps.test_isin..assert_isin_equalrrr)r3)rrr) !)r5r1)rr/)r5rrrr/rQrATFF)rZ vectorizeboolr#reshaperint64float64) rrOrUr[rrrdfrXr$arZ empty_arrayr rZr! test_isins@               zTestSetOps.test_isincCsJdD]}gd}gd|}tgd}t||d|d}t||d|d<tgd }t||d|d}t||d \|d<|d <tgd }t||d|d}t||tgd }gd|}gd}t|||d}t|||gd|}gd}t|||d}t||tgd}tgd|}tgd}t|||d}t||tgd}tgd|}tgd}t|||d}t||tddg}tddg|}tddg}t|||d}t||qtdg}tdg}tdg}t|||d}t|||dvrFttgg|dgdS)Nrr3rrTFTTT)rrOr1r)FFTT)rr1r)TFTFrrrrrrrrrrrrrrr])FTFTTTTTTFTFFFrW)rrr)TTTTTTTTTTTFTT)rrrrr)rrrrrr)TFTTTrrF>NrPrrrr )rrOmultrrrrr r r!rksZ                cCsBtgd}tddg}tgd}t||}t||dS)N)rrrrherrqrrr)TFTFFTFFror;r r r!test_isin_char_array?s  zTestSetOps.test_isin_char_arrayc CsdD]D}tgd}gd|}ttt|||dt||d|dq|dvrdD]Z}tjgdtjd}gd|}tj|tjd}ttt|||dt||d|dqVd S) zTest isin's invert parameterrlrnr]rWTinvertrO>NrPrAN)rrr rtrfloat32)rrOrprrr r r!test_isin_invertHs    zTestSetOps.test_isin_invertcCstjgdtjd}tjgdtjd}tjgdtd}t|t||tt|t||ddtjgdtjd}tjgdtjd}tgd }t||dd }t||d S) z(Hit the standard isin code with integers)rrrrrreArA)rrrrw)rrrrrrrTrtr)rrrrrrwrmrN)rrrfrdr rrt)rrrrIrrr r r!!test_isin_hit_alternate_algorithm]sz,TestSetOps.test_isin_hit_alternate_algorithmcCs^tddg}tgd}tddg}t|t|||dtt|t||d|ddS)z&Test that isin works for boolean inputTF)FFFrWrsN)rrr rrt)rrOrrrIr r r!test_isin_booleanms  zTestSetOps.test_isin_booleanrPcCsbtjd}|jdddd}|jdddd}t||}|d}|d}t|t|||ddS)z(Test that isin works for timedelta inputrdr3)sizetimedelta64[s]rWN)rrandomZ RandomStaterandintrastyper )rrOZrstaterrtruthZ a_timedeltaZ b_timedeltar r r!test_isin_timedeltaxs    zTestSetOps.test_isin_timedeltacCsRtjgddd}|}ttt||ddWdn1sD0YdS)Nrrrr}rArQrW)rrpytestraises ValueErrorrrrrr r r!test_isin_table_timedelta_failss z*TestSetOps.test_isin_table_timedelta_failsz dtype1,dtype2c Cst|tj}tjgd|d}|r8tjgd|d}ntjgd|d}tgd}|dkor|tjkor|tjk}|rtjtddt |||d Wd q1s0Ynt t |||d |d S) z7Test that isin works as expected for mixed dtype input.)rrrrrA)ir)rr)TTFFrQzexceed the maximum)matchrWN) rZ issubdtypeZ signedintegerrint16int8rr RuntimeErrorrr ) rZdtype1Zdtype2rOZis_dtype2_signedar1ar2rIZexpect_failurer r r!test_isin_mixed_dtypes.z TestSetOps.test_isin_mixed_dtypedatallllcCsT|d}tj|||d}t|ddg|tj}tj|||d}t|ddgdS)z>Test values outside intp range (negative ones if 32bit system)rrWFTN)rrr rint32)rrOrqueryr,r r r!test_isin_mixed_huge_valss  z$TestSetOps.test_isin_mixed_huge_valscCstjdD]~}tjgdtd}tjgd|d}tjgdtd}tt|||d|||}}tjgdtd}tt|||d|q dS) z4Test that isin works as expected for bool/int input. AllIntegerrcrArrrr)FTTrW)TTTTN)r typecodesrrdr r)rrOr$rrrIr r r!test_isin_mixed_booleans z"TestSetOps.test_isin_mixed_booleancCs<dg}tdgd}tdg}t||}t||dSNrr3FrorrrrIresultr r r!test_isin_first_array_is_objects   z*TestSetOps.test_isin_first_array_is_objectcCs:d}tdgd}tdg}t||}t||dSrrorr r r! test_isin_second_array_is_objects   z+TestSetOps.test_isin_second_array_is_objectcCs<dg}tdgd}tdg}t||}t||dS)Nr3Trorr r r! test_isin_both_arrays_are_objects   z+TestSetOps.test_isin_both_arrays_are_objectcCs`tdtfdtfg}tjdg|d}tjdgd|d}tdg}t||}t||dS)NZfield1Zfield2)rNrAr3T)rr$intobjectrrr )rdtrrrIrr r r!+test_isin_both_arrays_have_structured_dtypes   z6TestSetOps.test_isin_both_arrays_have_structured_dtypecCs`tjddgtd}tjddgtd}tddg}t||}t||tj||dd}t|t|tjgdtd}|dd}tjgdtd}|dd}tddg}t||}t||tj||dd}t|t|tjgdtd}|dd}tjddgtd}tdd g}t||}t||tj||dd}t|t|dS) NrrrATrx)r)rrrr?)rrrrF)rrrrr rtrr r r!'test_isin_with_arrays_containing_tupless0         z2TestSetOps.test_isin_with_arrays_containing_tuplescCstgd}tgd}ttt||ddtjgdtd}tjgdtd}ttt||ddtjtjfD]}tjgd|d}tjd t|j g|d}tt t||ddtj||d d}t |d gd gd tj||dd}t |d gd gd qpd S)z&Test that isin raises expected errors.)rrrrr)rrr/r1r3Z quicksortrW)rrrrrrArQ)r?rrrrr?NTFrrP) rrr rrrrrfZiinfomaxrr )rrrZobj_ar1Zobj_ar2r$Z overflow_ar2rr r r!test_isin_errors s"zTestSetOps.test_isin_errorsc Cstgd}tgd}tgd}t||}t||tgdgdg}tgd}tgd}t||}t||tgtggdS)N)rrrrrrrrrrrr)rrrrrrr)rrr)rrrrr)rrrrrr)rrrr ) rrrrrrXrYZezzr r r! test_union1d+s    zTestSetOps.test_union1dcCstgd}tgd}tddg}t||}t||td}td}tddg}t||}t||tgtggtdtj}tt|gjtjdS) N)r/rrrrrrrrr/rr^r )rrrr r#Zuint32r r$r;r r r!test_setdiff1d=s      zTestSetOps.test_setdiff1dcCsFtgd}tgd}tddg}t||dd}t||dS)N)rrr)rrrrrTr)rrrr )rrrrIrMr r r!test_setdiff1d_uniqueOs z TestSetOps.test_setdiff1d_uniquecCs8tgd}tgd}tt||tdgdS)N)rrr)rrsr)rrr rrr r r!test_setdiff1d_char_arrayVsz$TestSetOps.test_setdiff1d_char_arraycCsRtgd}tgd}t||}t||}t||}t||}t||dS)N)rrrrr1)r5r1rrrrr)rrrrrrr )rrrc1Zaux1Zaux2c2r r r! test_manyways[s    zTestSetOps.test_manyways).r'r(r)r"r-r:r<r@rmark parametrizerrrfnanrurrKrrgrrNrkrrrvryrzrruint8Zuint16Zuint64rrrrrrrrrrrrrrr r r r!rs '   9 =                !"rc@seZdZddZddZddZddZej d d d gd d Z ej d gdddZ ddZ ddZ ddZddZddZddZddZdS) TestUniquecCstdd}gdd}gd}gd}gdd}tgdd}g}|tjd |tjd |d |d |D].}t||} t||} || | ||||q|d }tt||} || dd<tt||} || dd<|| | ||||ddg}ttt |||} ttt |||} || | ||||gd} t t | ddggd}gd} ddgddgddgg} t |} t | | t |dd\}}t || t || tj d}d|d<tj |dd\}}t |tdg}tj |ddd}tj |ddd}tj |ddddd\}}t|jtjt|jtjt|jtjt|jtjdtjdtjg}ddtjg}gd }gd!}gd"}tt ||ttj |dd||fttj |dd||fttj |dd#||fd$tjd%td&tjtdtjg}d%d$td&tjg}gd'}gd(}gd)}tt ||ttj |dd||fttj |dd||fttj |dd#||ftd*}td+|td,|g}td,td+|g}gd }gd!}gd"}tt ||ttj |dd||fttj |dd||fttj |dd#||ftd*}tdd-|tdd.|g}tdd.tdd-|g}gd }gd!}gd"}tt ||ttj |dd||fttj |dd||fttj |dd#||ftjgd/}tjg}dg}gd0}d/g}tt ||ttj |dd||fttj |dd||fttj |dd#||fdS)1Nc Ssd}|d|}t|}t||||d|}t|ddd\}} t|||t| |||d|}t|ddd\}} t|||t| |||d|}t|ddd\}} t|||t| |||d|}t|ddd\}} } t|||t| ||t| |||d |}t|ddd\}} } t|||t| ||t| |||d |}t|ddd\}} } t|||t| ||t| |||d |}t|ddd\}} } } t|||t| ||t| ||t| ||dS) Nzcheck {0} failed for type {1}values return_indexTFreturn_inverse return_countszreturn_index and return_inversezreturn_index and return_countsz return_inverse and return_countsz.return_index, return_inverse and return_counts)rErr ) rrr7r8rrZbase_msgrJvjZj1Zj2Zj3r r r! check_alljsN                          z,TestUnique.test_unique_1d..check_all)rrrrrrrr3)rrrr)rrrr)rrrrrrr)rrrrrAllFloat datetime64[D]timedelta64[D]O)i)??rrr)rrrrr+rrrTrrr.)r)rrg@rB)rrr)rrrr)rrr)ry@y??)rrr)rrrrr)rrrnatz 2020-12-26z 2020-12-24Dhrr)rmultiplyextendrrHremptylenlistzipr rcharZ chararrayZzerosr r$ZintprcomplexZ datetime64Z timedelta64)rrrrr7r8rtypesrZaaZbbunqinvZa1Za2Za2_invZa1_idxZa3_idxZa3_invZuaZua_idxZua_invZua_cntrZall_nansr r r!test_unique_1dhs0                 zTestUnique.test_unique_1dcCsXtt|jttt|jdtfdtfgttttdddttttddddS)Nrrr3raxis) r rF_run_axis_testsrrr rrr#)rr r r!test_unique_axis_errorss z"TestUnique.test_unique_axis_errorscCsZd}gdgdg}t|}tt|ddt|dd|tt|ddt|dd|dS)NzUnique failed on list of listsrrrrrr)rrRr r)rrJinpZinp_arrr r r!test_unique_axis_lists  z TestUnique.test_unique_axis_listcCsg}|tjd|tjd|d|d|dtfdtfg|dtfdtfg|D]}||qhd}tjdtjd  d d  t }tj d d gd d ggt d }t t|dd||d}t ddgddgddgddgg}t ddgg}t t|dd||dS)Nrrrrrrz&Non-bitwise-equal booleans test failedr3rAr?rFTrrz"Negative zero equality test failedgr)rrrrHrfloatrr#rreviewrdrr r)rrr$rJrrr r r!test_unique_axis s"   "zTestUnique.test_unique_axisrrr?cCs,tgd}t||d}t|gddS)N)rrrrrrrrrr.rrrr )rrrXuniqr r r!test_unique_1d_with_axis6s z#TestUnique.test_unique_1d_with_axis)Nrr?cCshtgdgdgdgdg}t|d|d\}}t|j|durH|jndt|tj|||ddS)N)rrr)rrrT)rrrr)rrrr ndimr take)rrrXrrr r r!test_unique_inverse_with_axis<s"z(TestUnique.test_unique_inverse_with_axisc CsVtjdtjd}t|ddddd\}}}}t|j|jt|tjddt|tdgt|tddgt|tdgt|d dddd\}}}}t|j|jt|tjddt|tgt|tgt|tgd }tj|d}tt |D]H}t |} ||dkr,d| |<nd | |<tt||d tj| dqdS) N)rr)r6r$rTrrrr)rr)r6rr)rrrrrrrr) rrrrr r$r rrangerr) rZ single_zeroridxrcntr6Zmultiple_zerosrZexpected_shaper r r!test_unique_axis_zerosCs6    z!TestUnique.test_unique_axis_zeroscCshtjgddd}tj|d}t|}tj|ddd\}}}d}t|j|j|t|j|j|dS)N)@rrrr?rrrrrrrrrrrArT)rrz6Unique returned different results when asked for index)rrmaZ masked_equalrr rmask)rrXrYrZv2rrrJr r r!test_unique_maskedgs  zTestUnique.test_unique_maskedcCsDd}dD]6}tdgdgg|}tj|dd}t||||qdS)Nz*sort order incorrect for integer type '%s'Zbhilqr?rrr)rfmtrrrr r r! test_unique_sort_order_with_axists z+TestUnique.test_unique_sort_order_with_axisc Cstgdgdgdgdg|}d}tddg}tt||||d}tgdgdg}tt|dd|||d}tgd gd gd gd g}tt|dd|||d }tddgddggddgddggg|}tj|ddgd d}tt|d d||t|dd d d d\}}}} d}t||||d}ttj||dd|d}t| td d g|t|dd d d d\}}}} d}t|dd|f|d}ttj||dd|d}t| tgd|dS)N)rrrr)rrrrz&Unique with 1d array and axis=0 failedrrz&Unique with 2d array and axis=0 failedrz&Unique with 2d array and axis=1 failed)rrrrz&Unique with 3d array and axis=2 failedrTrz-Unique's return_index=True failed with axis=0z/Unique's return_inverse=True failed with axis=0z.Unique's return_counts=True failed with axis=0z-Unique's return_index=True failed with axis=1z/Unique's return_inverse=True failed with axis=1z.Unique's return_counts=True failed with axis=1)rrr)rrrr rr) rr$rrJrZdata3drrrrr r r!r}s\ " zTestUnique._run_axis_testscCsjtddtjtjtjg}t|}tj|dd}t|tdtjgt|tdtjtjtjgdS)NrF equal_nan)rrrrr )rrrZnot_unqr r r!test_unique_nanequalss  z TestUnique.test_unique_nanequalsc Csttjdddddtjddg }t|tj|ddft|tj|dddft|tj|ddd ft|tj|ddddd ffD]:\}}t|t|ksJt ||D]\}}t ||qqdS) NrrrrFrT)rr)rr)rrrr) rrrZ unique_valuesrZ unique_countsunique_inverse unique_allrrr )rarrZres_unique_array_apiZ res_uniquerMrIr r r!test_unique_array_api_functionss.   z*TestUnique.test_unique_array_api_functionscCs|tgdgdg}tj|dd\}}||j}tjtjfD]6}||}t||jt||j t||j|j q@dS)Nr+)rrrTr) rrrrer6rrr rZinverse_indices)rrZexpected_valuesZexpected_inversefuncrr r r!test_unique_inverse_shapes   z$TestUnique.test_unique_inverse_shapeN)r'r(r)rrrrrrrrrrrrrrrrr r r r!rfs *  $  ,r)__doc__numpyrrrrrrrrZnumpy.exceptionsr Z numpy.testingr r r r rrrr r r r!s$ [