B
    u9a;                 @   sz   d dl Z d dlmZ d dlZd dlmZ d dlZd dlZG dd dZ	G dd dZ
G dd	 d	e jZed
krve   dS )    N)WeakSet)
UserStringc               @   s   e Zd ZdS )FooN)__name__
__module____qualname__ r   r   "/usr/lib/python3.7/test_weakset.pyr   	   s   r   c               @   s   e Zd Zdd ZdS )RefCyclec             C   s
   | | _ d S )N)cycle)selfr   r   r	   __init__   s    zRefCycle.__init__N)r   r   r   r   r   r   r   r	   r
      s   r
   c               @   sl  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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYS )ZTestWeakSetc             C   s   dd dD | _ dd dD | _dd dD | _dd d	D | _d
d dD | _t| j| _t| j| _t| j| _dd t	j
D | _t| j | _t| j | _td| _t| jg| _d S )Nc             S   s   g | ]}t |qS r   )ustr).0cr   r   r	   
<listcomp>   s    z%TestWeakSet.setUp.<locals>.<listcomp>)abr   c             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r      s    )xyzc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r      s    Zabc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r      s    Zabcdec             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r      s    defc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r      s    F)itemsitems2Zab_itemsZabcde_itemsZ	def_itemsr   
ab_weaksetabcde_weaksetdef_weaksetstringascii_lettersletterssdictfromkeysdr   objfs)r   r   r   r	   setUp   s    
zTestWeakSet.setUpc             C   sD   t t}x6t tD ]*}|dks|dr*q| ||d|  qW d S )NZ
test_c_api_zWeakSet missing method )dirr   set
startswithassertIn)r   Zweaksetmethodsmethodr   r   r	   test_methods#   s    zTestWeakSet.test_methodsc             C   s   |  ttg d d S )N   )assertRaises	TypeErrorr   )r   r   r   r	   test_new_or_init+   s    zTestWeakSet.test_new_or_initc             C   sD   |  t| jt| j |  t| jd | `|  t| jd d S )N   r   )assertEquallenr"   r%   r'   r&   )r   r   r   r	   test_len.   s    zTestWeakSet.test_lenc             C   s`   x&| j D ]}| || jk|| jk qW | d| j | | j| j | `| td| j d S )Nr4   r   )	r!   r5   r"   r%   assertNotInr-   r&   r'   r   )r   r   r   r   r	   test_contains4   s    zTestWeakSet.test_containsc             C   s  | j | j}x.| jD ]$}| ||k|| jkp6|| jk qW | | j t| j | t|t | 	t
| j jg g xHtttjttfD ]4}t| j| j }|| j}| | j || ~qW | t|t| jt| j  | j  t  | t|t| jt| j  d S )N)r"   unionr   r!   r5   r%   r   r   typer1   r2   r+   	frozensetr#   r$   listtupler6   popgccollect)r   ur   Cr   r   r   r	   
test_union=   s    $
 
