
    .|hd                     ^   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	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z& G d d e      Z' G d! d"e      Z( G d# d$e      Z)e*d%k(  r ejV                          yy)&    N)jit)CFGraphControlFlowAnalysis)types)FunctionIdentityByteCode_fix_LOAD_GLOBAL_arg)TestCaseT)_nrtnopythonc                 4    d}t        |       D ]  }||z  }	 |S Nr   rangexyresultis       \/var/www/html/test/engine/venv/lib/python3.12/site-packages/numba/tests/test_flow_control.pyfor_loop_usecase1r      s(    F1X !M    c                 V    d}t        t        | |d            D ]  \  }}|||z  z  } |S )Nr   )	enumerater   r   r   r   r   js        r   for_loop_usecase2r      s9    F%1b/* 1!a%Mr   c                 *    d}| |g}|D ]  }||z  }	 |S r    )r   r   r   lstr   s        r   for_loop_usecase3r"      s-    Fa&C !Mr   c                 T    d}t        d      D ]  }t        d      D ]  }|dz  }	  |S )Nr   
      r   r   s        r   for_loop_usecase4r&   %   s=    F2Y r 	AaKF	 Mr   c                 D    d}t        |       D ]  }|dz  }||kD  s |S  |S Nr   r%   r   r   s       r   for_loop_usecase5r)   -   s9    F1X !A:M	 Mr   c                 @    d}t        |       D ]  }||kD  r	|dz  } |S r(   r   r   s       r   for_loop_usecase6r+   6   s4    F1X q5! Mr   c                 L    t        |       D ]  }d} t        |       D ]  }  y  yr(   r   )r   r   r   r   s       r   for_loop_usecase7r-   ?   s:    1X q 	A	  r   c                 D    d}t        | ||| z
  dz         D ]  }|dz  }	 |S r(   r   r   s       r   for_loop_usecase8r/   I   s5    F1aQ# !Mr   c                 v    d}t        |       D ](  }d} t        |       D ]  }|| dz  k(  s||z  } # ||z  }* |S )Nr      r   )r   r   zr   r   s        r   for_loop_usecase9r3   P   s[    	A1X q 	AAEzQ	
 FA Hr   c                 D    t        |       D ]  }||k(  s	|} |S  dz  }|S )Nr1   r   )r   r   r   r2   s       r   for_loop_usecase10r5   ^   s;    1X 6A H
 EHr   c                 8    d}d}|| k  r||z  }|dz  }|| k  r|S r(   r    r   s       r   while_loop_usecase1r7   h   s5    F	A
a%!	Q a% Mr   c                 *    d}|| k7  r|dz  }|| k7  r|S r(   r    r   r   r   s      r   while_loop_usecase2r:   q   s%    F
A+! A+Mr   c                 b    d}d}d}|| k  r#||k  r|||z   z  }|dz  }|dz  }||k  r|| k  r#|S r(   r    r   s        r   while_loop_usecase3r<   x   sW    F	A	A
a%!ea!eOFFAFA !e a%
 Mr   c                 $    d}	 |dz  }|| kD  r	 |S r(   r    r9   s      r   while_loop_usecase4r>      s'    F
!A:M	 r   c                 @    d}|| k  r||kD  r|dz  }|dz  }|| k  r|S )Nr   r1   r%   r    r9   s      r   while_loop_usecase5r@      s:    F
1*A:aKF!	 1*
 Mr   c                 "    | dkD  r	 y|dkD  r	 y	 y)Nr   Tr    r   r   s     r   ifelse_usecase1rC      s*    1u
 	 
Q  	r   c                 &    | |kD  ry| dk(  s|dk(  ryy)Nr%   r   r1      r    rB   s     r   ifelse_usecase2rF      s    1u	
a16r   c                 4    | dkD  r|dkD  ry|dk  ryy| dk  ryyr(   r    rB   s     r   ifelse_usecase3rH      s-    1uq5U	
Qr   c                     | |k(  ryy Nr%   r    rB   s     r   ifelse_usecase4rK      s    Av r   c                     | |kD  rdS dS )NTFr    rB   s     r   ternary_ifelse_usecase1rM      s    q54#e#r   c                 L    | }|}	 	 ||dz
  k(  rn|dz  }|dz  }||k\  r	 ||fS  rJ   r    )r   r   Lr   s       r   double_infinite_looprP      sQ    	A	A
AEzFA  	
Q6a4K r   c                  $    	 y # t         $ r Y y w xY wN)	Exceptionr    r   r   try_except_usecaserT      s     s   	c                   f   e Zd ZefdZefdZd ZefdZd ZefdZ	d Z
efdZd	 Zefd
Zd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd Z efdZ!d Z"efd Z#d! Z$efd"Z%d# Z&efd$Z'd% Z(efd&Z)d' Z*efd(Z+d) Z,efd*Z-d+ Z.y,)-TestFlowControlc                     t        t        j                  t        j                  ffi ||      }t        j                  ||      D ]E  \  }}d }d }		  |||      }
	  |||      }|| j                  d|z          | j                  
