o
    1&id                     @   s^  d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZ i Z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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&G d5d6 d6eZ'G d7d8 d8eZ(G d9d: d:eZ)e*d;kre+  dS dS )<    N)jit)CFGraphControlFlowAnalysis)types)FunctionIdentityByteCode_fix_LOAD_GLOBAL_arg)TestCaseT)Z_nrtZnopythonc                 C   s   d}t | D ]}||7 }q|S Nr   rangexyresulti r   HC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_flow_control.pyfor_loop_usecase1   s   
r   c                 C   s.   d}t t| |dD ]
\}}||| 7 }q
|S )Nr   )	enumerater   r   r   r   r   jr   r   r   for_loop_usecase2   s   r   c                 C   s"   d}| |g}|D ]}||7 }q|S r
   r   )r   r   r   lstr   r   r   r   for_loop_usecase3   s
   
r   c                 C   s,   d}t dD ]}t dD ]}|d7 }qq|S )Nr   
      r   r   r   r   r   for_loop_usecase4%   s   
r   c                 C   s,   d}t | D ]}|d7 }||kr |S q|S Nr   r   r   r   r   r   r   for_loop_usecase5-   s   r    c                 C   s(   d}t | D ]}||krq|d7 }q|S r   r   r   r   r   r   for_loop_usecase66   s   
r!   c                 C   s,   t | D ]}d} t | D ]}  dS 	 qdS r   r   )r   r   r   r   r   r   r   for_loop_usecase7?   s   r"   c                 C   s*   d}t | |||  d D ]}|d7 }q|S r   r   r   r   r   r   for_loop_usecase8I   s   
r#   c                 C   sH   d}t | D ]}d} t | D ]}|| d kr||7 } nq||7 }q|S )Nr      r   )r   r   zr   r   r   r   r   for_loop_usecase9P   s   r&   c                 C   s,   t | D ]}||kr|} |S q|d }|S )Nr$   r   )r   r   r   r%   r   r   r   for_loop_usecase10^   s   r'   c                 C   s,   d}d}|| k r||7 }|d7 }|| k s|S r   r   r   r   r   r   while_loop_usecase1h   s   r(   c                 C   s    d}|| kr|d7 }|| ks|S r   r   r   r   r   r   r   r   while_loop_usecase2q   s
   r*   c                 C   sL   d}d}d}|| k r$||k r ||| 7 }|d7 }|d7 }||k s|| k s
|S r   r   r   r   r   r   while_loop_usecase3x   s   r+   c                 C   s   d}	 |d7 }|| kr	 |S q)Nr   Tr   r   r)   r   r   r   while_loop_usecase4   s   r,   c                 C   s2   d}|| k r||kr|d7 }q|d7 }|| k s|S )Nr   r$   r   r   r)   r   r   r   while_loop_usecase5   s   r-   c                 C   s"   | dkr	 dS |dkr	 dS 	 dS )Nr   Tr   r   r   r   r   r   ifelse_usecase1   s   r/   c                 C   s$   | |krdS | dks|dkrdS dS )Nr   r   r$      r   r.   r   r   r   ifelse_usecase2   s
   r1   c                 C   s4   | dkr|dkr
dS |dk rdS dS | dk rdS dS r   r   r.   r   r   r   ifelse_usecase3   s   r2   c                 C   s   | |krdS d S )Nr   r   r.   r   r   r   ifelse_usecase4   s   r3   c                 C   s   | |krdS dS )NTFr   r.   r   r   r   ternary_ifelse_usecase1      r4   c                 C   s@   | }|}	 	 ||d krn|d7 }q|d7 }||kr	 ||fS q)NTr   r   )r   r   Lr   r   r   r   double_infinite_loop   s   r7   c                   C   s   zW d S  t y   Y d S w N)	Exceptionr   r   r   r   try_except_usecase   s
   r:   c                   @   s  e Zd ZefddZefddZdd ZefddZd	d
 ZefddZ	dd Z
