B
     \*5                 @   s   d dl mZmZm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 e Zed 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eZdS )    )print_functionabsolute_importdivisionN)jit)Flags   )TestCaseCompilationCacheMemoryLeakMixinZnrtc             C   s
   t | S )N)hqheapify)x r   5lib/python3.7/site-packages/numba/tests/test_heapq.pyr      s    r   c             C   s
   t | S )N)r   heappop)heapr   r   r   r      s    r   c             C   s   t | |S )N)r   heappush)r   itemr   r   r   r      s    r   c             C   s   t | |S )N)r   heappushpop)r   r   r   r   r   r      s    r   c             C   s   t | |S )N)r   heapreplace)r   r   r   r   r   r       s    r   c             C   s   t | |S )N)r   	nsmallest)niterabler   r   r   r   $   s    r   c             C   s   t | |S )N)r   nlargest)r   r   r   r   r   r   (   s    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	   ZccachenpZrandomZRandomStaternd)self)	__class__r   r   r   .   s    zTestHeapq.setUpc          
   C   s.  t }tdd|}ddddddd	d
ddg
}|d d  }|| || | || dddtjtj tjg}xDt|d
D ]4}t|}|d d  }|| || | || qxW xFt	t
|D ]6}|| g}|d d  }|| || | || qW dddg}|d d  }|| || | || d S )NT)nopythonr            	               r   gA`"	@g      $g      @)r#   !   )r      )r'      )r   r   assertPreciseEqualr   naninf	itertoolscombinations_with_replacementlistrangelen)r    pyfunccfuncabZelement_poolr   ir   r   r   test_heapify_basic_sanity3   s2    

z#TestHeapq.test_heapify_basic_sanityc             C   s<   x6t |D ]*\}}|r
|d d? }| || |k q
W d S )Nr   )	enumerateZ
assertTrue)r    r   posr   	parentposr   r   r   check_invariantZ   s    zTestHeapq.check_invariantc             C   s   t }tdd|}t}tdd|}dg}dg}| | x@tdD ]4}| jdd}|| ||| | | qDW g }	x$|r||}| | |	| qW |d d  }
|
	  | 
|
|	 | |	 d S )NT)r"   g         r   r   )r   r   r   r?   r4   r   Zrandnr   appendsortr.   )r    Zpyfunc_heappushcfunc_heappushZpyfunc_heappopcfunc_heappopr   datar:   r   ZresultsZdata_sortedr   r   r   test_push_pop`   s*    



zTestHeapq.test_push_popc             C   sV   t }tdd|}x>ttdddg D ]&}| j| }|| | | q(W d S )NT)r"   r      i N  )r   r   r3   r4   r   Zrandom_sampletolistr?   )r    r6   r7   sizer   r   r   r   test_heapify{   s    zTestHeapq.test_heapifyc          	   C   s   t }tdd|}|   |  }|d W d Q R X d}| |t|j |  }|ddg W d Q R X d}| |t|j d S )NT)r"   )r   r$   r(   zheap argument must be a listy      ?      ?y       @      z>'<' not supported between instances of 'complex' and 'complex')r   r   disable_leak_checkassertTypingErrorassertInstr	exception)r    r6   r7   emsgr   r   r   test_heapify_exceptions   s    

z!TestHeapq.test_heapify_exceptionsc       	      C   s   t }tdd|}dd }x`| D ]V}t| |d d  }x<tt|D ],}||}||}| || | || qHW q"W d S )NT)r"   c            
   s   sT   ddddddddd	d
g
V  dddgV  t jdt jd V  t ddd V  d S )Nr   r#   r$   r%   r&   r'   r(   r)   r*   r   )r#   r+   )r   o   )r'   i  )Z
fill_valueid   )r   Zfullr/   rH   linspacer   r   r   r   a_variations   s    z9TestHeapq.test_heappop_basic_sanity.<locals>.a_variations)r   r   r   r4   r5   r.   )	r    r6   r7   rW   r8   r9   r:   Zval_pyZval_cr   r   r   test_heappop_basic_sanity   s    z#TestHeapq.test_heappop_basic_sanityc          	   C   sP   t }tdd|}|   |  }|d W d Q R X d}| |t|j d S )NT)r"   )r   r$   r(   zheap argument must be a list)r   r   rK   rL   rM   rN   rO   )r    r6   r7   rP   rQ   r   r   r   test_heappop_exceptions   s    
z!TestHeapq.test_heappop_exceptionsc          
   c   s`   ddddddddd	d
