o
    1ήc                     @   s&  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZmZ G dd dZG dd dZG d	d
 d
ZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZG dd dZdS )    N)array_indexing)product)assert_assert_equalassert_raisesassert_raises_regexassert_array_equalassert_warnsHAS_REFCOUNTc                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(e)j*+dMdNdOe,-dPgge)j*+dQdRdSge)j*+dTdUdRgdVdW Z.dXdY Z/dZd[ Z0d\S )]TestIndexingc                    s  t dggg tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fd	d tt fd
d tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd d S )N   c                          d S N         r   ar   E/tmp/pip-target-vg8gfxp4/lib/python/numpy/core/tests/test_indexing.py<lambda>       z3TestIndexing.test_index_no_floats.<locals>.<lambda>c                      r   )N)r   r   r   r   r   r   r   r      r   c                      r   )N)r   r   r   r   r   r   r   r      r   c                          dd d f S r   r   r   r   r   r   r          c                          d d df S r   r   r   r   r   r   r      r   c                          d d dd d f S r   r   r   r   r   r   r          c                          dd d d d f S r   r   r   r   r   r   r      r   c                      r   )N)r   r   r   r   r   r   r   r   r      r   c                      r   )N)r   r   r   r   r   r   r   r   r      r   c                      r   )N)r   r   r   r   r   r   r   r   r      r   c                      r   Nffffffr   r   r   r   r   r      r   c                      r   )N)r   r   r   r   r   r   r   r       r   c                      r   )N)r   r   r   r   r   r   r   r   !   r   c                      r   r   r   r   r   r   r   r   "   r   c                      r   r   r   r   r   r   r   r   #   r   c                      r   r   r   r   r   r   r   r   $   r   c                      r   r   r   r   r   r   r   r   %   r   c                      r   )N)r   r   r   r   r   r   r   r   r   &   r   c                      r   )N)r   r   r   r   r   r   r   r   r   '   r   c                      r   )N)r   r   r   r   r   r   r   r   r   (   r   c                      s    dd df S r   r   r   r   r   r   r   )   r   c                      s    dd dd d f S r   r   r   r   r   r   r   *   r   )nparrayr   
IndexErrorselfr   r   r   test_index_no_floats   s.   z!TestIndexing.test_index_no_floatsc                    s  t dgg tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fd	d tt fd
d tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd tt fdd d S )Nr   c                      s    dd  S r   r   r   r   r   r   r   0       z5TestIndexing.test_slicing_no_floats.<locals>.<lambda>c                      s    dd ddf S )Nr   r      r   r   r   r   r   r   1       c                          dd dd df S Nr   r%   r   r   r   r   r   r   r   2   r   c                      s    dddd d f S )Nr      r%   r   r   r   r   r   r   3   r   c                      s    d d dd f S r   r   r   r   r   r   r   4   r&   c                      s    d d S r   r   r   r   r   r   r   6   r$   c                      s    d dddf S )Nr   r)          @r   r   r   r   r   r   7   r&   c                      s    d ddd df S r(   r   r   r   r   r   r   8   r   c                      s    d dd d f S r   r   r   r   r   r   r   9   r&   c                          d d dddf S )Nr         @r%   r   r   r   r   r   r   :   r   c                          d d d S )N      ?r   r   r   r   r   r   <       c                      s    dd d ddf S )Nr   r%   r*   r   r   r   r   r   r   =   r   c                      r'   )Nr)   r,   r   r   r   r   r   r   r   >   r   c                      s    d d dd d f S )N      @r   r   r   r   r   r   ?   r   c                      r+   )Nr      r*   r   r   r   r   r   r   @   r   c                      s    ddd S )Nr.   r%   r*   r   r   r   r   r   r   B   r/   c                      s    dd d S )Nr.   r*   r   r   r   r   r   r   C   r/   c                      s    dd d ddf S )Nr   r*   r   r   r   r   r   r   D   r   c                      s    dddd df S )Nr.   r)   r,   r   r   r   r   r   r   r   E   r   c                      s    dddd d f S )Nr.   r0   r   r   r   r   r   r   F   r   c                      r+   )Ng?r,   r*   r   r   r   r   r   r   G   r   c                      r-   r   r   r   r   r   r   r   I   r/   r   r   r   	TypeErrorr!   r   r   r   test_slicing_no_floats,   s.   z#TestIndexing.test_slicing_no_floatsc                    s&   t dggg tt fdd d S )Nr)   c                      s        S Nr   r   r   r   r   r   O   r/   z;TestIndexing.test_index_no_array_to_index.<locals>.<lambda>r2   r!   r   r   r   test_index_no_array_to_indexK   s   z)TestIndexing.test_index_no_array_to_indexc                 C   s<   t g d}t|d  |t j  t|d  j|jd  d S )Nr)   r%      r)   )r   r   r   newaxisndimr"   r   r   r   r   test_none_indexQ   s   zTestIndexing.test_none_indexc                 C   sP   t g d}t|d | t|d j|u  t d}tt|d t j d S )Nr7   r   r   )r   r   r   r   base
isinstanceint_r;   r   r   r   test_empty_tuple_indexW   s
   
