B
     \=                 @   s*  d dl mZmZmZ d dlZd dlmZmZ d dlm	Z
 d dlmZmZ ddlmZmZmZ e Zed ed	 e Zed
 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 G d!d" d"eeZ!G d#d$ d$eZ"G d%d& d&eeZ#e$d'kr&e
%  dS )(    )print_functiondivisionabsolute_importN)typesutils)unittest_support)compile_isolatedFlags   )TestCasetagMemoryLeakMixinZenable_pyobjectZenable_loopliftZenable_pyobject_loopliftc             C   s*   t d}xt|jD ]}| ||< qW |S )N   )npemptyrangesize)xai r   ;lib/python3.7/site-packages/numba/tests/test_looplifting.pylift1   s    
r   c             C   sJ   t d}x:t|jd D ](}x"t|jd D ]}| |||f< q.W qW |S )N)r      r   r
   )r   r   r   shape)r   r   r   jr   r   r   lift2   s
    
r   c             C   s@   t jdt jd}d}x&t|jd D ]}||| |  7 }q$W |S )N   )dtyper   )r   arangeint64r   r   )r   r   cr   r   r   r   lift3&   s
    r"   c             C   sP   t jdt jd}d}d}x.t|jd D ]}||| |  7 }||7 }q(W || S )Nr   )r   r   )r   r   r    r   r   )r   r   r!   dr   r   r   r   lift4.   s    r$   c             C   s0   t d}x t|jd D ]}|dkrP qW |S )Nr   r      )r   r   r   r   )r   r   r   r   r   r   lift58   s
    
r&   c             c   s<   t d}dV  xt|jD ]}| ||< qW t |V  d S )Nr   r   )r   r   r   r   sum)r   r   r   r   r   r   	lift_gen1@   s
    
r(   c              C   s4   t d x$tdD ]} xtdD ]}dS W qW dS )Nr
   
   r%   )r   r   r   )r   r   r   r   r   lift_issue2561I   s
    

r*   c             C   s(   t d}xt|jd D ]}|S W |S )Nr   r   )r   r   r   r   )r   r   r   r   r   r   reject1P   s    
r+   c             c   s0   t d}x t|jd D ]}|| V  qW d S )Nr   r   )r   r   r   r   )r   r   r   r   r   r   reject_gen1X   s    
r,   c             c   sN   t d}x>t|jD ]0}|| |  }xt|D ]}|d }q0W |V  qW d S )Nr   r%   )r   r   r   r   )r   r   r   resr   r   r   r   reject_gen2^   s    
r.   c             C   s>   t jdt jd}x(t|jD ]}t |d | ||< qW |S )Nr   )r   r
   )r   r   int32r   r   r   )r   r   r   r   r   r   reject_npm1j   s    r0   c               @   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eddd Zdd Zdd Zeddd Zdd Zd d! Zd"d# Zd$d% Zd&S )'TestLoopLiftingc             C   s$   t ||td}| t|jd |S )N)flagsr
   )r   looplift_flagsassertEquallenlifted)selfpyfuncargtypescresr   r   r   try_liftv   s    zTestLoopLifting.try_liftc             C   s(   |j d }|j \}| |jj d S )Nr   )r6   	overloadsvalues
assertTruefndescnative)r7   r:   jitlooploopcresr   r   r   assert_lifted_native}   s    
z$TestLoopLifting.assert_lifted_nativec             C   s8   |  ||}|| }|j| }| | | || dS )zH
        Check that pyfunc can loop-lift even in nopython mode.
        N)r;   entry_pointrC   assertPreciseEqual)r7   r8   r9   argsr:   expectedgotr   r   r   check_lift_ok   s
    

zTestLoopLifting.check_lift_okc             C   s@   |  ||}t|| }t|j| }| | | || dS )zg
        Check that pyfunc (a generator function) can loop-lift even in
        nopython mode.
        N)r;   listrD   rC   rE   )r7   r8   r9   rF   r:   rG   rH   r   r   r   check_lift_generator_ok   s
    
z'TestLoopLifting.check_lift_generator_okc             C   s<   t ||td}| |j || }|j| }| || dS )z4
        Check that pyfunc can't loop-lift.
        )r2   N)r   r3   assertFalser6   rD   rE   )r7   r8   r9   rF   r:   rG   rH   r   r   r   check_no_lift   s    