efddZdd ZefddZdd ZefddZdd ZefddZdd Zefdd Zd!d" Zefd#d$Zd%d& Zefd'd(Zd)d* Zefd+d,Zd-d. Zefd/d0Zd1d2 Zefd3d4Zd5d6 Zefd7d8Zd9d: Z efd;d<Z!d=d> Z"efd?d@Z#dAdB Z$efdCdDZ%dEdF Z&efdGdHZ'dIdJ Z(efdKdLZ)dMdN Z*efdOdPZ+dQdR Z,efdSdTZ-dUdV Z.dWS )XTestFlowControlc                 C   s   t tjtjffi ||}t||D ]`\}}d }d }	z|||}
W n ty7 } z|}W Y d }~nd }~ww z|||}W n# tyb } z|d u rL |}	| t|t|	 W Y d }~qd }~ww |d urn| d|  | |
| qd S )Nz(Invalid for pure-python but numba works
)	r   r   Zintp	itertoolsproductr9   assertEqualtypeZfail)selfZpyfuncZ
x_operandsZ
y_operandsflagsZcfuncr   r   ZpyerrZcerrZpyreseZcresr   r   r   run_test   s2    zTestFlowControl.run_testc                 C   s   | j tg ddg|d d S )Nir   r   r   rA   )rC   r   r@   rA   r   r   r   test_for_loop1      zTestFlowControl.test_for_loop1c                 C      | j td d S NrE   )rG   no_pyobj_flagsr@   r   r   r   test_for_loop1_npm   r5   z"TestFlowControl.test_for_loop1_npmc                 C      | j tg dg d|d d S )NrD   rE   )rC   r   rF   r   r   r   test_for_loop2      
zTestFlowControl.test_for_loop2c                 C   rI   rJ   )rO   rK   rL   r   r   r   test_for_loop2_npm   r5   z"TestFlowControl.test_for_loop2_npmc                 C   s   | j tdgdg|d dS )z(
        List requires pyobject
        r   r$   rE   N)rC   r   rF   r   r   r   test_for_loop3   s   