|       G y # t
        $ r}|}Y d }~Ed }~ww xY w# t
        $ r3}| |}	| j                  t        |      t        |	             Y d }~d }~ww xY w)Nz(Invalid for pure-python but numba works
)	r   r   intp	itertoolsproductrS   failassertEqualtype)selfpyfunc
x_operands
y_operandsflagscfuncr   r   pyerrcerrpyresecress                r   run_testzTestFlowControl.run_test   s    6UZZ,66v>%%j*= 	.DAqEDq!.Q{ $III#$ %  -'	.
  
  :=  ed4j99	:s0   	B	B(	B%B  B%(	C$1)CC$c                 <    | j                  t        g ddg|       y )Nir   r$   r   rb   )ri   r   r^   rb   s     r   test_for_loop1zTestFlowControl.test_for_loop1   s    's%Hr   c                 0    | j                  t               y Nrl   )rn   no_pyobj_flagsr^   s    r   test_for_loop1_npmz"TestFlowControl.test_for_loop1_npm       .1r   c                 >    | j                  t        g dg d|       y )Nrk   rl   )ri   r   rm   s     r   test_for_loop2zTestFlowControl.test_for_loop2   s    '|! 	 	#r   c                 0    | j                  t               y rp   )rv   rq   rr   s    r   test_for_loop2_npmz"TestFlowControl.test_for_loop2_npm   rt   r   c                 :    | j                  t        dgdg|       y)z(
        List requires pyobject
        r%   r1   rl   N)ri   r"   rm   s     r   test_for_loop3zTestFlowControl.test_for_loop3   s#     	'!qc! 	 	#r   c                 0    | j                  t               y rp   )rz   rq   rr   s    r   test_for_loop3_npmz"TestFlowControl.test_for_loop3_npm  rt   r   c                 :    | j                  t        dgdg|       y Nr$   rl   )ri   r&   rm   s     r   test_for_loop4zTestFlowControl.test_for_loop4	  s    '"t5Ar   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop4_npmz"TestFlowControl.test_for_loop4_npm  rt   r   c                 :    | j                  t        dgdg|       y Nd   2   rl   )ri   r)   rm   s     r   test_for_loop5zTestFlowControl.test_for_loop5      '#EBr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop5_npmz"TestFlowControl.test_for_loop5_npm  rt   r   c                 :    | j                  t        dgdg|       y r   )ri   r+   rm   s     r   test_for_loop6zTestFlowControl.test_for_loop6  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop6_npmz"TestFlowControl.test_for_loop6_npm  rt   r   c                 :    | j                  t        dgdg|       y )N   r   rl   )ri   r-   rm   s     r   test_for_loop7zTestFlowControl.test_for_loop7  s    '!qc?r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop7_npmz"TestFlowControl.test_for_loop7_npm  rt   r   c                 >    | j                  t        ddgg d|       y Nr   r%   )r   r1   r$   rl   )ri   r/   rm   s     r   test_for_loop8zTestFlowControl.test_for_loop8!      '!Q5Ir   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop8_npmz"TestFlowControl.test_for_loop8_npm$  rt   r   c                 >    | j                  t        ddgg d|       y r   )ri   r3   rm   s     r   test_for_loop9zTestFlowControl.test_for_loop9'  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop9_npmz"TestFlowControl.test_for_loop9_npm*  rt   r   c                 <    | j                  t        dgddg|       y )Nr   r1      rl   )ri   r5   rm   s     r   test_for_loop10zTestFlowControl.test_for_loop10-  s    (1#1vUCr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_for_loop10_npmz#TestFlowControl.test_for_loop10_npm0  s    >2r   c                 :    | j                  t        dgdg|       y Nr$   r   rl   )ri   r7   rm   s     r   test_while_loop1z TestFlowControl.test_while_loop13      )B4!EBr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_while_loop1_npmz$TestFlowControl.test_while_loop1_npm6      N3r   c                 :    | j                  t        dgdg|       y r   )ri   r:   rm   s     r   test_while_loop2z TestFlowControl.test_while_loop29  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_while_loop2_npmz$TestFlowControl.test_while_loop2_npm<  r   r   c                 :    | j                  t        dgdg|       y r~   )ri   r<   rm   s     r   test_while_loop3z TestFlowControl.test_while_loop3?  s    )B4"UCr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_while_loop3_npmz$TestFlowControl.test_while_loop3_npmB  r   r   c                 :    | j                  t        dgdg|       y r   )ri   r>   rm   s     r   test_while_loop4z TestFlowControl.test_while_loop4E  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_while_loop4_npmz$TestFlowControl.test_while_loop4_npmH  r   r   c                 >    | j                  t        g dg d|       y )N)r   r   r$   rl   )ri   r@   rm   s     r   test_while_loop5z TestFlowControl.test_while_loop5K  s    ):zOr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_while_loop5_npmz$TestFlowControl.test_while_loop5_npmN  r   r   c                 >    | j                  t        g dg d|       y N)r   r   r%   rl   )ri   rC   rm   s     r   test_ifelse1zTestFlowControl.test_ifelse1Q      oz:UKr   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_ifelse1_npmz TestFlowControl.test_ifelse1_npmT      /r   c                 >    | j                  t        g dg d|       y r   )ri   rF   rm   s     r   test_ifelse2zTestFlowControl.test_ifelse2W  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_ifelse2_npmz TestFlowControl.test_ifelse2_npmZ  r   r   c                 >    | j                  t        g dg d|       y r   )ri   rH   rm   s     r   test_ifelse3zTestFlowControl.test_ifelse3]  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_ifelse3_npmz TestFlowControl.test_ifelse3_npm`  r   r   c                 >    | j                  t        g dg d|       y r   )ri   rK   rm   s     r   test_ifelse4zTestFlowControl.test_ifelse4c  r   r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_ifelse4_npmz TestFlowControl.test_ifelse4_npmf  r   r   c                 >    | j                  t        g dg d|       y r   )ri   rM   rm   s     r   test_ternary_ifelse1z$TestFlowControl.test_ternary_ifelse1i  s    -z:! 	 	#r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_ternary_ifelse1_npmz(TestFlowControl.test_ternary_ifelse1_npmm  s    !!!7r   c                 :    | j                  t        dgdg|       y r   )ri   rP   rm   s     r   test_double_infinite_loopz)TestFlowControl.test_double_infinite_loopp  s!    *RD1#! 	 	#r   c                 0    | j                  t               y rp   )r   rq   rr   s    r   test_double_infinite_loop_npmz-TestFlowControl.test_double_infinite_loop_npmt  s    &&^&<r   N)/__name__
