a
    zeA                     @   s  d Z ddl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g d
ZG dd deZdd 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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(d) d)Zdd*lmZ d+d, ZG d-d. d.Z G d/d0 d0Z!G d1d2 d2e!eZ"eedG d3d4 d4e!eZ#e$d5kre%  dS )6z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"   t D ]}| tt|jd qd S )Nr   )
func_namesassertEqualgetattrpy_heapq
__module__selfZfname r   %/usr/lib/python3.9/test/test_heapq.pytest_py_functions   s    zTestModules.test_py_functionsrequires _heapqc                 C   s"   t D ]}| tt|jd qd S )Nr   )r   r   r   c_heapqr   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                 C   s(   G dd d}| tjt| d |S )Nc                   @   s   e Zd Zdd ZdS )z+load_tests.<locals>.HeapqMergeDocTestFinderc                 _   s   t  }|tjS N)doctestZDocTestFinderfindr   merge)r   argskwargsZdtfr   r   r   r"   '   s    z0load_tests.<locals>.HeapqMergeDocTestFinder.findN)r   r   r   r"   r   r   r   r   HeapqMergeDocTestFinder&   s   r&   )Ztest_finder)ZaddTestsr!   ZDocTestSuiter   )loaderZtestsignorer&   r   r   r   
load_tests   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d  Zd!d" Zd#S )$TestHeapc                 C   s   g }g }|  | tdD ].}t }|| | j|| |  | qg }|rt| j|}|  | || qN|d d  }|  | || |  | | 	t
| jjg  z*| 	t
| jjd d  | 	t
| jjd  W n ty   Y n0 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_pop1   s.    



zTestHeap.test_push_popc                 C   s8   t |D ]*\}}|r|d d? }| || |k qd S N   )	enumerateZ
assertTrue)r   r5   posr8   	parentposr   r   r   r,   M   s    zTestHeap.check_invariantc                 C   sV   t tddg D ],}dd t|D }| j| | | q| t| jjd  d S )N    N  c                 S   s   g | ]}t   qS r   r.   ).0dummyr   r   r   
<listcomp>V       z)TestHeap.test_heapify.<locals>.<listcomp>)listr-   r0   r   r,   r2   r3   )r   sizer5   r   r   r   test_heapifyT   s
    zTestHeap.test_heapifyc                 C   sj   dd t dD }g }|D ]*}| j|| t|dkr| j| q|  | |t|dd   d S )Nc                 S   s   g | ]}t d qS   r.   	randrangerC   r7   r   r   r   rE   ]   rF   z-TestHeap.test_naive_nbest.<locals>.<listcomp>  
   )r-   r0   r
   lenr	   r1   r   sortedr   r6   r5   r8   r   r   r   test_naive_nbest\   s    zTestHeap.test_naive_nbestc                 c   s,   z| j |V  qW n ty&   Y n0 d S r    )r0   r	   
IndexError)r   r5   r   r   r   heapiterf   s    zTestHeap.heapiterc                 C   s   dd t dD }|d d }| j| |dd  D ]}||d kr6| j|| q6| 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 rJ   rL   rN   r   r   r   rE   t   rF   z'TestHeap.test_nbest.<locals>.<listcomp>rO   rP   r   rQ   )r-   r0   r   r   r   rG   rW   rS   r2   r3   rV   rT   r   r   r   
test_nbestn   s    "zTestHeap.test_nbestc                 C   s   dd t dD }|d d }| j| |dd  D ]}| j|| q6| t| |t|dd   | | jg dd d S )Nc                 S   s   g | ]}t d qS rJ   rL   rN   r   r   r   rE      rF   z4TestHeap.test_nbest_with_pushpop.<locals>.<listcomp>rO   rP   rQ   x)r-   r0   r   r   r   rG   rW   rS   rT   r   r   r   test_nbest_with_pushpop   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 )NrP   g      $@r   	      )r0   r   r   typeintfloat)r   hrY   r   r   r   test_heappushpop   s    zTestHeap.test_heappushpopc                 C   s4   ddg}|  | j|d |  | j|d d S )N      )r   r0   r   )r   r`   r   r   r   test_heappop_max   s    zTestHeap.test_heappop_maxc                    s   t dD ]}td}dd t |D }|d@ rJ|d d   j  ng  |D ]}j | qR fddt |D }|t| qd S )Nd   2   c                 S   s   g | ]}t d qS )   rL   rN   r   r   r   rE      rF   z*TestHeap.test_heapsort.<locals>.<listcomp>r<   c                    s   g | ]}j  qS r   )r0   r	   rN   r5   r   r   r   rE      rF   )r-   r.   rM   r0   r   r
   r   rS   )r   ZtrialrH   r6   r8   Zheap_sortedr   rh   r   test_heapsort   s    
