o
    GXhz                     @   s  d dl Zd dlZd dlZd dlZd dlZd dlZd dlZd dlZzd dl	m
Z
 W n ey3   dZ
Y nw dd ZG dd dejZG dd dZG d	d
 d
eZG dd dZG dd deZG dd deZee
du dG dd dejZedkre  dS dS )    Nhamtc                    s   t   fdd}|S )z*Needed to make reftracking test mode work.c                     s    t  }|j g| R i |S NcontextvarsContextrun)argskwargsctxfunc ;/opt/python-3.10.19/usr/lib/python3.10/test/test_context.pywrapper   s   z!isolated_context.<locals>.wrapper)	functoolswraps)r   r   r   r   r   isolated_context   s   r   c                   @   s   e Zd Zdd Zedd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zedd Zdd Zdd Zdd Zedd Zedd Zedd  Zed!d" Zed#d$ Zd%d& Zed'd( Zd)S )*ContextTestc                 C   s   |  td t  W d    n1 sw   Y  |  td td W d    n1 s0w   Y  td}| |jd | t d|_W d    n1 sTw   Y  | t	|t	d d S )Nztakes exactly 1zmust be a str   aaabbb)
assertRaisesRegex	TypeErrorr   
ContextVarassertEqualnameassertRaisesAttributeErrorassertNotEqualhash)selfcr   r   r   test_context_var_new_1   s   

z"ContextTest.test_context_var_new_1c                 C   s   t d}| dt| t jddd}| dt| g }t jd|d}|| | dt| | dt| |d}| t|t| | dt| || | dt| d S )Na{   defaultZ123z...r   z used )r   r   assertInreprappendsetassertNotInreset)r!   r"   Zlsttr   r   r   test_context_var_repr_1)   s   



z#ContextTest.test_context_var_repr_1c                 C   s   |  td G dd dtj}W d    n1 sw   Y  |  td G dd dtj}W d    n1 s9w   Y  |  td G dd dtj}W d    d S 1 sYw   Y  d S )Nznot an acceptable base typec                   @      e Zd ZdS )z<ContextTest.test_context_subclassing_1.<locals>.MyContextVarN__name__
__module____qualname__r   r   r   r   MyContextVar?   s    r5   c                   @   r0   )z9ContextTest.test_context_subclassing_1.<locals>.MyContextNr1   r   r   r   r   	MyContextD       r6   c                   @   r0   )z7ContextTest.test_context_subclassing_1.<locals>.MyTokenNr1   r   r   r   r   MyTokenH   r7   r8   )r   r   r   r   r   Token)r!   r5   r6   r8   r   r   r   test_context_subclassing_1=   s   "z&ContextTest.test_context_subclassing_1c                 C   s   |  td td W d    n1 sw   Y  |  td tjddd W d    n1 s3w   Y  |  td tjdd W d    n1 sOw   Y  tjdi i  d S )Nzany argumentsr   r$   r   )r   r   r   r   r!   r   r   r   test_context_new_1K   s   zContextTest.test_context_new_1c                 C   s   t  }| td |d  W d    n1 sw   Y  | td d|v  W d    n1 s3w   Y  | td |d W d    d S 1 sOw   Y  d S )NzContextVar key was expectedr   )r   r   r   r   getr!   r   r   r   r   test_context_typerrors_1T   s   