__module____qualname__enable_pyobj_flagsri   rn   rs   rv   rx   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   r   r   r   r   r   r   r   r   r   r   r    r   r   rV   rV      sY    *.0 $6 I2 $6 #2 $6 #2 $6 B2 $6 C2 $6 C2 $6 @2 $6 J2 $6 J2 %7 D3 &8 C4 &8 C4 &8 D4 &8 C4 &8 P4 "4 L0 "4 L0 "4 L0 "4 L0 *< #8 /A #=r   rV   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy) TestCFGraphz3
    Test the numba.controlflow.CFGraph class.
    c                     t               }|D ]  }|j                  |        |j                         D ]  \  }}|D ]  }|j                  ||          |S )zG
        Build a CFGraph class from a dict of adjacency lists.
        )r   add_nodeitemsadd_edge)r^   dentry_pointgnodedestsdests          r   from_adj_listzTestCFGraph.from_adj_list}  sc     I 	DJJt	779 	'KD% '

4&'	' r   c                 |    | j                  ddgdgdgg d      }|j                  d       |j                          |S )z
        A simple CFG corresponding to the following code structure:

            c = (... if ... else ...) + ...
            return b + c
                 r   r   r   r   r   r   set_entry_pointprocessr^   r   s     r   	loopless1zTestCFGraph.loopless1  sA     B8"B4RHI	!			r   c                     | j                  ddgdgdgg ddgddgdgg d      }|j                  d       |j                          |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   s     r   loopless1_dead_nodesz TestCFGraph.loopless1_dead_nodes  sa    
 Rab	 	
!			r   c           	          | j                  ddgdgdgddgg g d      }|j                  d       |j                          |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   s     r   	loopless2zTestCFGraph.loopless2  sM     bt"B8LN	"			r   c                     | j                  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       }|j                  d       |j                          |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   r   r$   <         8          ,   9   =   D   W   G   P   X   r   r   s     r   multiple_loopszTestCFGraph.multiple_loops  s$      (A3  ( !B8 ( "RD ( !#RD ( !#RH	 (
 !#RH ( !#RD ( !#RD ( !#RD ( !#QC ( !#RD ( !#RD ( !#RH ( !#RH ( !#B ( !#RD (  !#B! ( !$ 	
!			r   c                     | j                  dgddgddgg ddgdgdgg d      }|j                  d	       |j                          |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
        r   r$   $      r      %   r   r   r$   r  r   r  r  r  r   r   r   s     r   multiple_exitszTestCFGraph.multiple_exits  sa     Rba	 	
!			r   c           	          | j                  ddgg dgddgdgdgd      }|j                  d       |j                          |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   s     r   infinite_loop1zTestCFGraph.infinite_loop1  sO     QBRDr2hRDrdKM	!			r   c                 ~    | j                  dgddgdgdgd      }|j                  d       |j                          |S )z
        A CFG with no exit point at all:

            while True:
                if a:
                    ...
                else:
                    ...
        rE      	   r   rE   r  r  r   r   r   s     r   infinite_loop2zTestCFGraph.infinite_loop2  sC     A3B7sDE	!			r   c                 |   | j                         }| j                  t        |j                  d            ddg       | j                  t        |j                  d            g        | j                  t        |j	                  d            g        | j                  t        |j	                  d            ddg       y )Nr   )r   N)r   Nr   )r   r\   sorted
