o
    ۾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)_nrtnopythonc                 C   s   d}t | D ]}||7 }q|S Nr   rangexyresulti r   Q/home/ubuntu/.local/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_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   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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   intp	itertoolsproductr;   assertEqualtypefail)selfpyfunc
x_operands
y_operandsflagscfuncr   r   pyerrcerrpyrese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   rH   )rO   r   rD   rH   r   r   r   test_for_loop1      zTestFlowControl.test_for_loop1c                 C      | j td d S NrQ   )rS   no_pyobj_flagsrD   r   r   r   test_for_loop1_npm   r7   z"TestFlowControl.test_for_loop1_npmc                 C      | j tg dg d|d d S )NrP   rQ   )rO   r   rR   r   r   r   test_for_loop2      
zTestFlowControl.test_for_loop2c                 C   rU   rV   )r[   rW   rX   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&   rQ   N)rO   r   rR   r   r   r   test_for_loop3   s   
zTestFlowControl.test_for_loop3c                 C   rU   rV   )r^   rW   rX   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   rQ   )rO   r    rR   r   r   r   test_for_loop4	     zTestFlowControl.test_for_loop4c                 C   rU   rV   )rb   rW   rX   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   rQ   )rO   r"   rR   r   r   r   test_for_loop5  rc   zTestFlowControl.test_for_loop5c                 C   rU   rV   )ri   rW   rX   r   r   r   test_for_loop5_npm  r7   z"TestFlowControl.test_for_loop5_npmc                 C   re   rf   )rO   r#   rR   r   r   r   test_for_loop6  rc   zTestFlowControl.test_for_loop6c                 C   rU   rV   )rk   rW   rX   r   r   r   test_for_loop6_npm  r7   z"TestFlowControl.test_for_loop6_npmc                 C   re   )N   r   rQ   )rO   r$   rR   r   r   r   test_for_loop7  rc   zTestFlowControl.test_for_loop7c                 C   rU   rV   )rn   rW   rX   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   rQ   )rO   r%   rR   r   r   r   test_for_loop8!     zTestFlowControl.test_for_loop8c                 C   rU   rV   )rr   rW   rX   r   r   r   test_for_loop8_npm$  r7   z"TestFlowControl.test_for_loop8_npmc                 C   rp   rq   )rO   r(   rR   r   r   r   test_for_loop9'  rs   zTestFlowControl.test_for_loop9c                 C   rU   rV   )ru   rW   rX   r   r   r   test_for_loop9_npm*  r7   z"TestFlowControl.test_for_loop9_npmc                 C   s   | j tdgddg|d d S )Nrm   r&      rQ   )rO   r)   rR   r   r   r   test_for_loop10-  rT   zTestFlowControl.test_for_loop10c                 C   rU   rV   )rx   rW   rX   r   r   r   test_for_loop10_npm0  r7   z#TestFlowControl.test_for_loop10_npmc                 C   re   Nr   r   rQ   )rO   r*   rR   r   r   r   test_while_loop13  rc   z TestFlowControl.test_while_loop1c                 C   rU   rV   )r{   rW   rX   r   r   r   test_while_loop1_npm6  r7   z$TestFlowControl.test_while_loop1_npmc                 C   re   rz   )rO   r,   rR   r   r   r   test_while_loop29  rc   z TestFlowControl.test_while_loop2c                 C   rU   rV   )r}   rW   rX   r   r   r   test_while_loop2_npm<  r7   z$TestFlowControl.test_while_loop2_npmc                 C   r`   ra   )rO   r-   rR   r   r   r   test_while_loop3?  rc   z TestFlowControl.test_while_loop3c                 C   rU   rV   )r   rW   rX   r   r   r   test_while_loop3_npmB  r7   z$TestFlowControl.test_while_loop3_npmc                 C   re   rz   )rO   r.   rR   r   r   r   test_while_loop4E  rc   z TestFlowControl.test_while_loop4c                 C   rU   rV   )r   rW   rX   r   r   r   test_while_loop4_npmH  r7   z$TestFlowControl.test_while_loop4_npmc                 C   rZ   )N)r   rm   r   rQ   )rO   r/   rR   r   r   r   test_while_loop5K  rs   z TestFlowControl.test_while_loop5c                 C   rU   rV   )r   rW   rX   r   r   r   test_while_loop5_npmN  r7   z$TestFlowControl.test_while_loop5_npmc                 C   rZ   N)r   r   r   rQ   )rO   r1   rR   r   r   r   test_ifelse1Q  rs   zTestFlowControl.test_ifelse1c                 C   rU   rV   )r   rW   rX   r   r   r   test_ifelse1_npmT  r7   z TestFlowControl.test_ifelse1_npmc                 C   rZ   r   )rO   r3   rR   r   r   r   test_ifelse2W  rs   zTestFlowControl.test_ifelse2c                 C   rU   rV   )r   rW   rX   r   r   r   test_ifelse2_npmZ  r7   z TestFlowControl.test_ifelse2_npmc                 C   rZ   r   )rO   r4   rR   r   r   r   test_ifelse3]  rs   zTestFlowControl.test_ifelse3c                 C   rU   rV   )r   rW   rX   r   r   r   test_ifelse3_npm`  r7   z TestFlowControl.test_ifelse3_npmc                 C   rZ   r   )rO   r5   rR   r   r   r   test_ifelse4c  rs   zTestFlowControl.test_ifelse4c                 C   rU   rV   )r   rW   rX   r   r   r   test_ifelse4_npmf  r7   z TestFlowControl.test_ifelse4_npmc                 C   rZ   r   )rO   r6   rR   r   r   r   test_ternary_ifelse1i  r\   z$TestFlowControl.test_ternary_ifelse1c                 C   rU   rV   )r   rW   rX   r   r   r   test_ternary_ifelse1_npmm  r7   z(TestFlowControl.test_ternary_ifelse1_npmc                 C   re   rz   )rO   r9   rR   r   r   r   test_double_infinite_loopp  s   
z)TestFlowControl.test_double_infinite_loopc                 C   rU   rV   )r   rW   rX   r   r   r   test_double_infinite_loop_npmt  r7   z-TestFlowControl.test_double_infinite_loop_npmN)/__name__
__module____qualname__enable_pyobj_flagsrO   rS   rY   r[   r]   r^   r_   rb   rd   ri   rj   rk   rl   rn   ro   rr   rt   ru   rv   rx   ry   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)rD   dentry_point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rD   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   rw   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
        rw   r   $      r      %   r   rw   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   rA   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   rA   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   rA   len
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   descendentsrA   r   r   )rD   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   r2   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_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   list	topo_sortrandomshuffle)r   expectedr   rD   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   rw   r   )rw   r   r   r   )rw   r   r   r   )rw   r   r   r   )r   r   )rD   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rA   r   )rD   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domsrD   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   rD   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   rw   )r   rw   r   )r   rw   r   r   )r   rw   r   r   )r   rw   r   r   r   )r   rw   r   )r   rw   r   r   r   )r   r   rw   r   )r   r   r   rw   r   )r   r   r   r   rw   r   )r   r   r   rw   r   r   r   )r   r   r   r   rw   r   r   r   )r   r   r   rw   r   r   r   r   )r   r   rw   r   r   r   r   )r   r   rw   r   r   r   r   r   )r   r   rw   r   )r   r   r   rw   r   )r   r   r   r   rw   r   )r   r   r   rw   r   r   r   )r   r   r   r   rw   r   r   r   )r   r   r   rw   r   r   r   )r   r   r   r   rw   r   r   r   )r   r   r   )r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   rD   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   rw   r   r   r   r   r   r   r   )r   r   r   r   rw   )r   r   r   rw   )	r   rw   r   r   r   r   r   r   r   r   )r   rw   r   r   r   r   r   r   r   )r   r   rw   r   r   r   r   )r   rw   r   r   r   r   r   r   r   )r   r   rw   r   r   r   r   r   r   )r   rw   r   r   r   r   r   r   r   )r   rw   r   r   r   r   r   )r   r   r   r   rw   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:   )dominator_treerA   )graphr   domtreerX   r   r   check     z.TestCFGraph.test_dominator_tree.<locals>.check>   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >   r   r   r   r   r   )r   rw   r   r   r   r   r   r   r   r   r   r2   r   r   r   r   setr   r   r   r   r   r   rD   r  r   rX   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_dominatorsrA   )r   r   idomsrX   r   r   r  1  r  z4TestCFGraph.test_immediate_dominators.<locals>.checkr   r   r   r   r   r   r   r   r   r   rw   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   rX   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   r:   )dominance_frontierrA   )r   r   dfrX   r   r   r  G  r  z2TestCFGraph.test_dominance_frontier.<locals>.checkr   r   r
  r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r2   r   r  r  r   rX   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   rA   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   rw   r   r   r   r   r2   )r   rA   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   )rw   r   r   rw   r   r   r   )	rw   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )rw   r   r   r   r   )r   r   r   r   )r   r   r   r2   )r2   r   r   r   )r   r   r   )rw   r   r   )r   r   r   r   )r   r   r   rA   r   loopsr   r   headerentriesexitsbodyin_loopsr   r   r   )rD   r   outer1inner1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   @   rh   :      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_edgesrA   )rD   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   rX   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   )rA   r   r   r   assertNotEqual)rD   rD  r   r   r   r'   ar   rX   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   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 r:   )r   run)rD   bccfar   r   r   rL  4  s   zTestRealCodeDomFront.cfac                 C   s2   t |}t|d}| |}| ||}||fS )N)func_id)r   from_functionr   rL  _scan_namedblocks)rD   fnfidrK  rL  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>
