B
    u9a<3  ã               @   sR   d dl Z d dlZd dlmZ G dd„ deƒZG dd„ dejƒZedkrNe ¡  dS )é    N)ÚBytecodeTestCasec               @   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%S )&ÚTestTranformsc             C   s0   dd„ }|   |d¡ |   |d¡ |  |d¡ d S )Nc             S   s   | dks
~ d S )Né   © )Úxr   r   ú$/usr/lib/python3.7/test_peepholer.pyÚunot
   s    z%TestTranforms.test_unot.<locals>.unotZ	UNARY_NOTÚPOP_JUMP_IF_FALSEZPOP_JUMP_IF_TRUE)ÚassertNotInBytecodeÚassertInBytecode)Úselfr   r   r   r   Ú	test_unot   s    zTestTranforms.test_unotc             C   s0   x*dD ]"\}}t |ddƒ}|  |d|¡ qW d S )N))z
not a is bzis not)z
not a in bznot in)znot a is not bÚis)znot a not in bÚinÚ ÚsingleZ
COMPARE_OP)Úcompiler   )r   ÚlineZcmp_opÚcoder   r   r   Útest_elim_inversion_of_is_or_in   s    z-TestTranforms.test_elim_inversion_of_is_or_inc             C   s|   dd„ }dd„ }dd„ }x<|d f|df|dffD ]"\}}|   |d	¡ |  |d
|¡ q0W dd„ }|   |d	¡ |  |d
d ¡ d S )Nc              S   s   d } d } | S )Nr   )r   r   r   r   Úf   s    z0TestTranforms.test_global_as_constant.<locals>.fc              S   s   d} | S )NTr   )r   r   r   r   Úg!   s    z0TestTranforms.test_global_as_constant.<locals>.gc              S   s   d} | S )NFr   )r   r   r   r   Úh$   s    z0TestTranforms.test_global_as_constant.<locals>.hTFZLOAD_GLOBALÚ
LOAD_CONSTc               S   s   dS )z1Adding a docstring made this test fail in Py2.5.0Nr   r   r   r   r   r   ,   s    )r
   r   )r   r   r   r   ÚfuncÚelemr   r   r   Útest_global_as_constant   s     z%TestTranforms.test_global_as_constantc             C   s@   dd„ }xdD ]}|   ||¡ qW xdD ]}|  ||¡ q(W d S )Nc               S   s
   xqW t S )N)Úlistr   r   r   r   r   5   s    z'TestTranforms.test_while_one.<locals>.f)r   r	   )ÚJUMP_ABSOLUTE)r
   r   )r   r   r   r   r   r   Útest_while_one3   s
    

zTestTranforms.test_while_onec             C   sF   x@dD ]8\}}t |ddƒ}|  ||¡ |  |d¡ |  |d¡ qW d S )N))za, = a,r   )za, b = a, bZROT_TWO)za, b, c = a, b, cZ	ROT_THREEr   r   ÚBUILD_TUPLEZUNPACK_TUPLE)r   r   r
   )r   r   r   r   r   r   r   Útest_pack_unpack>   s    zTestTranforms.test_pack_unpackc             C   sŒ   x6dD ].\}}t |ddƒ}|  |d|¡ |  |d¡ qW t tttdƒƒƒddƒ}|  |d¡ dd„ t |¡D ƒ}|  t	|ƒd	¡ d
d„ }d S )N))z	a = 1,2,3)é   r   é   )z("a","b","c"))ÚaÚbÚc)za,b,c = 1,2,3)r"   r   r#   )z(None, 1, None))Nr"   N)z((1, 2), 3, 4)))r"   r   r#   é   r   r   r   r    i'  c             S   s   g | ]}|j d kr|‘qS )r   )Úopname)Ú.0Úinstrr   r   r   ú
<listcomp>Y   s    zETestTranforms.test_folding_of_tuples_of_constants.<locals>.<listcomp>r   c            d   S   sÔ   ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
gd f d S )Nr   r"   r   r#   r'   é   é   é   é   é	   r   r   r   r   r   Úcrater`   s    zATestTranforms.test_folding_of_tuples_of_constants.<locals>.crater)
r   r   r
   ÚreprÚtupleÚrangeÚdisÚget_instructionsÚassertEqualÚlen)r   r   r   r   Zload_constsr1   r   r   r   Ú#test_folding_of_tuples_of_constantsI   s    z1TestTranforms.test_folding_of_tuples_of_constantsc             C   s<   x6dD ].\}}t |ddƒ}|  |d|¡ |  |d¡ qW d S )N))za in [1,2,3])r"   r   r#   )za not in ["a","b","c"])r$   r%   r&   )za in [None, 1, None])Nr"   N)za not in [(1, 2), 3, 4]))r"   r   r#   r'   r   r   r   Z