successorspredecessorsr   s     r   test_simple_propertiesz"TestCFGraph.test_simple_properties  s    NNQ0:z2JKR 0126q 12B7r 23j*5MNr   c                 4   | j                         }| j                  t        |j                               dg       | j	                         }| j                  t        |j                               dg       | j                         }| j                  t        |j                               ddg       | j                         }| j                  t        |j                               ddg       | j                         }| j                  t        |j                               dg       | j                         }| j                  t        |j                               g        | j                         }| j                  t        |j                               ddg       y )	Nr   r   r   r  r	  r  r  r  )
r   r\   r  exit_pointsr   r   r
  r  r  r  r   s     r   test_exit_pointszTestCFGraph.test_exit_points#  s/   NN02$7%%'02$7NN02r(;!02r(;!01#6!0"5!02r(;r   c                    | j                         }| j                  t        |j                               d       | j                  t	        |j                               g d       | j                         }| j                  t        |j                               d       | j                  t	        |j                               g d       | j                         }| j                  t        |j                               d       | j                         }| j                  t        |j                               d       | j                         }| j                  t        |j                               d       | j                         }| j                  t	        |j                               g d       | j                  t	        |j                               g d       y )Nr   r   r   r   r   r   r   r   )r   r   r   r   )r   r\   len
dead_nodesr  nodesr   r
  r  r  r   r   s     r   test_dead_nodeszTestCFGraph.test_dead_nodes3  sQ   NNQ\\^,a0	*(	*NNQ\\^,a0	*1	3!Q\\^,a0!Q\\^,a0!Q\\^,a0%%'/)	+	*(	*r   c                    | j                         }|j                  d      }| j                  t        |      g        |j                  d      }| j                  t        |      g        |j                  d      }| j                  t        |      ddg       |j                  d      }| j                  t        |      g d       | j	                         }|j                  d      }| j                  t        |      g        |j                  d      }| j                  t        |      g        |j                  d      }| j                  t        |      ddg       |j                  d	      }| j                  t        |      g d
       |j                  d      }| j                  t        |      g        |j                  d      }| j                  t        |      g d       y )Nr   r   r   r   )r   r   r   r   r   r  r  r   r$   r   r  r  r  r   )r  r$   r   r  r  )r   descendentsr\   r  r  )r^   r   r   s      r   test_descendentszTestCFGraph.test_descendentsI  sz   NNMM"B'MM"B'MM"RH-MM"$89!MM"B'MM"B'MM"RH-MM"L1MM!B'MM!$78r   c                    | j                         }| j                  |j                         g dg df       | j                         }| j                  |j                         g dg df       | j	                         }| j                  |j                         g dg df       | j                         }| j                  |j                         g dg dg d	g d