zTestLoopLifting.check_no_liftc             C   sD   t ||td}| |j t|| }t|j| }| || dS )zK
        Check that pyfunc (a generator function) can't loop-lift.
        )r2   N)r   r3   rL   r6   rJ   rD   rE   )r7   r8   r9   rF   r:   rG   rH   r   r   r   check_no_lift_generator   s    z'TestLoopLifting.check_no_lift_generatorc          	   C   st   t ||td}| |j |   |j|  W dQ R X t ||td}| |j || }|j| }| || dS )z
        Check that pyfunc will fail loop-lifting if pyobject mode
        is disabled inside the loop, succeed otherwise.
        )r2   N)r   r3   r>   r6   ZassertTypingErrorrD   pyobject_looplift_flagsrE   )r7   r8   r9   rF   r:   rG   rH   r   r   r   check_no_lift_nopython   s    

z&TestLoopLifting.check_no_lift_nopythonc             C   s   |  ttjfd d S )N){   )rI   r   r   intp)r7   r   r   r   
test_lift1   s    zTestLoopLifting.test_lift1c             C   s   |  ttjfd d S )N)rQ   )rI   r   r   rR   )r7   r   r   r   
test_lift2   s    zTestLoopLifting.test_lift2c             C   s   |  ttjfd d S )N)rQ   )rI   r"   r   rR   )r7   r   r   r   
test_lift3   s    zTestLoopLifting.test_lift3Z	importantc             C   s   |  ttjfd d S )N)rQ   )rI   r$   r   rR   )r7   r   r   r   
test_lift4   s    zTestLoopLifting.test_lift4c             C   s   |  ttjfd d S )N)rQ   )rI   r&   r   rR   )r7   r   r   r   
test_lift5   s    zTestLoopLifting.test_lift5c             C   s   |  tdd d S )Nr   )rM   r*   )r7   r   r   r   test_lift_issue2561   s    z#TestLoopLifting.test_lift_issue2561c             C   s   |  ttjfd d S )N)rQ   )rK   r(   r   rR   )r7   r   r   r   test_lift_gen1   s    zTestLoopLifting.test_lift_gen1c             C   s   |  ttjfd d S )N)rQ   )rM   r+   r   rR   )r7   r   r   r   test_reject1   s    zTestLoopLifting.test_reject1c             C   s   |  ttjfd d S )N)rQ   )rN   r,   r   rR   )r7   r   r   r   test_reject_gen1   s    z TestLoopLifting.test_reject_gen1c             C   s   |  ttjfd d S )N)rQ   )rN   r.   r   rR   )r7   r   r   r   test_reject_gen2   s    z TestLoopLifting.test_reject_gen2c             C   s   |  ttjfd d S )N)rQ   )rP   r0   r   rR   )r7   r   r   r   test_reject_npm1   s    z TestLoopLifting.test_reject_npm1N)__name__
__module____qualname__r;   rC   rI   rK   rM   rN   rP   rS   rT   rU   r   rV   rW   rX   rY   rZ   r[   r\   r]   r   r   r   r   r1   t   s$   r1   c               @   s   e Zd Zdd Zdd ZdS )TestLoopLiftingAnnotatec       
         s   ddl m} dd   fdd}||}td}| }||}tj||d  t }|j	|d	 |
 }|  | d
| |jjd }	| dj|	d| | d| dS )zO
        Verify that annotation works as expected with one lifted loop
        r   )jitc               S   s   d S )Nr   r   r   r   r   bar   s    z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.barc                s.      x"t | jD ]}| |  d7  < qW | S )Nr
   )r   r   )r   r   )rc   r   r   foo   s    z4TestLoopLiftingAnnotate.test_annotate_1.<locals>.foor)   r
   )filez"The function contains lifted loopsr%   zLoop at line {line})linezHas 1 overloadsN)numbarb   r   r   copytestingassert_equalr   StringIOinspect_typesgetvaluecloseassertIn__code__co_firstlinenoformat)
r7   rb   rd   cfoor   xcopyrbuf
annotationrf   r   )rc   r   test_annotate_1   s     
z'TestLoopLiftingAnnotate.test_annotate_1c                s   ddl m} dd   fdd}||}td}| }||}tj||d d	  t }|j	|d
 |
 }|  | d| |jjd }	|jjd }
