o
    8ήc9                     @   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	 d dl
mZmZmZ e	 Zde_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)Flags)TestCaseCompilationCacheMemoryLeakMixinTc                 C   
   t | S N)hqheapify)x r   =/tmp/pip-target-vg8gfxp4/lib/python/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 | _tjd| _d S )N*   )	superr   setUpr   ccachenprandom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nopython
            	               r   A`"	@g      $g      @r4   )r.   !   )r-      )r2      )r   r   listimplassertPreciseEquallistr#   naninf	itertoolscombinations_with_replacementrangelen)r'   pyfunccfuncabelement_poolr   ir   r   r   test_heapify_basic_sanity1   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_invariantX   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   r;   rO   rB   r&   randnr   appendsortr<   r=   )r'   pyfunc_heappushcfunc_heappushpyfunc_heappopcfunc_heappopr   datarI   r   resultsdata_sortedr   r   r   test_push_pop^   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   r=   rB   r;   r&   random_samplerO   )r'   rD   rE   sizer   r   r   r   test_heapifyy   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/   r3   heap argument must be a listy      ?      ?y       @      z>'<' not supported between instances of 'complex' and 'complex')r   r   disable_leak_checkassertTypingErrorassertInstr	exceptionr;   r'   rD   rE   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,   )r7   )r-   o   )r2   i  r/   )
fill_valued   )r#   fullr>   tolistlinspacer   r   r   r   a_variations   s
   

z:_TestHeapq.test_heappop_basic_sanity.<locals>.a_variations)r   r   r   r;   rB   rC   r<   r=   )	r'   rD   rE   rt   rF   rG   rI   val_py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*   ra   rb   )r   r   rc   rd   re   rf   rg   rh   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,   rn   r2      )r;   r#   rs   r&   shuffle)r'   rF   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_	cfunc_popr   r   r   
<listcomp>       z2_TestHeapq.test_heappush_basic.<locals>.<listcomp>)
r   r   r   r|   sortedr;   poprB   rC   r<   )r'   pyfunc_push
cfunc_push
pyfunc_popr   expectedvaluegotr   r   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*   ra   r4   rb   g      @'heap type must be the same as item type)r   r   rc   rd   re   rf   rg   r;   rh   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*   ro   r.   r-   Fr.   r2   r-   r2   r4   r/   r3   r.   r2   r-   r4   )r   r-   r2   )	r   r   r|   rB   rC   r<   r;   r#   aranger'   rD   rE   r   r   r   r   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*   ro   r.   r-   Fr   r2   r   r4   r/   )r/   r3   r.   )	r   r   r|   rB   rC   r<   r;   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 integerr2   rp   z+Second argument 'iterable' must be iterable)rc   rd   r;   re   rf   rg   )r'   rE   ri   rj   r   r   r   _assert_typing_error  s   

z_TestHeapq._assert_typing_errorc                 C       t }tdd|}| | d S NTr*   )r   r   r   r'   rD   rE   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,   )r3            r3   rz   g      r6   )r   r   r   r;   r<   r=   r#   rs   r>   r?   rr   )r'   rD   rE   rF   rG   r   r   r   r   test_heapreplace_basic0  s(   







z!_TestHeapq.test_heapreplace_basicc                 C   r   )NTr*   ra   rz   rb   rP   r   )r   r   rc   rd   re   rf   rg   r;   rh   r   r   r   test_heapreplace_exceptionsK  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   heapiter^  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   rn   )r   r   r   r&   choicerB   rr   r;   r<   r=   r   r   )r'   cfunc_heapifycfunc_heapreplacerY   r   r   r   r   r   
test_nbeste  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*   rp   r/   dtyper   r-   r   c                    r}   r   r   r~   rX   r   r   r   r     r   z,_TestHeapq.test_heapsort.<locals>.<listcomp>)r   r   r   r   rB   r#   r   float64r;   r&   r   r<   r   )r'   r   rV   trialvaluesrY   r   heap_sortedr   r   r   test_heapsortv  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-   r2   r   rp   i  i  r   iL  )	r   r   r;   r&   r   rB   r<   r=   r   r'   rD   rE   rY   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   r;   r&   r   rB   r<   r=   r   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   rn   )r   r   r   r#   r   r   r;   r&   r   r<   r=   r   r   )	r'   pyfunc_heappushpopcfunc_heappushpoppyfunc_heapifyr   r   rY   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   r1   r9   )r   r   r;   r<   r=   typefloat)r'   rD   rE   hr   r   r   r   test_heappushpop  s   


z_TestHeapq.test_heappushpopc                 C   r   )NTr*   ra   rz   rb   Fr   )r   r   rc   rd   re   rf   rg   r;   rh   r   r   r   test_heappushpop_exceptions  r   z&_TestHeapq.test_heappushpop_exceptions)__name__
__module____qualname__r!   rJ   rO   r\   r`   rk   rw   rx   r|   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__r=   r;   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   Listr;   r   r   r   r   r     s    
r   )heapqr
   r@   numpyr#   numbar   r   numba.core.compilerr   numba.tests.supportr   r   r   no_pyobj_flagsnrtr   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s*       9