f       y )Nr   )r   r   r   r   )r   r   r   r   r   r   r   r  )r   rE   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   s     r   test_topo_orderzTestCFGraph.test_topo_ordera  s    NNalln&8	:NNalln/1IJ	L!alln$m4	6!alln-/E-/EG	Hr   c                       fd} j                          |g dg df        |g dg dg df        j                          |g dg dg d	g d
f       y )Nc           
      |   j                  t        j                  |             |       j                  t        j                  | d d d               |       j                  t        j                  | d            d d d   |       j                  t        j                  | d d d   d            d d d   |       j                  j	                  |        j                  t        j                  |             |       j                  t        j                  | d            d d d   |       y )Nr   T)reverse)r/  list	topo_sortrandomshuffle)r(  expectedr   r^   s     r   check_topo_sortz3TestCFGraph.test_topo_sort.<locals>.check_topo_sortq  s    MM$q{{512H=MM$q{{52;78(CMM$q{{5${?@2F"$MM$q{{52;{EFttL"$KK&MM$q{{512H=MM$q{{5${?@2F"$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  )r   r  r$   r  )r   r  )r^   r:  r   s   ` @r   test_topo_sortzTestCFGraph.test_topo_sortp  s\    
	$ NN(+;*=>()+;<	>!(/?K	Mr   c                     | j                  t        |      t        |             t        |      D ]0  }| j                  t        ||         t        ||         d|       2 y )Nzmismatch for r\   r  )r^   gotr9  r   s       r   check_dominatorszTestCFGraph.check_dominators  sX    fX&673K 	:DVCI.x~0F268:	:r   c                      fd} j                          j                         fD ]=  }|j                          |ddg        |dddg        |dddg        |dddg       ?  j                         }|j                          |ddg        |dddg        |dddg        |dddg        |dg d        |d	g d
       y )Nc                 B    j                  t        |          |       y rR   r=  r   ldomsr^   s     r   eq_z1TestCFGraph.test_dominators_loopless.<locals>.eq_      VDG_a0r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   )r   r   
dominatorsr   r^   rE  r   rD  s   `  @r   test_dominators_looplessz$TestCFGraph.test_dominators_loopless  s    	1.."D$=$=$?@ 	A<<>DA3KQGQGQG	 NN||~BBRBRBRBBr   c                 ,   | j                         }|j                         }| j                  |dgddgg dg dg dg dg dg dd	       | j                         }|j                         }| j                  |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'       | j	                         }|j                         }| j                  |dgdd(gdd
gg d)g d*g d+d,       y )-Nr   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   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   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  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$   r   r  r  )r  rG  r?  r
  r  r^   r   rD  s      r   test_dominators_loopsz!TestCFGraph.test_dominators_loops  s   !||~d#$##$a&#-#1#1#5#-#-!		" !||~d!q1# ! 1a&!!:!  ">!  "#5	!
  "#9!  "#=!  "#=!  "#9!  "#=!  ":!  ">!  "#5!  "#9!  "#=!  "#9!   "#=!!	"& !||~d#$##$a&$%r7#.#2#2!	"r   c                      fd} j                          j                         fD ]=  }|j                          |dddg        |dddg        |dddg        |ddg       ?  j                         }|j                          |ddg        |ddg        |ddg        |dddg        |dddg        |dddg       y )	Nc                 B    j                  t        |          |       y rR   r=  rB  s     r   rE  z6TestCFGraph.test_post_dominators_loopless.<locals>.eq_  rF  r   r   r   r   r   r   r   r   )r   r   post_dominatorsr   rH  s   `  @r   test_post_dominators_looplessz)TestCFGraph.test_post_dominators_loopless  s    	1.."D$=$=$?@ 	A$$&DAr7ORHRHRDM	 NN  "BBBBRBRBRr   c                    | j                         }|j                         }| j                  |ddgdgdgdgdgddgddgdgd	       | j                         }|j                         }| j                  |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       y )"Nr   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  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   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  rO  r?  r
  rK  s      r   test_post_dominators_loopsz&TestCFGraph.test_post_dominators_loops  sr   !  "d#$a&#$#$&4$&4$&4$&8$&8$&4!		" !  "d!q"4 ! /!!#F!  "#B!  "#>	!
  "#B!  "#B!  "#B!  "#:!  "#6!  "<!  "B8!  "B4!  "B4!  "B4!  "B8!   "B4!!	"r   c           
          | j                         }|j                         }| j                  |dgdgddgdgdgdgd       | j                         }|j                         }| j                  |ddgdgd	gd