BUILD_LIST)r   r   r
   )r   r   r   r   r   r   r   Ú"test_folding_of_lists_of_constantsn   s
    z0TestTranforms.test_folding_of_lists_of_constantsc          	   C   sÖ   x„dt dddhƒfdt dddhƒfd	t dd hƒfd
t dddhƒfdt dddhƒffD ].\}}t|ddƒ}|  |d¡ |  |d|¡ qTW dd„ }dd„ }|  |dƒ¡ |  |dƒ ¡ |  |dƒ ¡ |  |dƒ¡ d S )Nza in {1,2,3}r"   r   r#   za not in {"a","b","c"}r$   r&   r%   za in {None, 1, None}za not in {(1, 2), 3, 4})r"   r   r'   za in {1, 2, 3, 3, 2, 1}r   r   Z	BUILD_SETr   c             S   s   | dkS )N>   r"   r   r#   r   )r$   r   r   r   r   ˆ   s    z:TestTranforms.test_folding_of_sets_of_constants.<locals>.fc             S   s   | dkS )N>   r"   r   r#   r   )r$   r   r   r   r   ‹   s    z:TestTranforms.test_folding_of_sets_of_constants.<locals>.g)Ú	frozensetr   r
   r   Z
assertTrue)r   r   r   r   r   r   r   r   r   Ú!test_folding_of_sets_of_constantsz   s    z/TestTranforms.test_folding_of_sets_of_constantsc             C   s  xPdD ]H\}}t |ddƒ}|  |d|¡ x$t |¡D ]}|  |j d¡¡ q4W qW t dddƒ}|  |dd¡ |  |dd¡ t d	ddƒ}|  |dd
¡ |  dd
 |j¡ t dddƒ}|  |dd¡ |  dd> |j¡ t dddƒ}|  |dd¡ |  dd |j¡ d S )N))z	a = 2+3+4r0   )z"@"*4z@@@@)za="abc" + "def"Zabcdef)za = 3**4éQ   )za = 3*4é   )z	a = 13//4r#   )za = 14%4r   )za = 2+3r,   )za = 13-4r0   )za = (12,13)[1]é   )za = 13 << 2é4   )za = 13 >> 2r#   )z
a = 13 & 7r,   )z
a = 13 ^ 7é
   )z
a = 13 | 7é   r   r   r   ÚBINARY_za=2+"b"r   r%   za="x"*10000i'  r   z	a=1<<1000iè  r"   z	a=2**1000)	r   r   r5   r6   ÚassertFalser(   Ú
startswithZassertNotInÚ	co_consts)r   r   r   r   r*   r   r   r   Ú#test_folding_of_binops_on_constants•   s$    z1TestTranforms.test_folding_of_binops_on_constantsc             C   sŽ   t dddƒ}|  |dd¡ |  |d¡ t dddƒ}|  |dd¡ |  |d¡ t d	ddƒ}|  |dd
¡ |  |d¡ t dddƒ}|  |d¡ d S )Nz"foo"[0]r   r   r   r   ZBINARY_SUBSCRu	   "aï¿¿"[1]u   ï¿¿u	   "ð’…"[0]u   ð’…z	"fuu"[10])r   r   r
   )r   r   r   r   r   Útest_binary_subscr_on_unicode¼   s    z+TestTranforms.test_binary_subscr_on_unicodec             C   s¾   xPdD ]H\}}t |ddƒ}|  |d|¡ x$t |¡D ]}|  |j d¡¡ q4W qW dd„ }x$t |¡D ]}|  |j d¡¡ qfW x8dD ]0\}}}t |ddƒ}|  |d|¡ |  ||¡ q†W d S )	N))z-0.5g      à¿)z-0.0g       €)z
-(1.0-1.0)g       €)z-0r   )z~-2r"   )z+1r"   r   r   r   ÚUNARY_c               S   s   dS )Ng       €r   r   r   r   r   ÚnegzeroÞ   s    zDTestTranforms.test_folding_of_unaryops_on_constants.<locals>.negzero))z-"abc"ÚabcZUNARY_NEGATIVE)z~"abc"rK   ZUNARY_INVERT)r   r   r5   r6   rD   r(   rE   )r   r   r   r   r*   rJ   r(   r   r   r   Ú%test_folding_of_unaryops_on_constantsÏ   s    z3TestTranforms.test_folding_of_unaryops_on_constantsc             C   s>   dd„ }|   |dd ¡ dd„ t |¡D ƒ}|  t|ƒd¡ d S )Nc             S   s   | S )Nr   )r   r   r   r   r   ï   s    z/TestTranforms.test_elim_extra_return.<locals>.fr   c             S   s   g | ]}|j d kr|‘qS )ÚRETURN_VALUE)r(   )r)   r*   r   r   r   r+   ò   s    z8TestTranforms.test_elim_extra_return.<locals>.<listcomp>r"   )r
   r5   r6   r7   r8   )r   r   Úreturnsr   r   r   Útest_elim_extra_returní   s    z$TestTranforms.test_elim_extra_returnc             C   sH   dd„ }|   |d¡ |   |d¡ dd„ t |¡D ƒ}|  t|ƒd¡ d S )Nc             S   s   | r|S |S )Nr   )ZcondZ
