a
    3j5                     @   s   d dl mZ d dlZd dlZd dlmZ ejZd adddZG dd dej	Z
G dd	 d	ej	ZG d
d dej	Zdd ZG dd dej	Zedkre  dS )    )supportN
cmp_to_keyc                 C   s   t rtd|  |d d  }|r0|jt|d n|  t|t|krtd|  tdt|t| t| t| t| td7 ad S t|D ]V\}}|| }||urtd|  td||| t| t| t| td7 a d S qd S )Nz    checkingkeyzerror inzlength mismatch;   zout of order at index)verboseprintsortr   lennerrors	enumerate)tagZexpectedrawZcompareZorigiZgoodmaybe r   J/www/server/python_manager/versions/3.9.10/lib/python3.9/test/test_sort.pycheck
   s0    


r   c                   @   s   e Zd Zdd ZdS )TestBasec           
   	      sJ  dg}t ddD ]$}d| }|t |d |d  q|g d G  fdddt G dd	 d	t|D ]}tt |}trtd
| |d d  }td|| |d d  }|  td|| |d d  }t	| td|| |d d  }|  |d d  }td||dd  tr.td td |d d  }|j
tdd d td|| t|dkrdd }|d d  }| jt|j
|d  fdd|D }|d d  }t	| d _d}z|
  W n ty   d}Y n0 |rd _td|| fddt |D }dd |D }	|	
  dd |	D }td || qld S )!Nr   r   
      )r   d   i  c                       s,   e Zd ZdZdd Z fddZdd ZdS )	z+TestBase.testStressfully.<locals>.ComplainsTc                 S   s
   || _ d S Nr   )selfr   r   r   r   __init__6   s    z4TestBase.testStressfully.<locals>.Complains.__init__c                    s2    j r&t dk r&tr"td| | t| j|jk S )NgMbP?z        complaining at)maybe_complainrandomr   r	   RuntimeErrorr   r   other	Complainsr   r   __lt__9   s
    z2TestBase.testStressfully.<locals>.Complains.__lt__c                 S   s
   d| j  S )NzComplains(%d)r   r   r   r   r   __repr__@   s    z4TestBase.testStressfully.<locals>.Complains.__repr__N)__name__
__module____qualname__r   r   r$   r&   r   r"   r   r   r#   3   s   r#   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )z(TestBase.testStressfully.<locals>.Stablec                 S   s   || _ || _d S r   r   index)r   r   r   r   r   r   r   D   s    z1TestBase.testStressfully.<locals>.Stable.__init__c                 S   s   | j |j k S r   r   r    r   r   r   r$   H   s    z/TestBase.testStressfully.<locals>.Stable.__lt__c                 S   s   d| j | jf S )NzStable(%d, %d)r*   r%   r   r   r   r&   K   s    z1TestBase.testStressfully.<locals>.Stable.__repr__N)r'   r(   r)   r   r$   r&   r   r   r   r   StableC   s   r,   zTesting sizeidentityreversedzrandom permutationzreversed via functionc                 S   s   || k|| k  S r   r   abr   r   r   <lambda>a       z*TestBase.testStressfully.<locals>.<lambda>z3    Checking against an insane comparison function.z?        If the implementation isn't careful, this may segfault.c                 S   s   t t d d S )N   r   )intr   r/   r   r   r   r2   g   r3   r   z(an insane function left some permutationc                 S   s   t d S r   r   xr   r   r   bad_keyk   s    z)TestBase.testStressfully.<locals>.bad_keyc                    s   g | ]} |qS r   r   .0r   r"   r   r   
<listcomp>p   r3   z,TestBase.testStressfully.<locals>.<listcomp>TFz+exception during sort left some permutationc                    s   g | ]} t d |qS )r   r   	randranger:   )r,   r   r   r<   }   r3   c                 S   s   g | ]}||j fqS r   )r+   )r;   er   r   r   r<   ~   r3   c                 S   s   g | ]\}}|qS r   r   )r;   r?   r   r   r   r   r<      r3   Z	stability)rangeextendobjectlistr   r	   r   reverser   shuffler
   r   r   assertRaisesr   r   )
r   ZsizesZpowernr8   syr9   Zit_complainedZ	augmentedr   )r#   r,   r   testStressfully+   sd    




