B
    u9a:                 @   s  d Z ddlZddlZddlmZ ddlmZmZ ddlmZ ej	ddgdZ
ej	ddgd	Zd
dddddddgZG dd deZG dd dZG dd deeZeedG dd deeZG dd dZG dd dZG dd  d Zd!d" ZG d#d$ d$ZG d%d& d&ZG d'd( d(ZG d)d* d*ZG d+d, d,ZG d-d. d.ZG d/d0 d0Zdd1lmZ d2d3 ZG d4d5 d5ZG d6d7 d7Z G d8d9 d9e eZ!eedG d:d; d;e eZ"e#d<kre$  dS )=zUnittests for heapq.    N)support)TestCase
skipUnless)
itemgetterheapq_heapq)Zblocked)Zfreshheapifyheappopheappushheappushpopheapreplace_heappop_max_heapreplace_max_heapify_maxc               @   s&   e Zd Zdd Zeeddd ZdS )TestModulesc             C   s&   x t D ]}| tt|jd qW d S )Nr   )
func_namesassertEqualgetattrpy_heapq
__module__)selffname r    /usr/lib/python3.7/test_heapq.pytest_py_functions   s    
zTestModules.test_py_functionszrequires _heapqc             C   s&   x t D ]}| tt|jd qW d S )Nr   )r   r   r   c_heapqr   )r   r   r   r   r   test_c_functions   s    
zTestModules.test_c_functionsN)__name__r   __qualname__r   r   r   r   r   r   r   r   r      s   r   c               @   s   e Z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S ) TestHeapc             C   s   g }g }|  | x:tdD ].}t }|| | j|| |  | qW g }x(|rz| j|}|  | || qTW |d d  }|  | || |  | | 	t
| jjg  y*| 	t
| jjd d  | 	t
| jjd  W n tk
r   Y nX d S )N   )check_invariantrangerandomappendmoduler
   r	   sortr   assertRaises	TypeErrorAttributeError)r   heapdataiitemresultsZdata_sortedr   r   r   test_push_pop   s.    



zTestHeap.test_push_popc             C   s<   x6t |D ]*\}}|r
|d d? }| || |k q
W d S )N   )	enumerateZ
assertTrue)r   r*   posr-   	parentposr   r   r   r!   ;   s    zTestHeap.check_invariantc             C   sZ   xBt tddg D ],}dd t|D }| j| | | qW | t| jjd  d S )N   i N  c             S   s   g | ]}t   qS r   )r#   ).0Zdummyr   r   r   
<listcomp>D   s    z)TestHeap.test_heapify.<locals>.<listcomp>)listr"   r%   r   r!   r'   r(   )r   sizer*   r   r   r   test_heapifyB   s
    zTestHeap.test_heapifyc             C   sn   dd t dD }g }x2|D ]*}| j|| t|dkr| j| qW |  | |t|dd   d S )Nc             S   s   g | ]}t d qS )i  )r#   	randrange)r5   r,   r   r   r   r6   K   s    z-TestHeap.test_naive_nbest.<locals>.<listcomp>i  
   i)r"   r%   r
   lenr	   r&   r   sorted)r   r+   r*   r-   r   r   r   test_naive_nbestJ   s    
zTestHeap.test_naive_nbestc             c   s2   yx| j |V  qW W n tk
r,   Y nX d S )N)r%   r	   
IndexError)r   r*   r   r   r   heapiterT   s
    zTestHeap.heapiterc             C   s   dd t dD }|d d }| j| x.|dd  D ]}||d kr8| j|| q8W | t| |t|dd   | t	| jjd  | t	| jjd d  | t