g
V  t ddd}| V  |d d d  V  | j| | V  d S )Nr   r#   r$   r%   r&   r'   r(   r)   r*   r   i   )r   rV   rH   r   Zshuffle)r    r8   r   r   r   	iterables   s    
zTestHeapq.iterablesc                s   t }tdd|}t}tdd| xd|  D ]X}t|}|dgx|D ]}|| qLW  fddttD }| || q.W d S )NT)r"   r   c                s   g | ]} qS r   r   ).0_)	cfunc_popr   r   r   
<listcomp>   s    z1TestHeapq.test_heappush_basic.<locals>.<listcomp>)	r   r   r   r\   sortedpopr4   r5   r.   )r    Zpyfunc_pushZ
cfunc_pushZ
pyfunc_popr   expectedvaluegotr   )r_   r   r   test_heappush_basic   s    
zTestHeapq.test_heappush_basicc          	   C   s   t }tdd|}|   |  }|dd W d Q R X d}| |t|j |  }|dddgd	 W d Q R X d
}| |t|j d S )NT)r"   )r   r$   r(   r)   zheap argument must be a listr   r$   r(   g      @z'heap type must be the same as item type)r   r   rK   rL   rM   rN   rO   )r    r6   r7   rP   rQ   r   r   r   test_heappush_exceptions   s    

z"TestHeapq.test_heappush_exceptionsc             C   s   t }tdd|}xL|  D ]@}x:tdt|d D ]$}|d|}|d|}| || q4W qW |ddddg}| |g  |ddddg}| |dg |dd}| |ddg |dtd	}| |d
ddg d S )NT)r"   rT   r#   r   Fr'   )r)   r$   r(   r#   r'   r   r)   r   )r   r   r\   r4   r5   r.   r   arange)r    r6   r7   r   r   rc   re   outr   r   r   test_nsmallest_basic   s    


zTestHeapq.test_nsmallest_basicc             C   s   t }tdd|}xL|  D ]@}x:tdt|d D ]$}|d|}|d|}| || q4W qW |ddddg}| |g  |ddddg}| |dg |dd}| |d	d
g |dtd	}| |d
ddg d S )NT)r"   rT   r#   r   Fr'   )r)   r$   r(   r#   r'   r   r)   r$   r(   )r   r   r\   r4   r5   r.   r   rh   )r    r6   r7   r   r   rc   re   ri   r   r   r   test_nlargest_basic   s    


zTestHeapq.test_nlargest_basicc          	   C   sz   |    |  }|ddddg W d Q R X d}| |t|j |  }|dd W d Q R X d}| |t|j d S )Ng@r#   r'   r   z%First argument 'n' must be an integerrU   z+Second argument 'iterable' must be iterable)rK   rL   rM   rN   rO   )r    r7   rP   rQ   r   r   r   _assert_typing_error  s    

zTestHeapq._assert_typing_errorc             C   s    t }tdd|}| | d S )NT)r"   )r   r   rl   )r    r6   r7   r   r   r   test_nsmallest_exceptions(  s    z#TestHeapq.test_nsmallest_exceptionsc             C   s    t }tdd|}| | d S )NT)r"   )r   r   rl   )r    r6   r7   r   r   r   test_nlargest_exceptions-  s    z"TestHeapq.test_nlargest_exceptionsc          
   C   s   t }tdd|}ddddddd	d
ddg
}t| |d d  }x,dD ]$}||| ||| | || qDW tddd}tj|d	< tj|d< | }t| |d d  }x:ddtj tjgD ]$}||| ||| | || qW d S )NT)r"   r   r#   r$   r%   r&   r'   r(   r)   r*   r   )r(            r[   g      gA`"	@)	r   r   r   r.   r   rV   r/   r0   rH   )r    r6   r7   r8   r9   r   r   r   r   test_heapreplace_basic2  s&    






