o
    HXhA                     @   s  	 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 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 dS )6    N)support)import_helper)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   "   t D ]}| tt|jd qd S )Nr   )
func_namesassertEqualgetattrpy_heapq
__module__selfZfname r   9/opt/python-3.10.19/usr/lib/python3.10/test/test_heapq.pytest_py_functions   s   zTestModules.test_py_functionsrequires _heapqc                 C   r   )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 }|r;| j|}|  | || |s)|d d  }|  | || |  | | 	t
| jjg  z| 	t
| jjd d  | 	t
| jjd  W d S  tyy   Y d S w )N   )check_invariantrangerandomappendmoduler   r
   sortr   assertRaises	TypeErrorAttributeError)r   heapdataiitemresultsZdata_sortedr   r   r   test_push_pop2   s2   




zTestHeap.test_push_popc                 C   s8   t |D ]\}}|r|d d? }| || |k qd S N   )	enumerateZ
assertTrue)r   r7   posr:   	parentposr   r   r   r.   N   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      g | ]}t   qS r   r0   ).0Zdummyr   r   r   
<listcomp>W       z)TestHeap.test_heapify.<locals>.<listcomp>)listr/   r2   r	   r.   r4   r5   )r   sizer7   r   r   r   test_heapifyU   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      g | ]}t d qS   r0   	randrangerF   r9   r   r   r   rG   ^       z-TestHeap.test_naive_nbest.<locals>.<listcomp>  
   )r/   r2   r   lenr
   r3   r   sortedr   r8   r7   r:   r   r   r   test_naive_nbest]   s   zTestHeap.test_naive_nbestc                 c   s*    z		 | j |V  q ty   Y d S w r"   )r2   r
   
IndexError)r   r7   r   r   r   heapiterg   s   zTestHeap.heapiterc                 C   s   dd t dD }|d d }| j| |dd  D ]}||d kr*| j|| q| t| |t|dd   | t	| jjd  | t	| jjd d  | t
| jjg d  d S )Nc                 S   rL   rM   rO   rQ   r   r   r   rG   u   rR   z'TestHeap.test_nbest.<locals>.<listcomp>rS   rT   r   rU   )r/   r2   r	   r   r   rI   r[   rW   r4   r5   rZ   rX   r   r   r   
test_nbesto   s   "zTestHeap.test_nbestc                 C   s   dd t dD }|d d }| j| |dd  D ]	}| j|| q| t| |t|dd   | | jg dd d S )Nc                 S   rL   rM   rO   rQ   r   r   r   rG      rR   z4TestHeap.test_nbest_with_pushpop.<locals>.<listcomp>rS   rT   rU   x)r/   r2   r	   r   r   rI   r[   rW   rX   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 )NrT   g      $@r   	      )r2   r   r   typeintfloat)r   hr]   r   r   r   test_heappushpop   s   zTestHeap.test_heappushpopc                 C   s4   ddg}|  | j|d |  | j|d d S )N      )r   r2   r   )r   rd   r   r   r   test_heappop_max   s   zTestHeap.test_heappop_maxc                    s   t dD ]C}td}dd t |D }|d@ r%|d d   j  ng  |D ]	}j | q) fddt |D }|t| qd S )Nd   2   c                 S   rL   )   rO   rQ   r   r   r   rG      rR   z*TestHeap.test_heapsort.<locals>.<listcomp>r>   c                    s   g | ]}j  qS r   )r2   r
   rQ   r7   r   r   r   rG      s    )r/   r0   rP   r2   r	   r   r   rW   )r   ZtrialrJ   r8   r:   Zheap_sortedr   rl   r   test_heapsort   s   
zTestHeap.test_heapsortc           
      C   s   g }t tdD ]#}g }t tdD ]}tdtddf}|| q|| q	d tdtdtddfD ]:}dD ]5}g }|D ]}	|t|	||d	 qD| tt| ||d	t	| j
j|||d	 | t	| j
 g  q>q:d S )