| jjg d  d S )Nc             S   s   g | ]}t d qS )i  )r#   r:   )r5   r,   r   r   r   r6   b   s    z'TestHeap.test_nbest.<locals>.<listcomp>i  r;   r   i)r"   r%   r   r   r   r7   r@   r=   r'   r(   r?   )r   r+   r*   r-   r   r   r   
test_nbest\   s    "zTestHeap.test_nbestc             C   s   dd t dD }|d d }| j| x"|dd  D ]}| j|| q8W | t| |t|dd   | | jg dd d S )Nc             S   s   g | ]}t d qS )i  )r#   r:   )r5   r,   r   r   r   r6   o   s    z4TestHeap.test_nbest_with_pushpop.<locals>.<listcomp>i  r;   ix)r"   r%   r   r   r   r7   r@   r=   )r   r+   r*   r-   r   r   r   test_nbest_with_pushpopn   s    "z TestHeap.test_nbest_with_pushpopc             C   s   g }| j |d}| ||fg df dg}| j |d}| ||fdgdf | t|d t | t|t dg}| j |d}| ||fdgdf dg}| j |d}| ||fdgdf d S )Nr;   g      $@r   	      )r%   r   r   typeintfloat)r   hrB   r   r   r   test_heappushpopw   s    zTestHeap.test_heappushpopc                s   xt dD ]}td}dd t |D }|d@ rL|d d   j  n g  x|D ]}j | qVW  fddt |D }|t| q
W d S )Nd   2   c             S   s   g | ]}t d qS )   )r#   r:   )r5   r,   r   r   r   r6      s    z*TestHeap.test_heapsort.<locals>.<listcomp>r0   c                s   g | ]}j  qS r   )r%   r	   )r5   r,   )r*   r   r   r   r6      s    )r"   r#   r:   r%   r   r
   r   r=   )r   Ztrialr8   r+   r-   Zheap_sortedr   )r*   r   r   test_heapsort   s    

zTestHeap.test_heapsortc       
      C   s  g }x\t tdD ]J}g }x6t tdD ]$}tdtddf}|| q,W || qW xd tdtdtddgD ]|}xvdD ]n}g }x |D ]}	|t|	||d	 qW | tt| ||d	t	| j
j|||d	 | t	| j
 g  qW q~W d S )
NrM   rK   ABCii  r   r0   )FT)keyreverse)r"   r#   r:   choicer$   r   r=   r   chainr7   r%   merge)
r   inputsr,   rowjtuprP   rQ   Zseqsseqr   r   r   
test_merge   s     

zTestHeap.test_mergec          	   C   s8   dd }|  t t| j| |  W d Q R X d S )Nc              s   s,   t td} xtdD ]}| | V  qW d S )Nr;      )r7   r"   )sr,   r   r   r   iterable   s    zCTestHeap.test_merge_does_not_suppress_index_error.<locals>.iterable)r'   r?   r7   r%   rT   )r   r]   r   r   r   (test_merge_does_not_suppress_index_error   s    z1TestHeap.test_merge_does_not_suppress_index_errorc             C   s   G dd dt }g g g g g}xDtdD ]8}td}td}||}||f|_|| | q&W x|D ]}|  qhW dd | jj| D }| 	|t
| d S )Nc               @   s   e Zd ZdS )z*TestHeap.test_merge_stability.<locals>.IntN)r   r   r   r   r   r   r   Int   s   r_   i N     i  c             S   s   g | ]
}|j qS r   )pair)r5   r,   r   r   r   r6      s    z1TestHeap.test_merge_stability.<locals>.<listcomp>)rG   r"   r#   r:   ra   r$   r&   r%   rT   r   r=   )r   r_   rU   r,   streamrB   objresultr   r   r   test_merge_stability   s    