z TestHeapq.test_heapreplace_basicc          	   C   s   t }tdd|}|   |  }|dd W d Q R X d}| |t|j |  }|dddgd	 W d Q R X d
}| |t|j d S )NT)r"   )r   r$   r(   r[   zheap argument must be a listr   r$   r(   g      z'heap type must be the same as item type)r   r   rK   rL   rM   rN   rO   )r    r6   r7   rP   rQ   r   r   r   test_heapreplace_exceptionsM  s    

z%TestHeapq.test_heapreplace_exceptionsc             c   s.   yxt |V  qW W n tk
r(   Y nX d S )N)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 }|| x*|dd  D ]}||d krT||| qTW | t| 	|t
|dd   d S )NT)r"   i  i  
   r   i)r   r   r   r   choicer4   rH   r.   r3   rw   ra   )r    cfunc_heapifyZcfunc_heapreplacerE   r   r   r   r   r   
test_nbestg  s    zTestHeapq.test_nbestc                s   t ddt}t ddt}t ddt xtdD ]}| jtdd }|d@ rl|d d  | n*|d gx|dd  D ]}|| qW  fdd	tdD }| |t	| q4W d S )
NT)r"   rU   r$   rx   r   r   c                s   g | ]} qS r   r   )r]   r^   )rD   r   r   r   r`     s    z+TestHeapq.test_heapsort.<locals>.<listcomp>)
r   r   r   r   r4   r   ry   rH   r.   ra   )r    rz   rC   ZtrialrE   r   Zheap_sortedr   )rD   r   r   test_heapsortw  s    

zTestHeapq.test_heapsortc             C   s\   t }tdd|}| jtdd }x.dD ]&}| t|||t|d |  q.W d S )NT)r"   i  i  )	r   r   r'   rx   rU   i  i  i  iL  )	r   r   r   ry   r4   rH   r.   r3   ra   )r    r6   r7   rE   r   r   r   r   test_nsmallest  s
    
zTestHeapq.test_nsmallestc             C   s`   t }tdd|}| jtdd }x2dD ]*}| t|||t|ddd |  q.W d S )NT)r"   i  i  )	r   r   r'   rx   rU   i  i  i  iL  )reverse)	r   r   r   ry   r4   rH   r.   r3   ra   )r    r6   r7   rE   r   r   r   r   test_nlargest  s    
zTestHeapq.test_nlargestc             C   s   t }tdd|}t}tdd|}| jtdd }|d d }|| x|dd  D ]}||| q\W | t| 	|t
|dd   d S )NT)r"   i  i  rx   i)r   r   r   r   ry   r4   rH   r.   r3   rw   ra   )r    Zpyfunc_heappushpopZcfunc_heappushpopZpyfunc_heapifyrz   rE   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}| ||fdgdf | t|d t | t|t dg}||d}| ||fdgdf dg}||d}| ||fdgdf d S )	NT)r"   g      ?g      $@r   rx   r&   r,   )r   r   r.   typefloat)r    r6   r7   hr   r   r   r   test_heappushpop  s    


zTestHeapq.test_heappushpopc          	   C   s   t }tdd|}|   |  }|dd W d Q R X d}| |t|j |  }|dddgd	 W d Q R X d
}| |t|j d S )NT)r"   )r   r$   r(   r[   zheap argument must be a listr   r$   r(   Fz'heap type must be the same as item type)r   r   rK   rL   rM   rN   rO   )r    r6   r7   rP   rQ   r   r   r   test_heappushpop_exceptions  s    

z%TestHeapq.test_heappushpop_exceptions)__name__
__module____qualname__r   r;   r?   rF   rJ   rR   rX   rY   r\   rf   rg   rj   rk   rl   rm   rn   rt   ru   rw   r{   r|   r}   r   r   r   r   __classcell__r   r   )r!   r   r   ,   s4   'r   )Z
__future__r   r   r   heapqr   r1   Znumpyr   Znumbar   Znumba.compilerr   Zsupportr   r	   r
   Zno_pyobj_flagssetr   r   r   r   r   r   r   r   r   r   r   r   <module>   s    
