o
    g5                     @   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rPe  dS dS )    )supportN
cmp_to_keyc                 C   s   t rtd|  |d d  }|r|jt|d n|  t|t|krEtd|  tdt|t| t| t| t| td7 ad S t|D ]+\}}|| }||urttd|  td||| t| t| t| td7 a d S qId 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   D/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_sort.pycheck
   s4   


r   c                   @      e Zd Zdd ZdS )TestBasec           
   	      s@  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rEtd
| |d d  }td|| |d d  }|  td|| |d d  }t	| td|| |d d  }|  |d d  }td||dd  trt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w |rd _td|| fddt |D }dd |D }	|	
  dd |	D }td || q6d 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      
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__@   r   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      z1TestBase.testStressfully.<locals>.Stable.__repr__N)r)   r*   r+   r   r&   r(   r   r   r   r   StableC   s    r/   zTesting sizeZidentityreversedzrandom permutationzreversed via functionc                 S   s   || k|| k  S r   r   abr   r   r   <lambda>a   s    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    r1   r   r   r   r4   g       r   z(an insane function left some permutationc                 S      t r   r!   xr   r   r   bad_keyk      z)TestBase.testStressfully.<locals>.bad_keyc                    s   g | ]} |qS r   r   .0r   r$   r   r   
<listcomp>p   r7   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@   }       c                 S   s   g | ]}||j fqS r   )r-   )r?   er   r   r   r@   ~   s    c                 S   s   g | ]\}}|qS r   r   )r?   rD   r   r   r   r   r@      r7   Z	stability)rangeextendobjectlistr   r	   r   reverser    shuffler
   r   r   assertRaisesr!   r   )
r   ZsizesZpowernr;   syr<   Zit_complainedZ	augmentedr   )r%   r/   r   testStressfully+   sh   


zTestBase.testStressfullyN)r)   r*   r+   rO   r   r   r   r   r   *       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      ?r5   g      ?)r    popappendr"   Lr   r   r&      s   

z)TestBugs.test_bug453523.<locals>.C.__lt__Nr)   r*   r+   r&   r   rT   r   r   C   s    rW   c                    s   g | ]}  qS r   r   r>   )rW   r   r   r@          z+TestBugs.test_bug453523.<locals>.<listcomp>2   )rE   rK   
ValueErrorr
   r'   r   )rW   rU   r   test_bug453523   s   zTestBugs.test_bug453523c                    sh   g }t dD ]+} 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 Nr5   )rS   rR   r;   rN   rT   r   r   mutating_cmp   s   