zTestWeakSet.test_unionc             C   sB   | j | j}| | j t| jB | | | j t| jB | d S )N)r"   r:   r   r5   r+   r<   )r   ir   r   r	   test_orN   s    zTestWeakSet.test_orc             C   s   t | j}|| j}x.| jD ]$}| ||k|| jko>|| jk qW | |t | j | t|t  x8tttj	t
tfD ]$}t g }| ||| j| qzW | t|t| j | j  t  | t|t| j d S )N)r   r!   intersectionr   r5   r;   r+   r<   r#   r$   r=   r>   r   r6   r?   r@   rA   )r   r"   rE   r   rC   r   r   r   r	   test_intersectionS   s    
$
zTestWeakSet.test_intersectionc             C   s6   |  | jt| j |  | jt| j  d S )N)
assertTruer"   
isdisjointr   r   r!   )r   r   r   r	   test_isdisjointb   s    zTestWeakSet.test_isdisjointc             C   sB   | j | j}| | j t| j@ | | | j t| j@ | d S )N)r"   rG   r   r5   r+   r<   )r   rE   r   r   r	   test_andf   s    zTestWeakSet.test_andc             C   sz   | j | j}x.| jD ]$}| ||k|| jko6|| jk qW | | j t| j | t|t | 	t
| j jg g d S )N)r"   
differencer   r!   r5   r%   r   r   r;   r1   r2   )r   rE   r   r   r   r	   test_differencek   s    $zTestWeakSet.test_differencec             C   sB   | j | j}| | j t| j | | | j t| j | d S )N)r"   rM   r   r5   r+   r<   )r   rE   r   r   r	   test_subs   s    zTestWeakSet.test_subc             C   s   | j | j}x.| jD ]$}| ||k|| jk|| jkA  qW | | j t| j | t|t | 	t
| j jg g | t|t| jt| j  | j  t  | t|t| jt| j  d S )N)r"   symmetric_differencer   r!   r5   r%   r   r   r;   r1   r2   r6   r?   r@   rA   )r   rE   r   r   r   r	   test_symmetric_differencex   s    $ 
z%TestWeakSet.test_symmetric_differencec             C   sB   | j | j}| | j t| jA | | | j t| jA | d S )N)r"   rP   r   r5   r+   r<   )r   rE   r   r   r	   test_xor   s    zTestWeakSet.test_xorc             C   s   |  | j| jk |  | j| jk |  | j| jk | | j| jk | | j| jk |  tdd |  tdd | tdd | tdd d S )Nr   abcZcbs)rI   r   r   assertFalser   r+   issubset
issuperset)r   r   r   r	   test_sub_and_super   s    zTestWeakSet.test_sub_and_superc             C   sL   |  | j| jk  | | j| jk  | | j| jk  | t t k  d S )N)rI   r   r   rT   r   r   )r   r   r   r	   test_lt   s    zTestWeakSet.test_ltc             C   sL   |  | j| jk | | j| jk | | j| jk | t t k d S )N)rI   r   r   rT   r   r   )r   r   r   r	   test_gt   s    zTestWeakSet.test_gtc             C   s@   t dd tdD }x$|D ]}||_||_t |g|_qW d S )Nc             s   s   | ]}t  V  qd S )N)r   )r   rE   r   r   r	   	<genexpr>   s    z&TestWeakSet.test_gc.<locals>.<genexpr>i  )r   ranger   subr+   )r   r"   elemr   r   r	   test_gc   s
    
zTestWeakSet.test_gcc             C   sT   G dd dt }| }t }|| | || || || || d S )Nc               @   s   e Zd Zdd ZdS )z5TestWeakSet.test_subclass_with_custom_hash.<locals>.Hc             S   s   t t| d@ S )Ni)intid)r   r   r   r	   __hash__   s    z>TestWeakSet.test_subclass_with_custom_hash.<locals>.H.__hash__N)r   r   r   ra   r   r   r   r	   H   s   rb   )r   r+   addr-   removediscard)r   rb   r"   fr   r   r	   test_subclass_with_custom_hash   s    