| dj|	d| | dj|
d| dS )zP
        Verify that annotation works as expected with two lifted loops
        r   )rb   c               S   s   d S )Nr   r   r   r   r   rc     s    z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.barc                sR      x"t | jD ]}| |  d7  < qW x"t | jD ]}| |  d9  < q6W | S )Nr
   r%   )r   r   )r   r   r   )rc   r   r   rd     s    z4TestLoopLiftingAnnotate.test_annotate_2.<locals>.foor)   r
   r%   )re   z"The function contains lifted loopsr      zLoop at line {line})rf   N)rg   rb   r   r   rh   ri   rj   r   rk   rl   rm   rn   ro   rp   rq   rr   )r7   rb   rd   rs   r   rt   ru   rv   rw   Zline1Zline2r   )rc   r   test_annotate_2	  s"    

z'TestLoopLiftingAnnotate.test_annotate_2N)r^   r_   r`   rx   rz   r   r   r   r   ra      s   "ra   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S )TestLoopLiftingInActionc             C   s&   |j |jd  j}| t|| d S )Nr   )r<   
signaturesr6   r4   r5   )r7   jitted	loopcountr6   r   r   r   assert_has_lifted1  s    z)TestLoopLiftingInAction.assert_has_liftedc       	      C   s   ddl m}m}m}m} ||||d d  dddd }xJdD ]B}tjdd	d
}tjdd	d
}||| ||| | || q@W d S )Nr   )rb   voidr/   doubleT)forceobjc             S   s^   | dkr2xPt |jd D ]}|| d ||< qW n(x&t |jd D ]}|| d ||< qBW d S )Nr   g       @g      ?)r   r   )ur   r   r   r   r   forloop_with_if8  s
    z?TestLoopLiftingInAction.test_issue_734.<locals>.forloop_with_if)r   r
   r)   r/   )r   )	rg   rb   r   r/   r   r   r   py_funcrE   )	r7   rb   r   r/   r   r   r   Znb_aZnp_ar   r   r   test_issue_7345  s    "	

z&TestLoopLiftingInAction.test_issue_734c             C   sZ   ddl m} |ddddd }tdd	d
ddd
ddd
dg
}tj|||| d S )Nr   )rb   zf8[:](f8[:])T)r   c             S   sr   t t| }d}x6tt| D ]&}|d7 }| | ||< | | dkr P q W x"t|d t| D ]}d||< q^W |S )Nr   r
   r)   )r   Zzerosr5   r   )r   r-   ZindZiir   r   r   testK  s    z4TestLoopLiftingInAction.test_issue_812.<locals>.testg      ?r   r%   r   r)   ry   )rg   rb   r   Zarrayri   rj   r   )r7   rb   r   r   r   r   r   test_issue_812H  s    z&TestLoopLiftingInAction.test_issue_812c       
      C   s   ddl m} dd }td}t }||}|||}|||}| |d |d  | |d |d  |j|jd  jd }|j	 \}	| 
|	jj d S )Nr   )rb   c             S   s*   d}x| D ]}||7 }q
W |  }||fS )Nr   )__hash__)r   bsehr   r   r   lift_issue2368a  s
    
z?TestLoopLiftingInAction.test_issue_2368.<locals>.lift_issue2368r)   r
   )rg   rb   r   Zonesobjectr4   r<   r|   r6   r=   r>   r?   r@   )
r7   rb   r   r   r   r}   rG   rH   rA   rB   r   r   r   test_issue_2368^  s    


z'TestLoopLiftingInAction.test_issue_2368c             C   sj   ddl m} |dddd }| |d|d | j|dd	 | |d|d | j|dd	 d S )
Nr   )rb   T)r   c             S   s   d}xt | D ]}|}qW |S )Nr   )r   )nr-   r   r   r   r   r   |  s    z?TestLoopLiftingInAction.test_no_iteration_w_redef.<locals>.testr
   )r~   )rg   rb   r4   r   r   )r7   rb   r   r   r   r   test_no_iteration_w_redefw  s    z1TestLoopLiftingInAction.test_no_iteration_w_redefc             C   sj   ddl m} |dddd }| |d|d | j|dd	 | |d|d | j|dd	 d S )
Nr   )rb   T)r   c             S   s"   d}xt | D ]}||7 }qW |S )Nr   )r   )r   r-   r   r   r   r   r     s    z7TestLoopLiftingInAction.test_no_iteration.<locals>.testr   r
   )r~   )rg   rb   r4   r   r   )r7   rb   r   r   r   r   test_no_iteration  s    z)TestLoopLiftingInAction.test_no_iterationc             C   sF   ddl m} |dddd }| |d|d | j|dd d S )	Nr   )rb   T)r   c             S   s   xt | D ]}|}q