true_valueZfalse_valuer   r   r   r   ø   s    z1TestTranforms.test_elim_jump_to_return.<locals>.fÚJUMP_FORWARDr   c             S   s   g | ]}|j d kr|‘qS )rM   )r(   )r)   r*   r   r   r   r+   ü   s    z:TestTranforms.test_elim_jump_to_return.<locals>.<listcomp>r   )r
   r5   r6   r7   r8   )r   r   rN   r   r   r   Útest_elim_jump_to_returnö   s
    z&TestTranforms.test_elim_jump_to_returnc             C   sH   dd„ }|   |d¡ |   |d¡ dd„ t |¡D ƒ}|  t|ƒd¡ d S )Nc             S   s(   | rdS |rdS xdS x| r dS dS dS )Nr"   r   r#   r'   r,   r-   r   )Úcond1Úcond2r   r   r   r     s       z5TestTranforms.test_elim_jump_after_return1.<locals>.frP   r   c             S   s   g | ]}|j d kr|‘qS )rM   )r(   )r)   r*   r   r   r   r+     s    z>TestTranforms.test_elim_jump_after_return1.<locals>.<listcomp>r-   )r
   r5   r6   r7   r8   )r   r   rN   r   r   r   Útest_elim_jump_after_return1   s
    	z*TestTranforms.test_elim_jump_after_return1c             C   s`   dd„ }|   |d¡ dd„ t |¡D ƒ}|  t|ƒd¡ dd„ t |¡D ƒ}|  t|ƒd¡ d S )	Nc             S   s   x| rdS qW d S )Nr'   r   )rR   rS   r   r   r   r     s     z5TestTranforms.test_elim_jump_after_return2.<locals>.frP   c             S   s   g | ]}|j d kr|‘qS )r   )r(   )r)   r*   r   r   r   r+     s    z>TestTranforms.test_elim_jump_after_return2.<locals>.<listcomp>r"   c             S   s   g | ]}|j d kr|‘qS )rM   )r(   )r)   r*   r   r   r   r+     s    r   )r
   r5   r6   r7   r8   )r   r   rN   r   r   r   Útest_elim_jump_after_return2  s    z*TestTranforms.test_elim_jump_after_return2c             C   s   dd„ }|   |d¡ d S )Nc              S   s   ddœdd„} | S )Nr   )Úreturnc               S   s   d S )Nr   r   r   r   r   r   !  s    zBTestTranforms.test_make_function_doesnt_bail.<locals>.f.<locals>.gr   )r   r   r   r   r      s    z7TestTranforms.test_make_function_doesnt_bail.<locals>.fZ
BINARY_ADD)r
   )r   r   r   r   r   Útest_make_function_doesnt_bail  s    z,TestTranforms.test_make_function_doesnt_bailc          	   C   s~   ddddddddd	g	}xb|D ]Z}t |d
dƒ}xHt |¡D ]:}|  |j d¡¡ |  |j d¡¡ |  |j d¡¡ q8W qW d S )Nz3 * -5z-3 * 5z2 * (3 * 4)z(2 * 3) * 4z
(-1, 2, 3)z
(1, -2, 3)z
(1, 2, -3)z(1, 2, -3) * 6z:lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}r   r   rI   rC   ZBUILD_)r   r5   r6   rD   r(   rE   )r   ZexprsÚer   r*   r   r   r   Útest_constant_folding&  s    
z#TestTranforms.test_constant_foldingc             C   s   dd„ }|   |ƒ d¡ d S )Nc               S   s   dsdrdS dS )NTFr"   r   r   r   r   r   r   r   ;  s    z=TestTranforms.test_condition_with_binop_with_bools.<locals>.fr"   )r7   )r   r   r   r   r   Ú$test_condition_with_binop_with_bools:  s    z2TestTranforms.test_condition_with_binop_with_boolsN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r!   r9   r:   r<   rG   rH   rL   rO   rQ   rT   rU   rW   rY   rZ   r   r   r   r   r      s$   	
%'	
r   c               @   s   e Zd Zdd„ ZdS )ÚTestBugletsc          	   C   s(   dd„ }|   t¡ |ƒ  W d Q R X d S )Nc              S   s   ddh\} }| |fS )Nr"   r   )r   Úyr   r   r   r   H  s    z%TestBuglets.test_bug_11510.<locals>.f)ZassertRaisesÚ
ValueError)r   r   r   r   r   Útest_bug_11510D  s    zTestBuglets.test_bug_11510N)r[   r\   r]   ra   r   r   r   r   r^   B  s   r^   Ú__main__)	r5   ZunittestZtest.bytecode_helperr   r   ZTestCaser^   r[   Úmainr   r   r   r   Ú<module>   s     >