z7TestBugs.test_undetected_mutation.<locals>.mutating_cmpr   r   r   c                    s$     d  d d = | |k| |k  S r]   )rS   r^   rT   r   r   r_      s   

)rE   rK   rZ   r
   r   )r   Zmemorywasterr   r_   r   rT   r   test_undetected_mutation   s   z!TestBugs.test_undetected_mutationN)r)   r*   r+   r[   r`   r   r   r   r   rQ      s    rQ   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)r;   rN   ZxlowerZylowerr   r   r   my_cmp      z9TestDecorateSortUndecorate.test_decorated.<locals>.my_cmp)splitr    rJ   r
   strrc   r   )r   datacopyrd   r   r   r   test_decorated   s   
z)TestDecorateSortUndecorate.test_decoratedc                 C   s"   d  }| jt|jdd d d S )Nrb   c                 S   s   dS Nr   r   r^   r   r   r   r4      s    z>TestDecorateSortUndecorate.test_baddecorator.<locals>.<lambda>r   )rf   rK   	TypeErrorr
   r   rh   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      g | ]	}t d |fqS r   rA   r>   r   r   r   r@          z=TestDecorateSortUndecorate.test_stability.<locals>.<listcomp>   c                 S      | d S rk   r   )tr   r   r   r4          z;TestDecorateSortUndecorate.test_stability.<locals>.<lambda>r   )rE   r
   assertEqual)r   rh   ri   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   r:   r   r   r   r4      ru   zDTestDecorateSortUndecorate.test_key_with_exception.<locals>.<lambda>r   )rH   rE   rK   ZeroDivisionErrorr
   rv   )r   rh   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 Nr\   rE   r:   rh   r   r   k   s   
z<TestDecorateSortUndecorate.test_key_with_mutation.<locals>.kr   )rH   rE   rK   rZ   r
   )r   r   r   r~   r   test_key_with_mutation   s   z1TestDecorateSortUndecorate.test_key_with_mutationc                    6   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   r;   r   r   r   r      r=   zRTestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKiller.__init__c                    s    d d = t d d d < d S r|   r}   r'   r~   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&      r.   zPTestDecorateSortUndecorate.test_key_with_mutating_del.<locals>.SortKiller.__lt__N)r)   r*   r+   r   r   r&   r   r~   r   r   
SortKiller   s    r   r   )rH   rE   rG   rK   rZ   r
   r   r   r   r~   r   test_key_with_mutating_del   s   z5TestDecorateSortUndecorate.test_key_with_mutating_delc                    r   )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   r9   r   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 r|   )rH   rE   r'   r~   r   r   r      s   
z_TestDecorateSortUndecorate.test_key_with_mutating_del_and_exception.<locals>.SortKiller.__del__N)r)   r*   r+   r   r   r   r~   r   r   r      s    r   r   )rH   rE   rG   rK   r!   r
   r   r   r~   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)rI   c   )rH   rE   r    rJ   r
   rv   rm   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   ro   rp   rA   r>   r   r   r   r@      rq   zETestDecorateSortUndecorate.test_reverse_stability.<locals>.<listcomp>rr   c                 S   s"   | d |d }}||k||k  S rk   r   r;   rN   Zx0Zy0r   r   r   rd      re   zATestDecorateSortUndecorate.test_reverse_stability.<locals>.my_cmpc                 S   s"   | d |d }}||k||k  S rk   r   r   r   r   r   my_cmp_reversed   re   zJTestDecorateSortUndecorate.test_reverse_stability.<locals>.my_cmp_reversedT)r   rI   r   c                 S   rs   rk   r   r:   r   r   r   r4     ru   zCTestDecorateSortUndecorate.test_reverse_stability.<locals>.<lambda>)rE   r
   r   rv   )r   rh   Zcopy1copy2rd   r   r   r   r   test_reverse_stability   s   z1TestDecorateSortUndecorate.test_reverse_stabilityN)r)   r*   r+   rj   rn   rw   r{   r   r   r   r   r   r   r   r   r   ra      s    
ra   c           	      C   s   t d t | |d d  }dd |D }dd |D }|||fD ]$}t|}dd tdd |D D }t||D ]
\}}| || q<q#d S )Nr   c                 S      g | ]}|fqS r   r   r?   r;   r   r   r   r@     rX   z:check_against_PyObject_RichCompareBool.<locals>.<listcomp>c                 S      g | ]}|ffqS r   r   r   r   r   r   r@     r7   c                 S   s   g | ]}|d  qS )r   r   )r?   rN   r   r   r   r@     r7   c                 S   s   g | ]}d |fqS )r   r   r   r   r   r   r@     r7   )r    seedrJ   sortedzipZassertIs)	r   rU   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 ]!}|  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   r   r   r   r   r   r   r   r@   *  rX   zBTestOptimizedCompares.test_safe_object_compare.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r@   +  r7   r   g?            )rK   rl   r
   r   )r   Zheterogeneous_listsrU   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?w   Y   fddtd	D }|d
 | t dd |D   W d    n1 smw   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| | qd 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__r6   r&   r"   
WackyList2elemr   r   r&   :  s   zPTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyComparator.__lt__NrV   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                   @   r   )zDTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyList2c                 S   r8   r   )rZ   r"   r   r   r   r&   B  r=   zKTestOptimizedCompares.test_unsafe_object_compare.<locals>.WackyList2.__lt__NrV   r   r   r   r   r   A  rP   r   c                       g | ]
} ||gqS r   r   r>   r   r   r   r   r@   E  rC   zDTestOptimizedCompares.test_unsafe_object_compare.<locals>.<listcomp>r   r   c                    r   r   r   r>   r   r   r   r@   J  rC   c                 S   r   r   r   r   r   r   r   r@   M  rX   c                   @   r   )zMTestOptimizedCompares.test_unsafe_object_compare.<locals>.PointlessComparatorc                 S   s   t S r   )NotImplementedr"   r   r   r   r&   R  r=   zTTestOptimizedCompares.test_unsafe_object_compare.<locals>.PointlessComparator.__lt__NrV   r   r   r   r   PointlessComparatorQ  rP   r   c                 S   r   r   r   r   r   r   r   r@   V  rX   r   r   c                 S      g | ]}t |qS r   rg   r   r   r   r   r@   [  r7   u   ￿c                 S   r   r   )bytesr   r   r   r   r@   \  r7   c                 S   s   g | ]
}t d d |qS )c                 S   s   | |k S r   r   r^   r   r   r   r4   ]  ru   zMTestOptimizedCompares.test_unsafe_object_compare.<locals>.<listcomp>.<lambda>r   r   r   r   r   r@   ]  rC   )r6   rH   rE   rK   rZ   r
   rl   r   )r   rU   r   Zlistsr   )r   r   r   r   r   test_unsafe_object_compare4  s2   
z0TestOptimizedCompares.test_unsafe_object_comparec                 C      t | dd tdD  d S )Nc                 S   r   r   r   r   r   r   r   r@   b      zCTestOptimizedCompares.test_unsafe_latin_compare.<locals>.<listcomp>r   r   rE   r'   r   r   r   test_unsafe_latin_comparea     
z/TestOptimizedCompares.test_unsafe_latin_comparec                 C   r   )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  r   z.TestOptimizedCompares.test_unsafe_long_comparec                 C   r   )Nc                 S   r   r   floatr   r   r   r   r@   j  r   zCTestOptimizedCompares.test_unsafe_float_compare.<locals>.<listcomp>r   r   r'   r   r   r   test_unsafe_float_comparei  r   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  r   zCTestOptimizedCompares.test_unsafe_tuple_compare.<locals>.<listcomp>)r   r   rE   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   )r2   r   )r   r2   )rK   rl   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__r   )testr   r    Zunittest	functoolsr   r   r   r   ZTestCaser   rQ   ra   r   r   r)   mainr   r   r   r   <module>   s    
 [%]\