W |S )N)r   )r   r   r-   r   r   r   r     s    z>TestLoopLiftingInAction.test_define_in_loop_body.<locals>.testr
   )r~   )rg   rb   r4   r   r   )r7   rb   r   r   r   r   test_define_in_loop_body  s    z0TestLoopLiftingInAction.test_define_in_loop_bodyc             C   sB   ddl m} |dddd }td}| |||| dS )	zTest a problem caused by invalid discovery of loop argument
        when a variable is used afterwards but not before.

        Before the fix, this will result in::

        numba.ir.NotDefinedError: 'i' is not defined
        r   )rb   T)r   c             S   sT   t | tjkr@| jdkr:d}d}xt| jd D ]}q0W qPtnd}d\}}|S d S )Nr
   g        r   )r   r   )typer   Zndarrayndimr   r   	Exception)argresultr   r   r   r   r   r     s    
z;TestLoopLiftingInAction.test_invalid_argument.<locals>.testr)   N)rg   rb   r   r   r4   r   )r7   rb   r   r   r   r   r   test_invalid_argument  s    
z-TestLoopLiftingInAction.test_invalid_argumentc             C   sB   ddl m} |dddd }| | |  | j|dd d S )	Nr   )rb   T)r   c              S   s6   d} d}x$t dD ]}|dkr"d} || 7 }qW || fS )Nr   r   r%   ry   )r   )r   yr   r   r   r   r     s    zHTestLoopLiftingInAction.test_conditionally_defined_in_loop.<locals>.testr
   )r~   )rg   rb   r4   r   r   )r7   rb   r   r   r   r   "test_conditionally_defined_in_loop  s    
z:TestLoopLiftingInAction.test_conditionally_defined_in_loopc          	   C   s`   ddl m} dd l}dd }| 2 |d |dd|}| |d|d W d Q R X d S )Nr   )rb   c             S   s   | rxt dD ]}qW d S )Nr)   )r   )r   r   r   r   r   r8     s    zRTestLoopLiftingInAction.test_stack_offset_error_when_has_no_return.<locals>.pyfuncerrorT)r   )rg   rb   warningscatch_warningssimplefilterr4   )r7   rb   r   r8   Zcfuncr   r   r   *test_stack_offset_error_when_has_no_return  s    

zBTestLoopLiftingInAction.test_stack_offset_error_when_has_no_returnc                s@   ddl m} dd   fdd}||}| |d|d dS )	z
        https://github.com/numba/numba/issues/2179

        Looplifting transformation is using the wrong verion of variable `h`.
        r   )rb   c             S   s   | S )Nr   )r   r   r   r   rc     s    z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.barc                s.   d}xt | D ]}|| }qW | |  }|S )Ng        )r   )r   r   k)rc   r   r   rd     s
    z<TestLoopLiftingInAction.test_variable_scope_bug.<locals>.foor)   N)rg   rb   r4   )r7   rb   rd   rs   r   )rc   r   test_variable_scope_bug  s
    z/TestLoopLiftingInAction.test_variable_scope_bugc             C   s   ddl m} dd }||}tddf}| || ||  | t|j|jd  jd |j|jd  jd }| t|jd tddf}| || ||  | t|jd d	S )
z<
        https://github.com/numba/numba/issues/2481
        r   )rb   c             S   sF   | d d | }d}x.t |jD ] }t  ||d d d |  }qW |S )Nr
   r   )r   r   r   )r   r   Ar!   r   r   r   r   rd     s    z<TestLoopLiftingInAction.test_recompilation_loop.<locals>.foor)   r
   r   r%   N)	rg   rb   r   r   r4   r5   r<   r|   r6   )r7   rb   rd   rs   rF   r6   r   r   r   test_recompilation_loop  s    
z/TestLoopLiftingInAction.test_recompilation_loopN)r^   r_   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r{   0  s   r{   __main__)&Z
__future__r   r   r   Znumpyr   rg   r   r   r   ZunittestZnumba.compilerr   r	   Zsupportr   r   r   r3   setrh   rO   r   r   r"   r$   r&   r(   r*   r+   r,   r.   r0   r1   ra   r{   r^   mainr   r   r   r   <module>   s6   


	

	
rJ i