z#TestIndexing.test_empty_tuple_indexc                 C   sF   t jddd}t|d j|j t|d | tt|d t j d S )Nr   V4dtype.)r   zerosr   rC   typendarray)r"   sr   r   r   test_void_scalar_empty_tuple_   s   z)TestIndexing.test_void_scalar_empty_tuplec                 C   s   t d}|t j}t d}t|| ||  t d||< t|t d t ddd}t|| ||  t dd d d f ||< t|t dd d d f jddd t ddd}t|||f |||f  d S )Nr   
   r%   r)   axis   )r   arangeastypeuintpr   reshaperepeat)r"   indexu_indexarrr   r   r   test_same_kind_index_castinge   s   

&z)TestIndexing.test_same_kind_index_castingc                 C   sz   t g d}t|g  g  t|g  j|j t jg t jd}t|g  g  t|g  j|j t g }tt|j| d S )Nr7   rB   )r   r   r   rC   intpr   r    __getitem__r"   r   br   r   r   test_empty_fancy_indexy   s   
z#TestIndexing.test_empty_fancy_indexc                 C   s   t g dg dg dg}t|d |u t|d | t|d j|u  t|d |d  t|d |dd d f  t|d |d d df  t|d t d	 t d
}d	|tf< t|d	 d S )Nr7   r1   r            	   .)r   .r   .r   )r   .r)   r%   r)   )r   r   r   r   r=   EllipsisrX   r   r   r   test_ellipsis_index   s   


z TestIndexing.test_ellipsis_indexc                 C   s`   t g dg dg dg}t|d g d t|d g d tt|jd tt|jd d S )Nr7   r[   r]   r   i   @l            )r   r   r   r   r    rW   r;   r   r   r   test_single_int_index   s   
z"TestIndexing.test_single_int_indexc                 C   sX   t g dg dg dg}t|t d |d   t|t d |d  dd  d S )Nr7   r[   r]   TFr   r   r   r   r;   r   r   r   test_single_bool_index   s   
$z#TestIndexing.test_single_bool_indexc                 C   sx   t d}t dg}tt|j| t dgd }tt|j| t jdtd}tt|j| tt|jtd |f d S )N)r   r1   r8   TFr\   r1   r1   rB   )	r   onesr   r   r    rW   rD   boolslice)r"   rT   rR   r   r   r   test_boolean_shape_mismatch   s   
z(TestIndexing.test_boolean_shape_mismatchc                 C   sJ   t g dg}t jdgtd}t|| | d||< t|g dg d S )N)r   r   r   TrB   r.   )r.   r.   r.   )r   r   rj   r   rX   r   r   r   test_boolean_indexing_onedim   s
   z)TestIndexing.test_boolean_indexing_onedimc                 C   sP   t d}dd }tt||g  tt||g d tt||d d g d d S )Nr1   c                 S   s   || | dk< d S )Nrd   r   )r   vr   r   r   f   s   z>TestIndexing.test_boolean_assignment_value_mismatch.<locals>.fr7   r)   )r   rM   r   
ValueError)r"   r   ro   r   r   r   &test_boolean_assignment_value_mismatch   s
   
z3TestIndexing.test_boolean_assignment_value_mismatchc                 C   sZ   t d}t jdtd}d|d d< t jdtd||< t d}d|d d< t|| d S )N  rB   Td   r)   )r   rD   rj   ri   objectr   )r"   rT   indxexpectedr   r   r   !test_boolean_assignment_needs_api   s   

z.TestIndexing.test_boolean_assignment_needs_apic                 C   s   t g dg dg dg}t g dg dg dg}t|| g d t||d  g dg t||d  ||d	   d||< t|g d
g dg dg d S )Nr7   r[   r]   TFT)FTF)r)   r8   r   r^   r`   r)   r   r%   )r   r%   r   )r1   r   r\   )r   r_   r   rf   rX   r   r   r   test_boolean_indexing_twodim   s    



z)TestIndexing.test_boolean_indexing_twodimc                 C   sD   t g d}g d}t|| ddg t|d |f ddgg d S )Nr7   rx   r)   r8   rf   rX   r   r   r   test_boolean_indexing_list   s   z'TestIndexing.test_boolean_indexing_listc                 C   s   t d}t jdt jdd d d }t dd d d }|||< t|d d t d}t dddd d d }|||d d f< t|d ddg d S )	Nr   rB   rd   r   r   r%   rI   r%   r)   )r   ri   rD   rV   rM   r   rP   )r"   r   rY   cr   r   r   ,test_reverse_strides_and_subspace_bufferinit  s   

z9TestIndexing.test_reverse_strides_and_subspace_bufferinitc                 C   sX   t dd d d f }t dd d d }t|| ||d  t ddd}d S )NrI   rd   C   r%   )r   rM   r   copyrP   )r"   r   ir   r   r   'test_reversed_strides_result_allocation  s   z4TestIndexing.test_reversed_strides_result_allocationc                 C   sf   t dd}t dd}t ddddj|ddg< t ddddj |ddg< t|| d S )N)r8   r1   r%   rd      r%   r1   r   r)   )r   fullrM   rP   Tr   r   rX   r   r   r   %test_uncontiguous_subspace_assignment  s
   "z2TestIndexing.test_uncontiguous_subspace_assignmentc                 C   s*   t d}tt|jt dgfd  d S )N) r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r)   r       )r   ri   r   r    rW   r   r;   r   r   r   (test_too_many_fancy_indices_special_case)  s   
 z5TestIndexing.test_too_many_fancy_indices_special_casec                 C   sJ   t d}t|t d |t d  t|t d |t d  d S )Nr)   TF)r   r   r   bool_r;   r   r   r   test_scalar_array_bool.  s   
