a
    zez                     @   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yf   dZ
Y n0 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re  dS )    Nhamtc                    s   t   fdd}|S )Nc                     s    t  }|j g| R i |S NcontextvarsContextrun)argskwargsctxfunc '/usr/lib/python3.9/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*0    Y  |  td td W d    n1 s`0    Y  td}| |jd | t d|_W d    n1 s0    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 s40    Y  |  td" G dd dtj}W d    n1 sr0    Y  |  td" G dd dtj}W d    n1 s0    Y  d S )Nznot an acceptable base typec                   @   s   e Zd ZdS )z<ContextTest.test_context_subclassing_1.<locals>.MyContextVarN__name__
__module____qualname__r   r   r   r   MyContextVar?   s   r4   c                   @   s   e Zd ZdS )z9ContextTest.test_context_subclassing_1.<locals>.MyContextNr0   r   r   r   r   	MyContextD   s   r5   c                   @   s   e Zd ZdS )z7ContextTest.test_context_subclassing_1.<locals>.MyTokenNr0   r   r   r   r   MyTokenH   s   r6   )r   r   r   r   r   Token)r!   r4   r5   r6   r   r   r   test_context_subclassing_1=   s    00z&ContextTest.test_context_subclassing_1c                 C   s   |  td td W d    n1 s,0    Y  |  td tjddd W d    n1 sf0    Y  |  td tjdd W d    n1 s0    Y  tjf i i  d S )Nzany argumentsr   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 s20    Y  | td d|v  W d    n1 sf0    Y  | td |d W d    n1 s0    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   s@   t  }| td |  W d    n1 s20    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 ]}| ||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>.funcrD   rC   rB   r   )r   rE      r9   )r$   rC      )rH   rE   )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:0    Y  | t ||dd W d    n1 sr0    Y  | t" |j|dddd W d    n1 s0    Y  d S )Nc                  _   s   dd  d S )Nr   r   r   rF   r   r   r   r      s    z,ContextTest.test_context_run_3.<locals>.funcr   rG   r%   r9   )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!   rO   r   r   func2   s    z-ContextTest.test_context_run_4.<locals>.func2c                     sb    d  d   d d t } t| d |  d | S )NrC   r   )rP   r<   r+   r   r   r   r?   len)Zcurctx2rR   r!   rO   r   r   func1   s    

z-ContextTest.test_context_run_4.<locals>.func1rC   )r   r   r   r   r   r(   )r!   ctx1rV   Zreturned_ctxr   rT   r   test_context_run_4   s    

zContextTest.test_context_run_4c                    sh   t  }t d fdd} t || W d    n1 sJ0    Y   d  d S )NrO   c                      s&     d  d dd  d S )NrC   r   r   )rP   r<   r+   r   rQ   r   r   r      s    
z,ContextTest.test_context_run_5.<locals>.func)r   r   r   r   rL   r   rP   r<   rM   r   rQ   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<   rP   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    n1 s,0    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   ra   r   test_context_run_7   s    zContextTest.test_context_run_7c                 C   s  t d}| t |  W d    n1 s20    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 sF0    Y  | | d t  }| || || | td || W d    n1 s0    Y  | |d  | || | || d | ||dd | t|d | t| |dfg | t| dg | t| |g | t||g t  }| || | t ||  W d    n1 s0    Y  | ||dd | t|d | t|g  d S )	Nr"   rZ   rC   Zspam2zhas already been usedaar   r   )r   r   r   LookupErrorr<   rP   r+   r   assertIs	old_valueMISSINGr7   rO   r-   r   r`   r?   r(   rS   listitemsvalueskeysr,   KeyError)r!   r"   t0r.   rW   rU   r   r   r   test_context_getset_1   sT    
&



*
*(z!ContextTest.test_context_getset_1c                 C   sX   t d}t d}|d}| td || W d    n1 sJ0    Y  d S )Nv1v2rZ   zby a different)r   r   r+   r   
ValueErrorr-   )r!   ro   rp   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"   rZ   r&   c                     s
     d t    W d    n1 s80    Y      dd    t g   d}  t  g    d  	|   t g  t    W d    n1 s0    Y  d S )NrZ   rC   r   )
r   r<   r   rl   rP   r,   rh   rk   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    n1 sP0    Y  d S )Nr"   rZ   r&   r   zdifferent Context)r   r   r   r   r+   r   rq   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"   rZ   r&   c                      s.     g    d   dg d S )NrZ   )r+   r<   r*   r   r   r"   r!   r   r   r\   6  s    
z.ContextTest.test_context_getset_5.<locals>.fun)r   r   r+   r?   r   r   r<   r^   r   rw   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"   rZ   r&   c                     sz     d  } |   d   d   d |   d |    d |   d d S )N
         )r+   copyr   r   )rU   r"   rW   ctx2_funr!   r   r   ctx1_funB  s    