zTestBase.testStressfullyN)r'   r(   r)   rJ   r   r   r   r   r   *   s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestBugsc                    s:   G fddd  fddt dD | tj d S )Nc                       s   e Zd Z fddZdS )z"TestBugs.test_bug453523.<locals>.Cc                    s0    rt   dk r   n
 d t   dk S )Ng      ?r4   g      ?)r   popappendr    Lr   r   r$      s    

z)TestBugs.test_bug453523.<locals>.C.__lt__Nr'   r(   r)   r$   r   rN   r   r   C   s   rQ   c                    s   g | ]
}  qS r   r   r:   )rQ   r   r   r<      r3   z+TestBugs.test_bug453523.<locals>.<listcomp>2   )r@   rF   
ValueErrorr
   r%   r   )rQ   rO   r   test_bug453523   s    zTestBugs.test_bug453523c                    sh   g }t dD ]V} fdd}ddg | jt jt|d  fdd}| jt jt|d |g}qd S )N   c                    s"     d    | |k| |k  S Nr4   )rM   rL   r8   rI   rN   r   r   mutating_cmp   s    
z7TestBugs.test_undetected_mutation.<locals>.mutating_cmpr   r   r   c                    s$     d  d d = | |k| |k  S rV   )rM   rW   rN   r   r   rX      s    

)r@   rF   rS   r
   r   )r   Zmemorywasterr   rX   r   rN   r   test_undetected_mutation   s    z!TestBugs.test_undetected_mutationN)r'   r(   r)   rT   rY   r   r   r   r   rK      s   rK   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 )TestDecorateSortUndecoratec                 C   sH   d  }|d d  }t| |jtjd dd }|jt|d d S )N,The quick Brown fox Jumped over The lazy Dogr   c                 S   s"   |   |   }}||k||k  S r   )lower)r8   rI   ZxlowerZylowerr   r   r   my_cmp   s    z9TestDecorateSortUndecorate.test_decorated.<locals>.my_cmp)splitr   rE   r
   strr\   r   )r   datacopyr]   r   r   r   test_decorated   s    
z)TestDecorateSortUndecorate.test_decoratedc                 C   s"   d  }| jt|jdd d d S )Nr[   c                 S   s   dS Nr   r   rW   r   r   r   r2      r3   z>TestDecorateSortUndecorate.test_baddecorator.<locals>.<lambda>r   )r^   rF   	TypeErrorr
   r   r`   r   r   r   test_baddecorator   s    z,TestDecorateSortUndecorate.test_baddecoratorc                 C   sF   dd t dD }|d d  }|jdd d |  | || d S )Nc                 S   s   g | ]}t d |fqS r   r=   r:   r   r   r   r<      r3   z=TestDecorateSortUndecorate.test_stability.<locals>.<listcomp>   c                 S   s   | d S rc   r   )tr   r   r   r2      r3   z;TestDecorateSortUndecorate.test_stability.<locals>.<lambda>r   )r@   r
   assertEqual)r   r`   ra   r   r   r   test_stability   s
    z)TestDecorateSortUndecorate.test_stabilityc                 C   s@   t tdd}|d d  }| jt|jdd d | || d S )Nr   c                 S   s   d|  S )Nr   r   r7   r   r   r   r2      r3   zDTestDecorateSortUndecorate.test_key_with_exception.<locals>.<lambda>r   )rC   r@   rF   ZeroDivisionErrorr
   rj   )r   r`   dupr   r   r   test_key_with_exception   s    z2TestDecorateSortUndecorate.test_key_with_exceptionc                    s.   t td  fdd}| jt j|d d S )Nr   c                    s    d d = t d d d < | S NrU   r@   r7   r`   r   r   k   s    
z<TestDecorateSortUndecorate.test_key_with_mutation.<locals>.kr   )rC   r@   rF   rS   r
   )r   rs   r   rr   r   test_key_with_mutation   s    z1TestDecorateSortUndecorate.test_key_with_mutationc                    s6   t td G  fdddt}| jt j|d d S )Nr   c                       s(   e Zd Zdd Z fddZdd ZdS )zITestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKillerc                 S   s   d S r   r   r   r8   r   r   r   r      s    zRTestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKiller.__init__c                    s    d d = t d d d < d S rp   rq   r%   rr   r   r   __del__   s    
zQTestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKiller.__del__c                 S   s   t | t |k S r   )idr    r   r   r   r$      s    zPTestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKiller.__lt__N)r'   r(   r)   r   rv   r$   r   rr   r   r   
SortKiller   s   rx   r   )rC   r@   rB   rF   rS   r
   r   rx   r   rr   r   test_key_with_mutating_del   s    z5TestDecorateSortUndecorate.test_key_with_mutating_delc                    s6   t td G  fdddt}| jt j|d d S )Nr   c                       s    e Zd Zdd Z fddZdS )zWTestDecorateSortUndecorate.test_key_with_mutating_del_and_exception.<locals>.SortKillerc                 S   s   |dkrt d S )Nr   r6   ru   r   r   r   r      s    z`TestDecorateSortUndecorate.test_key_with_mutating_del_and_exception.<locals>.SortKiller.__init__c                    s"    d d = t td d d < d S rp   )rC   r@   r%   rr   r   r   rv      s    
z_TestDecorateSortUndecorate.test_key_with_mutating_del_and_exception.<locals>.SortKiller.__del__N)r'   r(   r)   r   rv   r   rr   r   r   rx      s   rx   r   )rC   r@   rB   rF   r   r
   ry   r   rr   r   (test_key_with_mutating_del_and_exception   s    zCTestDecorateSortUndecorate.test_key_with_mutating_del_and_exceptionc                 C   s>   t td}t| |jdd | |t tddd d S )Nr   T)rD   c   )rC   r@   r   rE   r
   rj   re   r   r   r   test_reverse   s    
z'TestDecorateSortUndecorate.test_reversec                 C   s   dd t dD }|d d  }|d d  }dd }dd }|jt|dd	 |jt|d
 | || |jdd dd	 | || d S )Nc                 S   s   g | ]}t d |fqS rg   r=   r:   r   r   r   r<      r3   zETestDecorateSortUndecorate.test_reverse_stability.<locals>.<listcomp>rh   c                 S   s"   | d |d  }}||k||k  S rc   r   r8   rI   Zx0Zy0r   r   r   r]      s    zATestDecorateSortUndecorate.test_reverse_stability.<locals>.my_cmpc                 S   s"   | d |d  }}||k||k  S rc   r   r   r   r   r   my_cmp_reversed   s    zJTestDecorateSortUndecorate.test_reverse_stability.<locals>.my_cmp_reversedT)r   rD   r   c                 S   s   | d S rc   r   r7   r   r   r   r2     r3   zCTestDecorateSortUndecorate.test_reverse_stability.<locals>.<lambda>)r@   r
   r   rj   )r   r`   Zcopy1copy2r]   r   r   r   r   test_reverse_stability   s    z1TestDecorateSortUndecorate.test_reverse_stabilityN)r'   r(   r)   rb   rf   rk   ro   rt   rz   r{   r~   r   r   r   r   r   rZ      s   
rZ   c           	      C   s   t d t | |d d  }dd |D }dd |D }|||fD ]H}t|}dd tdd |D D }t||D ]\}}| || qxqFd S )Nr   c                 S   s   g | ]
}|fqS r   r   r;   r8   r   r   r   r<     r3   z:check_against_PyObject_RichCompareBool.<locals>.<listcomp>c                 S   s   g | ]}|ffqS r   r   r   r   r   r   r<     r3   c                 S   s   g | ]}|d  qS )r   r   )r;   rI   r   r   r   r<     r3   c                 S   s   g | ]}d |fqS )r   r   r   r   r   r   r<     r3   )r   seedrE   sortedzipZassertIs)	r   rO   ZL_1ZL_2ZL_3Z	optimizedZ	referenceoptrefr   r   r   &check_against_PyObject_RichCompareBool  s    

r   c                   @   sD   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S )TestOptimizedComparesc                 C   s   ddgddgddgg}|D ]B}|  t|j |  tdd |D j |  tdd |D j qdd	gd
d	gd	dgd	d
gg}|D ]}t| | q~d S )Nr   foog        )r   c                 S   s   g | ]
}|fqS r   r   r   r   r   r   r<   *  r3   zBTestOptimizedCompares.test_safe_object_compare.<locals>.<listcomp>c                 S   s   g | ]}|ffqS r   r   r   r   r   r   r<   +  r3   r   g?            )rF   rd   r
   r   )r   Zheterogeneous_listsrO   Zfloat_int_listsr   r   r   test_safe_object_compare$  s    z.TestOptimizedCompares.test_safe_object_comparec                    s  G fdddt  G dd dtG dd dt fddtd	D }|d
 | t |  W d    n1 s~0    Y   fddtd	D }|d
 | t" dd |D   W d    n1 s0    Y  G dd d}| | g}| t|j | tdd |D j ttddg dd tdD dg dd tdD dd tdD g}|D ]}t| | qrd S )Nc                       s   e Zd Z fddZdS )zITestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyComparatorc                    s    _ t| |S r   )	__class__r5   r$   r    