Nrk   ri   ABCi  r   r>   )FTkeyreverse)r/   r0   rP   choicer1   r   rW   r   chainrI   r2   r%   )
r   inputsr9   rowjtuprq   rr   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      dS )N   r   r   r   r   r   <lambda>       z,TestHeap.test_empty_merges.<locals>.<lambda>rq   )r   rI   r2   r%   r   r   r   r   test_empty_merges   s   &zTestHeap.test_empty_mergesc                 C   sN   dd }|  t t| j| |  W d    d S 1 s w   Y  d S )Nc                  s   s*    t td} tdD ]}| | V  qd S )NrT      )rI   r/   )sr9   r   r   r   iterable   s
   zCTestHeap.test_merge_does_not_suppress_index_error.<locals>.iterable)r4   rZ   rI   r2   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 ]}td}td}||}||f|_|| | q|D ]}|  q1d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   rC      ro   c                 S   s   g | ]}|j qS r   )pairrQ   r   r   r   rG      s    z1TestHeap.test_merge_stability.<locals>.<listcomp>)rb   r/   r0   rP   r   r1   r3   r2   r%   r   rW   )r   r   ru   r9   streamr]   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 ]1}dD ],}| t| j||t|d |  | t| jj|||dt||dd |  qqd S )Nc                 S      g | ]	}t d |fqS rM   rO   rQ   r   r   r   rG          z+TestHeap.test_nsmallest.<locals>.<listcomp>rS   c                 S      | d d d S Nr   i#  rN   r   r]   r   r   r   r}          z)TestHeap.test_nsmallest.<locals>.<lambda>	r   r>   rg   rT   ri   i  i  rS   iL  r   )r/   r   rI   r2   	nsmallestrW   r   r8   fnr   r   r   test_nsmallest   s   zTestHeap.test_nsmallestc              
   C   s   dd t dD }d dd fD ]4}dD ]/}| t| j||t|ddd |  | t| jj|||d	t||dd
d |  qqd S )Nc                 S   r   rM   rO   rQ   r   r   r   rG      r   z*TestHeap.test_nlargest.<locals>.<listcomp>rS   c                 S   r   r   r   r   r   r   r   r}      r   z(TestHeap.test_nlargest.<locals>.<lambda>r   Trr   r   rp   )r/   r   rI   r2   nlargestrW   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   )rF   r]   )compr   r   rG      rH   zDTestHeap.test_comparison_operator.<locals>.hsort.<locals>.<listcomp>c                    s   g | ]	}j  jqS r   )r2   r
   r]   rQ   )r8   r   r   r   rG      r   )r2   r	   r/   rV   )r8   r   r   )r   r8   r   hsort   s   z0TestHeap.test_comparison_operator.<locals>.hsortc                   @      e Zd Zdd Zdd ZdS )z-TestHeap.test_comparison_operator.<locals>.LTc                 S   
   || _ d S r"   r   r   r]   r   r   r   __init__      
z6TestHeap.test_comparison_operator.<locals>.LT.__init__c                 S   s   | j |j kS r"   r   r   otherr   r   r   __lt__      z4TestHeap.test_comparison_operator.<locals>.LT.__lt__Nr    r   r!   r   r   r   r   r   r   LT       r   c                   @   r   )z-TestHeap.test_comparison_operator.<locals>.LEc                 S   r   r"   r   r   r   r   r   r     r   z6TestHeap.test_comparison_operator.<locals>.LE.__init__c                 S   s   | j |j kS r"   r   r   r   r   r   __le__  r   z4TestHeap.test_comparison_operator.<locals>.LE.__le__N)r    r   r!   r   r   r   r   r   r   LE  r   r   c                 S   rD   r   rE   rQ   r   r   r   rG     rH   z5TestHeap.test_comparison_operator.<locals>.<listcomp>ri   Tr   )r/   rW   r   r4   r5   )r   r   r   r   r8   targetr   r   r   test_comparison_operator   s   z!TestHeap.test_comparison_operatorN)r    r   r!   r<   r.   rK   rY   r[   r\   r^   re   rh   rm   rz   r   r   r   r   r   r   r   r   r   r   r,   0   s$    
				r,   c                   @      e Zd ZeZdS )TestHeapPythonNr    r   r!   r   r2   r   r   r   r   r         r   r   c                   @   r   )	TestHeapCNr    r   r!   r   r2   r   r   r   r   r         r   c                   @   s   e Zd Z	 dd ZdS )LenOnlyc                 C   r{   NrT   r   r   r   r   r   __len__     zLenOnly.__len__N)r    r   r!   r   r   r   r   r   r     s    r   c                   @   s*   e Zd Z	 dd Ze Z Z Z ZZdS )CmpErrc                 C      t r"   )ZeroDivisionErrorr   r   r   r   __eq__  r   zCmpErr.__eq__N)	r    r   r!   r   __ne__r   r   __gt____ge__r   r   r   r   r     s    r   c                 c   s    	 | D ]}|V  qd S r"   r   seqnr9   r   r   r   R"  s
   r   c                   @      e Zd Z	 dd Zdd ZdS )Gc                 C   r   r"   r   r   r   r   r   r   r   )  r   z
G.__init__c                 C   s
   | j | S r"   r   )r   r9   r   r   r   __getitem__+  r   zG.__getitem__N)r    r   r!   r   r   r   r   r   r   r   '  s    r   c                   @   &   e Zd Z	 dd Zdd Zdd ZdS )Ic                 C      || _ d| _d S Nr   r   r   r   r   r   r   0     
z
I.__init__c                 C      | S r"   r   r   r   r   r   __iter__3  r   z
I.__iter__c                 C   2   | j t| jkr
t| j| j  }|  j d7  _ |S r=   r9   rV   r   StopIterationr   vr   r   r   __next__5     z
I.__next__Nr    r   r!   r   r   r   r   r   r   r   r   .  
    r   c                   @   r   )Igc                 C   r   r   r   r   r   r   r   r   =  r   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   c                   @   r   )Xc                 C   r   r   r   r   r   r   r   r   F  r   z