z*TestWeakSet.test_subclass_with_custom_hashc             C   sd   t  }|| j | || j || j | |t | j | t|j|d | t|jd d S )Nr0   r4   )r   r   r   r5   r"   r   r1   r2   )r   r"   r   r   r	   	test_init   s    zTestWeakSet.test_initc             C   s*   t | j}t |}| t|t| d S )N)r   r   assertNotEqualr`   )r   r"   tr   r   r	   test_constructor_identity   s    
z%TestWeakSet.test_constructor_identityc             C   s   |  tt| j d S )N)r1   r2   hashr"   )r   r   r   r	   	test_hash   s    zTestWeakSet.test_hashc             C   s2   | j   | | j tg  | t| j d d S )Nr   )r"   clearr5   r   r6   )r   r   r   r	   
test_clear   s    
zTestWeakSet.test_clearc             C   s2   | j  }| | j | | t| j t| d S )N)r"   copyr5   ri   r`   )r   dupr   r   r	   	test_copy   s    
zTestWeakSet.test_copyc             C   s   t d}| j| | || j | j }| j| | | j| | t| jjg  | jt	  | 
t| jdk | j| j | 
t| jdk d S )NQr4   )r   r"   rc   r-   rp   r5   r1   r2   r'   r   rI   r6   r&   )r   r   rq   r   r   r	   test_add   s    
zTestWeakSet.test_addc             C   sJ   t d}| j| | || j | t| jj| | t| jjg  d S )Nr   )r   r"   rd   r8   r1   KeyErrorr2   )r   r   r   r   r	   test_remove   s
    zTestWeakSet.test_removec             C   sN   t dt d }}| j| | || j | j| | t| jjg  d S )Nr   rs   )r   r"   re   r8   r1   r2   )r   r   qr   r   r	   test_discard   s
    zTestWeakSet.test_discardc             C   sD   x.t t| jD ]}| j }| || j qW | t| jj d S )N)r[   r6   r"   r?   r8   r1   ru   )r   rE   r]   r   r   r	   test_pop   s    
zTestWeakSet.test_popc             C   sV   | j | j}| |d  x"| j| j D ]}| || j  q(W | t| j jg g d S )N)r"   updater   r5   r   r-   r1   r2   )r   retvalr   r   r   r	   test_update   s
    zTestWeakSet.test_updatec             C   s:   | j t| j x"| j| j D ]}| || j  q W d S )N)r"   rz   r+   r   r   r-   )r   r   r   r   r	   test_update_set   s    zTestWeakSet.test_update_setc             C   s<   |  j t| jO  _ x"| j| j D ]}| || j  q"W d S )N)r"   r+   r   r   r-   )r   r   r   r   r	   test_ior   s    zTestWeakSet.test_iorc             C   sz   | j | j}| |d  xF| j| j D ]6}|| jkrP|| jkrP| || j  q(| || j  q(W | t| j jg g d S )N)	r"   intersection_updater   r5   r   r-   r8   r1   r2   )r   r{   r   r   r   r	   test_intersection_update  s    z$TestWeakSet.test_intersection_updatec             C   s`   |  j t| jM  _ xF| j| j D ]6}|| jkrJ|| jkrJ| || j  q"| || j  q"W d S )N)r"   r+   r   r   r-   r8   )r   r   r   r   r	   	test_iand  s
    zTestWeakSet.test_iandc             C   s   | j | j}| |d  xF| j| j D ]6}|| jkrP|| jkrP| || j  q(| || j  q(W | t| j jg g | t| j j	g g d S )N)
r"   difference_updater   r5   r   r-   r8   r1   r2   symmetric_difference_update)r   r{   r   r   r   r	   test_difference_update  s    z"TestWeakSet.test_difference_updatec             C   s`   |  j t| j8  _ xF| j| j D ]6}|| jkrJ|| jkrJ| || j  q"| || j  q"W d S )N)r"   r+   r   r   r-   r8   )r   r   r   r   r	   	test_isub  s
    zTestWeakSet.test_isubc             C   sz   | j | j}| |d  xF| j| j D ]6}|| jk|| jkA rP| || j  q(| || j  q(W | t| j jg g d S )N)	r"   r   r   r5   r   r-   r8   r1   r2   )r   r{   r   r   r   r	    test_symmetric_difference_update'  s    z,TestWeakSet.test_symmetric_difference_updatec             C   s`   |  j t| jN  _ xF| j| j D ]6}|| jk|| jkA rJ| || j  q"| || j  q"W d S )N)r"   r+   r   r   r-   r8   )r   r   r   r   r	   	test_ixor1  s
    zTestWeakSet.test_ixorc             C   sp   | j  }||O }| || j  ||M }| || j  ||8 }| |t  | j  }||N }| |t  d S )N)r"   rp   r5   r   )r   rj   r   r   r	   test_inplace_on_self9  s    

