o
    1&i\:                     @   s   d dl Zd dlZd dlZd dlmZmZ d dlm	Z	m
Z
 dd Zdd Zdd	 Zd
d Zdd Zdd Zdd ZG dd de
ZG dd dee	ZG dd dee	ZdS )    N)jittyped)TestCaseMemoryLeakMixinc                 C   
   t | S N)hqheapify)x r   AC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_heapq.pyr	   
      
r	   c                 C   r   r   )r   heappop)heapr   r   r   r      r   r   c                 C      t | |S r   )r   heappushr   itemr   r   r   r         r   c                 C   r   r   )r   heappushpopr   r   r   r   r      r   r   c                 C   r   r   )r   heapreplacer   r   r   r   r      r   r   c                 C   r   r   )r   	nsmallestniterabler   r   r   r      r   r   c                 C   r   r   )r   nlargestr   r   r   r   r   "   r   r   c                       s   e Zd Z f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  ZS )5
_TestHeapqc                    s    t t|   tjd| _d S )N*   )superr   setUpnprandomZRandomStaternd)self	__class__r   r   r   (   s   z_TestHeapq.setUpc                 C   s  t }tdd|}g d}| |}|| || | |t| dddtjtj tjg}t	|dD ]}t|}| |}|| || | |t| q4t
t|D ]}|| g}| |}|| || | |t| qVg d}| |}|| || | |t| d S )	NTZnopython
            	               r   A`"	@g      $g      @r/   )r)   !   )r(      )r-      )r	   r   listimplassertPreciseEquallistr    naninf	itertoolscombinations_with_replacementrangelen)r#   pyfunccfuncabZelement_poolr
   ir   r   r   test_heapify_basic_sanity,   s2   




z$_TestHeapq.test_heapify_basic_sanityc                 C   s8   t |D ]\}}|r|d d? }| || |k qd S )Nr(   )	enumerate
assertTrue)r#   r   posr   	parentposr   r   r   check_invariantS   s   z_TestHeapq.check_invariantc                 C   s   t }tdd|}t}tdd|}| dg}| dg}| | tdD ]}| jdd}|	| ||| | | q'g }	|rV||}| | |		| |sF|d d  }
|

  | t|
|	 | |	 d S )NTr&            r(   r   )r   r   r   r6   rI   r=   r"   Zrandnr   appendsortr7   r8   )r#   Zpyfunc_heappushcfunc_heappushZpyfunc_heappopcfunc_heappopr   datarC   r   resultsZdata_sortedr   r   r   test_push_popY   s,   




z_TestHeapq.test_push_popc                 C   sT   t }tdd|}ttdddg D ]}| | j|}|| | | qd S )NTr&   r(      i N  )r	   r   r8   r=   r6   r"   Zrandom_samplerI   )r#   r?   r@   sizer   r   r   r   test_heapifyt   s   z_TestHeapq.test_heapifyc                 C   s   t }tdd|}|   |  }|d W d    n1 s w   Y  d}| |t|j |  }|| ddg W d    n1 sHw   Y  d}| |t|j d S )NTr&   r(   r*   r.   heap argument must be a listy      ?      ?y       @      z>'<' not supported between instances of 'complex' and 'complex')r	   r   disable_leak_checkassertTypingErrorassertInstr	exceptionr6   r#   r?   r@   emsgr   r   r   test_heapify_exceptions   s   


z"_TestHeapq.test_heapify_exceptionsc           	      C   sz   t }tdd|}dd }| D ]*}t| | |}tt|D ]}||}||}| |t| | || q!qd S )NTr&   c                   s   sD    g dV  g dV  t jdt jd V  t ddd V  d S )Nr'   )r2   )r(   o   )r-   i  r*   )Z
fill_valued   )r    fullr9   tolistlinspacer   r   r   r   a_variations   s
   

z:_TestHeapq.test_heappop_basic_sanity.<locals>.a_variations)r   r   r	   r6   r=   r>   r7   r8   )	r#   r?   r@   rh   rA   rB   rC   Zval_pyZval_cr   r   r   test_heappop_basic_sanity   s   

z$_TestHeapq.test_heappop_basic_sanityc                 C   sd   t }tdd|}|   |  }|d W d    n1 s w   Y  d}| |t|j d S )NTr&   rV   rW   )r   r   rX   rY   rZ   r[   r\   r]   r   r   r   test_heappop_exceptions   s   

z"_TestHeapq.test_heappop_exceptionsc                 c   s^    |  g dV  tddd}|  |V  |  |d d d V  | j| |  |V  d S )Nr'   rb   r-      )r6   r    rg   r"   shuffle)r#   rA   r   r   r   	iterables   s   z_TestHeapq.iterablesc                    s   t }tdd|}t}tdd| |  D ]-}t|}| |dg|D ]}|| q' fddttD }| 	|| qd S )NTr&   r   c                       g | ]} qS r   r   .0_Z	cfunc_popr   r   r   
<listcomp>       z2_TestHeapq.test_heappush_basic.<locals>.<listcomp>)
r   r   r   rn   sortedr6   popr=   r>   r7   )r#   Zpyfunc_pushZ
cfunc_pushZ
pyfunc_popr   expectedvaluegotr   rs   r   test_heappush_basic   s   z_TestHeapq.test_heappush_basicc                 C      t }tdd|}|   |  }|dd W d    n1 s!w   Y  d}| |t|j |  }|| g dd W d    n1 sJw   Y  d}| |t|j d S )NTr&   rV   r/   rW   g      @'heap type must be the same as item type)r   r   rX   rY   rZ   r[   r\   r6   r]   r   r   r   test_heappush_exceptions      

z#_TestHeapq.test_heappush_exceptionsc                 C   s   t }tdd|}|  D ]}tdt|d D ]}|d|}|d|}| || qq|d| g d}| |g  |d| g d}| |dg |dd	}| |ddg |dtd
}| |g d d S )NTr&   rc   r)   r(   Fr)   r-   r(   r-   r/   r*   r.   r)   r-   r(   r/   )r   r(   r-   )	r   r   rn   r=   r>   r7   r6   r    aranger#   r?   r@   r   r   rx   rz   outr   r   r   test_nsmallest_basic       


z_TestHeapq.test_nsmallest_basicc                 C   s   t }tdd|}|  D ]}tdt|d D ]}|d|}|d|}| || qq|d| g d}| |g  |d| g d}| |dg |dd	}| |d
dg |dtd
}| |g d d S )NTr&   rc   r)   r(   Fr   r-   r   r/   r*   )r*   r.   r)   )	r   r   rn   r=   r>   r7   r6   r    r   r   r   r   r   test_nlargest_basic   r   z_TestHeapq.test_nlargest_basicc                 C   s   |    |  }|d| g d W d    n1 sw   Y  d}| |t|j |  }|dd W d    n1 sAw   Y  d}| |t|j d S )Ng@r   z%First argument 'n' must be an integerr-   rd   z+Second argument 'iterable' must be iterable)rX   rY   r6   rZ   r[   r\   )r#   r@   r^   r_   r   r   r   _assert_typing_error  s   

z_TestHeapq._assert_typing_errorc                 C       t }tdd|}| | d S NTr&   )r   r   r   r#   r?   r@   r   r   r   test_nsmallest_exceptions!     z$_TestHeapq.test_nsmallest_exceptionsc                 C   r   r   )r   r   r   r   r   r   r   test_nlargest_exceptions&  r   z#_TestHeapq.test_nlargest_exceptionsc                 C   s   t }tdd|}g d}t| | |}dD ]}||| ||| | |t| qtddd}tj|d< tj	|d	< |
 }t| | |}d
dtj	 tj	fD ]}||| ||| | |t| qTd S )NTr&   r'   )r.            r.   rl   g      r1   )r   r   r	   r6   r7   r8   r    rg   r9   r:   rf   )r#   r?   r@   rA   rB   r   r   r   r   test_heapreplace_basic+  s(   







z!_TestHeapq.test_heapreplace_basicc                 C   r|   )NTr&   rV   rl   rW   rJ   r}   )r   r   rX   rY   rZ   r[   r\   r6   r]   r   r   r   test_heapreplace_exceptionsF  r   z&_TestHeapq.test_heapreplace_exceptionsc                 c   s&    z	 t |V  q ty   Y d S w r   )r   
IndexError)r#   r   r   r   r   heapiterY  s   
z_TestHeapq.heapiterc                 C   s   t ddt}t ddt}| jtdd }| |d d }|| |dd  D ]}||d kr9||| q,| t	| 
t	|t|dd   d S )NTr&       
   r   rb   )r   r	   r   r"   choicer=   rf   r6   r7   r8   r   rv   )r#   cfunc_heapifyZcfunc_heapreplacerP   r   r   r   r   r   
test_nbest`  s   
z_TestHeapq.test_nbestc                    s   t ddt}t ddt}t ddt tdD ]M}tjdtjd}| | j	
|d}|d@ r<|d d  | n| |d g|dd  D ]}|| qJ fd	d
tdD }| |t| qd S )NTr&   rd   r*   Zdtyper   r(   r   c                    ro   r   r   rp   rO   r   r   r   rt     ru   z,_TestHeapq.test_heapsort.<locals>.<listcomp>)r   r	   r   r   r=   r    r   float64r6   r"   r   r7   rv   )r#   r   rN   ZtrialvaluesrP   r   Zheap_sortedr   r   r   test_heapsortq  s   
z_TestHeapq.test_heapsortc                 C   sZ   t }tdd|}| | jtdd}dD ]}| t|||t|d |  qd S )NTr&   r   r   	r   r(   r-   r   rd   i  i  r   iL  )	r   r   r6   r"   r   r=   r7   r8   rv   r#   r?   r@   rP   r   r   r   r   test_nsmallest  s   $z_TestHeapq.test_nsmallestc                 C   s^   t }tdd|}| | jtdd}dD ]}| t|||t|ddd |  qd S )NTr&   r   r   r   )reverse)	r   r   r6   r"   r   r=   r7   r8   rv   r   r   r   r   test_nlargest  s   z_TestHeapq.test_nlargestc           	      C   s   t }tdd|}t}tdd|}tjdtjd}| | j|d}|d d }|| |dd  D ]}||| q4| 	t
| t
|t|dd   d S )NTr&   r   r   r   r   rb   )r   r   r	   r    r   r   r6   r"   r   r7   r8   r   rv   )	r#   Zpyfunc_heappushpopZcfunc_heappushpopZpyfunc_heapifyr   r   rP   r   r   r   r   r   test_nbest_with_pushpop  s   z"_TestHeapq.test_nbest_with_pushpopc                 C   s   t }tdd|}| dg}||d}| t||fdgdf | t|d t | t|t | dg}||d}| t||fdgdf | dg}||d}| t||fdgdf d S )	NTr&   g      ?g      $@r   r   r,   r4   )r   r   r6   r7   r8   typefloat)r#   r?   r@   hr
   r   r   r   test_heappushpop  s   


z_TestHeapq.test_heappushpopc                 C   r|   )NTr&   rV   rl   rW   Fr}   )r   r   rX   rY   rZ   r[   r\   r6   r]   r   r   r   test_heappushpop_exceptions  r   z&_TestHeapq.test_heappushpop_exceptions)__name__
__module____qualname__r   rD   rI   rR   rU   r`   ri   rj   rn   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r$   r   r   &   s6    'r   c                   @   s   e Zd ZdZeZdS )TestHeapqReflectedListzTest heapq with reflected listsN)r   r   r   __doc__r8   r6   r   r   r   r   r     s    r   c                   @   s   e Zd ZdZejZdS )TestHeapqTypedListzTest heapq with typed listsN)r   r   r   r   r   Listr6   r   r   r   r   r     s    
r   )heapqr   r;   numpyr    Znumbar   r   Znumba.tests.supportr   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s$       8