"z$ContextTest.test_context_typerrors_1c                 C   s   t  }| |t j d S r   )r   copy_contextZassertIsInstancer   r?   r   r   r   test_context_get_context_1^   s   z&ContextTest.test_context_get_context_1c                 C   sB   t  }| td |  W d    d S 1 sw   Y  d S )Nzmissing 1 required)r   r   r   r   r   r?   r   r   r   test_context_run_1b   s   
"zContextTest.test_context_run_1c                 C   s   t  }dd }|t|fD ]X}| ||dddif | ||ddddif | |j|dd	dddd
f | |j|ddd	dddd
f i }| |j|dfi |dddif | |i  qd S )Nc                  _   s   d|d< | d7 } | |fS )Nfoospambarr   r	   r
   r   r   r   r   k   s   z,ContextTest.test_context_run_2.<locals>.funcrF   rE   rD   r   )r   rG      r;   )r$   rE      )rJ   rG   )r   r   r   partialr   r   )r!   r   r   fr$   r   r   r   test_context_run_2h   s(   
zContextTest.test_context_run_2c                 C   s   t  }dd }| t || W d    n1 sw   Y  | t ||dd W d    n1 s9w   Y  | t |j|dddd W d    d S 1 sXw   Y  d S )Nc                  _   s   dd  d S )Nr   r   r   rH   r   r   r   r      s   z,ContextTest.test_context_run_3.<locals>.funcr   rI   r%   r;   )r   r   r   ZeroDivisionErrorr   r!   r   r   r   r   r   test_context_run_3   s   "zContextTest.test_context_run_3c                    sp   t  }t   t dfdd fdd}||}|| | d | d S )Nvarc                      s     d  d S r   )assertIsNoner>   r   r!   rQ   r   r   func2   s   z-ContextTest.test_context_run_4.<locals>.func2c                     sb    d  d   d d t } t| d |  d | S )NrE   r   )rR   r>   r+   r   r   r   rA   len)Zcurctx2rT   r!   rQ   r   r   func1   s   

z-ContextTest.test_context_run_4.<locals>.func1rE   )r   r   r   r   r   r(   )r!   ctx1rX   Zreturned_ctxr   rV   r   test_context_run_4   s   

zContextTest.test_context_run_4c                    sh   t  }t d fdd} t || W d    n1 s%w   Y   d  d S )NrQ   c                      s&     d  d dd  d S )NrE   r   r   )rR   r>   r+   r   rS   r   r   r      s   
z,ContextTest.test_context_run_5.<locals>.func)r   r   r   r   rN   r   rR   r>   rO   r   rS   r   test_context_run_5   s   
zContextTest.test_context_run_5c                    s4   t  t jddd  fdd}| d S )Nr$   r   r&   c                      sP      d    d    d   d d S )Nr   *   )r   r>   rR   r+   r   r"   r   r!   r   r   fun   s
   
z+ContextTest.test_context_run_6.<locals>.funr   r   r   r   r!   r^   r   r]   r   test_context_run_6   s   zContextTest.test_context_run_6c                    s&   t    fdd  d S )Nc                      s<    td   W d    d S 1 sw   Y  d S )Nzis already entered)r   RuntimeErrorr   r   r   r^   r!   r   r   r^      s   "z+ContextTest.test_context_run_7.<locals>.funr   r<   r   rc   r   test_context_run_7   s   zContextTest.test_context_run_7c                 C   s  t d}| t |  W d    n1 sw   Y  | |d  |d}| | d | |d d | |j	|j
 | |j	t jj
 | |j| |d}| | d | |d d | |j	d || | | d | |d d |d | td || W d    n1 sw   Y  | | d t  }| || || | td || W d    n1 sw   Y  | |d  | || | || d | ||dd | t|d | t| |dfg | t| dg | t| |g | t||g t  }| || | t ||  W d    n	1 sLw   Y  | ||dd | t|d | t|g  d S )	Nr"   r\   rE   Zspam2zhas already been usedaar   r   )r   r   r   LookupErrorr>   rR   r+   r   assertIs	old_valueMISSINGr9   rQ   r-   r   rb   rA   r(   rU   listitemsvalueskeysr,   KeyError)r!   r"   t0r.   rY   rW   r   r   r   test_context_getset_1   s\   







z!ContextTest.test_context_getset_1c                 C   sZ   t d}t d}|d}| td || W d    d S 1 s&w   Y  d S )Nv1v2r\   zby a different)r   r   r+   r   
ValueErrorr-   )r!   rq   rr   t1r   r   r   test_context_getset_2  s   


"z!ContextTest.test_context_getset_2c                    s4   t jddd t   fdd}| d S )Nr"   r\   r&   c                     s     d t    W d    n1 sw   Y      dd    t g   d}  t  g    d  	|   t g  t    W d    d S 1 sw   Y  d S )Nr\   rE   r   )
r   r>   r   rn   rR   r,   rj   rm   r+   r-   )r.   r]   r   r   r^     s    



