o
    8ήc?                     @   s  d dl Z d dlZd dlmZm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 d dlmZ e Zde_e Zde_e Zd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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/e0d;kre1  dS dS )<    N)CFGraphControlFlowAnalysis)compile_isolatedFlags)types)FunctionIdentityByteCode)	PYVERSION)TestCaseTc                 C   s   d}t | D ]}||7 }q|S Nr   rangexyresulti r   D/tmp/pip-target-vg8gfxp4/lib/python/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_usecase53   s   r!   c                 C   s(   d}t | D ]}||krq|d7 }q|S r    r   r   r   r   r   for_loop_usecase6<   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_usecase7E   s   r#   c                 C   s*   d}t | |||  d D ]}|d7 }q|S r    r   r   r   r   r   for_loop_usecase8O   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_usecase9V   s   r'   c                 C   s,   t | D ]}||kr|} |S q|d }|S )Nr%   r   )r   r   r   r&   r   r   r   for_loop_usecase10d   s   r(   c                 C   s,   d}d}|| k r||7 }|d7 }|| k s|S r    r   r   r   r   r   while_loop_usecase1n   s   r)   c                 C   s    d}|| kr|d7 }|| ks|S r    r   r   r   r   r   r   r   while_loop_usecase2w   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_usecase3~   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   r0   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
   r2   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   r3   c                 C   s   | |krdS d S )Nr   r   r/   r   r   r   ifelse_usecase4   s   r4   c                 C   s   | |krdS dS )NTFr   r/   r   r   r   ternary_ifelse_usecase1      r5   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   r8   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|d}|j}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 )Nflagsz(Invalid for pure-python but numba works
)
r   r   intpentry_point	itertoolsproductr:   assertEqualtypefail)selfpyfunc
x_operands
y_operandsr>   crcfuncr   r   pyerrcerrpyresecresr   r   r   run_test   s4    zTestFlowControl.run_testc                 C   s   | j tg ddg|d d S )Nir   r   r   r=   )rQ   r   rF   r>   r   r   r   test_for_loop1      zTestFlowControl.test_for_loop1c                 C      | j td d S Nr=   )rT   no_pyobj_flagsrF   r   r   r   test_for_loop1_npm   r6   z"TestFlowControl.test_for_loop1_npmc                 C      | j tg dg d|d d S )NrR   r=   )rQ   r   rS   r   r   r   test_for_loop2      
zTestFlowControl.test_for_loop2c                 C   rV   rW   )r\   rX   rY   r   r   r   test_for_loop2_npm  r6   z"TestFlowControl.test_for_loop2_npmc                 C   s   | j tdgdg|d dS )z(
        List requires pyobject
        r   r%   r=   N)rQ   r   rS   r   r   r   test_for_loop3  s   