"z#TestIndexing.test_scalar_array_boolc                 C   sB   t d}t||d u t||d u t||d d  u d S )Nr   r   .)r   rM   r   r;   r   r   r   test_everything_returns_views;  s   
z*TestIndexing.test_everything_returns_viewsc                 C   sD   t d}tt|jddgg df tt|jddgg dfd d S )Nr   r   r   r)   r   r)   r%   )r   rD   r   r    rW   __setitem__r;   r   r   r   test_broaderrors_indexingC  s   
 z&TestIndexing.test_broaderrors_indexingc                 C   sz   t d}t jdt jd}d|d< tt|j| tt|j|d t jdt jd}d|d< tt|j| tt|j|d d S )Nr   r   rB   rI   rd   r      )r   rD   ri   rV   r   r    rW   r   )r"   r   indr   r   r    test_trivial_fancy_out_of_boundsH  s   
z-TestIndexing.test_trivial_fancy_out_of_boundsc                 C   st   t d}t jdt jddddd d d d df }t|| | d||< t d}d|d< d|d< t|| d S )Nr\   rB   r%   r)   r8   r   rd   )r   rM   rV   rP   r   )r"   r   idxresr   r   r   test_trivial_fancy_not_possibleS  s   
,
z,TestIndexing.test_trivial_fancy_not_possiblec                 C   s   G dd dt j}t d}| j|d}|d ||g dd d f< t|dk  ||d d g df< t|dk  |d ||d< t|dk  d S )	Nc                   @      e Zd Zdd ZdS )z7TestIndexing.test_nonbaseclass_values.<locals>.SubClassc                 S   s   |  d d S )Nc   )fillr"   oldr   r   r   __array_finalize__d  s   zJTestIndexing.test_nonbaseclass_values.<locals>.SubClass.__array_finalize__N__name__
__module____qualname__r   r   r   r   r   SubClassc      r   r   )rE   r)   r   r)   r%   r8   r1   r   .)r   rF   rD   r   viewr   r   all)r"   r   r   rG   r   r   r   test_nonbaseclass_valuesb  s   


z%TestIndexing.test_nonbaseclass_valuesc                 C   s   t d}t jdt jddd}t||g dd d f< t|| t||d d g df< t|| t||d< t|| d S )Nr   rL   rB   r   r   .)r   rD   rM   float64rP   
memoryviewr   )r"   r   rG   r   r   r   test_array_like_valuesw  s   


z#TestIndexing.test_array_like_valuesc                 C   s|   t jjg dddgd}t jg dtd}t|| jj t ddg}t|| jj t|d jj t|d jj d S )	N))NGC1001r   )NGC1002r.   )NGC1003r.   )targetS20)V_magz>f4rB   )FTTr   r)   .)r   recr   rj   r   flags	writeable)r"   dr   r   r   r   test_subclass_writeable  s   z$TestIndexing.test_subclass_writeablec                 C   sR   t d}t dddj}t|| jj |dd}t||df jj d S )NrI   r   r%   rd   r)   r   )r   rM   rP   r   r   r   f_contiguousrX   r   r   r   test_memory_order  s
   
zTestIndexing.test_memory_orderc                 C   sB  G dd d}| }G dd d}t d}tt|d t j t d}tt|| t j t d}tt||t df t j tt||| f t j t d}t jdtd	}||d< tt|d t j t |d g}tt|| t j t |d gg}tt||t df t j tt||| f t j d S )
Nc                   @   r   )z2TestIndexing.test_scalar_return_type.<locals>.Zeroc                 S      dS Nr   r   r!   r   r   r   	__index__     z<TestIndexing.test_scalar_return_type.<locals>.Zero.__index__N)r   r   r   r   r   r   r   r   Zero      r   c                   @   r   )z7TestIndexing.test_scalar_return_type.<locals>.ArrayLikec                 S   s
   t dS r   r   r   r!   r   r   r   	__array__  s   
zATestIndexing.test_scalar_return_type.<locals>.ArrayLike.__array__N)r   r   r   r   r   r   r   r   	ArrayLike  r   r   r   r)   )r)   r)   r   rB   )r   rD   r   r>   float_r   rt   rF   )r"   r   zr   r   rY   r   r   r   test_scalar_return_type  s&   



z$TestIndexing.test_scalar_return_typec                 C   s   t dg}trtt t j}d|t jdgt jd< d|t jdgt jd< tt	|j
t jdgt jdd tt	|j
t jdgt jdd trUttt t j| d S d S )Nr   r)   rB   )r   r   r
   sysgetrefcountrC   rV   uint8r   r    r   r   )r"   r   refcountr   r   r   test_small_regressions  s   z#TestIndexing.test_small_regressionsc                 C   s<  t jdt jdtd dd }|t d}t jdt jdtd dd }|t d}t jdd	d|d