zTestFlowControl.test_for_loop3c                 C   rI   rJ   )rR   rK   rL   r   r   r   test_for_loop3_npm  r5   z"TestFlowControl.test_for_loop3_npmc                 C      | j tdgdg|d d S Nr   rE   )rC   r   rF   r   r   r   test_for_loop4	     zTestFlowControl.test_for_loop4c                 C   rI   rJ   )rV   rK   rL   r   r   r   test_for_loop4_npm  r5   z"TestFlowControl.test_for_loop4_npmc                 C      | j tdgdg|d d S Nd   2   rE   )rC   r    rF   r   r   r   test_for_loop5  rW   zTestFlowControl.test_for_loop5c                 C   rI   rJ   )r]   rK   rL   r   r   r   test_for_loop5_npm  r5   z"TestFlowControl.test_for_loop5_npmc                 C   rY   rZ   )rC   r!   rF   r   r   r   test_for_loop6  rW   zTestFlowControl.test_for_loop6c                 C   rI   rJ   )r_   rK   rL   r   r   r   test_for_loop6_npm  r5   z"TestFlowControl.test_for_loop6_npmc                 C   rY   )N   r   rE   )rC   r"   rF   r   r   r   test_for_loop7  rW   zTestFlowControl.test_for_loop7c                 C   rI   rJ   )rb   rK   rL   r   r   r   test_for_loop7_npm  r5   z"TestFlowControl.test_for_loop7_npmc                 C      | j tddgg d|d d S Nr   r   )r   r$   r   rE   )rC   r#   rF   r   r   r   test_for_loop8!     zTestFlowControl.test_for_loop8c                 C   rI   rJ   )rf   rK   rL   r   r   r   test_for_loop8_npm$  r5   z"TestFlowControl.test_for_loop8_npmc                 C   rd   re   )rC   r&   rF   r   r   r   test_for_loop9'  rg   zTestFlowControl.test_for_loop9c                 C   rI   rJ   )ri   rK   rL   r   r   r   test_for_loop9_npm*  r5   z"TestFlowControl.test_for_loop9_npmc                 C   s   | j tdgddg|d d S )Nra   r$      rE   )rC   r'   rF   r   r   r   test_for_loop10-  rH   zTestFlowControl.test_for_loop10c                 C   rI   rJ   )rl   rK   rL   r   r   r   test_for_loop10_npm0  r5   z#TestFlowControl.test_for_loop10_npmc                 C   rY   Nr   r   rE   )rC   r(   rF   r   r   r   test_while_loop13  rW   z TestFlowControl.test_while_loop1c                 C   rI   rJ   )ro   rK   rL   r   r   r   test_while_loop1_npm6  r5   z$TestFlowControl.test_while_loop1_npmc                 C   rY   rn   )rC   r*   rF   r   r   r   test_while_loop29  rW   z TestFlowControl.test_while_loop2c                 C   rI   rJ   )rq   rK   rL   r   r   r   test_while_loop2_npm<  r5   z$TestFlowControl.test_while_loop2_npmc                 C   rT   rU   )rC   r+   rF   r   r   r   test_while_loop3?  rW   z TestFlowControl.test_while_loop3c                 C   rI   rJ   )rs   rK   rL   r   r   r   test_while_loop3_npmB  r5   z$TestFlowControl.test_while_loop3_npmc                 C   rY   rn   )rC   r,   rF   r   r   r   test_while_loop4E  rW   z TestFlowControl.test_while_loop4c                 C   rI   rJ   )ru   rK   rL   r   r   r   test_while_loop4_npmH  r5   z$TestFlowControl.test_while_loop4_npmc                 C   rN   )N)r   ra   r   rE   )rC   r-   rF   r   r   r   test_while_loop5K  rg   z TestFlowControl.test_while_loop5c                 C   rI   rJ   )rw   rK   rL   r   r   r   test_while_loop5_npmN  r5   z$TestFlowControl.test_while_loop5_npmc                 C   rN   N)r   r   r   rE   )rC   r/   rF   r   r   r   test_ifelse1Q  rg   zTestFlowControl.test_ifelse1c                 C   rI   rJ   )rz   rK   rL   r   r   r   test_ifelse1_npmT  r5   z TestFlowControl.test_ifelse1_npmc                 C   rN   ry   )rC   r1   rF   r   r   r   test_ifelse2W  rg   zTestFlowControl.test_ifelse2c                 C   rI   rJ   )r|   rK   rL   r   r   r   test_ifelse2_npmZ  r5   z TestFlowControl.test_ifelse2_npmc                 C   rN   ry   )rC   r2   rF   r   r   r   test_ifelse3]  rg   zTestFlowControl.test_ifelse3c                 C   rI   rJ   )r~   rK   rL   r   r   r   test_ifelse3_npm`  r5   z TestFlowControl.test_ifelse3_npmc                 C   rN   ry   )rC   r3   rF   r   r   r   test_ifelse4c  rg   zTestFlowControl.test_ifelse4c                 C   rI   rJ   )r   rK   rL   r   r   r   test_ifelse4_npmf  r5   z TestFlowControl.test_ifelse4_npmc                 C   rN   ry   )rC   r4   rF   r   r   r   test_ternary_ifelse1i  rP   z$TestFlowControl.test_ternary_ifelse1c                 C   rI   rJ   )r   rK   rL   r   r   r   test_ternary_ifelse1_npmm  r5   z(TestFlowControl.test_ternary_ifelse1_npmc                 C   rY   rn   )rC   r7   rF   r   r   r   test_double_infinite_loopp  s   
z)TestFlowControl.test_double_infinite_loopc                 C   rI   rJ   )r   rK   rL   r   r   r   test_double_infinite_loop_npmt  r5   z-TestFlowControl.test_double_infinite_loop_npmN)/__name__
__module____qualname__enable_pyobj_flagsrC   rG   rM   rO   rQ   rR   rS   rV   rX   r]   r^   r_   r`   rb   rc   rf   rh   ri   rj   rl   rm   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r;      sZ    
r;   c                   @   s   e Zd ZdZ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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;S )=TestCFGraphz3
    Test the numba.controlflow.CFGraph class.
    r   c                 C   sF   t  }|D ]}|| q| D ]\}}|D ]}||| qq|S )zG
        Build a CFGraph class from a dict of adjacency lists.
        )r   add_nodeitemsZadd_edge)r@   dentry_pointgnodeZdestsdestr   r   r   from_adj_list}  s   zTestCFGraph.from_adj_listc                 C   s2   |  ddgdgdgg d}|d |  |S )z
        A simple CFG corresponding to the following code structure:

            c = (... if ... else ...) + ...
            return b + c
                 r   r   r   r   r   r   set_entry_pointprocessr@   r   r   r   r   	loopless1  s   
zTestCFGraph.loopless1c                 C   sD   |  ddgdgdgg ddgddgdgg d}|d |  |S )	zc
        Same as loopless1(), but with added dead blocks (some of them
        in a loop).
        r   r   r   r   [   ]   \   )r   r   r   r   r   r   r   ^   r   r   r   r   r   loopless1_dead_nodes  s   

z TestCFGraph.loopless1_dead_nodesc              	   C   s:   |  ddgdgdgddgg g d}|d |  |S )aA  
        A loopless CFG corresponding to the following code structure:

            c = (... if ... else ...) + ...
            if c:
                return ...
            else:
                return ...

        Note there are two exit points, and the entry point has been
        changed to a non-zero value.
        r   r   r   *   "   c   r   r   r   r   r   r   r   r   r   r   r   	loopless2  s   
zTestCFGraph.loopless2c                 C   s   |  i ddgdddgddgddgdddgdd	d
gd	dgd
dgddgddgddgddgdddgdddgdg ddgdg }|d |  |S )a^  
        A CFG with multiple nested loops:

            for y in b:
                for x in a:
                    # This loop has two back edges
                    if b:
                        continue
                    else:
                        continue
            for z in c:
                if z:
                    return ...
        r   rk   r   <         8          ,   9   =   D   W   G   P   X   r   r   r   r   r   multiple_loops  sH   	

zTestCFGraph.multiple_loopsc                 C   sD   |  dgddgddgg ddgdgdgg d}|d	 |  |S )
a  
        A CFG with three loop exits, one of which is also a function
        exit point, and another function exit point:

            for x in a:
                if a:
                    return b
                elif b:
                    break
            return c
        rk   r   $      r      %   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   multiple_exits  s   

zTestCFGraph.multiple_exitsc              	   C   s<   |  ddgg dgddgdgdgd}|d |  |S )z
        A CFG with a infinite loop and an alternate exit point:

            if c:
                return
            while True:
                if a:
                    ...
                else:
                    ...
        r      r      r   r   r   r   r   r   r   r   r   r   r   r   r   infinite_loop1  s   
zTestCFGraph.infinite_loop1c                 C   s4   |  dgddgdgdgd}|d |  |S )z
        A CFG with no exit point at all:

            while True:
                if a:
                    ...
                else:
                    ...
        r0      	   r   r0   r   r   r   r   r   r   r   r   infinite_loop2  s   

zTestCFGraph.infinite_loop2c                 C   sl   |   }| t|dddg | t|dg  | t|dg  | t|dddg d S )Nr   )r   N)r   Nr   )r   r>   sorted
successorsZpredecessorsr   r   r   r   test_simple_properties  s
   z"TestCFGraph.test_simple_propertiesc                 C   s   |   }| t| dg |  }| t| dg |  }| t| ddg |  }| t| ddg |  }| t| dg |  }| t| g  | 	 }| t| ddg d S )	Nr   r   r   r   r   r   r   r   )
r   r>   r   Zexit_pointsr   r   r   r   r   r   r   r   r   r   test_exit_points#  s   zTestCFGraph.test_exit_pointsc                 C   s   |   }| t| d | t| g d |  }| t| d | t| g d |  }| t| d |  }| t| d | 	 }| t| d | 
 }| t| g d | t| g d d S )Nr   r   r   r   r   r   r   r   )r   r   r   r   )r   r>   lenZ
dead_nodesr   nodesr   r   r   r   r   r   r   r   r   test_dead_nodes3  s.   zTestCFGraph.test_dead_nodesc                 C   s,  |   }|d}| t|g  |d}| t|g  |d}| t|ddg |d}| t|g d |  }|d}| t|g  |d}| t|g  |d}| t|ddg |d	}| t|g d
 |d}| t|g  |d}| t|g d d S )Nr   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   )r   Zdescendentsr>   r   r   )r@   r   r   r   r   r   test_descendentsI  s,   









zTestCFGraph.test_descendentsc                 C   s   |   }| | g dg df |  }| | g dg df |  }| | g dg df |  }| | g dg dg d	g d
f d S )Nr   )r   r   r   r   )r   r   r   r   r   r   r   r   )r   r0   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   assertInZ
topo_orderr   r   r   r   r   r   r   test_topo_ordera  s$   



zTestCFGraph.test_topo_orderc                    sp    fdd}   |g dg df |g dg dg df   |g dg d	g d
g df d S )Nc              	      s    t | |  t | d d d |  t j| ddd d d |  t j| d d d ddd d d | j|   t | |  t j| ddd d d | d S )Nr   T)reverse)r   listZ	topo_sortrandomshuffle)r   expectedr   r@   r   r   check_topo_sortq  s    (z3TestCFGraph.test_topo_sort.<locals>.check_topo_sort)r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   rk   r   )rk   r   r   r   )rk   r   r   r   )rk   r   r   r   )r   r   )r@   r   r   r   r   test_topo_sortp  s   zTestCFGraph.test_topo_sortc                 C   sJ   |  t|t| t|D ]}|  t|| t|| d|f  qd S )Nzmismatch for %rr>   r   )r@   gotr   r   r   r   r   check_dominators  s   zTestCFGraph.check_dominatorsc                    s    fdd}   fD ]!}|  |ddg |dddg |dddg |dddg q }|  |ddg |dddg |dddg |dddg |dg d	 |d
g d d S )Nc                        t |  | d S r8   r   r   ldomsr@   r   r   eq_     z1TestCFGraph.test_dominators_loopless.<locals>.eq_r   r   r   r   r   r   )r   r   r   r   )r   r   r   )r   r   
dominatorsr   r@   r   r   r   r   r   test_dominators_loopless  s   z$TestCFGraph.test_dominators_looplessc                 C   sT  |   }| }| |dgddgg dg dg dg dg dg dd	 |  }| }| |i ddgdddgd
g ddg ddg ddg ddg ddg ddg ddg ddg ddg ddg dd g d!d"g d#d$g d%d&g d' |  }| }| |dgdd(gdd
gg d)g d*g d+d, d S )-Nr   rk   )r   rk   r   )r   rk   r   r   )r   rk   r   r   )r   rk   r   r   r   )r   rk   r   )r   rk   r   r   r   )r   r   rk   r   )r   r   r   rk   r   )r   r   r   r   rk   r   )r   r   r   rk   r   r   r   )r   r   r   r   rk   r   r   r   )r   r   r   rk   r   r   r   r   )r   r   rk   r   r   r   r   )r   r   rk   r   r   r   r   r   )r   r   rk   r   )r   r   r   rk   r   )r   r   r   r   rk   r   )r   r   r   rk   r   r   r   )r   r   r   r   rk   r   r   r   )r   r   r   rk   r   r   r   )r   r   r   r   rk   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   test_dominators_loops  sz   

	
z!TestCFGraph.test_dominators_loopsc                    s    fdd}   fD ]!}|  |dddg |dddg |dddg |ddg q }|  |ddg |ddg |ddg |dddg |dddg |d	dd	g d S )
Nc                    r   r8   r   r   r   r   r   r     r   z6TestCFGraph.test_post_dominators_loopless.<locals>.eq_r   r   r   r   r   r   r   )r   r   post_dominatorsr   r   r   r   r   test_post_dominators_loopless  s   z)TestCFGraph.test_post_dominators_looplessc                 C   s  |   }| }| |ddgdgdgdgdgddgddgdgd	 |  }| }| |i dg d
dg ddg ddg ddg ddg ddg ddg ddg ddg ddg ddddgddgddgddgd d!d gd!d!g d S )"Nr   rk   r   r   r   r   r   r   r   )r   r   r   r   rk   )r   r   r   rk   )	r   rk   r   r   r   r   r   r   r   r   )r   rk   r   r   r   r   r   r   r   )r   r   rk   r   r   r   r   )r   rk   r   r   r   r   r   r   r   )r   r   rk   r   r   r   r   r   r   )r   rk   r   r   r   r   r   r   r   )r   rk   r   r   r   r   r   )r   r   r   r   rk   r   )r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   test_post_dominators_loops  sd   
	
z&TestCFGraph.test_post_dominators_loopsc              
   C   sl   |   }| }| |dgdgddgdgdgdgd |  }| }| |ddgdgd	gd
gd d S )Nr   r   r   r   r   r   r   r0   r   r   r   )r   r   r   r   r   r   r   r   #test_post_dominators_infinite_loops   s(   z/TestCFGraph.test_post_dominators_infinite_loopsc                    s   fdd}|   h dt t t d |  t t ddht t h dd |  h dt t t d |  i dd	hd	d
dhddhddhdddhddhdt ddhdt d
dhddhdddhdddhdt dt ddhdt  |  d	hh dt ddhdht t t d |  d
dht dhddht t d |  d hd!d"ht t d# d S )$Nc                       |   } || d S r8   )Zdominator_treer>   )graphr   ZdomtreerL   r   r   check     z.TestCFGraph.test_dominator_tree.<locals>.check>   r   r   r   r   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >   r   r   r   r   r   )r   rk   r   r   r   r   r   r   r   r   r   r0   r   r   r   r   setr   r   r   r   r   r   r@   r   r   rL   r   test_dominator_tree  sh   .zTestCFGraph.test_dominator_treec                    s$   fdd}|   ddddd |  ddddddd |  ddddd |  i 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 S )Nc                    r   r8   )immediate_dominatorsr>   )r   r   idomsrL   r   r   r   1  r   z4TestCFGraph.test_immediate_dominators.<locals>.checkr   r   r   r   r   r   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   )r   r   r   r   r   r   r   r   r   rL   r   test_immediate_dominators0  sX   &z%TestCFGraph.test_immediate_dominatorsc                    s   fdd}|   t dhdht d |  dhdht t t t d |  t dhdht d |  i dt ddhddhd	dhd
d
dhdd
hdd
hdd
hddhddhdt dt ddhddhdt dt dt  |  t dhddht ddhdhdht d |  t t t d	hd	hd	hd |  t dhdhdhd d S )Nc                    r   r8   )dominance_frontierr>   )r   r   ZdfrL   r   r   r   G  r   z2TestCFGraph.test_dominance_frontier.<locals>.checkr   r   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   r   r   r   rL   r   test_dominance_frontierF  s\   4z#TestCFGraph.test_dominance_frontierc                 C   sR   |   |  fD ]}| t| ddg q|  }| t| ddg d S )Nr   r   r   )r   r   r>   r   backboner   r   r   r   r   test_backbone_loopless]  s   z"TestCFGraph.test_backbone_looplessc                 C   sb   |   }| t| g d |  }| t| dg |  }| t| ddg d S )N)r   rk   r   r   r   r   r0   )r   r>   r   r   r   r   r   r   r   r   test_backbone_loopsc  s   zTestCFGraph.test_backbone_loopsc                 C   s  |   |  |  fD ]}| t| d q|  }| t| g d | d }| d }| d }| |jd | t|j	dg | t|j
dg | t|jg d | |jd | t|j	dg | t|j
d	g | t|jg d
 | |jd | t|j	dg | t|j
ddg | t|jddg dD ]}| ||g  qdD ]}| |||g qd
D ]}| ||||g qdD ]}| |||g q|  }| t| dg | d }| |jd | t|j	dg | t|j
g  | t|jg d dD ]}| ||g  q%dD ]}| |||g q4|  }| t| dg | d }| |jd | t|j	dg | t|j
g  | t|jg d dD ]}| ||g  q~dD ]}| |||g q|  }| t| dg | d }| |jd | t|j	dg | t|j
g d | t|jg d dD ]}| ||g  qdD ]}| |||g qd S )Nr   )rk   r   r   rk   r   r   r   )	rk   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )rk   r   r   r   r   )r   r   r   r   )r   r   r   r0   )r0   r   r   r   )r   r   r   )rk   r   r   )r   r   r   r   )r   r   r   r>   r   Zloopsr   r   headerentriesZexitsbodyZin_loopsr   r   r   )r@   r   Zouter1Zinner1Zouter2r   loopr   r   r   
test_loopsk  s   zTestCFGraph.test_loopsc                 C   sd  |  i dddhdddhdddhdddhddhdddhddd	hd	d
hdddhdddhdd
hdddhdddhdd
hdd
dhdd
dhdd
hi d
ddhddhdddhddhdddhddhdddhddhdddhddhddhdddhdd hdd hd d!d"hd!d"hd"d#d$hi d$d%d&hd%d&hd&d'd(hd'd)d(hd)t d(d*d+hd+d*hd*d,d-hd-d.hd.d/hd/d0d1hd1d2d3hd3d4d5hd4d2d5hd5d2hd2d/hd0d6hd#hd7hd8hd9d:hd;d<hd=d>hd;d>hd;hd8hd#ht d?}|d |  i }|j|d@}| |dAdBh | |dC dD d S )ENr   &            r   @   r\   :      H   V   r   l   r   f   t   |                     i  i  i(  iF  iJ  ir  iT  iv  i|  i~  2  i  i  i  i  i6  i$  iJ  i<    iT  i`  b  i  il    iv  i|  i        0    
        )r  r  r  r  r  r  r  r   r  r  r  )stats)r  r  )r  r  Ziteration_count   )r   r   r   r   Z_find_back_edgesr>   )r@   r   r!  Z