"z.ContextTest.test_context_getset_3.<locals>.fun)r   r   r   r   r`   r   r]   r   test_context_getset_3  s   z!ContextTest.test_context_getset_3c                 C   s`   t jddd}t  }||jd}| td || W d    d S 1 s)w   Y  d S )Nr"   r\   r&   r   zdifferent Context)r   r   r   r   r+   r   rs   r-   )r!   r"   r   tokr   r   r   test_context_getset_4'  s   "z!ContextTest.test_context_getset_4c                    sH   t jddd  g   fdd}t  |   g  d S )Nr"   r\   r&   c                      s.     g    d   dg d S )Nr\   )r+   r>   r*   r   r   r"   r!   r   r   r^   6  s   
z.ContextTest.test_context_getset_5.<locals>.fun)r   r   r+   rA   r   r   r>   r`   r   ry   r   test_context_getset_51  s
   
z!ContextTest.test_context_getset_5c                    sD   t  t jddd  fdd} fdd| d S )Nr"   r\   r&   c                     sz     d  } |   d   d   d |   d |    d |   d d S )N
         )r+   copyr   r   )rW   r"   rY   ctx2_funr!   r   r   ctx1_funB  s   


z1ContextTest.test_context_copy_1.<locals>.ctx1_func                      s.      d  d    d d S )Nr{   r}   )r   r>   r+   r   ry   r   r   r   P  s   
z1ContextTest.test_context_copy_1.<locals>.ctx2_funr_   )r!   r   r   r   r   test_context_copy_1>  s
   zContextTest.test_context_copy_1c                    sj   t d  fdd}tjjdd}zt||td}W |  n|  w 	|ttd d S )Ncvarc                    sF   t dD ]} | |  ttdd   | |  q| S )Nr{   gMbP?g?)ranger+   timesleeprandomuniformr   r>   )numir   r!   r   r   sub[  s
   z/ContextTest.test_context_threads_1.<locals>.subr{   )max_workers)
r   r   
concurrentfuturesThreadPoolExecutorrj   mapr   shutdownr   )r!   r   tpresultsr   r   r   test_context_threads_1W  s   
z"ContextTest.test_context_threads_1N)r2   r3   r4   r#   r   r/   r:   r=   r@   rB   rC   rM   rP   rZ   r[   ra   rd   rp   ru   rv   rx   rz   r   r   r   r   r   r   r      s:    
	

	
5


	
r   c                   @   s6   e Zd ZdZddddZdd Zdd Zd	d
 ZdS )HashKeyNerror_on_eq_toc                C   s   || _ || _|| _d S r   )r   r    r   )r!   r    r   r   r   r   r   __init__p  s   
zHashKey.__init__c                 C   s   d| j  d| j dS )Nz
<Key name:z hash:>)r   r    r<   r   r   r   __repr__v  s   zHashKey.__repr__c                 C   s   | j d ur| j jrt| jS r   )_crashererror_on_hashHashingErrorr    r<   r   r   r   __hash__y  s   zHashKey.__hash__c                 C   s   t |tstS | jd ur| jjrt| jd ur&| j|u r&td| d||jd ur:|j| u r:td|d| | j| j	f|j|j	fkS )Nzcannot compare z to )

isinstancer   NotImplementedr   error_on_eqEqErrorr   rs   r   r    r!   otherr   r   r   __eq__  s   
zHashKey.__eq__)r2   r3   r4   r   r   r   r   r   r   r   r   r   r   m  s    r   c                       s(   e Zd Z fddZ fddZ  ZS )KeyStrc                    s    t jd urt jjrtt  S r   )r   r   r   r   superr   r<   	__class__r   r   r     s   
zKeyStr.__hash__c                    s"   t jd urt jjrtt |S r   )r   r   r   r   r   r   r   r   r   r   r     s   zKeyStr.__eq__)r2   r3   r4   r   r   __classcell__r   r   r   r   r     s    r   c                   @   s,   e Zd ZdddddZdd Zdd Zd	S )
HaskKeyCrasherFr   r   c                C   s   || _ || _d S r   r   )r!   r   r   r   r   r   r     s   
zHaskKeyCrasher.__init__c                 C   s   t jd ur	td| t _d S )Nzcannot nest crashers)r   r   rb   r<   r   r   r   	__enter__  s   

zHaskKeyCrasher.__enter__c                 G   s
   d t _d S r   )r   r   )r!   excr   r   r   __exit__  s   
zHaskKeyCrasher.__exit__N)r2   r3   r4   r   r   r   r   r   r   r   r     s    r   c                   @   r0   )r   Nr1   r   r   r   r   r     r7   r   c                   @   r0   )r   Nr1   r   r   r   r   r     r7   r   z!_testcapi lacks "hamt()" functionc                   @   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#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )0HamtTestc                 C   sn   t dd}t dd}| || | t|t| t }d||< d||< | || d | || d d S )Nr{   r   r   r$   b)r   r   r   r    dict)r!   k1k2dr   r   r   test_hashkey_helper_1  s   

zHamtTest.test_hashkey_helper_1c                 C   s   t  }d }d S r   r   r!   hr   r   r   test_hamt_basics_1  s   zHamtTest.test_hamt_basics_1c                 C   sB  t  }| t|d |dd}| || | t|d | t|d | |d | |ddd | |dd |dd}| || | t|d | t|d | t|d | |dd | |dd | |d | |d | |d | |dd d  } }}d S )Nr   r$   r   r   r\   r{   rI   )r   r   rU   r+   assertIsNotrR   r>   )r!   r   h2h3r   r   r   test_hamt_basics_2  s*   zHamtTest.test_hamt_basics_2c                 C   s4   t  }t }|d|}|d|}| || d S )N1)r   objectr+   rg   )r!   r   oh1r   r   r   r   test_hamt_basics_3  s
   zHamtTest.test_hamt_basics_3c                 C   sf   t  }|dg }|dg }| || | t|d | t|d | |d|d d S )Nkeyr   )r   r+   r   r   rU   r>   )r!   r   r   r   r   r   r   test_hamt_basics_4  s   zHamtTest.test_hamt_basics_4c           	      C   s  t dd}t dd}t dd}t }||d}||d}| ||d  | ||d  | ||d | ||d  | ||d | ||d ||d}||d}| ||d | ||d | ||d | ||d | ||d  | ||d | ||d | ||d | ||d | t|d	 | t|d
 | t|d | t|d | t|d d S )Nr{   r   r   cccr$   r   Zccre   r   r   rI      )r   r   r+   r   r>   rU   )	r!   r   r   Zk3r   r   r   Zh4Zh5r   r   r   test_hamt_collision_1  s8   


zHamtTest.test_hamt_collision_1c                 C   sj   t dd}t dd}t dd}t }||d}||d}||d}| dd | D h d d S )	Nl        CDr   Ec                 S   s   h | ]}|j qS r   )r   ).0kr   r   r   	<setcomp>9  s    z1HamtTest.test_hamt_collision_3.<locals>.<setcomp>>   r   r   r   )r   r   r+   r   rm   )r!   r   r   r   r   r   r   r   test_hamt_collision_3  s   


"zHamtTest.test_hamt_collision_3c                 C   s  d}d}d}d}d}t |D ]2}t }t }t |D ]}	t|	}
|	| sUtdd# | t ||
|	 W d    n1 sAw   Y  W d    n1 sPw   Y  ||
|	}|	| stdd$ | t |	t|	 W d    n1 s|w   Y  W d    n1 sw   Y  |	||
< | 
t|t| |	| s| 
t| t|  | 
t| t|  q| 
t|| t |D ]}
| 
|	t|
d	|
 qtt |}t| t|D ]\}}	t|	}
|| s'tdd# | t ||
 W d    n	1 sw   Y  W d    n	1 s"w   Y  || s_tdd% | t |t|	 W d    n	1 sJw   Y  W d    n	1 sZw   Y  ||
}| 
|	|
d	d	 ||
= | 
t|t| ||d
 kr|}| }|| s| 
t| t|  | 
t| t|  q| 
t|d | 
t|d |D ]}
| 
|	t|
||
  q| 
t|t| t|D ]K\}	}
|t|
}| 
|	t|
d	d	 |t|
d  | 
t|t| |	| s%| 
t| t|  | 
t| t|  q| 
t|d | 
t|d | 
t| g  qd S )NiX  i  a   rJ   r   Tr   r   z	not foundrI   r   )r   r   r   r   r   r   r   r+   r   r>   r   rU   rk   rj   r   shuffle	enumeratedeleter~   rm   strpoprl   )r!   ZCOLLECTION_SIZEZTEST_ITERS_EVERYZCRASH_HASH_EVERYZCRASH_EQ_EVERYZ
RUN_XTIMES_r   r   r   r   Zkeys_to_deleteZiter_iZhmZdmr   r   r   test_hamt_stress;  s   





zHamtTest.test_hamt_stressc                 C   sv  t dd}t dd}t dd}t dd}t d	d
}t dd}t dd|d}t }||d}||d}||d}||d}||d}t|}	||}| t||	d  | td || W d    n1 sow   Y  ||}| t||	d  ||}
| |
| ||}| t||	d  | |	|dd | |	|d | |	|d d S )Nd   Ae   Bf   r   g   r   h   r   ZErr   r$   r   r"   r   er   cannot comparerI   r   r\   )
r   r   r+   rU   r   r   r   rs   rg   r>   )r!   r   r   r   r   r   r   r   r   orig_lenr   r   r   r   test_hamt_delete_1  s8   






	


zHamtTest.test_hamt_delete_1c           
      C   s  t dd}t dd}t dd}t dd}t d	d
}t dd}t dd|d}t }||d}||d}||d}||d}||d}t|}	| td || W d    n1 s`w   Y  ||}| t||	 ||}| t||	d  ||}| t||	d  ||}| t||	d  | ||d | ||d ||}||}||}||}| t|d d S )Nr   r   ) r   鉊 r   r   r   r   r   r   r   r   r   r$   r   r"   r   r   r   r   rI   r   r   )	r   r   r+   rU   r   rs   r   r   r>   )
r!   r   r   r   r   r   r   r   r   r   r   r   r   test_hamt_delete_2  s@   