z1ContextTest.test_context_copy_1.<locals>.ctx1_func                      s.      d  d    d d S )Nry   r{   )r   r<   r+   r   rw   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
|  0 	|ttd d S )Ncvarc                    sF   t dD ]8} | |  ttdd   | |  q| S )Nry   gMbP?g?)ranger+   timesleeprandomuniformr   r<   )numir   r!   r   r   sub[  s
    z/ContextTest.test_context_threads_1.<locals>.subry   )max_workers)
r   r   
concurrentfuturesThreadPoolExecutorrh   mapr   shutdownr   )r!   r   tpresultsr   r   r   test_context_threads_1W  s    
z"ContextTest.test_context_threads_1N)r1   r2   r3   r#   r   r/   r8   r;   r>   r@   rA   rK   rN   rX   rY   r_   rb   rn   rs   rt   rv   rx   r   r   r   r   r   r   r      s8   
	

	
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rL| j|u rLtd| d||jd urt|j| u rttd|d| | j| j	f|j|j	fkS )Nzcannot compare z to )

isinstancer   NotImplementedr   error_on_eqEqErrorr   rq   r   r    r!   otherr   r   r   __eq__  s    
zHashKey.__eq__)r1   r2   r3   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__)r1   r2   r3   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   r`   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)r1   r2   r3   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdS )r   Nr0   r   r   r   r   r     s   r   c                   @   s   e Zd ZdS )r   Nr0   r   r   r   r   r     s   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 )Nry   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   rZ   ry   rG   )r   r   rS   r+   assertIsNotrP   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+   re   )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   rS   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 )Nry   r   r   cccr$   r   Zccrc   r   r   rG      )r   r   r+   r   r<   rS   )	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      z1HamtTest.test_hamt_collision_3.<locals>.<setcomp>>   r   r   r   )r   r   r+   r   rk   )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 ]p}t }t }t |D ]J}	t|	}
|	| stddF | t ||
|	 W d    n1 s0    Y  W d    n1 s0    Y  ||
|	}|	| s&tddH | t |	t|	 W d    n1 s0    Y  W d    n1 s0    Y  |	||
< | 
t|t| |	| s6| 
t| t|  | 
t| t|  q6| 
t|| t |D ]}
| 
|	t|
d	|
 qtt |}t| t|D ]\}}	t|	}
|| sXtddF | t ||
 W d    n1 s.0    Y  W d    n1 sN0    Y  || stddJ | t |t|	 W d    n1 s0    Y  W d    n1 s0    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 ]\}	}
|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   rH   r   Tr   r   z	not foundrG   r   )r   r   r   r   r   r   r   r+   r   r<   r   rS   ri   rh   r   shuffle	enumeratedeleter|   rk   strpoprj   )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    H
L

J
N

 
 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 s0    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 comparerG   r   rZ   )
r   r   r+   rS   r   r   r   rq   re   r<   )r!   r   r   r   r   r   r   r   r   orig_lenr   r   r   r   test_hamt_delete_1  s6    





	
(


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 s0    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   rG   r   r   )	r   r   r+   rS   r   rq   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   rG   )r   r   r+   rS   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   rG   r      r   )r   r   r+   rS   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   rG      r      r   )	r   r   r   r   r*   r+   r   rS   r   )r!   r   rk   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   rJ   r   r   r+   ri   r   rh   	r!   r   r   r   r   r   r   r   itr   r   r   test_hamt_items_1t  s$    






&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   rJ   r   r   r   r   r   test_hamt_items_2  s$    






&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   rh   rk   )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   rS   ri   rh   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 sf0    Y  | td ||k W d    n1 s0    Y  d S )Nr   r   r   r   r$   r   )r   r   r+   r   rq   )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collectrP   )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  ri   nextr  r  rP   )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B tdd ||v  W d    n1 s0    Y  W d    n1 s0    Y  | tB tdd ||v  W d    n1 s0    Y  W d    n1 s0    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    


DzHamtTest.test_hamt_in_1c              	   C   sB  t dd}t dd}t dd}t }||d}| || d | || d | t ||  W d    n1 sx0    Y  | tB tdd ||  W d    n1 s0    Y  W d    n1 s0    Y  | tD tdd ||  W d    n1 s0    Y  W d    n1 s40    Y  d S r  )	r   r   r+   r   r   rl   r   r   r   r  r   r   r   test_hamt_getitem_14  s    


&DzHamtTest.test_hamt_getitem_1N)r1   r2   r3   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     s.   	%#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   r1   mainr   r   r   r   <module>   s:   
	  V!      