back_edgesr   r   r   test_loop_dfs_pathological  s   	
 !"#$%&'()*+,-./01234

@z&TestCFGraph.test_loop_dfs_pathologicalc                    s$   fdd}| }| }  ||  ddgdgg dgd}|d |    || | }|d |   ||  ddgdgdgdgg d	}|d |   ||  ddgdgg d
}|d |   ddgdgg d
}|d |   || d S )Nc                     s2     ddgdgdgg d} | d |   | S )Nr   r   r   r   r   r   )r   rL   r   r   get_new  s   
z(TestCFGraph.test_equals.<locals>.get_newr   r   r   )r   r   r   r   r   r  )r   r   r   r   r  )r   r   r   )r>   r   r   r   ZassertNotEqual)r@   r$  r   r   r   r%   ar   rL   r   test_equals  s2   




zTestCFGraph.test_equalsNr   ) r   r   r   __doc__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&  r   r   r   r   r   x  s<    
%-#HJr   c                   @   sP   e Zd 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S )TestRealCodeDomFronta)  Test IDOM and DOMFRONT computation on real python bytecode.
    Note: there will be less testing on IDOM (esp in loop) because of
    the extra blocks inserted by the interpreter.  But, testing on DOMFRONT
    (which depends on IDOM) is easier.

    Testing is done by associating names to basicblock by using globals of
    the pattern "SET_BLOCK_<name>", which are scanned by
    `.get_cfa_and_namedblocks` into *namedblocks* dictionary.  That way, we
    can check that a block of a certain name is a IDOM or DOMFRONT of another
    named block.
    c                 C   s   t |}|  |S r8   )r   run)r@   bccfar   r   r   r+  4  s   zTestRealCodeDomFront.cfac                 C   s2   t |}t|d}| |}| ||}||fS )N)Zfunc_id)r   from_functionr   r+  _scan_namedblocks)r@   fnZfidr*  r+  namedblocksr   r   r   get_cfa_and_namedblocks9  s
   