zHamtTest.test_hamt_delete_2c                 C   s   t dd}t dd}t dd}t dd}t dd	}t }||d
}||d}||d}||d}||d}t|}||}| t||d  ||}| t||d  | ||d | ||d d S )Nr   r   r   r    r   r   r   r   r$   r   r"   r   r   r   rI   )r   r   r+   rU   r   r   r>   r!   r   r   r   r   r   r   r   r   r   r   test_hamt_delete_3  s$   






zHamtTest.test_hamt_delete_3c                 C   s  t dd}t dd}t dd}t dd}t dd}t }||d	}||d
}||d}||d}||d}t|}||}| t||d  ||}| t||d  ||}| t||d  ||}| t||d  ||}| t|d d S )Nr   r   r   r   r   r   r   r   r$   r   r"   r   r   r   rI   r      r   )r   r   r+   rU   r   r   r   r   r   r   test_hamt_delete_4  s,   









zHamtTest.test_hamt_delete_4c                 C   s"  t  }g }tdD ]}t|t|}|| ||d| }q	tdd}||d}| t|d ||d }| t|d ||}| t|d ||d }| t|d ||d	 }| t|d
 ||d	 }| t|d
 |D ]}||}q| t|d d S )N   zval-   Z18Z	collision   rI      r      r   )	r   r   r   r   r*   r+   r   rU   r   )r!   r   rm   r   r   Zcollision_key16r   r   r   test_hamt_delete_5C  s,   