gd       y )Nr   r  r$   r   r  r  r  rE   r  r  r  )r  rO  r?  r  rK  s      r   #test_post_dominators_infinite_loopsz/TestCFGraph.test_post_dominators_infinite_loops   s     !  "d#$##$#$&8$&4$&4$&4!	" !  "d#$a&#$##$#$&4!	"r   c                      fd} | j                         h dt               t               t               d        | j                         t               t               ddht               t               h dd        | j                         h dt               t               t               d        | j	                         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                       | j                         dhh dt               ddhdht               t               t               d        | j                         d	dht               dhddht               t               d        | j                         dhd d!ht               t               d"       y )#Nc                 J    | j                         }j                  ||       y rR   )dominator_treer\   )graphr9  domtreer^   s      r   checkz.TestCFGraph.test_dominator_tree.<locals>.check  s!    **,GWh/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  r$   r   r  r  r  r  r  r  rE   r  r  r  r   setr   r   r
  r  r  r  r^   rZ  s   ` r   test_dominator_treezTestCFGraph.test_dominator_tree  s   	0 	dnnCEsu#%@	Bdnn5ce"bsu#%!	" 	d'')CEsu#%@	Bd!!#/q1# /q2r( /B /b2$ /RH /B4/SU/$&/.0#%/9;bT/B4/b"X/')B8/57/ 35/ rd/ %'/	0
 	d!!##,CEBx4SU35:	; 	d!!#q'ce"B85	 	d!!##2q'ce7	9r   c                      fd} | j                         ddddd        | j                         ddddddd        | j                         ddddd        | j                         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        | j	                         ddddddddd        | j                         ddddd	d	d        | j                         ddddd       y )Nc                 J    | j                         }j                  ||       y rR   )immediate_dominatorsr\   )rX  r9  idomsr^   s      r   rZ  z4TestCFGraph.test_immediate_dominators.<locals>.check1  s!    ..0EUH-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  rE   r  )r   r   r   r
  r  r  r  r]  s   ` r   test_immediate_dominatorsz%TestCFGraph.test_immediate_dominators0  s   	. 	dnnq)	+dnn22222>	@d'')q)	+d!!#7q! 7Q 72q 7"b 7"b 7"b 72727!27')27/11768"72727!27')27/127	8 	d!!#qaRRRQAF	Hd!!#qbbb9	;d!!#aQ'	)r   c                      fd} | j                         t               dhdht               d        | j                         dhdht               t               t               t               d        | j                         t               dhdht               d        | j	                         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                       | j                         t               dhddht               ddhdhdht               d        | j                         t               t               t               dhdhdhd        | j                         t               dhdhdhd       y )Nc                 J    | j                         }j                  ||       y rR   )dominance_frontierr\   )rX  r9  dfr^   s      r   rZ  z2TestCFGraph.test_dominance_frontier.<locals>.checkG  s!    ))+BR*r   r   r   rc  r   r   r$   r   r   r   r  r  r   r  r   r  r  r  r  r  r	  r  r  r  rE   r  r[  r]  s   ` r   test_dominance_frontierz#TestCFGraph.test_dominance_frontierF  s   	+ 	dnn%bTt7	9dnn4bTsu#%SUN	Pd'')%bTt7	9d!!#Dq#% DQC DaS D"qc D2Aw DRD DB4DbTD#%sD,.D57D@BCEDB4DbTD#%suD.0#%D9;SUD	E 	d!!#%QCb!W#%72$CE;	< 	d!!#%CEsu2$RDrdK	Md!!#%QCQCaS1	3r   c                    | j                         | j                         fD ]-  }| j                  t        |j	                               ddg       / | j                         }| j                  t        |j	                               ddg       y )Nr   r   r   )r   r   r\   r  backboner   r   s     r   test_backbone_looplessz"TestCFGraph.test_backbone_loopless]  sp    .."D$=$=$?@ 	<AVAJJL1Ar7;	<NN

-Bx8r   c                 d   | j                         }| j                  t        |j                               g d       | j	                         }| j                  t        |j                               dg       | j                         }| j                  t        |j                               ddg       y )N)r   r   r   r  r  r   rE   )r
  r\   r  rk  r  r  r   s     r   test_backbone_loopszTestCFGraph.test_backbone_loopsc  s    !

-/AB!

-s3!

-1v6r   c                 V   | j                         | j                         | j                         fD ]+  }| j                  t	        |j                               d       - | j                         }| j                  t        |j                               g d       |j                         d   }|j                         d   }|j                         d   }| j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        dg       | j                  t        |j                        g d       | j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        d	g       | j                  t        |j                        g d
       | j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        ddg       | j                  t        |j                        ddg       dD ]#  }| j                  |j                  |      g        % dD ]$  }| j                  |j                  |      |g       & d
D ]%  }| j                  |j                  |      ||g       ' dD ]$  }| j                  |j                  |      |g       & | j                         }| j                  t        |j                               dg       |j                         d   }| j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        g        | j                  t        |j                        g d       dD ]#  }| j                  |j                  |      g        % dD ]$  }| j                  |j                  |      |g       & | j                         }| j                  t        |j                               dg       |j                         d   }| j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        g        | j                  t        |j                        g d       dD ]#  }| j                  |j                  |      g        % dD ]$  }| j                  |j                  |      |g       & | j                         }| j                  t        |j                               dg       |j                         d   }| j                  |j                  d       | j                  t        |j                        dg       | j                  t        |j                        g d       | j                  t        |j                        g d       dD ]#  }| j                  |j                  |      g        % dD ]$  }| j                  |j                  |      |g       & y )Nr   )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   r$   r   r   r  )r  r  r$   r+  )r   r  r$   rE   )rE   r  r  r   )r  r  r  )r   r$   r   )r   r  r  r  )r   r   r   r\   r&  loopsr
  r  headerentriesexitsbodyin_loopsr  r  r  )r^   r   outer1inner1outer2r   loops          r   
test_loopszTestCFGraph.test_loopsk  sO   .."D$=$=$?.."$ 	0AS^Q/	0 !	*K8122*/!5-t4,<	>+/"6-t4,.>?+/"6-Bx8,r2h7+ 	3DQZZ-r2	3' 	9DQZZ-x8	9$ 	ADQZZ-/?@	A 	9DQZZ-x8	9 !	*RD1wwy}b)-t4