z,TestRealCodeDomFront.get_cfa_and_namedblocksc                 C   s   i }t dd | D }d}|D ]J}|jdkr[|jt|j }||r[|t|d }t||dd |d d g D ]\}	}
|	|j	  krL|
k rPn q= nq=t
d|	}|||< q|S )	zScan namedblocks as denoted by a LOAD_GLOBAL bytecode referring
        to global variables with the pattern "SET_BLOCK_<name>", where "<name>"
        would be the name for the current block.
        c                 S   s   g | ]}|j qS r   )offset).0r   r   r   r   
<listcomp>R  s    z:TestRealCodeDomFront._scan_namedblocks.<locals>.<listcomp>Z
SET_BLOCK_LOAD_GLOBALNr   r   zunreachable loop)r   Z
iterblocksopnameco_namesr   arg
startswithr   zipr1  AssertionError)r@   r*  r+  r/  blocksprefixinstgvnamesrB   Zblknor   r   r   r-  L  s"   

(z&TestRealCodeDomFront._scan_namedblocksc                 C   sH   dd }|  |\}}|j }| ||d   | ||d   d S )Nc                 S   s0   d}t  d}trt |d7 }|d7 }tst |S r   )SET_BLOCK_ASET_BLOCK_B0SET_BLOCK_B1SET_BLOCK_C)ncr   r   r   r   foof  s   z+TestRealCodeDomFront.test_loop.<locals>.fooAC)r0  r   r   assertFalser@   rG  r+  blkptsdomfrontr   r   r   	test_loope  s
   