zTestHeap.test_merge_stabilityc          
   C   s   dd t dD }xvd dd fD ]f}x`dD ]X}| t| j||t|d |  | t| jj|||dt||dd |  q*W q W d S )Nc             S   s   g | ]}t d |fqS )i  )r#   r:   )r5   r,   r   r   r   r6      s    z+TestHeap.test_nsmallest.<locals>.<listcomp>i  c             S   s   | d d d S )Nr   i#  i  r   )rB   r   r   r   <lambda>       z)TestHeap.test_nsmallest.<locals>.<lambda>)	r   r0      r;   rK   i  i  i  iL  )rP   )r"   r   r7   r%   	nsmallestr=   )r   r+   fnr   r   r   test_nsmallest   s    
zTestHeap.test_nsmallestc          
   C   s   dd t dD }x|d dd fD ]l}xfdD ]^}| t| j||t|ddd |  | t| jj|||d	t||dd
d |  q*W q W d S )Nc             S   s   g | ]}t d |fqS )i  )r#   r:   )r5   r,   r   r   r   r6      s    z*TestHeap.test_nlargest.<locals>.<listcomp>i  c             S   s   | d d d S )Nr   i#  i  r   )rB   r   r   r   rf      rg   z(TestHeap.test_nlargest.<locals>.<lambda>)	r   r0   rh   r;   rK   i  i  i  iL  T)rQ   )rP   )rP   rQ   )r"   r   r7   r%   nlargestr=   )r   r+   rj   rk   r   r   r   test_nlargest   s    
zTestHeap.test_nlargestc                sj    fdd}G dd d}G dd d}dd t d	D }t|d
d} ||||  t|| d S )Nc                s:    fddD j  fddttD S )Nc                s   g | ]} |qS r   r   )r5   rB   )compr   r   r6      s    zDTestHeap.test_comparison_operator.<locals>.hsort.<locals>.<listcomp>c                s   g | ]}j  jqS r   )r%   r	   rB   )r5   r,   )r+   r   r   r   r6      s    )r%   r   r"   r<   )r+   ro   )r   )ro   r+   r   hsort   s    z0TestHeap.test_comparison_operator.<locals>.hsortc               @   s   e Zd Zdd Zdd ZdS )z-TestHeap.test_comparison_operator.<locals>.LTc             S   s
   || _ d S )N)rB   )r   rB   r   r   r   __init__   s    z6TestHeap.test_comparison_operator.<locals>.LT.__init__c             S   s   | j |j kS )N)rB   )r   otherr   r   r   __lt__   s    z4TestHeap.test_comparison_operator.<locals>.LT.__lt__N)r   r   r   rq   rs   r   r   r   r   LT   s   rt   c               @   s   e Zd Zdd Zdd ZdS )z-TestHeap.test_comparison_operator.<locals>.LEc             S   s
   || _ d S )N)rB   )r   rB   r   r   r   rq      s    z6TestHeap.test_comparison_operator.<locals>.LE.__init__c             S   s   | j |j kS )N)rB   )r   rr   r   r   r   __le__   s    z4TestHeap.test_comparison_operator.<locals>.LE.__le__N)r   r   r   rq   ru   r   r   r   r   LE   s   rv   c             S   s   g | ]}t   qS r   )r#   )r5   r,   r   r   r   r6      s    z5TestHeap.test_comparison_operator.<locals>.<listcomp>rK   T)rQ   )r"   r=   r   r'   r(   )r   rp   rt   rv   r+   targetr   )r   r   test_comparison_operator   s    z!TestHeap.test_comparison_operatorN)r   r   r   r/   r!   r9   r>   r@   rA   rC   rJ   rN   rZ   r^   re   rl   rn   rx   r   r   r   r   r      s   
				r   c               @   s   e Zd ZeZdS )TestHeapPythonN)r   r   r   r   r%   r   r   r   r   ry      s   ry   zrequires _heapqc               @   s   e Zd ZeZdS )	TestHeapCN)r   r   r   r   r%   r   r   r   r   rz      s   rz   c               @   s   e Zd ZdZdd ZdS )LenOnlyz:Dummy sequence class defining __len__ but not __getitem__.c             C   s   dS )Nr;   r   )r   r   r   r   __len__   s    zLenOnly.__len__N)r   r   r   __doc__r|   r   r   r   r   r{      s   r{   c               @   s   e Zd ZdZdd ZdS )GetOnlyz:Dummy sequence class defining __getitem__ but not __len__.c             C   s   dS )Nr;   r   )r   Zndxr   r   r   __getitem__   s    zGetOnly.__getitem__N)r   r   r   r}   r   r   r   r   r   r~      s   r~   c               @   s,   e Zd ZdZdd Ze Z Z Z ZZ	dS )CmpErrz;Dummy element that always raises an error during comparisonc             C   s   t d S )N)ZeroDivisionError)r   rr   r   r   r   __eq__  s    zCmpErr.__eq__N)
r   r   r   r}   r   __ne__rs   ru   __gt____ge__r   r   r   r   r     s   r   c             c   s   x| D ]
}|V  qW dS )zRegular generatorNr   )seqnr,   r   r   r   R  s    
r   c               @   s    e Zd ZdZdd Zdd ZdS )GzSequence using __getitem__c             C   s
   || _ d S )N)r   )r   r   r   r   r   rq     s    z
G.__init__c             C   s
   | j | S )N)r   )r   r,   r   r   r   r     s    zG.__getitem__N)r   r   r   r}   rq   r   r   r   r   r   r     s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Iz Sequence using iterator protocolc             C   s   || _ d| _d S )Nr   )r   r,   )r   r   r   r   r   rq     s    z
I.__init__c             C   s   | S )Nr   )r   r   r   r   __iter__  s    z
I.__iter__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr0   )r,   r<   r   StopIteration)r   vr   r   r   __next__  s
     z
I.__next__N)r   r   r   r}   rq   r   r   r   r   r   r   r     s   r   c               @   s    e Zd ZdZdd Zdd ZdS )Igz9Sequence using iterator protocol defined with a generatorc             C   s   || _ d| _d S )Nr   )r   r,   )r   r   r   r   r   rq   "  s    zIg.__init__c             c   s   x| j D ]
}|V  qW d S )N)r   )r   valr   r   r   r   %  s    zIg.__iter__N)r   r   r   r}   rq   r   r   r   r   r   r      s   r   c               @   s    e Zd ZdZdd Zdd ZdS )Xz Missing __getitem__ and __iter__c             C   s   || _ d| _d S )Nr   )r   r,   )r   r   r   r   r   rq   +  s    z
X.__init__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr0   )r,   r<   r   r   )r   r   r   r   r   r   .  s
     z
X.__next__N)r   r   r   r}   rq   r   r   r   r   r   r   )  s   r   c               @   s    e Zd ZdZdd Zdd ZdS )NzIterator missing __next__()c             C   s   || _ d| _d S )Nr   )r   r,   )r   r   r   r   r   rq   6  s    z
N.__init__c             C   s   | S )Nr   )r   r   r   r   r   9  s    z
N.__iter__N)r   r   r   r}   rq   r   r   r   r   r   r   4  s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	EzTest propagation of exceptionsc             C   s   || _ d| _d S )Nr   )r   r,   )r   r   r   r   r   rq   >  s    z
E.__init__c             C   s   | S )Nr   )r   r   r   r   r   A  s    z
E.__iter__c             C   s   dd  d S )N   r   r   )r   r   r   r   r   C  s    z
E.__next__N)r   r   r   r}   rq   r   r   r   r   r   r   r   <  s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SzTest immediate stopc             C   s   d S )Nr   )r   r   r   r   r   rq   H  s    z
S.__init__c             C   s   | S )Nr   )r   r   r   r   r   J  s    z
S.__iter__c             C   s   t d S )N)r   )r   r   r   r   r   L  s    z
S.__next__N)r   r   r   r}   rq   r   r   r   r   r   r   r   F  s   r   )rS   c             C   s   t tdd ttt| S )z Test multiple tiers of iteratorsc             S   s   | S )Nr   )rB   r   r   r   rf   R  rg   zL.<locals>.<lambda>)rS   mapr   r   r   )r   r   r   r   LP  s    r   c               @   s   e Zd Zdd Zdd ZdS )SideEffectLTc             C   s   || _ || _d S )N)valuer*   )r   r   r*   r   r   r   rq   V  s    zSideEffectLT.__init__c             C   s   g | j d d < | j|jk S )N)r*   r   )r   rr   r   r   r   rs   Z  s    zSideEffectLT.__lt__N)r   r   r   rq   rs   r   r   r   r   r   U  s   r   c               @   sL   e Z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 )TestErrorHandlingc             C   sj   x*| j j| j jfD ]}| ttf|d qW x8| j j| j j| j j| j j	fD ]}| ttf|dd qJW d S )Nr;   )
r%   r   r	   r'   r(   r)   r
   r   rm   ri   )r   rj   r   r   r   test_non_sequencea  s
    z#TestErrorHandling.test_non_sequencec             C   s   x,| j j| j jfD ]}| ttf|t  qW x.| j j| j jfD ]}| ttf|t d q@W x*| j j	| j j
fD ]}| t|dt  qpW d S )Nr;   rh   )r%   r   r	   r'   r(   r)   r{   r
   r   rm   ri   )r   rj   r   r   r   test_len_onlyh  s    zTestErrorHandling.test_len_onlyc             C   s   x(| j j| j jfD ]}| t|t  qW x*| j j| j jfD ]}| t|t d q<W x*| j j| j j	fD ]}| t|dt  qhW d S )Nr;   rh   )
r%   r   r	   r'   r(   r~   r
   r   rm   ri   )r   rj   r   r   r   test_get_onlyp  s    zTestErrorHandling.test_get_onlyc             C   s   t  t  t  g}x&| jj| jjfD ]}| t|| q"W x(| jj| jjfD ]}| t||d qJW x(| jj| jj	fD ]}| t|d| qtW d S )Nr;   rh   )
r   r%   r   r	   r'   r   r
   r   rm   ri   )r   rY   rj   r   r   r   r   x  s    c             C   sH   xB| j j| j j| j j| j j| j j| j jfD ]}| tt	f|d q*W d S )Nr;   )
r%   r   r	   r
   r   rm   ri   r'   r(   r)   )r   rj   r   r   r   test_arg_parsing  s    z"TestErrorHandling.test_arg_parsingc          
   C   s   x| j j| j jfD ]}xddtddtdddfD ]}x:tttttfD ](}| 	t
|d||t
|d| qFW | 	t
|dt|g  | t|dt| | t|dt| | t|dt| q2W qW d S )	NZ123 i  )r0   g333333?i  i     rh   )r%   rm   ri   r"   r   r   r   r   r   r   r7   r   r'   r(   r   r   r   r   )r   rj   r\   gr   r   r   test_iterable_args  s     (z$TestErrorHandling.test_iterable_argsc          	      sR   g     fddtdD  | ttf | j td  W d Q R X d S )Nc             3   s   | ]}t | V  qd S )N)r   )r5   r,   )r*   r   r   	<genexpr>  s    z@TestErrorHandling.test_heappush_mutating_heap.<locals>.<genexpr>   r   )extendr"   r'   r?   RuntimeErrorr%   r
   r   )r   r   )r*   r   test_heappush_mutating_heap  s    z-TestErrorHandling.test_heappush_mutating_heapc          	      sJ   g     fddtdD  | ttf | j  W d Q R X d S )Nc             3   s   | ]}t | V  qd S )N)r   )r5   r,   )r*   r   r   r     s    z?TestErrorHandling.test_heappop_mutating_heap.<locals>.<genexpr>r   )r   r"   r'   r?   r   r%   r	   )r   r   )r*   r   test_heappop_mutating_heap  s    z,TestErrorHandling.test_heappop_mutating_heapN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _  s   	r   c               @   s   e Zd ZeZdS )TestErrorHandlingPythonN)r   r   r   r   r%   r   r   r   r   r     s   r   c               @   s   e Zd ZeZdS )TestErrorHandlingCN)r   r   r   r   r%   r   r   r   r   r     s   r   __main__)%r}   r#   ZunittestZtestr   r   r   operatorr   Zimport_fresh_moduler   r   r   r   r   ry   rz   r{   r~   r   r   r   r   r   r   r   r   r   	itertoolsrS   r   r   r   r   r   r   mainr   r   r   r   <module>   sF   

 P
	
	
C