+R0		*L9 	3DQZZ-r2	3  	7DQZZ-v6	7 !	*QC0wwy|a(-s3

+R0		*J7 	3DQZZ-r2	3 	7DQZZ-v6	7 !	*QC0wwy|a(-s3

+\:		*K8# 	3DQZZ-r2	3 	7DQZZ-v6	7r   c                    | j                  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?      }|j                  d       |j                          i }|j	                  |@      }| j                  |dAdBh       | j                  |dC   dD       y )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  iteration_count   )r   r\  r   r   _find_back_edgesr\   )r^   r   r  
back_edgess       r   test_loop_dfs_pathologicalz&TestCFGraph.test_loop_dfs_pathological  s    ? 
Bx? 
R? 
 R? 
 R	? 

 ? 
 R? 
 R? 
 ? 
 R? 
 R? 
 ? 
 b	? 
 c
? 
 #? 
 #s? 
  #s!? 
" ##? 
$ #s%? 
& #'? 
( #s)? 
* #+? 
, #s-? 
. #/? 
0 #s1? 
2 #3? 
4 #s5? 
6 #7? 
8 #9? 
: #s;? 
< #=? 
> #?? 
@ #sA? 
B #C? 
D #sE? 
F #sG? 
H #I? 
J #sK? 
L #sM? 
N O? 
P #sQ? 
R #S? 
T #sU? 
V #W? 
X #Y? 
Z #s[? 
\ #s]? 
^ #s_? 
` #sa? 
b #c? 
d #e? 
f #g? 
h ssss}? 
 ?@ 	
!			''e'4
j*%=>0137r   c                      fd} |       } |       } j                  ||        j                  ddgdgg dgd      }|j                  d       |j                           j                  ||        |       }|j                  d       |j                           j	                  ||        j                  ddgdgdgdgg d      }|j                  d       |j                           j	                  ||        j                  ddgdgg d	      }|j                  d       |j                           j                  ddgdgg d	      }|j                  d       |j                           j	                  ||       y )
Nc                  ~    j                  ddgdgdgg d      } | j                  d       | j                          | S )Nr   r   r   r   r   r   )r   r^   s    r   get_newz(TestCFGraph.test_equals.<locals>.get_new  s@    ""BxbTt#LMAa IIKHr   r   r   r   )r   r   r   r   r   r  )r   r   r   r   r  )r   r   r   )r\   r   r   r   assertNotEqual)r^   r  r   r   r   r2   as   `      r   test_equalszTestCFGraph.test_equals  sq   	 II 	A B8"2B4HI	!			A I	"			Aq! B8"B4bT$& ( )	!			Aq! B8!"=>	!			B8"2>?	!			Aq!r   Nrp  ) r   r   r   __doc__r   r   r   r   r
  r  r  r  r   r#  r)  r-  r1  r;  r?  rI  rL  rP  rR  rT  r^  rd  ri  rl  rn  r{  r  r  r    r   r   r   r   x  s    
&&#J4$O< *,90HM,:$+"Z$!"F",94),3.97F7PH8T("r   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)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                 <    t        |      }|j                          |S rR   )r   run)r^   bccfas      r   r  zTestRealCodeDomFront.cfa4  s    !"%	
r   c                     t        j                  |      }t        |      }| j                  |      }| j	                  ||      }||fS )N)func_id)r   from_functionr   r  _scan_namedblocks)r^   fnfidr  r  namedblockss         r   get_cfa_and_namedblocksz,TestRealCodeDomFront.get_cfa_and_namedblocks9  sH    ,,R0c"hhrl,,R5 Kr   c                    i }t        |j                         D cg c]  }|j                   c}      }d}|D ]  }|j                  dk(  s|j                  t        |j                           }|j                  |      sG|t        |      d }	t        ||dd |d   dz   gz         D ]  \  }
}|
|j                  cxk  r|k  sn  n t        d      |
}|||	<    |S c c}w )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.
        