zTestRealCodeDomFront.test_loopc                 C   s   dd }|  |\}}| |d |d  |j }| ||d   | ||d   | |d h||d   | |d	 h||d
   d S )Nc                 S   s>   t  trt trt trt n| rt t tst	 tst
 d S r8   )rA  rB  rC  SET_BLOCK_C0SET_BLOCK_C1ZSET_BLOCK_D0ZSET_BLOCK_D1ZSET_BLOCK_D2SET_BLOCK_ESET_BLOCK_FSET_BLOCK_G)rE  r   r   r   rG  {  s   z<TestRealCodeDomFront.test_loop_nested_and_break.<locals>.fooZD0C1rH  GFZD1EZD2)r0  r>   r   r   rJ  rK  r   r   r   test_loop_nested_and_breakz  s   
z/TestRealCodeDomFront.test_loop_nested_and_breakc                 C   s  dd }|  |\}}|j }| |d ||d   | |d ||d   | |d ||d   | |d ||d   | |d ||d   | |d ||d	   | |d ||d
   |j }| ||d   | ||d   | ||d
   | |d h||d   | |d h||d   | |d h||d   | |d h||d   | |d
 h||d	   d S )Nc                 S   sT   d}t  | |k rt d}ntrt d}nt d}t | | dkr&t |d7 }t |S )Nr   r   r$   r0   )rA  SET_BLOCK_BrO  rP  SET_BLOCK_DrQ  rR  rS  )r%  brF  r   r   r   rG    s    z.TestRealCodeDomFront.test_if_else.<locals>.foorH  BC0rT  DrW  rV  rU  r0  r   r   r>   r   rJ  r@   rG  r+  rL  r   rM  r   r   r   test_if_else  s&   