< t |j}t	|| | |||< t |jd t jdt 
djd   }|t j
d |j }t |j|d
< t	||t j | |||t j< ||d dk  |d d d ||d dk< d S )N@   rB   r   r)   iS8r   bbbbbbbbS.r   r%   )r   rD   int8ordr   rC   r   rM   sizer   rV   itemsizerN   int16)r"   rn   r   xrY   r   r   r   test_unaligned  s     .zTestIndexing.test_unalignedc                 C   sX   t d}G dd dt}dgdgf}||}t|| jdk t||f jdk d S )Nr   c                   @      e Zd ZdS )z7TestIndexing.test_tuple_subclass.<locals>.TupleSubclassNr   r   r   r   r   r   r   TupleSubclass      r   r)   r)   )r   ri   tupler   shape)r"   rT   r   rR   r   r   r   test_tuple_subclass  s   
z TestIndexing.test_tuple_subclassc                 C   s^   G dd d}t d}t||  || f  t jdddgd}t||  || f  d S )Nc                   @   $   e Zd Zdd Zdd Zdd ZdS )zDTestIndexing.test_broken_sequence_not_nd_index.<locals>.SequenceLikec                 S   r   r   r   r!   r   r   r   r     r   zNTestIndexing.test_broken_sequence_not_nd_index.<locals>.SequenceLike.__index__c                 S   r   )Nr)   r   r!   r   r   r   __len__  r   zLTestIndexing.test_broken_sequence_not_nd_index.<locals>.SequenceLike.__len__c                 S   s   t d)NzNot possible)r    )r"   itemr   r   r   rW        zPTestIndexing.test_broken_sequence_not_nd_index.<locals>.SequenceLike.__getitem__N)r   r   r   r   r   rW   r   r   r   r   SequenceLike  s    r   rI   r   )f1i8)f2r   rB   )r   rM   r   rD   )r"   r   rT   r   r   r   !test_broken_sequence_not_nd_index  s
   

z.TestIndexing.test_broken_sequence_not_nd_indexc                 C   s   t d}t d}t dd d d d d f }t |d}t|| ||   t jdt jd}t|||f || |f  d S )NrI   )rI   r%   )rI   7   r1   r1   r1   rB   )r   ri   rM   broadcast_tor   r   rD   rV   )r"   r   x2r   zindr   r   r   !test_indexing_array_weird_strides  s   

"z.TestIndexing.test_indexing_array_weird_stridesc                 C   sH   t d}|d d dd d df }td g df}d||< t|d d S )Nrh   rd   r   r)   r%   r8   rI   g      $@)r   rD   rk   r   )r"   arrorT   slicesr   r   r   $test_indexing_array_negative_strides  s
   
z1TestIndexing.test_indexing_array_negative_stridesc                 C   sH   t jddd}t d|d< t|d t jddd |d dks"J d S )N)r)   r   r|   rB   asdfgr   r   r)      s)r   rD   str_r   r   )r"   rT   r   r   r   test_character_assignment  s   z&TestIndexing.test_character_assignmentrR   TFr   numr   (   original_ndimr)   c                 C   s   t d| }tt ||f|   W d    n1 sw   Y  tt d||f| < W d    d S 1 s;w   Y  d S )Nr   r.   )r   ri   pytestraisesr    )r"   rR   r   r   rT   r   r   r   test_too_many_advanced_indices(  s   "z+TestIndexing.test_too_many_advanced_indicesc                    s   ddl m} tdg}td|fgd }td|fgd }tjjdd|d d df  tj fdd	|d
d fddtdD }|D ]}|	  qO j|u s]J d S )Nr   )ThreadPoolExecutor) f8r   r%   )p  r_   )r   c                    s$    j dt| dtjd}| |  d S )Nr   r   )r   rC   )integerslenr   rV   )rT   ru   )rngr   r   funcC  s   z<TestIndexing.test_structured_advanced_indexing.<locals>.funcr_   )max_workersc                    s   g | ]}  qS r   )submit).0_)rT   r   tper   r   
<listcomp>H  s    zBTestIndexing.test_structured_advanced_indexing.<locals>.<listcomp>rI   )
concurrent.futuresr   r   rC   randomuniformr   default_rngrangeresult)r"   r   dtfuturesro   r   )rT   r   r   r  r   !test_structured_advanced_indexing6  s    


z.TestIndexing.test_structured_advanced_indexingc                 C   sp   t dd}t|ddg t |d |d g t|ddgddgf t ddg tt|jtd g d S )NrL   r   r   r)   r\   )	r   rM   rP   r   r   r   r    rW   rk   r;   r   r   r   test_nontuple_ndindexN  s   $$z"TestIndexing.test_nontuple_ndindexN)1r   r   r   r#   r4   r6   r<   r@   rH   rU   rZ   rc   re   rg   rl   rm   rq   rw   ry   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   markparametrizer   r   r   r  r  r   r   r   r   r      s^    	
		
"
	
r   c                   @   r   )TestFieldIndexingc                 C   s<   t ddg}tt|d t j tt|dg t j d S )Nr   )r   r   r   )r   rD   r   r>   rF   r;   r   r   r   r   V  s   z)TestFieldIndexing.test_scalar_return_typeN)r   r   r   r   r   r   r   r   r  U  r   r  c                	   @   s~   e Zd Zdd Zdd Zdd Zdd Zej	d	d
ddge
dfddgd
dfd
ddgddgfgdd Zdd Zdd ZdS )TestBroadcastedAssignmentsc                 C   s   |||< |S r5   r   )r"   r   r   valr   r   r   assign_  s   z!TestBroadcastedAssignments.assignc                 C   sr   t d}t d|d< t d|g dd d f< t d|d d ddgf< t d|dgdgdggddgf< d S )N)r8   r%   )r)   r8   r%   .r   r   r)   r%   )r   rD   ri   r;   r   r   r   test_prepending_onesc  s
   