SET_BLOCK_LOAD_GLOBALNr%   r   zunreachable loop)r  
iterblocksoffsetopnameco_namesr	   arg
startswithr&  zipAssertionError)r^   r  r  r  r   blocksprefixinstgvnamesrg   blknos                r   r  z&TestRealCodeDomFront._scan_namedblocksL  s    
 3>>+;<a<= 	.D{{m+[[!5dhh!?@==(c&kl+D #FF12J&*q.9I,I J A1/a/!A --?@@E(-K%	. # =s   Cc                     d }| j                  |      \  }}|j                  j                         }| j                  ||d             | j                  ||d             y )Nc                 `    d}t          d}t        rt         |dz  }|dz  }t        rt         |S r(   )SET_BLOCK_ASET_BLOCK_B0SET_BLOCK_B1SET_BLOCK_C)ncr   s      r   fooz+TestRealCodeDomFront.test_loop.<locals>.foof  s6    AAQQ  Hr   AC)r  rX  rg  assertFalser^   r  r  blkptsdomfronts        r   	test_loopzTestRealCodeDomFront.test_loope  s_    		 2237V
 99//1&+./&+./r   c                 b   d }| j                  |      \  }}| j                  |d   |d          |j                  j                         }| j	                  ||d             | j	                  ||d             | j                  |d   h||d             | j                  |d   h||d	             y )
Nc                     t          t        rAt         t        r(t         t
        rt         n| rt         t         t        r(t         t        rAt         y rR   )r  r  r  SET_BLOCK_C0SET_BLOCK_C1SET_BLOCK_D0SET_BLOCK_D1SET_BLOCK_D2SET_BLOCK_ESET_BLOCK_FSET_BLOCK_G)r  s    r   r  z<TestRealCodeDomFront.test_loop_nested_and_break.<locals>.foo{  s7    " #$$ #   r   D0C1r  GFD1ED2)r  r\   rX  rg  r  r  s        r   test_loop_nested_and_breakz/TestRealCodeDomFront.test_loop_nested_and_breakz  s    	 2237Vvd|4 99//1&+./&+./ 	&+(>?&+(>?r   c                    d }| j                  |      \  }}|j                  j                         }| j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d	             |j                  j	                         }| j                  ||d             | j                  ||d             | j                  ||d	             | j                  |d   h||d             | j                  |d   h||d             | j                  |d   h||d             | j                  |d   h||d             | j                  |d	   h||d             y )
Nc                     d}t          | |k  r	t         d}nt        r	t         d}nt         d}t
         | |z  dk(  rt         |dz  }t         |S )Nr   r%   r1   rE   )r  SET_BLOCK_Br  r  SET_BLOCK_Dr  r  r  )r  br  s      r   r  z.TestRealCodeDomFront.test_if_else.<locals>.foo  sN    A1u1uzQHr   r  BC0r  Dr  r  r  r  rX  ra  r\   rg  r  r^   r  r  r  rb  r  s         r   test_if_elsez!TestRealCodeDomFront.test_if_else  s   	( 2237V		..0eF3K&89eF4L&9:uVD\':;uVC['9:eF3K&89eF3K&89eF3K&8999//1&+./&+./&+./&+(=>&+(>?&+(>?&+(=>&+(=>r   c                    d }| j                  |      \  }}|j                  j                         }| j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             | j                  |d   ||d             |j                  j	                         }| j                  ||d             | j                  ||d             | j                  |d   h||d             | j                  |d   h||d	             | j                  |d   h||d             | j                  |d   h||d             y )
Nc                      t         r8t         t        r	t         d} nt        r	t
         d} nt         d} t         t         t          S )Nr   r%   r1   )
SET_BLOCK_A0SET_BLOCK_A1r  r  r  r  SET_BLOCK_C2r  r  r  )r  s    r   r  z5TestRealCodeDomFront.test_if_else_nested.<locals>.foo  s4     A#$$Hr   A0A1B1r  r  r  r  r  r  r  s         r   test_if_else_nestedz(TestRealCodeDomFront.test_if_else_nested  s   	$ 2237V		..0uVD\':;uVD\':;uVD\':;uVC['9:uVC['9:uVC['9:99//1&,/0&+./&+(>?&+(>?&+(=>&+(=>r   c                    d }| j                  |      \  }}|j                  j                         }| j                  d|       | j	                  |d   ||d             | j	                  |d   ||d             |j                  j                         }| j                  ||d             | j                  ||d             | j	                  |d   h||d             | j	                  |d   h||d             y )Nc                  8    t          	 t        rt         y t         rR   )r  r  r  r  r  r    r   r   r  z4TestRealCodeDomFront.test_infinite_loop.<locals>.foo  s    	 r   r  r  r  r  r  )r  rX  ra  assertNotInr\   rg  r  r  s         r   test_infinite_loopz'TestRealCodeDomFront.test_infinite_loop  s    	 2237V		..0 	f%eF3K&89eF3K&8999//1&+./&+./&+(=>&+(=>r   N)r   r   r   r  r  r  r  r  r  r  r  r  r    r   r   r  r  (  s2    

 &20*@8(?T#?J?r   r  __main__),rY   unittestnumbar   numba.core.controlflowr   r   
numba.corer   numba.core.bytecoder   r   r	   numba.tests.supportr
   r   rq   r   r   r"   r&   r)   r+   r-   r/   r3   r5   r7   r:   r<   r>   r@   rC   rF   rH   rK   rM   rP   rT   rV   r   r  r   mainr    r   r   <module>r     s       ?  P P ( D1	
$ ]=h ]=@m
"( m
"`T?8 T?n zHMMO r   