o
    ik                     @   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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Z0e1d;kre2  dS dS )<    N)CFGraphControlFlowAnalysis)compile_isolatedFlags)types)FunctionIdentityByteCode_fix_LOAD_GLOBAL_arg)	PYVERSION)TestCaseTc                 C   s   d}t | D ]}||7 }q|S Nr   rangexyresulti r   l/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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_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   r1   c                 C   s$   | |krdS | dks|dkrdS dS )Nr   r   r&      r   r0   r   r   r   ifelse_usecase2   s
   r3   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   r0   r   r   r   ifelse_usecase3   s   r4   c                 C   s   | |krdS d S )Nr   r   r0   r   r   r   ifelse_usecase4   s   r5   c                 C   s   | |krdS dS )NTFr   r0   r   r   r   ternary_ifelse_usecase1      r6   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   r9   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   Zintpentry_point	itertoolsproductr;   assertEqualtypeZfail)selfZpyfuncZ
x_operandsZ
y_operandsr?   crZcfuncr   r   ZpyerrZcerrZpyreseZ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>   )rH   r   rE   r?   r   r   r   test_for_loop1      zTestFlowControl.test_for_loop1c                 C      | j td d S Nr>   )rK   no_pyobj_flagsrE   r   r   r   test_for_loop1_npm   r7   z"TestFlowControl.test_for_loop1_npmc                 C      | j tg dg d|d d S )NrI   r>   )rH   r   rJ   r   r   r   test_for_loop2      
zTestFlowControl.test_for_loop2c                 C   rM   rN   )rS   rO   rP   r   r   r   test_for_loop2_npm  r7   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)rH   r   rJ   r   r   r   test_for_loop3  s   
zTestFlowControl.test_for_loop3c                 C   rM   rN   )rV   rO   rP   r   r   r   test_for_loop3_npm  r7   z"TestFlowControl.test_for_loop3_npmc                 C      | j tdgdg|d d S Nr   r>   )rH   r    rJ   r   r   r   test_for_loop4     zTestFlowControl.test_for_loop4c                 C   rM   rN   )rZ   rO   rP   r   r   r   test_for_loop4_npm  r7   z"TestFlowControl.test_for_loop4_npmc                 C      | j tdgdg|d d S Nd   2   r>   )rH   r"   rJ   r   r   r   test_for_loop5  r[   zTestFlowControl.test_for_loop5c                 C   rM   rN   )ra   rO   rP   r   r   r   test_for_loop5_npm  r7   z"TestFlowControl.test_for_loop5_npmc                 C   r]   r^   )rH   r#   rJ   r   r   r   test_for_loop6  r[   zTestFlowControl.test_for_loop6c                 C   rM   rN   )rc   rO   rP   r   r   r   test_for_loop6_npm  r7   z"TestFlowControl.test_for_loop6_npmc                 C   r]   )N   r   r>   )rH   r$   rJ   r   r   r   test_for_loop7"  r[   zTestFlowControl.test_for_loop7c                 C   rM   rN   )rf   rO   rP   r   r   r   test_for_loop7_npm%  r7   z"TestFlowControl.test_for_loop7_npmc                 C      | j tddgg d|d d S Nr   r   )r   r&   r   r>   )rH   r%   rJ   r   r   r   test_for_loop8(     zTestFlowControl.test_for_loop8c                 C   rM   rN   )rj   rO   rP   r   r   r   test_for_loop8_npm+  r7   z"TestFlowControl.test_for_loop8_npmc                 C   rh   ri   )rH   r(   rJ   r   r   r   test_for_loop9.  rk   zTestFlowControl.test_for_loop9c                 C   rM   rN   )rm   rO   rP   r   r   r   test_for_loop9_npm1  r7   z"TestFlowControl.test_for_loop9_npmc                 C   s   | j tdgddg|d d S )Nre   r&      r>   )rH   r)   rJ   r   r   r   test_for_loop104  rL   zTestFlowControl.test_for_loop10c                 C   rM   rN   )rp   rO   rP   r   r   r   test_for_loop10_npm7  r7   z#TestFlowControl.test_for_loop10_npmc                 C   r]   Nr   r   r>   )rH   r*   rJ   r   r   r   test_while_loop1:  r[   z TestFlowControl.test_while_loop1c                 C   rM   rN   )rs   rO   rP   r   r   r   test_while_loop1_npm=  r7   z$TestFlowControl.test_while_loop1_npmc                 C   r]   rr   )rH   r,   rJ   r   r   r   test_while_loop2@  r[   z TestFlowControl.test_while_loop2c                 C   rM   rN   )ru   rO   rP   r   r   r   test_while_loop2_npmC  r7   z$TestFlowControl.test_while_loop2_npmc                 C   rX   rY   )rH   r-   rJ   r   r   r   test_while_loop3F  r[   z TestFlowControl.test_while_loop3c                 C   rM   rN   )rw   rO   rP   r   r   r   test_while_loop3_npmI  r7   z$TestFlowControl.test_while_loop3_npmc                 C   r]   rr   )rH   r.   rJ   r   r   r   test_while_loop4L  r[   z TestFlowControl.test_while_loop4c                 C   rM   rN   )ry   rO   rP   r   r   r   test_while_loop4_npmO  r7   z$TestFlowControl.test_while_loop4_npmc                 C   rR   )N)r   re   r   r>   )rH   r/   rJ   r   r   r   test_while_loop5R  rk   z TestFlowControl.test_while_loop5c                 C   rM   rN   )r{   rO   rP   r   r   r   test_while_loop5_npmU  r7   z$TestFlowControl.test_while_loop5_npmc                 C   rR   N)r   r   r   r>   )rH   r1   rJ   r   r   r   test_ifelse1X  rk   zTestFlowControl.test_ifelse1c                 C   rM   rN   )r~   rO   rP   r   r   r   test_ifelse1_npm[  r7   z TestFlowControl.test_ifelse1_npmc                 C   rR   r}   )rH   r3   rJ   r   r   r   test_ifelse2^  rk   zTestFlowControl.test_ifelse2c                 C   rM   rN   )r   rO   rP   r   r   r   test_ifelse2_npma  r7   z TestFlowControl.test_ifelse2_npmc                 C   rR   r}   )rH   r4   rJ   r   r   r   test_ifelse3d  rk   zTestFlowControl.test_ifelse3c                 C   rM   rN   )r   rO   rP   r   r   r   test_ifelse3_npmg  r7   z TestFlowControl.test_ifelse3_npmc                 C   rR   r}   )rH   r5   rJ   r   r   r   test_ifelse4j  rk   zTestFlowControl.test_ifelse4c                 C   rM   rN   )r   rO   rP   r   r   r   test_ifelse4_npmm  r7   z TestFlowControl.test_ifelse4_npmc                 C   rR   r}   )rH   r6   rJ   r   r   r   test_ternary_ifelse1p  rT   z$TestFlowControl.test_ternary_ifelse1c                 C   rM   rN   )r   rO   rP   r   r   r   test_ternary_ifelse1_npmt  r7   z(TestFlowControl.test_ternary_ifelse1_npmc                 C   r]   rr   )rH   r9   rJ   r   r   r   test_double_infinite_loopw  s   
z)TestFlowControl.test_double_infinite_loopc                 C   rM   rN   )r   rO   rP   r   r   r   test_double_infinite_loop_npm{  r7   z-TestFlowControl.test_double_infinite_loop_npmN)/__name__
__module____qualname__enable_pyobj_flagsrH   rK   rQ   rS   rU   rV   rW   rZ   r\   ra   rb   rc   rd   rf   rg   rj   rl   rm   rn   rp   rq   rs   rt   ru   rv   rw   rx   ry   rz   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itemsZadd_edge)rE   dr@   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rE   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   ro   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
        ro   r   $      r      %   r   ro   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:
                    ...
        r2      	   r   r2   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
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   rC   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   rC   lenZ
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   ZdescendentsrC   r   r   )rE   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   r2   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_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   listZ	topo_sortrandomshuffle)r   expectedr   rE   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   ro   r   )ro   r   r   r   )ro   r   r   r   )ro   r   r   r   )r   r   )rE   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   )rE   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 r:   r   r   ldomsrE   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   rE   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   ro   )r   ro   r   )r   ro   r   r   )r   ro   r   r   )r   ro   r   r   r   )r   ro   r   )r   ro   r   r   r   )r   r   ro   r   )r   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r   ro   r   r   r   )r   r   r   r   ro   r   r   r   )r   r   r   ro   r   r   r   r   )r   r   ro   r   r   r   r   )r   r   ro   r   r   r   r   r   )r   r   ro   r   )r   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r   ro   r   r   r   )r   r   r   r   ro   r   r   r   )r   r   r   ro   r   r   r   )r   r   r   r   ro   r   r   r   )r   r   r   )r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   rE   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   r:   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   ro   r   r   r   r   r   r   r   )r   r   r   r   ro   )r   r   r   ro   )	r   ro   r   r   r   r   r   r   r   r   )r   ro   r   r   r   r   r   r   r   )r   r   ro   r   r   r   r   )r   ro   r   r   r   r   r   r   r   )r   r   ro   r   r   r   r   r   r   )r   ro   r   r   r   r   r   r   r   )r   ro   r   r   r   r   r   )r   r   r   r   ro   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   r2   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 r:   )Zdominator_treerC   )graphr   ZdomtreerP   r   r   check     z.TestCFGraph.test_dominator_tree.<locals>.check>   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >   r   r   r   r   r   )r   ro   r   r   r   r   r   r   r   r   r   r2   r   r   r   r   setr   r   r   r   r   r   rE   r   r   rP   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   r:   )immediate_dominatorsrC   )r   r   idomsrP   r   r   r   8  r   z4TestCFGraph.test_immediate_dominators.<locals>.checkr   r   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r   )r   r   r   r   r   r   r   r   r   rP   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   r:   )dominance_frontierrC   )r   r   ZdfrP   r   r   r   N  r   z2TestCFGraph.test_dominance_frontier.<locals>.checkr   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r   r   r   r   rP   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   ro   r   r   r   r   r2   )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   )ro   r   r   ro   r   r   r   )	ro   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )ro   r   r   r   r   )r   r   r   r   )r   r   r   r2   )r2   r   r   r   )r   r   r   )ro   r   r   )r   r   r   r   )r   r   r   rC   r   Zloopsr   r   headerentriesZexitsbodyZin_loopsr   r   r   )rE   r   Zouter1Zinner1Z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   @   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_edgesrC   )rE   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   rP   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   ZassertNotEqual)rE   r'  r   r   r   r'   ar   rP   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     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 r:   )r   run)rE   bccfar   r   r   r.  ;  s   zTestRealCodeDomFront.cfac                 C   s2   t |}t|d}| |}| ||}||fS )N)Zfunc_id)r   from_functionr   r.  _scan_namedblocks)rE   fnZfidr-  r.  namedblocksr   r   r   get_cfa_and_namedblocks@  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>Y  s    z:TestRealCodeDomFront._scan_namedblocks.<locals>.<listcomp>Z
SET_BLOCK_LOAD_GLOBALNr   r   zunreachable loop)r   Z
iterblocksopnameco_namesr	   arg
startswithr   zipr4  AssertionError)rE   r-  r.  r2  blocksprefixinstgvnamesrG   Zblknor   r   r   r0  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r2   r   B0B1AC)r3  r   r   r
   rC   r   assertFalserE   rJ  r.  Zblkptsr   Z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 r:   )rD  rE  rF  SET_BLOCK_C0SET_BLOCK_C1ZSET_BLOCK_D0ZSET_BLOCK_D1ZSET_BLOCK_D2SET_BLOCK_ESET_BLOCK_FSET_BLOCK_G)rH  r   r   r   rJ    s   z<TestRealCodeDomFront.test_loop_nested_and_break.<locals>.fooZD0C1rK  C0rN  GrL  rM  FZD1EZD2)r3  r   r   rC   r
   r   rP  rQ  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&   r2   )rD  SET_BLOCK_BrS  rT  SET_BLOCK_DrU  rV  rW  )r(  brI  r   r   r   rJ    s    z.TestRealCodeDomFront.test_if_else.<locals>.foorN  BrY  rX  Dr\  r[  rZ  r3  r   r   rC   r   rP  rQ  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_A1rE  rF  rS  rT  ZSET_BLOCK_C2r_  rU  rV  )r(  r   r   r   rJ    s   z5TestRealCodeDomFront.test_if_else_nested.<locals>.fooZA0A1rM  rY  rb  r\  r[  rX  rc  rQ  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r:   )rD  r^  rG  r_  rU  r   r   r   r   rJ     s   z4TestRealCodeDomFront.test_infinite_loop.<locals>.foorK  r\  ra  rO  rb  rN  )r3  r   r   r
   ZassertNotInrC   r   rP  rQ  r   r   r   test_infinite_loop  s   	

z'TestRealCodeDomFront.test_infinite_loopN)r   r   r   r*  r.  r3  r0  rR  r]  rd  rf  rg  r   r   r   r   r+  /  s    )*%r+  __main__)3rA   ZunittestZnumba.core.controlflowr   r   Znumba.core.compilerr   r   Z
numba.corer   Znumba.core.bytecoder   r   r	   Znumba.core.utilsr
   Znumba.tests.supportr   r   Zenable_pyobjectZforceobj_flagsZforce_pyobjectrO   Znrtr   r   r   r    r"   r#   r$   r%   r(   r)   r*   r,   r-   r.   r/   r1   r3   r4   r5   r6   r9   r<   r=   r   r+  r   mainr   r   r   r   <module>   sb    		

		

	 "     5 p