B
    18\:                 @   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)blocked)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   S/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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   )	enumerate
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%   ).0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)r8   r.   r   r   r   r:   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>   )r8   r.   r   r   r   r:   b   s    z'TestHeap.test_nbest.<locals>.<listcomp>i  r?   r   i)r$   r'   r
   r   r   r;   rD   rA   r)   r*   rC   )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>   )r8   r.   r   r   r   r:   o   s    z4TestHeap.test_nbest_with_pushpop.<locals>.<listcomp>i  r?   ix)r$   r'   r
   r   r   r;   rD   rA   )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   hrF   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>   )r8   r.   r   r   r   r:      s    z*TestHeap.test_heapsort.<locals>.<listcomp>r2   c                s   g | ]}j  qS r   )r'   r   )r8   r.   )r,   r   r   r   r:      s    )r$   r%   r>   r'   r
   r   r   rA   )r   Ztrialr<   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 )
NrQ   rO   ABCii  r   r2   )FT)keyreverse)r$   r%   r>   choicer&   r   rA   r   chainr;   r'   merge)
r   inputsr.   rowjtuprT   rU   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?      )r;   r$   )sr.   r   r   r   iterable   s    zCTestHeap.test_merge_does_not_suppress_index_error.<locals>.iterable)r)   rC   r;   r'   rX   )r   ra   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   rc   i N     i  c             S   s   g | ]
}|j qS r   )pair)r8   r.   r   r   r   r:      s    z1TestHeap.test_merge_stability.<locals>.<listcomp>)rK   r$   r%   r>   re   r&   r(   r'   rX   r   rA   )r   rc   rY   r.   streamrF   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>   )r8   r.   r   r   r   r:      s    z+TestHeap.test_nsmallest.<locals>.<listcomp>i  c             S   s   | d d d S )Nr   i#  i  r   )rF   r   r   r   <lambda>       z)TestHeap.test_nsmallest.<locals>.<lambda>)	r   r2      r?   rO   i  i  i  iL  )rT   )r$   r   r;   r'   	nsmallestrA   )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>   )r8   r.   r   r   r   r:      s    z*TestHeap.test_nlargest.<locals>.<listcomp>i  c             S   s   | d d d S )Nr   i#  i  r   )rF   r   r   r   rj      rk   z(TestHeap.test_nlargest.<locals>.<lambda>)	r   r2   rl   r?   rO   i  i  i  iL  T)rU   )rT   )rT   rU   )r$   r   r;   r'   nlargestrA   )r   r-   rn   ro   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   )r8   rF   )compr   r   r:      s    zDTestHeap.test_comparison_operator.<locals>.hsort.<locals>.<listcomp>c                s   g | ]}j  jqS r   )r'   r   rF   )r8   r.   )r-   r   r   r   r:      s    )r'   r
   r$   r@   )r-   rs   )r   )rs   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)rF   )r   rF   r   r   r   __init__   s    z6TestHeap.test_comparison_operator.<locals>.LT.__init__c             S   s   | j |j kS )N)rF   )r   otherr   r   r   __lt__   s    z4TestHeap.test_comparison_operator.<locals>.LT.__lt__N)r   r   r    ru   rw   r   r   r   r   LT   s   rx   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)rF   )r   rF   r   r   r   ru      s    z6TestHeap.test_comparison_operator.<locals>.LE.__init__c             S   s   | j |j kS )N)rF   )r   rv   r   r   r   __le__   s    z4TestHeap.test_comparison_operator.<locals>.LE.__le__N)r   r   r    ru   ry   r   r   r   r   LE   s   rz   c             S   s   g | ]}t   qS r   )r%   )r8   r.   r   r   r   r:      s    z5TestHeap.test_comparison_operator.<locals>.<listcomp>rO   T)rU   )r$   rA   r   r)   r*   )r   rt   rx   rz   r-   targetr   )r   r   test_comparison_operator   s    z!TestHeap.test_comparison_operatorN)r   r   r    r1   r#   r=   rB   rD   rE   rG   rN   rR   r^   rb   ri   rp   rr   r|   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   r}      s   r}   zrequires _heapqc               @   s   e Zd ZeZdS )	TestHeapCN)r   r   r    r   r'   r   r   r   r   r~      s   r~   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   rv   r   r   r   __eq__  s    zCmpErr.__eq__N)
r   r   r    r   r   __ne__rw   ry   __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   ru     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   ru   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   ru     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 )Nr2   )r.   r@   r   StopIteration)r   vr   r   r   __next__  s
     z
I.__next__N)r   r   r    r   ru   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   ru   "  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   ru   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   ru   +  s    z
X.__init__c             C   s2   | j t| jkrt| j| j  }|  j d7  _ |S )Nr2   )r.   r@   r   r   )r   r   r   r   r   r   .  s
     z
X.__next__N)r   r   r    r   ru   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   ru   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   ru   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   ru   >  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   ru   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   ru   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   ru   r   r   r   r   r   r   r   F  s   r   )rW   c             C   s   t tdd ttt| S )z Test multiple tiers of iteratorsc             S   s   | S )Nr   )rF   r   r   r   rj   R  rk   zL.<locals>.<lambda>)rW   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   ru   V  s    zSideEffectLT.__init__c             C   s   g | j d d < | j|jk S )N)r,   r   )r   rv   r   r   r   rw   Z  s    zSideEffectLT.__lt__N)r   r   r    ru   rw   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   rq   rm   )r   rn   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?   rl   )r'   r
   r   r)   r*   r+   r   r   r   rq   rm   )r   rn   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?   rl   )
r'   r
   r   r)   r*   r   r   r   rq   rm   )r   rn   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?   rl   )
r   r'   r
   r   r)   r   r   r   rq   rm   )r   r]   rn   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   rq   rm   r)   r*   r+   )r   rn   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 )	N123 i  )r2   g333333?i  i     rl   )r'   rq   rm   r$   r   r   r   r   r   r   r;   r   r)   r*   r   r   r   r   )r   rn   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   )r8   r.   )r,   r   r   	<genexpr>  s    z@TestErrorHandling.test_heappush_mutating_heap.<locals>.<genexpr>   r   )extendr$   r)   rC   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   )r8   r.   )r,   r   r   r     s    z?TestErrorHandling.test_heappop_mutating_heap.<locals>.<genexpr>r   )r   r$   r)   rC   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%   unittesttestr   r   r   operatorr   import_fresh_moduler   r   r   r   r!   r}   r~   r   r   r   r   r   r   r   r   r   r   r   	itertoolsrW   r   r   r   r   r   r   mainr   r   r   r   <module>   sF   

 P
	
	
C