z TestWeakSet.test_inplace_on_selfc             C   s   |  | j| jk |  | jt| jk | | jt| jk | | jt| jk | | jt| jk | | jttgk | | jdk d S )Nr4   )	rI   r"   r   r   rT   r+   r=   r>   r   )r   r   r   r	   test_eqE  s    zTestWeakSet.test_eqc             C   s4   |  | jt| jk t }t }| ||k d S )N)rI   r"   r+   r   r   rT   )r   s1s2r   r   r	   test_neO  s    zTestWeakSet.test_nec             C   sr   dd dD }t |}t|}t| |d= t  | tt|t|t|d g ~| t|t| d S )Nc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r   X  s    zATestWeakSet.test_weak_destroy_while_iterating.<locals>.<listcomp>)r   r   r   r4   )	r   iternextr@   rA   r-   r6   r=   r5   )r   r   r"   itr   r   r	   !test_weak_destroy_while_iteratingU  s    $z-TestWeakSet.test_weak_destroy_while_iteratingc          	      s  dd t jD  t tj fdd}| }| | W d Q R X | }| tj| W d Q R X | | | }	| W d Q R X | 
|  }| }| W d Q R X | tt| | }  W d Q R X | td d S )Nc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r	   r   g  s    zLTestWeakSet.test_weak_destroy_and_mutate_while_iterating.<locals>.<listcomp>c              3   sV   zJt } ttt| }tt  }||kr:t|  t  |V  W d d } X d S )N)r   r   strr   r?   r@   rA   )r   ZyieldedrB   )r   r"   r   r	   testcontexti  s    
zMTestWeakSet.test_weak_destroy_and_mutate_while_iterating.<locals>.testcontextr   )r   r    r   
contextlibcontextmanagerr8   r1   ru   rd   rc   r-   rp   rz   r5   r6   rn   )r   r   rB   rj   r   )r   r"   r	   ,test_weak_destroy_and_mutate_while_iteratinge  s$    z8TestWeakSet.test_weak_destroy_and_mutate_while_iteratingc             C   s   d}dd t |D }t|}~t|}yt| W n tk
rH   Y nX t  t|}~t  t|}| |d | 	|d d S )N   c             S   s   g | ]
}t  qS r   )r
   )r   rE   r   r   r	   r     s    z/TestWeakSet.test_len_cycles.<locals>.<listcomp>)r   r4   r   )
r[   r   r   r   StopIterationr@   rA   r6   r-   r5   )r   Nr   r"   r   n1n2r   r   r	   test_len_cycles  s     zTestWeakSet.test_len_cyclesc          	   C   s   | j tjft   xtddD ]}d}td t||| dd t|D }t|}~t|}yt| W n t	k
r   Y nX t
|}~t
|}| |d | || | |d | || q"W d S )Nr4   d   r   r   c             S   s   g | ]
}t  qS r   )r
   )r   rE   r   r   r	   r     s    z-TestWeakSet.test_len_race.<locals>.<listcomp>)Z
addCleanupr@   Zset_thresholdZget_thresholdr[   rA   r   r   r   r   r6   ZassertGreaterEqualZassertLessEqual)r   thr   r   r"   r   r   r   r   r   r	   test_len_race  s(    
zTestWeakSet.test_len_raceN)/r   r   r   r(   r/   r3   r7   r9   rD   rF   rH   rK   rL   rN   rO   rQ   rR   rW   rX   rY   r^   rg   rh   rk   rm   ro   rr   rt   rv   rx   ry   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r      sX   		


%r   __main__)Zunittestweakrefr   r   collectionsr   r   r@   r   r   r
   ZTestCaser   r   mainr   r   r   r	   <module>   s      (