a
    
d9F                     @   s   d dl Z d dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZ dd Zdd	 Zd
d Zdd ZG dd deeZG dd deeZedkre  dS )    N)jittypeofnjit)types)TypingError)MemoryLeakMixinTestCasec                 C   s   | | S N )abr
   r
   h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/numba/tests/test_fancy_indexing.pygetitem_usecase   s    r   c                 C   s   || |< d S r	   r
   )r   idxr   r
   r
   r   setitem_usecase   s    r   c                 C   s   t | |S r	   npZtake)Aindicesr
   r
   r   np_take   s    r   c                 C   s   t j| ||dS )Naxisr   )r   r   r   r
   r
   r   np_take_kws   s    r   c                   @   s   e Zd Zd#ddZd$ddZd%ddZd&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"S )'TestFancyIndexingTc                 C   sD   t d|d dgg}|r@|t dd|d gt g dg7 }|S )Nr      )r   r   r   r   )r   int16Zuint16Zbool_)selfNmanychoicesr
   r
   r   generate_advanced_indices   s    z+TestFancyIndexing.generate_advanced_indicesc              	   c   s   |rVt dddt d|d dt dddt |d ddt | d ddt d| dg}nt d|d dt d| dg}t|d D ]}tj||dD ]
}|V  qqdS )zG
        Generate basic index tuples with 0 to *maxdim* items.
        Nr   r      r   )repeat)slicerange	itertoolsproduct)r   r   maxdimr   r    ndimtupr
   r
   r   generate_basic_index_tuples!   s    

z-TestFancyIndexing.generate_basic_index_tuplesc                 c   sj   t | j||d}t|d D ]F}| ||d |D ].}|D ]$}|d| |f ||d  V  q<q4qdS )z
        Generate advanced index tuples by generating basic index tuples
        and adding a single advanced index item.
        r   r   N)listr!   r&   r,   )r   r   r)   r   r    ir+   Zadvr
   r
   r   generate_advanced_index_tuples8   s
    z0TestFancyIndexing.generate_advanced_index_tuplesc                 c   sN   |  |||D ]:}tt|d D ]$}|d| tf ||d  V  q"qdS )zr
        Same as generate_advanced_index_tuples(), but also insert an
        ellipsis at various points.
        r   N)r0   r&   lenEllipsis)r   r   r)   r   r+   r/   r
   r
   r   ,generate_advanced_index_tuples_with_ellipsisE   s    z>TestFancyIndexing.generate_advanced_index_tuples_with_ellipsisc           
      C   s   t }tdd|}| }|jp"|}|D ]r}|||}|j|usDJ |||}	| |	j|j | |	j|j tj	|	| |	j
r(|	d tj	|| q(d S )NTZnopython*   )r   r   copybaseassertEqualshapedtyper   testingassert_equalsizefill)
r   arrr   pyfunccfuncorig	orig_baseindexexpectedgotr
   r
   r   check_getitem_indicesN   s    



z'TestFancyIndexing.check_getitem_indicesc                 C   sF   d}d}t || |f| t j}| ||}| || d S N      )r   arangereshapeastypeint32r0   rG   r   r   r*   r?   r   r
   r
   r   test_getitem_tupled   s
    "z$TestFancyIndexing.test_getitem_tuplec                 C   sJ   d}d}t || |f| t j}| j||dd}| || d S NrI   rJ   Fr-   )r   rK   rL   rM   rN   r3   rG   rO   r
   r
   r   test_getitem_tuple_and_ellipsism   s    "z1TestFancyIndexing.test_getitem_tuple_and_ellipsisc                 C   s>   t dddd }td}||d | |d |d  d S )NTr4   c                 S   s   | d | d< d S )N).r   ).r   r
   )r?   vr
   r
   r   fooy   s    z7TestFancyIndexing.test_ellipsis_getsetitem.<locals>.foor"   r   r   )r   r   rK   r8   )r   rT   r?   r
   r
   r   test_ellipsis_getsetitemw   s
    


z*TestFancyIndexing.test_ellipsis_getsetitemc                 C   sD   d}d}t || |f| t j}| |}| || d S rH   )r   rK   rL   rM   rN   r!   rG   rO   r
   r
   r   test_getitem_array   s
    "
z$TestFancyIndexing.test_getitem_arrayc           	      C   s   t }tdd|}|D ]f}|| }t|}t|}|||| |||| | |j|j | |j|j tj|| qd S )NTr4   )	r   r   r   
zeros_liker8   r9   r:   r;   r<   )	r   r?   r   r@   rA   rD   srcrE   rF   r
   r
   r   check_setitem_indices   s    