zHamtTest.test_hamt_delete_5c           	   
   C   s   t dd}t dd}t dd}t dd}t d	d
}t dd}t }||d}||d}||d}||d}||d}||d}| }| tt||df|df|df|df|df|dfh d S )Nr   r   r   r   r   r   r   r   r   r   n   Fr$   r   r"   r   r   rL   r   r   r+   rk   r   rj   	r!   r   r   r   r   r   r   r   itr   r   r   test_hamt_items_1t  $   






&zHamtTest.test_hamt_items_1c           	   
   C   s   t dd}t dd}t dd}t dd}t dd}t d	d
}t }||d}||d}||d}||d}||d}||d}| }| tt||df|df|df|df|df|dfh d S Nr   r   r   r   r   r   r   r   r   r   r$   r   r"   r   r   rL   r   r   r   r   r   test_hamt_items_2  r   zHamtTest.test_hamt_items_2c              	   C   s   t dd}t dd}t dd}t dd}t dd}t d	d
}t }||d}||d}||d}||d}||d}||d}| tt| ||||||h | tt|||||||h d S r   )r   r   r+   r   rj   rm   )r!   r   r   r   r   r   r   r   r   r   r   test_hamt_keys_1  s   





$$zHamtTest.test_hamt_keys_1c                 C   s2   t  }| t| d | t| g  d S )Nr   )r   r   rU   rk   rj   r   r   r   r   test_hamt_items_3  s   zHamtTest.test_hamt_items_3c                 C   s  t dd}t dd}t dd}t dd}t dd	}t }||d
}||d}||d}||d}t }||d
}| ||k | ||k ||d}| ||k | ||k ||d}| ||k | ||k ||d}| ||k | ||k ||d}| ||k | ||k ||d}| ||k | ||k ||}| ||k | ||k ||d}| ||k | ||k d S )Nr   r   r   r   r   r   r   x   r   r$   r   r"   r   Zd2r   )r   r   r+   assertFalse
assertTruer   )r!   r   r   r   r   r   r   r   r   r   r   test_hamt_eq_1  sF   