z!TestRealCodeDomFront.test_if_elsec                 C   sJ  dd }|  |\}}|j }| |d ||d   | |d ||d   | |d ||d   | |d ||d   | |d ||d   | |d ||d	   |j }| ||d   | ||d	   | |d h||d   | |d h||d
   | |d h||d   | |d	 h||d   d S )Nc                  S   s<   t rt trt d} ntrt d} nt d} t t t	 | S )Nr   r   r$   )
ZSET_BLOCK_A0ZSET_BLOCK_A1rB  rC  rO  rP  ZSET_BLOCK_C2rZ  rQ  rR  )r%  r   r   r   rG    s   z5TestRealCodeDomFront.test_if_else_nested.<locals>.fooZA0A1ZB1r]  r^  rW  rV  rT  r_  r`  r   r   r   test_if_else_nested  s    

z(TestRealCodeDomFront.test_if_else_nestedc                 C   s   dd }|  |\}}|j }| d| | |d ||d   | |d ||d   |j }| ||d   | ||d   | |d h||d   | |d h||d   d S )Nc                   S   s   t  	 tr	t d S t qr8   )rA  rY  rD  rZ  rQ  r   r   r   r   rG    s   z4TestRealCodeDomFront.test_infinite_loop.<locals>.foorW  r\  rI  r^  rH  )r0  r   r   ZassertNotInr>   r   rJ  r`  r   r   r   test_infinite_loop  s   	

z'TestRealCodeDomFront.test_infinite_loopN)r   r   r   r'  r+  r0  r-  rN  rX  ra  rc  rd  r   r   r   r   r(  (  s    *%r(  __main__),r<   ZunittestZnumbar   Znumba.core.controlflowr   r   Z
numba.corer   Znumba.core.bytecoder   r   r   Znumba.tests.supportr	   r   rK   r   r   r   r   r    r!   r"   r#   r&   r'   r(   r*   r+   r,   r-   r/   r1   r2   r3   r4   r7   r:   r;   r   r(  r   mainr   r   r   r   <module>   sX    
		

		

	 !     5 X