zTestHeap.test_heapsortc           
      C   s   g }t tdD ]F}g }t tdD ]$}tdtddf}|| q(|| qd tdtdtddfD ]t}dD ]j}g }|D ]}	|t|	||d	 q| tt| ||d	t	| j
j|||d	 | t	| j
 g  q|qtd S )
Nrg   re   ABCi  r   r<   )FTkeyreverse)r-   r.   rM   choicer/   r   rS   r   chainrG   r0   r#   )
r   inputsr7   rowjtuprm   rn   Zseqsseqr   r   r   
test_merge   s     zTestHeap.test_mergec                 C   s@   |  t| jg g g  |  t| jjg g dd dg  d S )Nc                   S   s   dS )N   r   r   r   r   r   <lambda>   rF   z,TestHeap.test_empty_merges.<locals>.<lambda>rm   )r   rG   r0   r#   r   r   r   r   test_empty_merges   s    zTestHeap.test_empty_mergesc                 C   sL   dd }|  t& t| j| |  W d    n1 s>0    Y  d S )Nc                  s   s(   t td} tdD ]}| | V  qd S )NrP      )rG   r-   )sr7   r   r   r   iterable   s    zCTestHeap.test_merge_does_not_suppress_index_error.<locals>.iterable)r2   rV   rG   r0   r#   )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}tdD ]8}td}td}||}||f|_|| | q$|D ]}|  qbd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   rA      rk   c                 S   s   g | ]
}|j qS r   )pairrN   r   r   r   rE      rF   z1TestHeap.test_merge_stability.<locals>.<listcomp>)r^   r-   r.   rM   r   r/   r1   r0   r#   r   rS   )r   r   rq   r7   streamrY   objresultr   r   r   test_merge_stability   s    



zTestHeap.test_merge_stabilityc              
   C   s   dd t dD }d dd fD ]b}dD ]X}| t| j||t|d |  | t| jj|||dt||dd |  q&qd S )Nc                 S   s   g | ]}t d |fqS rJ   rL   rN   r   r   r   rE      rF   z+TestHeap.test_nsmallest.<locals>.<listcomp>rO   c                 S   s   | d d d S Nr   i#  rK   r   rY   r   r   r   rx      rF   z)TestHeap.test_nsmallest.<locals>.<lambda>	r   r<   rc   rP   re   i  i  rO   iL  ry   )r-   r   rG   r0   	nsmallestrS   r   r6   fnr   r   r   test_nsmallest   s    zTestHeap.test_nsmallestc              
   C   s   dd t dD }d dd fD ]h}dD ]^}| t| j||t|ddd |  | t| jj|||d	t||dd
d |  q&qd S )Nc                 S   s   g | ]}t d |fqS rJ   rL   rN   r   r   r   rE      rF   z*TestHeap.test_nlargest.<locals>.<listcomp>rO   c                 S   s   | d d d S r   r   r   r   r   r   rx      rF   z(TestHeap.test_nlargest.<locals>.<lambda>r   Trn   ry   rl   )r-   r   rG   r0   nlargestrS   r   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   )rC   rY   )compr   r   rE      rF   zDTestHeap.test_comparison_operator.<locals>.hsort.<locals>.<listcomp>c                    s   g | ]}j  jqS r   )r0   r	   rY   rN   )r6   r   r   r   rE      rF   )r0   r   r-   rR   )r6   r   rz   )r   r6   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 r    r   r   rY   r   r   r   __init__   s    z6TestHeap.test_comparison_operator.<locals>.LT.__init__c                 S   s   | j |j kS r    r   r   otherr   r   r   __lt__   s    z4TestHeap.test_comparison_operator.<locals>.LT.__lt__Nr   r   r   r   r   r   r   r   r   LT   s   r   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 r    r   r   r   r   r   r     s    z6TestHeap.test_comparison_operator.<locals>.LE.__init__c                 S   s   | j |j kS r    r   r   r   r   r   __le__  s    z4TestHeap.test_comparison_operator.<locals>.LE.__le__N)r   r   r   r   r   r   r   r   r   LE   s   r   c                 S   s   g | ]}t   qS r   rB   rN   r   r   r   rE     rF   z5TestHeap.test_comparison_operator.<locals>.<listcomp>re   Tr   )r-   rS   r   r2   r3   )r   r   r   r   r6   targetr   rz   r   test_comparison_operator   s    z!TestHeap.test_comparison_operatorN)r   r   r   r:   r,   rI   rU   rW   rX   rZ   ra   rd   ri   rv   r{   r   r   r   r   r   r   r   r   r   r*   /   s"   
				r*   c                   @   s   e Zd ZeZdS )TestHeapPythonNr   r   r   r   r0   r   r   r   r   r     s   r   r   c                   @   s   e Zd ZeZdS )	TestHeapCNr   r   r   r   r0   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 NrP   r   rz   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e Z Z Z ZZ	dS )CmpErrz;Dummy element that always raises an error during comparisonc                 C   s   t d S r    )ZeroDivisionErrorr   r   r   r   __eq__  s    zCmpErr.__eq__N)
