o
    Z۷ix8                     @   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   L/home/ubuntu/vllm_env/lib/python3.10/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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      @r1   )r+   !   )r*      )r/      )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_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   r8   rL   r?   r#   randnr   appendsortr9   r:   )r$   pyfunc_heappushcfunc_heappushpyfunc_heappopcfunc_heappopr   datarF   r   results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   r:   r?   r8   r#   random_samplerL   )r$   rA   rB   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,   r0   heap argument must be a listy      ?      ?y       @      z>'<' not supported between instances of 'complex' and 'complex')r	   r   disable_leak_checkassertTypingErrorassertInstr	exceptionr8   r$   rA   rB   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)   )r4   )r*   o   )r/   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	   r8   r?   r@   r9   r:   )	r$   rA   rB   rq   rC   rD   rF   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'   r^   r_   )r   r   r`   ra   rb   rc   rd   re   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)   rk   r/      )r8   r    rp   r#   shuffle)r$   rC   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   ry   sortedr8   popr?   r@   r9   )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'   r^   r1   r_   g      @'heap type must be the same as item type)r   r   r`   ra   rb   rc   rd   r8   re   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'   rl   r+   r*   Fr+   r/   r*   r/   r1   r,   r0   r+   r/   r*   r1   )r   r*   r/   )	r   r   ry   r?   r@   r9   r8   r    aranger$   rA   rB   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'   rl   r+   r*   Fr   r/   r   r1   r,   )r,   r0   r+   )	r   r   ry   r?   r@   r9   r8   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/   rm   z+Second argument 'iterable' must be iterable)r`   ra   r8   rb   rc   rd   )r$   rB   rf   rg   r   r   r   _assert_typing_error  s   

z_TestHeapq._assert_typing_errorc                 C       t }tdd|}| | d S NTr'   )r   r   r   r$   rA   rB   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)   )r0            r0   rw   g      r3   )r   r   r	   r8   r9   r:   r    rp   r;   r<   ro   )r$   rA   rB   rC   rD   r   r   r   r   test_heapreplace_basic+  s(   







z!_TestHeapq.test_heapreplace_basicc                 C   r   )NTr'   r^   rw   r_   rM   r   )r   r   r`   ra   rb   rc   rd   r8   re   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   rk   )r   r	   r   r#   choicer?   ro   r8   r9   r:   r   r   )r$   cfunc_heapifycfunc_heapreplacerV   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'   rm   r,   dtyper   r*   r   c                    rz   r   r   r{   rU   r   r   r   r     r   z,_TestHeapq.test_heapsort.<locals>.<listcomp>)r   r	   r   r   r?   r    r   float64r8   r#   r   r9   r   )r$   r   rS   trialvaluesrV   r   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   rm   i  i  r   iL  )	r   r   r8   r#   r   r?   r9   r:   r   r$   rA   rB   rV   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   r8   r#   r   r?   r9   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   rk   )r   r   r	   r    r   r   r8   r#   r   r9   r:   r   r   )	r$   pyfunc_heappushpopcfunc_heappushpoppyfunc_heapifyr   r   rV   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.   r6   )r   r   r8   r9   r:   typefloat)r$   rA   rB   hr
   r   r   r   test_heappushpop  s   


z_TestHeapq.test_heappushpopc                 C   r   )NTr'   r^   rw   r_   Fr   )r   r   r`   ra   rb   rc   rd   r8   re   r   r   r   test_heappushpop_exceptions  r   z&_TestHeapq.test_heappushpop_exceptions)__name__
__module____qualname__r   rG   rL   rY   r]   rh   rt   ru   ry   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:   r8   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   Listr8   r   r   r   r   r     s    
r   )heapqr   r=   numpyr    numbar   r   numba.tests.supportr   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s$       8