z'TestFancyIndexing.check_setitem_indicesc                 C   sF   d}d}t || |f| t j}| ||}| || d S rH   )r   rK   rL   rM   rN   r0   rY   rO   r
   r
   r   test_setitem_tuple   s
    "z$TestFancyIndexing.test_setitem_tuplec                 C   sJ   d}d}t || |f| t j}| j||dd}| || d S rQ   )r   rK   rL   rM   rN   r3   rY   rO   r
   r
   r   test_setitem_tuple_and_ellipsis   s    "z1TestFancyIndexing.test_setitem_tuple_and_ellipsisc                 C   sH   d}d}t || |f| t jd }| |}| || d S )NrI   rJ   
   )r   rK   rL   rM   rN   r!   rY   rO   r
   r
   r   test_setitem_array   s
    &
z$TestFancyIndexing.test_setitem_arrayc                 C   s  t }tdd|}tdtdftdtdftjdtjdtjdtjdftjdtjdtjdtjdftjdd	dtd
ftjdd	dtdftjg dd	dtjdddftjdtdtjdtdfg}|D ]4\}}| }||d| ||d| | 	|| qd S )NTr4   rJ   gQ	@r"   r:   r      z<U3abc)rJ   r   )r`   defZghiZWXYZz<U4y       @      @r   )
r   r   r   zerosarrayint64float64complexr6   assertPreciseEqual)r   r@   rA   Zinpsx1rS   Zx2r
   r
   r   test_setitem_0d   s$    z!TestFancyIndexing.test_setitem_0dc           
   	      s  t tdd  fdd}g }|d |tg d |tdgdggdgdggg |g d |d	 |d
 tjtjfD ],}tjd|dd}|D ]}||| qq|j	}|| d t|t| d |g| d gg}|D ]<}
t  || W d    n1 s00    Y  q 
t  |dg W d    n1 sl0    Y  
t* tddt}	|	|dd W d    n1 s0    Y  
t, tddt}	|	|ddd W d    n1 s0    Y    d S )NTr4   c                    s@   | |} | |} || t|dr<|j|jk d S )Norder)rg   hasattrr8   rj   )r?   indrE   rF   rA   r@   r   r
   r   check   s
    


z-TestFancyIndexing.test_np_take.<locals>.checkr   )r   r_   r      rJ   r_   ro   r   r_   r   )rp   )ro   rJ   r"      r^   )rI   rJ   g333333?r   )r   r   appendr   rc   rd   Z
complex128rK   rL   r=   assertRaises
IndexErrorr   r   Zdisable_leak_check)
r   rn   Ztest_indicesdtr   rl   ZszAZillegal_indicesxZtake_kwsr
   rm   r   test_np_take   s<    
$

 .,,.zTestFancyIndexing.test_np_takec                 C   s  t dd }t dd }tdd}td tjftjtd ftdtjtg dftjtg dtd ftdttjtg dftg dtjtftjtdtjtg dftg dtd tjftjtdttjtg dftg dtjtjtftjtg dtjtftd	tg dtjd ftjtg dtd fg}|j}|}|j}|}|D ]h}	|||	}
|||	}tj	
|
| t|}||	 }|| |	|}
|| |	|}tj	
|
| qjd S )
Nc                 S   s   | | S r	   r
   )r   r   r
   r
   r   np_new_axis_getitem  s    z;TestFancyIndexing.test_newaxis.<locals>.np_new_axis_getitemc                 S   s   || |< | S r	   r
   )r   r   itemr
   r
   r   np_new_axis_setitem  s    z;TestFancyIndexing.test_newaxis.<locals>.np_new_axis_setitemiH  )rI   r_         r   )r   r"   r   rJ   )r   r   rK   rL   r%   Znewaxisrc   r2   py_funcr;   r<   rW   r6   )r   rx   rz   r   Z	idx_casesZpyfunc_getitemZcfunc_getitemZpyfunc_setitemZcfunc_setitemr   rE   rF   Za_emptyry   r
   r
   r   test_newaxis  s@    




zTestFancyIndexing.test_newaxisN)T)T)T)T)__name__
__module____qualname__r!   r,   r0   r3   rG   rP   rR   rU   rV   rY   rZ   r[   r]   ri   rw   r~   r
   r
   r
   r   r      s    



		