&z/TestBroadcastedAssignments.test_prepending_onesc                 C   sx   | j }tj}td}tt|||d td tt|||g df td tt|||dgdggf td d S )Nr   .r%   r)   r7   r)   r%   )r%   r%   r)   )r  r   s_rD   r   rp   ri   r"   r  r  r   r   r   r   test_prepend_not_onem  s   
 (z/TestBroadcastedAssignments.test_prepend_not_onec                 C   s   | j }tj}td}tt|||d td tt|||d td tt|||d d dgf td tt|||d d dgf td tt|||dgd d f td d S )N)r   r)   .r{   )r   r   r   r  )r  r   r  rD   r   rp   r  r   r   r   test_simple_broadcasting_errorsw  s   
$$(z:TestBroadcastedAssignments.test_simple_broadcasting_errorsrR   .r)   r%   Nr   c                 C   st   t d}t d}t|| jdd}tt}|||< W d    n1 s)w   Y  t|j	|s8J d S )Nrs   rs   )r8   r1   r   r\   r^    r   )
r   rD   strr   replacer   r   rp   valueendswith)r"   rR   valuesrT   	shape_strer   r   r   *test_broadcast_error_reports_correct_shape  s   


zETestBroadcastedAssignments.test_broadcast_error_reports_correct_shapec                 C   sR   t d}g d|dgdgdggg df< t|d dd df g dk  d S )Nr   r%   r8   r1   r   r)   r%   r   r8   )r   rD   r   r   r;   r   r   r   test_index_is_larger  s   
 (z/TestBroadcastedAssignments.test_index_is_largerc                 C   sZ   t d}t dd d d f }t dd d d }|||< t|d d d |k  d S )Nr  rs   rd   )r   rD   rM   r   r   )r"   r   rn   rY   r   r   r   test_broadcast_subspace  s
   
z2TestBroadcastedAssignments.test_broadcast_subspace)r   r   r   r  r  r  r  r   r  r  rk   r$  r&  r'  r   r   r   r   r  ^  s    


r  c                   @   r   )TestSubclassesc                 C   s.  G dd dt j}t d}||}|d d }tt||u  t|j|u  t||d d  |g d }tt||u  t|j|u tt|jt ju  t||g d  t|j|g d  ||dk }tt||u  t|j|u tt|jt ju  t|||dk  t|j||dk  d S )Nc                   @   r   )z+TestSubclasses.test_basic.<locals>.SubClassNr   r   r   r   r   r     r   r   r   r8   r   r   )r   rF   rM   r   r   rE   r=   r   )r"   r   r   rG   s_slices_fancys_boolr   r   r   
test_basic  s&   

zTestSubclasses.test_basicc                 C   sJ   G dd dt j}t d}||}d|j_|g d }t|jj d S )Nc                   @   r   )z8TestSubclasses.test_fancy_on_read_only.<locals>.SubClassNr   r   r   r   r   r     r   r   r   Fr   )r   rF   rM   r   r   r   r   )r"   r   r   rG   r*  r   r   r   test_fancy_on_read_only  s   

z&TestSubclasses.test_fancy_on_read_onlyc                 C   s   G dd dt j}t d|}|d d }t|j| t|j| |g d }t|j| t|j| ||dk }t|j| t|j| d S )Nc                   @   r   )z=TestSubclasses.test_finalize_gets_full_info.<locals>.SubClassc                 S   s   t | | _|| _d S r5   )r   r   finalize_statusr   r   r   r   r   r     s   
zPTestSubclasses.test_finalize_gets_full_info.<locals>.SubClass.__array_finalize__Nr   r   r   r   r   r     r   r   rI   r8   r   r   )r   rF   rM   r   r   r.  r   )r"   r   rG   new_sr   r   r   test_finalize_gets_full_info  s   z+TestSubclasses.test_finalize_gets_full_infoN)r   r   r   r,  r-  r0  r   r   r   r   r(    s    r(  c                   @   r   )TestFancyIndexingCastc                 C   s   d}t |t}d|d< t |}t dg||< t|d d tt j|jdgdgft dg t|d d tt j|j|t dg t|d d d S )	N)r_   ?   Tr   r)   r   y       @      ?r%   y              ?)	r   rD   rN   rj   r   r   r	   ComplexWarningr   )r"   r   
bool_index
zero_arrayr   r   r   test_boolean_index_cast_assign  s   
z4TestFancyIndexingCast.test_boolean_index_cast_assignN)r   r   r   r6  r   r   r   r   r1    r   r1  c                   @      e Zd Zdd Zdd ZdS )TestFancyIndexingEquivalencec                 C   sT  t jdtd}| }g d|d d< g d|g d< t|| t jdtdd d d f }g dg|dgd df< t||d  |j}dgdgdgg|d ddgf< t||d d df  t jd	td}| }dgd
gdgdggg|d ddf< dgd
gdgdggg|dgdf< t|| |d}dgd
gdgdggg|dgdf< t|| d S )Nr   rB   )r)   r)   r%   r8   r8   r   r   r)   r9  )r8   r1   r   r%   r1   .F)r   rM   rt   r   r   r   ri   )r"   r   rY   rT   cmp_arrr   r   r   test_object_assign  s&   
" 

 z/TestFancyIndexingEquivalence.test_object_assignc                 C   s   t d}| }t g d|d d< t g d|g d< t|| t dd d d f }t g dg|dgd df< t||d  d S )Nr   )2-3-1r8   )r=  r?  r>  )r   r%   r)   r   )r   rM   r   r   r   rX   r   r   r   test_cast_equivalence  s   