SET_BLOCK_LOAD_GLOBALNr   r   zunreachable loop)r   
iterblocksopnameco_namesr   arg
startswithr   ziprT  AssertionError)rD   rK  rL  rR  blocksprefixinstgvnamesrM   blknor   r   r   rO  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)rS  r   r  assertFalserD   rm  rL  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 r:   )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  GFD1ED2)rS  rA   r   r  rp  rq  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&   r2   )rg  SET_BLOCK_Bru  rv  SET_BLOCK_Drz  r{  r|  )rF  brl  r   r   r   rm    s    z.TestRealCodeDomFront.test_if_else.<locals>.foorn  BC0r~  Dr  r  r  rS  r   r  rA   r  rp  rD   rm  rL  rr  r	  rs  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  ru  rv  SET_BLOCK_C2r  rz  r{  )rF  r   r   r   rm    s   z5TestRealCodeDomFront.test_if_else_nested.<locals>.fooA0A1B1r  r  r  r  r~  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r:   )rg  r  rj  r  rz  r   r   r   r   rm    s   z4TestRealCodeDomFront.test_infinite_loop.<locals>.foor  r  ro  r  rn  )rS  r   r  assertNotInrA   r  rp  r  r   r   r   test_infinite_loop  s   	

z'TestRealCodeDomFront.test_infinite_loopN)r   r   r   rH  rL  rS  rO  rt  r  r  r  r  r   r   r   r   rI  (  s    *%rI  __main__),r?   unittestnumbar   numba.core.controlflowr   r   
numba.corer   numba.core.bytecoder   r   r   numba.tests.supportr	   r   rW   r   r   r   r    r"   r#   r$   r%   r(   r)   r*   r,   r-   r.   r/   r1   r3   r4   r5   r6   r9   r<   r=   r   rI  r   mainr   r   r   r   <module>   sX    
		

		

	 !     5 X