r   r   r   r   r   __ne__r   r   __gt____ge__r   r   r   r   r     s   r   c                 c   s   | D ]
}|V  qdS )zRegular generatorNr   seqnr7   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 r    r   r   r   r   r   r   r   (  s    z
G.__init__c                 C   s
   | j | S r    r   )r   r7   r   r   r   __getitem__*  s    zG.__getitem__N)r   r   r   r   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 )	Iz Sequence using iterator protocolc                 C   s   || _ d| _d S Nr   r   r   r   r   r   r   /  s    z
I.__init__c                 C   s   | S r    r   rz   r   r   r   __iter__2  s    z
I.__iter__c                 C   s2   | j t| jkrt| j| j  }|  j d7  _ |S r;   r7   rR   r   StopIterationr   vr   r   r   __next__4  s    z
I.__next__Nr   r   r   r   r   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 r   r   r   r   r   r   r   <  s    zIg.__init__c                 c   s   | j D ]
}|V  qd S r    r   )r   valr   r   r   r   ?  s    
zIg.__iter__Nr   r   r   r   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 )Xz Missing __getitem__ and __iter__c                 C   s   || _ d| _d S r   r   r   r   r   r   r   E  s    z
X.__init__c                 C   s2   | j t| jkrt| j| j  }|  j d7  _ |S r;   r   r   r   r   r   r   H  s    z
X.__next__N)r   r   r   r   r   r   r   r   r   r   r   C  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 r   r   r   r   r   r   r   P  s    z
N.__init__c                 C   s   | S r    r   rz   r   r   r   r   S  s    z
N.__iter__Nr   r   r   r   r   r   N  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 r   r   r   r   r   r   r   X  s    z
E.__init__c                 C   s   | S r    r   rz   r   r   r   r   [  s    z
E.__iter__c                 C   s   dd  d S )Nrb   r   r   rz   r   r   r   r   ]  s    z
E.__next__Nr   r   r   r   r   r   V  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 r    r   r   r   r   r   r   b  s    z
S.__init__c                 C   s   | S r    r   rz   r   r   r   r   d  s    z
S.__iter__c                 C   s   t d S r    )r   rz   r   r   r   r   f  s    z
S.__next__Nr   r   r   r   r   r   `  s   r   )rp   c                 C   s   t tdd ttt| S )z Test multiple tiers of iteratorsc                 S   s   | S r    r   r   r   r   r   rx   l  rF   zL.<locals>.<lambda>)rp   mapr   r   r   r   r   r   r   Lj  s    r   c                   @   s   e Zd Zdd Zdd ZdS )SideEffectLTc                 C   s   || _ || _d S r    )valuer5   )r   r   r5   r   r   r   r   p  s    zSideEffectLT.__init__c                 C   s   g | j d d < | j|jk S r    )r5   r   r   r   r   r   r   t  s    zSideEffectLT.__lt__Nr   r   r   r   r   r   o  s   r   c                   @   sT   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S )TestErrorHandlingc                 C   sb   | j j| j jfD ]}| ttf|d q| j j| j j| j j| j j	fD ]}| ttf|dd qDd S r   )
r0   r   r	   r2   r3   r4   r
   r   r   r   r   r   r   r   r   test_non_sequence{  s    z#TestErrorHandling.test_non_sequencec                 C   s   | j j| j jfD ]}| ttf|t  q| j j| j jfD ]}| ttf|t d q:| j j	| j j
fD ]}| t|dt  qfd S NrP   rc   )r0   r   r	   r2   r3   r4   r   r
   r   r   r   r   r   r   r   test_len_only  s    zTestErrorHandling.test_len_onlyc                 C   s   t  t  t  g}| jj| jjfD ]}| t|| q | jj| jjfD ]}| t||d qD| jj| jj	fD ]}| t|d| qjd S r   )
r   r0   r   r	   r2   r   r
   r   r   r   )r   ru   r   r   r   r   test_cmp_err  s    zTestErrorHandling.test_cmp_errc                 C   sD   | j j| j j| j j| j j| j j| j jfD ]}| tt	f|d q(d S r   )
r0   r   r	   r
   r   r   r   r2   r3   r4   r   r   r   r   test_arg_parsing  s
    z"TestErrorHandling.test_arg_parsingc              
   C   s   | j j| j jfD ]}ddtddtdddfD ]}tttttfD ](}| 	t
|d||t
|d| q@| 	t
|dt|g  | t|dt| | t|dt| | t|dt| q.qd S )	NZ123 rO   )r<   g333333?rK   i     rc   )r0   r   r   r-   r   r   r   r   r   r   rG   r   r2   r3   r   r   r   r   )r   r   r}   gr   r   r   test_iterable_args  s    &z$TestErrorHandling.test_iterable_argsc                    sf   g     fddtdD  | ttf$ | j td  W d    n1 sX0    Y  d S )Nc                 3   s   | ]}t | V  qd S r    r   rN   r5   r   r   	<genexpr>  rF   z@TestErrorHandling.test_heappush_mutating_heap.<locals>.<genexpr>   r   )extendr-   r2   rV   RuntimeErrorr0   r
   r   rz   r   r   r   test_heappush_mutating_heap  s    z-TestErrorHandling.test_heappush_mutating_heapc                    s^   g     fddtdD  | ttf | j  W d    n1 sP0    Y  d S )Nc                 3   s   | ]}t | V  qd S r    r   rN   r   r   r   r     rF   z?TestErrorHandling.test_heappop_mutating_heap.<locals>.<genexpr>r   )r   r-   r2   rV   r   r0   r	   rz   r   r   r   test_heappop_mutating_heap  s    z,TestErrorHandling.test_heappop_mutating_heapc                    sB   G  fdddt }g  | j |d | t| jj d d S )Nc                       s   e Zd Z fddZdS )zMTestErrorHandling.test_comparison_operator_modifiying_heap.<locals>.EvilClassc                    s       tS r    clearNotImplementedr   or   r   r   r     s    zTTestErrorHandling.test_comparison_operator_modifiying_heap.<locals>.EvilClass.__lt__Nr   r   r   r   r   r   r   r   	EvilClass  s   r   r   r<   )r^   r0   r
   r2   rV   r   )r   r   r   r   r   (test_comparison_operator_modifiying_heap  s    z:TestErrorHandling.test_comparison_operator_modifiying_heapc                    s   G fdddt }G  fdddt }g g   | j |d | j|d | ttf| jj |d | ttf| jj|d d S )Nc                       s   e Zd Z fddZdS )zOTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.hc                    s       tS r    r   r   list2r   r   r     s    zVTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.h.__lt__Nr   r   r   r   r   r`     s   r`   c                       s   e Zd Z fddZdS )zOTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.gc                    s       tS r    r   r   list1r   r   r     s    zVTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.g.__lt__Nr   r   r   r   r   r     s   r   r   r<   )r^   r0   r
   r2   rV   r   )r   r`   r   r   )r   r   r   2test_comparison_operator_modifiying_heap_two_heaps  s    
zDTestErrorHandling.test_comparison_operator_modifiying_heap_two_heapsN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   y  s   	r   c                   @   s   e Zd ZeZdS )TestErrorHandlingPythonNr   r   r   r   r   r     s   r   c                   @   s   e Zd ZeZdS )TestErrorHandlingCNr   r   r   r   r   r     s   r   __main__)&r   r.   Zunittestr!   testr   r   r   operatorr   Zimport_fresh_moduler   r   r   r   r)   r*   r   r   r   r   r   r   r   r   r   r   r   r   	itertoolsrp   r   r   r   r   r   r   mainr   r   r   r   <module>   sF    ]	
	
Z