z2TestFancyIndexingEquivalence.test_cast_equivalenceN)r   r   r   r<  r@  r   r   r   r   r8    s     r8  c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )TestMultiIndexingAutomateda:  
    These tests use code to mimic the C-Code indexing for selection.

    NOTE:

        * This still lacks tests for complex item setting.
        * If you change behavior of indexing, you might want to modify
          these tests to try more combinations.
        * Behavior was written to match numpy version 1.8. (though a
          first version matched 1.7.)
        * Only tuple indices are supported by the mimicking code.
          (and tested as of writing this)
        * Error types should match most of the time as long as there
          is only one error. For multiple errors, what gets raised
          will usually not be the same one. They are *not* tested.

    Update 2016-11-30: It is probably not worth maintaining this test
    indefinitely and it can be dropped if maintenance becomes a burden.

    c                 C   sH  t t g ddddd| _t d| _dtdt g d	t d
dgdd
ggt ddgddgggt	dddt	dddt	dddt	d d dt jdt j
dt g dt dgdgdggt jddgddggt d dt jddgt jdt jdgd tdt ddgg| _td ddgt d
gdg| _t	d d dg| _d S )N)r8   r)   r   r\   r8   r)   r   r\   )r8   r   r   r\   skipr   )TFFTFr%   rs   r1   rd   )r   r)   r)   rB   )r   r)   rD  rV      r   r.   )r   rM   prodrP   r   emptyrY   rb   r   rk   rV   rC   newbyteorderr   rD   intcomplex_indicessimple_indicesfill_indicesr!   r   r   r   setup_method=  s.   "



"z'TestMultiIndexingAutomated.setup_methodc              
   C   s  t |}g }d}d}d}d}d}d}	t|D ]}\}
}|du rqt|tjr>|jtkr>d}|jdkr3t||j7 }||j7 }q|t	u rK|	du rI|
}	qtt|t
rU|d7 }qt|tjstz
tj|tjd}W n	 tyn   tw |||
< n|jjdkr|jjdkrtd	|jdkrd}|d7 }|d7 }q|j| dk rt|dkrd|vr| |fS |	durt
ddg|j|  ||	|	d < t|D ]\}}t|t
rtj||j|  }|d
|g q|du r|dtjdgtjdg ||jd| d |j|d  }qt|tjr|jtkr|j|j|||j  kr%tztjt||j|||j  dd}W n tyS   d}tjdg|  tjd}Y nw |jdkr||jd| t|j|||j  f |j||j d  }|}nt|jdkr||j| ks||j|  k rt|jdkr||j| ks||j|  k rtt|dkr|d d dkr||	kr|d | q|d7 }|d|g q|dkrM|sM|dd }t t|j}g }|ddg d}d}|D ]C}|d7 }|d dkr:|d |dd  ||= |d8 }t||t|dd  D ]}|| || q,|t|d 7 }q |}|j||  }d}|D ]}|d dkrGt|dkrdqQ|j}|||t|dd   }||jd| t| t!f |j|t|dd  d  }tj"|dd  }|j#dkr|rtt$|dd |D ] \}}|j#dkrqt%||kst%|| k rtqt|dd t|krt&|dkrztj|dd |dd}W n ty   tdw tj|dd |dd}nt|j'|( |d}z||jd| |j |j|d d  }W n
 ty?   tw ||j7 }qQ|j'|d |d}|d7 }qQ||fS )a  Mimic multi dimensional indexing.

        Parameters
        ----------
        arr : ndarray
            Array to be indexed.
        indices : tuple of index objects

        Returns
        -------
        out : ndarray
            An array equivalent to the indexing operation (but always a copy).
            `arr[indices]` should be identical.
        no_copy : bool
            Whether the indexing operation requires a copy. If this is `True`,
            `np.may_share_memory(arr, arr[indices])` should be `True` (with
            some exceptions for scalars and possibly 0-d arrays).

        Notes
        -----
        While the function may mostly match the errors of normal indexing this
        is generally not the case.
        Tr   FNr)   rB   rY   r   z;arrays used as indices must be of integer (or boolean) typerG   nr   raise)moderd   ro   zinvalid index into 0-sizedwraprJ   ))list	enumerater>   r   rF   rC   rj   r:   r    rb   rk   r   rV   rp   kindr   rM   indicesr   appendrP   ravel_multi_indexnonzero	ExceptionsumrG  r   r	  insertextendremove	transposerN   rJ  	broadcastr   zipanyr   takeravel)r"   rT   rV  
in_indicesno_copy	num_fancy	fancy_dimerror_unless_broadcast_to_emptyr:   ellipsis_posr   ru   ax	flat_indxnew_indicesaxes
fancy_axesniai
orig_shape
orig_slicer   _indx_sizemir   r   r   _get_multi_indexZ  s<  