WackyList2elemr   r   r$   :  s    zPTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyComparator.__lt__NrP   r   r   r   r   WackyComparator9  s   r   c                   @   s   e Zd ZdS )zDTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyList1N)r'   r(   r)   r   r   r   r   
WackyList1>  s   r   c                   @   s   e Zd Zdd ZdS )zDTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyList2c                 S   s   t d S r   )rS   r    r   r   r   r$   B  s    zKTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyList2.__lt__NrP   r   r   r   r   r   A  s   r   c                    s   g | ]} ||gqS r   r   r:   r   r   r   r   r<   E  r3   zDTestOptimizedCompares.test_unsafe_object_compare.<locals>.<listcomp>r   r}   c                    s   g | ]} ||gqS r   r   r:   r   r   r   r<   J  r3   c                 S   s   g | ]
}|fqS r   r   r   r   r   r   r<   M  r3   c                   @   s   e Zd Zdd ZdS )zMTestOptimizedCompares.test_unsafe_object_compare.<locals>.PointlessComparatorc                 S   s   t S r   )NotImplementedr    r   r   r   r$   R  s    zTTestOptimizedCompares.test_unsafe_object_compare.<locals>.PointlessComparator.__lt__NrP   r   r   r   r   PointlessComparatorQ  s   r   c                 S   s   g | ]
}|fqS r   r   r   r   r   r   r<   V  r3   r   r   c                 S   s   g | ]}t |qS r   r_   r   r   r   r   r<   [  r3   u   ￿c                 S   s   g | ]}t |qS r   )bytesr   r   r   r   r<   \  r3   c                 S   s   g | ]}t d d |qS )c                 S   s   | |k S r   r   rW   r   r   r   r2   ]  r3   zMTestOptimizedCompares.test_unsafe_object_compare.<locals>.<listcomp>.<lambda>r   r   r   r   r   r<   ]  r3   )r5   rC   r@   rF   rS   r
   rd   r   )r   rO   r   Zlistsr   )r   r   r   r   r   test_unsafe_object_compare4  s,    &0z0TestOptimizedCompares.test_unsafe_object_comparec                 C   s   t | dd tdD  d S )Nc                 S   s   g | ]}t |qS r   r   r   r   r   r   r<   b  s   zCTestOptimizedCompares.test_unsafe_latin_compare.<locals>.<listcomp>r   r   r@   r%   r   r   r   test_unsafe_latin_comparea  s    
z/TestOptimizedCompares.test_unsafe_latin_comparec                 C   s   t | dd tdD  d S )Nc                 S   s   g | ]}|qS r   r   r   r   r   r   r<   f  s   zBTestOptimizedCompares.test_unsafe_long_compare.<locals>.<listcomp>r   r   r%   r   r   r   test_unsafe_long_comparee  s    
z.TestOptimizedCompares.test_unsafe_long_comparec                 C   s   t | dd tdD  d S )Nc                 S   s   g | ]}t |qS r   floatr   r   r   r   r<   j  s   zCTestOptimizedCompares.test_unsafe_float_compare.<locals>.<listcomp>r   r   r%   r   r   r   test_unsafe_float_comparei  s    
z/TestOptimizedCompares.test_unsafe_float_comparec                 C   s0   t | tdgd  t | dd tdD  d S )Nnanr   c                 S   s   g | ]}t d qS )r   r   )r;   _r   r   r   r<   v  s   zCTestOptimizedCompares.test_unsafe_tuple_compare.<locals>.<listcomp>)r   r   r@   r%   r   r   r   test_unsafe_tuple_comparem  s    
z/TestOptimizedCompares.test_unsafe_tuple_comparec                 C   s:   |  tg dj |  tddgj |  tddgj d S )N))      ?r   )FA   )r0   r   )r   r0   )rF   rd   r
   r%   r   r   r   test_not_all_tuplesy  s    z)TestOptimizedCompares.test_not_all_tuplesN)
r'   r(   r)   r   r   r   r   r   r   r   r   r   r   r   r   #  s   -r   __main__)N)testr   r   Zunittest	functoolsr   r   r   r   ZTestCaser   rK   rZ   r   r   r'   mainr   r   r   r   <module>   s   
 [%]\