9r   c                       s  e Zd ZdZedddeg ddfdeg dededfedeg dfeg ddefedeg ddeddfeg ddeeddfedddeg ddfdeg d	ededfgZ fd
dZ	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )TestFancyIndexingMultiDim)r_   r{   r|      	   r\   rI   r_   rJ   )r   r   rJ   rI   r"   r   N)TFTFTFF)TFTFTFc                    s   t    tjd| _d S )Nr   )supersetUpr   randomZdefault_rngrng)r   	__class__r
   r   r   I  s    
zTestFancyIndexingMultiDim.setUpc                 C   s   t | j}td d d td|d d tdd dt|d d dt| d dd td| dtd|d d td| dg}tt|}g }d}t|D ]L}| jjdddd	}| jj	|d
d	
 }| j	d
}	|||	< |t| qt|D ]L}| jjdddd	}| jj	|d
d	
 }| j	d
}	|||	< |t| qt|D ]d}| jjdddd	}| jj	|d
d	
 }| jj	d
ddd}	|||	d < t||	d < |t| q>t|D ]n}| jjdddd	}| jj	|d
d	
 }| j	d
}	| j|	 }
tj| jj	d|
d	td||	< |t| q|S )Nr   r   r"   r   r#      r_      r=   rI   F)r=   replacer^   )minr9   r%   r.   r   rK   r&   r   Zintegerschoicetolistrr   tupler2   rc   bool)r   r   Zslice_choicesZinteger_choicesr   K_Z	array_idxZcurr_idxZ
_array_idxZbool_arr_shaper
   r
   r   generate_random_indicesM  sV    


	

z1TestFancyIndexingMultiDim.generate_random_indicesc           	      C   s   t dd }tjjdd|d}|j}|jp,|}|||}|||}| |j| | |j|j | |j	|j	 tj
|| | t|| d S )Nc                 S   s   | | S r	   r
   )rc   r   r
   r
   r   numba_get_item  s    zGTestFancyIndexingMultiDim.check_getitem_indices.<locals>.numba_get_itemr   ro   r   )r   r   r   randintr}   r7   ZassertNotInr8   r9   r:   r;   r<   ZassertFalseZmay_share_memory)	r   	arr_shaperD   r   r?   Zget_itemrC   rE   rF   r
   r
   r   rG     s    



z/TestFancyIndexingMultiDim.check_getitem_indicesc                 C   s   t dd }tjjdd|d}|| }t|}t|}|||| |||| | |j|j | |j|j tj	
|| d S )Nc                 S   s   || |< d S r	   r
   )rc   r   ry   r
   r
   r   set_item  s    zATestFancyIndexingMultiDim.check_setitem_indices.<locals>.set_itemr   ro   r   )r   r   r   r   rW   r}   r8   r9   r:   r;   r<   )r   r   rD   r   r?   rX   rE   rF   r
   r
   r   rY     s    


z/TestFancyIndexingMultiDim.check_setitem_indicesc              	   C   s^   | j  }||  7 }|D ]>}| j|d | | j| W d    q1 sN0    Y  qd S N)r   )indexing_casesr6   r   subTestrG   r9   r   r   r   r
   r
   r   test_getitem  s
    
z&TestFancyIndexingMultiDim.test_getitemc              	   C   s^   | j  }||  7 }|D ]>}| j|d | | j| W d    q1 sN0    Y  qd S r   )r   r6   r   r   rY   r9   r   r
   r
   r   test_setitem  s
    
z&TestFancyIndexingMultiDim.test_setitemc              	   C   s   dddt ddgddggffdddt ddgt ddgffddt ddgtd ddffg}|D ]R\}}| t}| | j| W d    n1 s0    Y  | |t|j	 qfd S )	Nz,Multi-dimensional indices are not supported.r   rJ   r   r"   z:Using more than one non-scalar array index is unsupported.zUsing more than one indexing subspace is unsupported. An indexing subspace is a group of one or more consecutive indices comprising integer or array types.rI   )
r   rc   r%   rs   r   rG   r9   ZassertInstr	exception)r   Zerr_idx_caseserrr   Zraisesr
   r
   r   %test_unsupported_condition_exceptions  s"    ,z?TestFancyIndexingMultiDim.test_unsupported_condition_exceptions)r   r   r   r9   r%   r   rc   r2   r   r   r   rG   rY   r   r   r   __classcell__r
   r
   r   r   r   0  s,   
@r   __main__)r'   numpyr   ZunittestZnumbar   r   r   Z
numba.corer   Znumba.core.errorsr   Znumba.tests.supportr   r   r   r   r   r   r   r   r   mainr
   r
   r
   r   <module>   s"      8