&
""

"


z+TestMultiIndexingAutomated._get_multi_indexc              
   C   s   z
|  ||\}}W n7 tyA } z+trt|}tt||j| tt||j|d tr6t	|t| W Y d}~dS d}~ww | 
|||| dS )a   Check a multi index item getting and simple setting.

        Parameters
        ----------
        arr : ndarray
            Array to be indexed, must be a reshaped arange.
        index : tuple of indexing objects
            Index being tested.
        r   Nrw  rZ  r
   r   r   r   rE   rW   r   r   _compare_index_resultr"   rT   rR   	mimic_getrf  r#  prev_refcountr   r   r   _check_multi_indexI  s   
	z-TestMultiIndexingAutomated._check_multi_indexc              
   C   s   z|  ||f\}}W n7 tyB } z+trt|}tt||j| tt||j|d tr7t	|t| W Y d}~dS d}~ww | 
|||| dS )aW  Check a single index item getting and simple setting.

        Parameters
        ----------
        arr : ndarray
            Array to be indexed, must be an arange.
        index : indexing object
            Index being tested. Must be a single index and not a tuple
            of indexing objects (see also `_check_multi_index`).
        r   Nrx  rz  r   r   r   _check_single_indexa  s   
	z.TestMultiIndexingAutomated._check_single_indexc                 C   s   |  }|| }t|| |jdkr6|jdkr6tt|||k tr6|r.tt	
|d ntt	
|d |  }|d ||< |jdkrGdS |rY|jdkrY|d7 }t|| dS |j|   d7  < t|| dS )z4Compare mimicked result to indexing result.
        r   r8   r%   rr   N)r   r   r   r:   r   r   may_share_memoryr
   r   r   r   flatrd  )r"   rT   rR   r{  rf  indexed_arrrY   r   r   r   ry  y  s&   


z0TestMultiIndexingAutomated._compare_index_resultc                 C   s   t d}t|t d d d|t d< t|d | | jt j| jtdf | | jt j| jtdd f | | jt j| jtdd f d S )Nr   Tr)   rB   ra   )N.)r   r   r   r}  r   
zeros_likerj   r;   r   r   r   test_boolean  s   

z'TestMultiIndexingAutomated.test_booleanc                    s2  t  O t ddt t ddtj dd  dD ]1}| j| j| j| jg}| j||< t	| D ]}t
 fdd|D }| | j| | | j| q/qW d    n1 sVw   Y  | | jd | | jd tt| jjd	 tt| jjd	d
 tt| jjd
d
dgd
d
f tt| jjd
d
dgd
d
fd
 d S )Nerrorr   c                 S   s   t | to| dkS )NrB  )r>   r  )r   r   r   r   isskip  s   z8TestMultiIndexingAutomated.test_multidim.<locals>.isskip)r   r%   r8   c                 3   s    | ]	} |s|V  qd S r5   r   )r  r   r  r   r   	<genexpr>  s    z;TestMultiIndexingAutomated.test_multidim.<locals>.<genexpr>)r   r   r   r   )r   r   r   r   r   r   r)   )warningscatch_warningsfilterwarningsDeprecationWarningr   VisibleDeprecationWarningrM  rK  rL  r   r   r}  r   rY   r   r    rW   r   )r"   
simple_postocheckrR   r   r  r   test_multidim  s,   

"z(TestMultiIndexingAutomated.test_multidimc                 C   s&   t d}| jD ]}| || qd S )NrI   )r   rM   rK  r~  )r"   r   rR   r   r   r   test_1d  s   

z"TestMultiIndexingAutomated.test_1dN)r   r   r   __doc__rN  rw  r}  r~  ry  r  r  r  r   r   r   r   rA  '  s     p rA  c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TestFloatNonIntegerArgumentz
    These test that ``TypeError`` is raised when you try to use
    non-integers as arguments to for indexing and slicing e.g. ``a[0.0:5]``
    and ``a[0.5]``, or other functions like ``array.reshape(1., -1)``.

    c                 C   st   t dggg}|t dg  |ddg  |d d ddgf  |d d dd d f  |d d d d d d f  d S )Nr   r   r   r;   r   r   r   test_valid_indexing  s   z/TestFloatNonIntegerArgument.test_valid_indexingc                 C   s|   t dggg}|d d   |dd   |d d  |dd  |d d d  |dd d  |d dd  |ddd  d S )Nr   r   r%   r)   r   r;   r   r   r   test_valid_slicing  s   z.TestFloatNonIntegerArgument.test_valid_slicingc                 C   sj   t dgg}ttt j|d ttt j|t ddf ttt j|dgd ttt j|dgt d d S )Nr   )r.   r.   rd   r.   rd   r   )r   r   r   r3   rP   rc  r   r;   r   r   r    test_non_integer_argument_errors  s
   z<TestFloatNonIntegerArgument.test_non_integer_argument_errorsc                 C   s4   dd }t t|dgtd |dgtd d S )Nc                 S   s   | | S r5   r   )r   rY   r   r   r   mult  r   zRTestFloatNonIntegerArgument.test_non_integer_sequence_multiplication.<locals>.multr)   r8   )r   r3   r   r   r?   )r"   r  r   r   r   (test_non_integer_sequence_multiplication  s   zDTestFloatNonIntegerArgument.test_non_integer_sequence_multiplicationc                 C   sN   t d}ttt j|d ttt j|d ttt j|d ttt j|d d S )N)r8   r8   r8         ?)r  r)   )r)   g@)g?g333333?)r   rD   r   r3   min)r"   r   r   r   r   test_reduce_axis_float_index  s
   