zTestFlowControl.test_for_loop3c                 C   rV   rW   )r_   rX   rY   r   r   r   test_for_loop3_npm  r6   z"TestFlowControl.test_for_loop3_npmc                 C      | j tdgdg|d d S Nr   r=   )rQ   r   rS   r   r   r   test_for_loop4     zTestFlowControl.test_for_loop4c                 C   rV   rW   )rc   rX   rY   r   r   r   test_for_loop4_npm  r6   z"TestFlowControl.test_for_loop4_npmc                 C      | j tdgdg|d d S Nd   2   r=   )rQ   r!   rS   r   r   r   test_for_loop5  rd   zTestFlowControl.test_for_loop5c                 C   rV   rW   )rj   rX   rY   r   r   r   test_for_loop5_npm  r6   z"TestFlowControl.test_for_loop5_npmc                 C   rf   rg   )rQ   r"   rS   r   r   r   test_for_loop6  rd   zTestFlowControl.test_for_loop6c                 C   rV   rW   )rl   rX   rY   r   r   r   test_for_loop6_npm  r6   z"TestFlowControl.test_for_loop6_npmc                 C   rf   )N   r   r=   )rQ   r#   rS   r   r   r   test_for_loop7"  rd   zTestFlowControl.test_for_loop7c                 C   rV   rW   )ro   rX   rY   r   r   r   test_for_loop7_npm%  r6   z"TestFlowControl.test_for_loop7_npmc                 C      | j tddgg d|d d S Nr   r   )r   r%   r   r=   )rQ   r$   rS   r   r   r   test_for_loop8(     zTestFlowControl.test_for_loop8c                 C   rV   rW   )rs   rX   rY   r   r   r   test_for_loop8_npm+  r6   z"TestFlowControl.test_for_loop8_npmc                 C   rq   rr   )rQ   r'   rS   r   r   r   test_for_loop9.  rt   zTestFlowControl.test_for_loop9c                 C   rV   rW   )rv   rX   rY   r   r   r   test_for_loop9_npm1  r6   z"TestFlowControl.test_for_loop9_npmc                 C   s   | j tdgddg|d d S )Nrn   r%      r=   )rQ   r(   rS   r   r   r   test_for_loop104  rU   zTestFlowControl.test_for_loop10c                 C   rV   rW   )ry   rX   rY   r   r   r   test_for_loop10_npm7  r6   z#TestFlowControl.test_for_loop10_npmc                 C   rf   Nr   r   r=   )rQ   r)   rS   r   r   r   test_while_loop1:  rd   z TestFlowControl.test_while_loop1c                 C   rV   rW   )r|   rX   rY   r   r   r   test_while_loop1_npm=  r6   z$TestFlowControl.test_while_loop1_npmc                 C   rf   r{   )rQ   r+   rS   r   r   r   test_while_loop2@  rd   z TestFlowControl.test_while_loop2c                 C   rV   rW   )r~   rX   rY   r   r   r   test_while_loop2_npmC  r6   z$TestFlowControl.test_while_loop2_npmc                 C   ra   rb   )rQ   r,   rS   r   r   r   test_while_loop3F  rd   z TestFlowControl.test_while_loop3c                 C   rV   rW   )r   rX   rY   r   r   r   test_while_loop3_npmI  r6   z$TestFlowControl.test_while_loop3_npmc                 C   rf   r{   )rQ   r-   rS   r   r   r   test_while_loop4L  rd   z TestFlowControl.test_while_loop4c                 C   rV   rW   )r   rX   rY   r   r   r   test_while_loop4_npmO  r6   z$TestFlowControl.test_while_loop4_npmc                 C   r[   )N)r   rn   r   r=   )rQ   r.   rS   r   r   r   test_while_loop5R  rt   z TestFlowControl.test_while_loop5c                 C   rV   rW   )r   rX   rY   r   r   r   test_while_loop5_npmU  r6   z$TestFlowControl.test_while_loop5_npmc                 C   r[   N)r   r   r   r=   )rQ   r0   rS   r   r   r   test_ifelse1X  rt   zTestFlowControl.test_ifelse1c                 C   rV   rW   )r   rX   rY   r   r   r   test_ifelse1_npm[  r6   z TestFlowControl.test_ifelse1_npmc                 C   r[   r   )rQ   r2   rS   r   r   r   test_ifelse2^  rt   zTestFlowControl.test_ifelse2c                 C   rV   rW   )r   rX   rY   r   r   r   test_ifelse2_npma  r6   z TestFlowControl.test_ifelse2_npmc                 C   r[   r   )rQ   r3   rS   r   r   r   test_ifelse3d  rt   zTestFlowControl.test_ifelse3c                 C   rV   rW   )r   rX   rY   r   r   r   test_ifelse3_npmg  r6   z TestFlowControl.test_ifelse3_npmc                 C   r[   r   )rQ   r4   rS   r   r   r   test_ifelse4j  rt   zTestFlowControl.test_ifelse4c                 C   rV   rW   )r   rX   rY   r   r   r   test_ifelse4_npmm  r6   z TestFlowControl.test_ifelse4_npmc                 C   r[   r   )rQ   r5   rS   r   r   r   test_ternary_ifelse1p  r]   z$TestFlowControl.test_ternary_ifelse1c                 C   rV   rW   )r   rX   rY   r   r   r   test_ternary_ifelse1_npmt  r6   z(TestFlowControl.test_ternary_ifelse1_npmc                 C   rf   r{   )rQ   r8   rS   r   r   r   test_double_infinite_loopw  s   
z)TestFlowControl.test_double_infinite_loopc                 C   rV   rW   )r   rX   rY   r   r   r   test_double_infinite_loop_npm{  r6   z-TestFlowControl.test_double_infinite_loop_npmN)/__name__
__module____qualname__enable_pyobj_flagsrQ   rT   rZ   r\   r^   r_   r`   rc   re   rj   rk   rl   rm   ro   rp   rs   ru   rv   rw   ry   rz   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<      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itemsadd_edge)rF   dr@   gnode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rF   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   rx   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
        rx   r   $      r      %   r   rx   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:
                    ...
        r1      	   r   r1   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   rC   sorted
successors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   rC   r   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   rC   len
dead_nodesr   nodesr   r   r   r   r   r   r   r   r   test_dead_nodes:  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   descendentsrC   r   r   )rF   r   r   r   r   r   test_descendentsP  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   r1   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   assertIn
topo_orderr   r   r   r   r   r   r   test_topo_orderh  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   list	topo_sortrandomshuffle)r   expectedr   rF   r   r   check_topo_sortx  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   rx   r   )rx   r   r   r   )rx   r   r   r   )rx   r   r   r   )r   r   )rF   r   r   r   r   test_topo_sortw  s   zTestCFGraph.test_topo_sortc                 C   sJ   |  t|t| t|D ]}|  t|| t|| d|f  qd S )Nzmismatch for %rrC   r   )rF   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 r9   r   r   ldomsrF   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   rF   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   rx   )r   rx   r   )r   rx   r   r   )r   rx   r   r   )r   rx   r   r   r   )r   rx   r   )r   rx   r   r   r   )r   r   rx   r   )r   r   r   rx   r   )r   r   r   r   rx   r   )r   r   r   rx   r   r   r   )r   r   r   r   rx   r   r   r   )r   r   r   rx   r   r   r   r   )r   r   rx   r   r   r   r   )r   r   rx   r   r   r   r   r   )r   r   rx   r   )r   r   r   rx   r   )r   r   r   r   rx   r   )r   r   r   rx   r   r   r   )r   r   r   r   rx   r   r   r   )r   r   r   rx   r   r   r   )r   r   r   r   rx   r   r   r   )r   r   r   )r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   rF   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   r9   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   rx   r   r   r   r   r   r   r   )r   r   r   r   rx   )r   r   r   rx   )	r   rx   r   r   r   r   r   r   r   r   )r   rx   r   r   r   r   r   r   r   )r   r   rx   r   r   r   r   )r   rx   r   r   r   r   r   r   r   )r   r   rx   r   r   r   r   r   r   )r   rx   r   r   r   r   r   r   r   )r   rx   r   r   r   r   r   )r   r   r   r   rx   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   r1   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 r9   )dominator_treerC   )graphr   domtreerY   r   r   check     z.TestCFGraph.test_dominator_tree.<locals>.check>   r   r   r   r   r   r   r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >   r   r   r   r   r   )r   rx   r   r   r   r   r   r   r   r   r   r1   r   r   r   r   setr   r   r   r   r   r   rF   r  r   rY   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   r9   )immediate_dominatorsrC   )r   r   idomsrY   r   r   r  8  r  z4TestCFGraph.test_immediate_dominators.<locals>.checkr   r   r   r   r   r   r   r   r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r   )r   r   r   r   r   r   r   r  r   rY   r   test_immediate_dominators7  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   r9   )dominance_frontierrC   )r   r   dfrY   r   r   r  N  r  z2TestCFGraph.test_dominance_frontier.<locals>.checkr   r   r
  r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r   r  r  r   rY   r   test_dominance_frontierM  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   rC   r   backboner   r   r   r   r   test_backbone_looplessd  s   z"TestCFGraph.test_backbone_looplessc                 C   sb   |   }| t| g d |  }| t| dg |  }| t| ddg d S )N)r   rx   r   r   r   r   r1   )r   rC   r   r  r   r   r   r   r   r   test_backbone_loopsj  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   )rx   r   r   rx   r   r   r   )	rx   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )rx   r   r   r   r   )r   r   r   r   )r   r   r   r1   )r1   r   r   r   )r   r   r   )rx   r   r   )r   r   r   r   )r   r   r   rC   r   loopsr   r   headerentriesexitsbodyin_loopsr   r   r   )rF   r   outer1inner1outer2r   loopr   r   r   
test_loopsr  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   @   ri   :      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    
        )r5  r2  r6  r7  r9  r;  r<  r=  r:  r8  r1  )stats)r4  r3  )r:  r7  iteration_count   )r   r  r   r   _find_back_edgesrC   )rF   r   r>  
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   rY   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   )rC   r   r   r   assertNotEqual)rF   rD  r   r   r   r&   ar   rY   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  rC  rG  r   r   r   r   r     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 r9   )r   run)rF   bccfar   r   r   rL  ;  s   zTestRealCodeDomFront.cfac                 C   s2   t |}t|d}| |}| ||}||fS )N)func_id)r   from_functionr   rL  _scan_namedblocks)rF   fnfidrK  rL  namedblocksr   r   r   get_cfa_and_namedblocks@  s
   


z,TestRealCodeDomFront.get_cfa_and_namedblocksc                 C   s   i }t dd | D }d}|D ]H}|jdkrY|j|j }||rY|t|d }t||dd |d d g D ]\}	}
|	|j  krJ|
k rNn 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>Y  s    z:TestRealCodeDomFront._scan_namedblocks.<locals>.<listcomp>
SET_BLOCK_LOAD_GLOBALNr   r   zunreachable loop)
r   
iterblocksopnameco_namesarg
startswithr   ziprT  AssertionError)rF   rK  rL  rR  blocksprefixinstgvnamesrO   blknor   r   r   rO  S  s"   

(z&TestRealCodeDomFront._scan_namedblocksc                 C   s   dd }|  |\}}|j }tdk r | |d ||d   |j }| ||d   | ||d   tdk rJ| |d h||d   d S 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   foom  s   z+TestRealCodeDomFront.test_loop.<locals>.foor1   r   B0B1AC)rS  r   r  r	   rC   r  assertFalserF   rm  rL  blkptsr	  domfrontr   r   r   	test_loopl  s   

zTestRealCodeDomFront.test_loopc                 C   s^  dd }|  |\}}|j }| |d |d  tdk r*| |d ||d   |j }| ||d   | ||d   tdk rR| |d	 h||d
   tdk rf| |d |d h||d   | |d h||d   | |d h||d   tdk r| |d h||d   | |d	 h||d   | |d	 h||d	   d S 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 r9   )rg  rh  ri  SET_BLOCK_C0SET_BLOCK_C1SET_BLOCK_D0SET_BLOCK_D1SET_BLOCK_D2SET_BLOCK_ESET_BLOCK_FSET_BLOCK_G)rk  r   r   r   rm    s   z<TestRealCodeDomFront.test_loop_nested_and_break.<locals>.fooD0C1rn  C0rq  Gro  rp  FD1ED2)rS  r   r  rC   r	   r  rs  rt  r   r   r   test_loop_nested_and_break  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%   r1   )rg  SET_BLOCK_Brx  ry  SET_BLOCK_Dr}  r~  r  )rF  brl  r   r   r   rm    s    z.TestRealCodeDomFront.test_if_else.<locals>.foorq  Br  r  Dr  r  r  rS  r   r  rC   r  rs  rt  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%   )
SET_BLOCK_A0SET_BLOCK_A1rh  ri  rx  ry  SET_BLOCK_C2r  r}  r~  )rF  r   r   r   rm    s   z5TestRealCodeDomFront.test_if_else_nested.<locals>.fooA0A1rp  r  r  r  r  r  r  rt  r   r   r   test_if_else_nested  s    

z(TestRealCodeDomFront.test_if_else_nestedc                 C   s   dd }|  |\}}|j }tdkr| d| n| |d | | |d ||d   | |d ||d   |j }tdk rL| |d | | ||d   | ||d   | |d h||d   | |d h||d   d S )	Nc                   S   s   t  	 tr	t d S t qr9   )rg  r  rj  r  r}  r   r   r   r   rm     s   z4TestRealCodeDomFront.test_infinite_loop.<locals>.foorn  r  r  rr  r  rq  )rS  r   r  r	   assertNotInrC   r  rs  rt  r   r   r   test_infinite_loop  s   	

z'TestRealCodeDomFront.test_infinite_loopN)r   r   r   rH  rL  rS  rO  rw  r  r  r  r  r   r   r   r   rI  /  s    )*%rI  __main__)2rA   unittestnumba.core.controlflowr   r   numba.core.compilerr   r   
numba.corer   numba.core.bytecoder   r   numba.core.utilsr	   numba.tests.supportr
   r   enable_pyobjectforceobj_flagsforce_pyobjectrX   nrtr   r   r   r   r!   r"   r#   r$   r'   r(   r)   r+   r,   r-   r.   r0   r2   r3   r4   r5   r8   r;   r<   r   rI  r   mainr   r   r   r   <module>   sb    		

		

	 "     5 p