zHamtTest.test_hamt_eq_1c                 C   s   t dd}t dd|d}t }||d}t }||d}| td ||k W d    n1 s3w   Y  | td ||k W d    d S 1 sNw   Y  d S )Nr   r   r   r   r$   r   )r   r   r+   r   rs   )r!   r   r   r   r   r   r   r   test_hamt_eq_2  s   


"zHamtTest.test_hamt_eq_2c                 C   s   t dd}t }|dd}t|}g }|| || g }|| || |||}~~~t  t  t  | |  d S )Nr   r   r   )	r   r   r+   weakrefrefr*   gccollectrR   )r!   r   r   r  r$   r   r   r   r   test_hamt_gc_1  s    





zHamtTest.test_hamt_gc_1c                 C   sx   t dd}t dd}t }||d}|||}t|}| }t| ~~t  t  t  | 	|  d S )Nr   r   r   r   r$   )
r   r   r+   r  r  rk   nextr  r	  rR   )r!   r   r   r   r  hir   r   r   test_hamt_gc_2  s   


zHamtTest.test_hamt_gc_2c              	   C   s  t dd}t dd}t dd}t }||d}| ||v  | ||v  | t! tdd ||v  W d    n1 s@w   Y  W d    n1 sOw   Y  | t* tdd ||v  W d    n1 snw   Y  W d    d S W d    d S 1 sw   Y  d S 	Nr   r   r   r   r   Tr   r   )	r   r   r+   r  r  r   r   r   r   r!   r   ZAAr   r   r   r   r   test_hamt_in_1   s(   




"zHamtTest.test_hamt_in_1c              	   C   sP  t dd}t dd}t dd}t }||d}| || d | || d | t ||  W d    n1 s<w   Y  | t! tdd ||  W d    n1 s[w   Y  W d    n1 sjw   Y  | t* tdd ||  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S r  )	r   r   r+   r   r   rn   r   r   r   r  r   r   r   test_hamt_getitem_14  s.   





"zHamtTest.test_hamt_getitem_1N)r2   r3   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r   r   r   r   r     s0    	%#Y-4$*1/r   __main__)concurrent.futuresr   r   r   r  r   r   Zunittestr  Z	_testcapir   ImportErrorr   ZTestCaser   r   r   r   r   	Exceptionr   r   ZskipIfr   r2   mainr   r   r   r   <module>   s@    	  V!      