z8TestFloatNonIntegerArgument.test_reduce_axis_float_indexN)	r   r   r   r  r  r  r  r  r  r   r   r   r   r    s    
	r  c                   @   r   )TestBooleanIndexingc                 C   sz   t dggg}ttt j|d ttt j|t ddf tttjt d tt	tjt j
 ttt j|dgdfd d S )Nr)   )Trd   Trd   r   F)args)r   r   r   r3   rP   r   operatorrR   r	   r  True_rc  r;   r   r   r    test_bool_as_int_argument_errors  s   z4TestBooleanIndexing.test_bool_as_int_argument_errorsc                    sP   t d  d jdk  dddgdddgdggf dk tt fd	d
 d S )Nr%  )FT.)r   r%   r8   r1   Tr   r)   r%   r9  c                      s    dddgdf S )NFr   r)   .r   r   r   r   r   r     s    zETestBooleanIndexing.test_boolean_indexing_weirdness.<locals>.<lambda>)r   ri   r   r   r    r!   r   r   r   test_boolean_indexing_weirdness  s   
"z3TestBooleanIndexing.test_boolean_indexing_weirdnessc                    s   t d t dgd gttd fdd t dgd dg gttd fd	d t dgd
 gttd fdd t d t dgdgggttd fdd d S )N)r8   r8   Fr`   ztboolean index did not match indexed array along dimension 0; dimension is 3 but corresponding boolean dimension is 1c                           S r5   r   r   )r   idx1r   r   r   #  r   zETestBooleanIndexing.test_boolean_indexing_fast_path.<locals>.<lambda>r_   Tc                      r  r5   r   r   )r   idx2r   r   r   *  r   rI   c                      r  r5   r   r   )r   idx3r   r   r   1  r   )r)   r)   r%   ztboolean index did not match indexed array along dimension 1; dimension is 1 but corresponding boolean dimension is 2c                      r  r5   r   r   )r   r   r   r   r   9  r   )r   ri   r   r   r    r!   r   )r   r   r  r  r  r   test_boolean_indexing_fast_path  s,   

z3TestBooleanIndexing.test_boolean_indexing_fast_pathN)r   r   r   r  r  r  r   r   r   r   r    s    r  c                   @      e Zd ZdZdd ZdS )TestArrayToIndexDeprecationz7Creating an index from array not 0-D is an error.

    c                 C   sR   t dggg}tttjt dg ttt j||df ttt j|dg| d S )Nr)   rd   r   )r   r   r   r3   r  rR   rP   rc  r;   r   r   r   test_array_to_index_error@  s   z5TestArrayToIndexDeprecation.test_array_to_index_errorN)r   r   r   r  r  r   r   r   r   r  <      r  c                   @   r  )TestNonIntegerArrayLikezTests that array_likes only valid if can safely cast to integer.

    For instance, lists give IndexError when they cannot be safely cast to
    an integer.

    c                 C   s>   t d}tt|jddg tt|jddgf |g  d S )NrI   r  g      ?1r=  )r   rM   r   r    rW   r;   r   r   r   r,  P  s   
z"TestNonIntegerArrayLike.test_basicNr   r   r   r  r,  r   r   r   r   r  I  s    r  c                   @   r  )TestMultipleEllipsisErrorz/An index can only have a single ellipsis.

    c                    sL   t d tt fdd tt jtfd f tt jtfd f d S )NrI   c                      r   )N)..r   r   r   r   r   r   `  r   z6TestMultipleEllipsisError.test_basic.<locals>.<lambda>r%   r8   )r   rM   r   r    rW   rb   r!   r   r   r   r,  ^  s   
z$TestMultipleEllipsisError.test_basicNr  r   r   r   r   r  Z  r  r  c                   @   r7  )TestCApiAccessc                 C   s   t td}tt|tdd tt|tdd tt|tdd tt|tdd tt|tdd td}t|d ||d |	dd	}t|d
 ||d
 d S )Nr   r   rI   r   rI   rI   r1   r   r%   )
	functoolspartialr   r   r    r   ri   rM   r   rP   )r"   	subscriptr   r   r   r   test_getitemf  s   
zTestCApiAccess.test_getitemc                 C   s   t td}tt|tdd tt|tddd tt|tddd tt|tddd tt|tddd tt|tddd td}||dd t	|d dk |
d	d
}||dd t|d ddg d S )Nr)   rI   r   r   r   r  r  r1   r   r%   rd   )r  r  r   r   rp   r   ri   r    rM   r   rP   r   )r"   r  r   r   r   r   test_setitemv  s   
zTestCApiAccess.test_setitemN)r   r   r   r  r  r   r   r   r   r  e  s    r  ) r   r  r  r  r   numpyr   numpy.core._multiarray_testsr   	itertoolsr   numpy.testingr   r   r   r   r   r	   r
   r   r  r  r(  r1  r8  rA  r  r  r  r  r  r  r   r   r   r   <module>   s:    $    H	C@0   )78