X.__init__c                 C   r   r=   r   r   r   r   r   r   I  r   z
X.__next__N)r    r   r!   r   r   r   r   r   r   r   D  r   r   c                   @   r   )Nc                 C   r   r   r   r   r   r   r   r   Q  r   z
N.__init__c                 C   r   r"   r   r   r   r   r   r   T  r   z
N.__iter__Nr   r   r   r   r   r   O  r   r   c                   @   r   )Ec                 C   r   r   r   r   r   r   r   r   Y  r   z
E.__init__c                 C   r   r"   r   r   r   r   r   r   \  r   z
E.__iter__c                 C   s   dd  d S )Nrf   r   r   r   r   r   r   r   ^  r   z
E.__next__Nr   r   r   r   r   r   W  r   r   c                   @   r   )Sc                 C   s   d S r"   r   r   r   r   r   r   c  r   z
S.__init__c                 C   r   r"   r   r   r   r   r   r   e  r   z
S.__iter__c                 C   r   r"   )r   r   r   r   r   r   g  r   z
S.__next__Nr   r   r   r   r   r   a  s
    r   )rt   c                 C   s    	 t tdd ttt| S )Nc                 S   r   r"   r   r   r   r   r   r}   m  r~   zL.<locals>.<lambda>)rt   mapr   r   r   r   r   r   r   Lk  s   r   c                   @   r   )SideEffectLTc                 C   s   || _ || _d S r"   )valuer7   )r   r   r7   r   r   r   r   q  r   zSideEffectLT.__init__c                 C   s   g | j d d < | j|jk S r"   )r7   r   r   r   r   r   r   u  s   zSideEffectLT.__lt__Nr   r   r   r   r   r   p  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 q"d S r   )
r2   r	   r
   r4   r5   r6   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  q3d S NrT   rg   )r2   r	   r
   r4   r5   r6   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 q"| jj| jj	fD ]
}| t|d| q5d S r   )
r   r2   r	   r
   r4   r   r   r   r   r   )r   ry   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   )
r2   r	   r
   r   r   r   r   r4   r5   r6   r   r   r   r   test_arg_parsing  s   z"TestErrorHandling.test_arg_parsingc              
   C   s   | j j| j jfD ]Y}ddtddtdddfD ]I}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 rS   )r>   g333333?rN   i     rg   )r2   r   r   r/   r   r   r   r   r   r   rI   r   r4   r5   r   r   r   r   )r   r   r   gr   r   r   test_iterable_args  s   &z$TestErrorHandling.test_iterable_argsc                    sh   g     fddtdD  | ttf | j td  W d    d S 1 s-w   Y  d S )Nc                 3       | ]}t | V  qd S r"   r   rQ   r7   r   r   	<genexpr>      z@TestErrorHandling.test_heappush_mutating_heap.<locals>.<genexpr>   r   )extendr/   r4   rZ   RuntimeErrorr2   r   r   r   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    d S 1 s)w   Y  d S )Nc                 3   r   r"   r   rQ   r   r   r   r     r   z?TestErrorHandling.test_heappop_mutating_heap.<locals>.<genexpr>r   )r   r/   r4   rZ   r   r2   r
   r   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                          e Zd Z fddZdS )zMTestErrorHandling.test_comparison_operator_modifiying_heap.<locals>.EvilClassc                           tS r"   clearNotImplementedr   or   r   r   r        zTTestErrorHandling.test_comparison_operator_modifiying_heap.<locals>.EvilClass.__lt__Nr    r   r!   r   r   r   r   r   	EvilClass      r  r   r>   )rb   r2   r   r4   rZ   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                       r   )zOTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.hc                    r   r"   r   r   list2r   r   r     r   zVTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.h.__lt__Nr  r   r  r   r   rd     r  rd   c                       r   )zOTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.gc                    r   r"   r   r   list1r   r   r     r   zVTestErrorHandling.test_comparison_operator_modifiying_heap_two_heaps.<locals>.g.__lt__Nr  r   r  r   r   r     r  r   r   r>   )rb   r2   r   r4   rZ   r   )r   rd   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   z  s    	r   c                   @   r   )TestErrorHandlingPythonNr   r   r   r   r   r
    r   r
  c                   @   r   )TestErrorHandlingCNr   r   r   r   r   r    r   r  __main__)'r0   Zunittestr#   testr   Ztest.supportr   r   r   operatorr   Zimport_fresh_moduler   r   r   r   r+   r,   r   r   r   r   r   r   r   r   r   r   r   r   	itertoolsrt   r   r   r   r
  r  r    mainr   r   r   r   <module>   sL     ]	
	
Z