
    xKg                     j   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 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jZ                          yy)'    N)jit)CFGraphControlFlowAnalysis)types)FunctionIdentityByteCode_fix_LOAD_GLOBAL_arg)	PYVERSION)TestCaseT)_nrtnopythonc                 4    d}t        |       D ]  }||z  }	 |S Nr   rangexyresultis       a/home/alanp/www/video.onchill/myenv/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      s4    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'   &   s3    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*   .   s4    F1X!A:M	  Mr   c                 @    d}t        |       D ]  }||kD  r	|dz  } |S r)   r   r   s       r   for_loop_usecase6r,   7   s/    F1Xq5!  Mr   c                 L    t        |       D ]  }d} t        |       D ]  }  y  yr)   r   )r   r   r   r   s       r   for_loop_usecase7r.   @   s0    1XqA    r   c                 D    d}t        | ||| z
  dz         D ]  }|dz  }	 |S r)   r   r   s       r   for_loop_usecase8r0   J   s0    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_usecase9r4   Q   sQ    	A1XqAAEzQ 
 FA  Hr   c                 D    t        |       D ]  }||k(  s	|} |S  dz  }|S )Nr2   r   )r   r   r   r3   s       r   for_loop_usecase10r6   _   s6    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_usecase1r8   i   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;   r   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=   y   sW    F	A	A
a%!ea!eOFFAFA !e a%
 Mr   c                 $    d}	 |dz  }|| kD  r	 |S r)   r!   r:   s      r   while_loop_usecase4r?      s'    F
!A:M	 r   c                 @    d}|| k  r||kD  r|dz  }|dz  }|| k  r|S )Nr   r2   r&   r!   r:   s      r   while_loop_usecase5rA      s:    F
1*A:aKF!	 1*
 Mr   c                 "    | dkD  r	 y|dkD  r	 y	 y)Nr   Tr!   r   r   s     r   ifelse_usecase1rD      s*    1u
 	 
Q  	r   c                 &    | |kD  ry| dk(  s|dk(  ryy)Nr&   r   r2      r!   rC   s     r   ifelse_usecase2rG      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!   rC   s     r   ifelse_usecase3rI      s-    1uq5U	
Qr   c                     | |k(  ryy Nr&   r!   rC   s     r   ifelse_usecase4rL      s    Av r   c                     | |kD  rdS dS )NTFr!   rC   s     r   ternary_ifelse_usecase1rN      s    q54#e#r   c                 L    | }|}	 	 ||dz
  k(  rn|dz  }|dz  }||k\  r	 ||fS  rK   r!   )r   r   Lr   s       r   double_infinite_looprQ      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_usecaserU      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productrT   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   rc   )rj   r   r_   rc   s     r   test_for_loop1zTestFlowControl.test_for_loop1   s    's%Hr   c                 0    | j                  t               y Nrm   )ro   no_pyobj_flagsr_   s    r   test_for_loop1_npmz"TestFlowControl.test_for_loop1_npm       .1r   c                 >    | j                  t        g dg d|       y )Nrl   rm   )rj   r   rn   s     r   test_for_loop2zTestFlowControl.test_for_loop2   s    '|! 	 	#r   c                 0    | j                  t               y rq   )rw   rr   rs   s    r   test_for_loop2_npmz"TestFlowControl.test_for_loop2_npm   ru   r   c                 :    | j                  t        dgdg|       y)z(
        List requires pyobject
        r&   r2   rm   N)rj   r#   rn   s     r   test_for_loop3zTestFlowControl.test_for_loop3   s#     	'!qc! 	 	#r   c                 0    | j                  t               y rq   )r{   rr   rs   s    r   test_for_loop3_npmz"TestFlowControl.test_for_loop3_npm  ru   r   c                 :    | j                  t        dgdg|       y Nr%   rm   )rj   r'   rn   s     r   test_for_loop4zTestFlowControl.test_for_loop4
  s    '"t5Ar   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop4_npmz"TestFlowControl.test_for_loop4_npm  ru   r   c                 :    | j                  t        dgdg|       y Nd   2   rm   )rj   r*   rn   s     r   test_for_loop5zTestFlowControl.test_for_loop5      '#EBr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop5_npmz"TestFlowControl.test_for_loop5_npm  ru   r   c                 :    | j                  t        dgdg|       y r   )rj   r,   rn   s     r   test_for_loop6zTestFlowControl.test_for_loop6  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop6_npmz"TestFlowControl.test_for_loop6_npm  ru   r   c                 :    | j                  t        dgdg|       y )N   r   rm   )rj   r.   rn   s     r   test_for_loop7zTestFlowControl.test_for_loop7  s    '!qc?r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop7_npmz"TestFlowControl.test_for_loop7_npm  ru   r   c                 >    | j                  t        ddgg d|       y Nr   r&   )r   r2   r%   rm   )rj   r0   rn   s     r   test_for_loop8zTestFlowControl.test_for_loop8"      '!Q5Ir   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop8_npmz"TestFlowControl.test_for_loop8_npm%  ru   r   c                 >    | j                  t        ddgg d|       y r   )rj   r4   rn   s     r   test_for_loop9zTestFlowControl.test_for_loop9(  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop9_npmz"TestFlowControl.test_for_loop9_npm+  ru   r   c                 <    | j                  t        dgddg|       y )Nr   r2      rm   )rj   r6   rn   s     r   test_for_loop10zTestFlowControl.test_for_loop10.  s    (1#1vUCr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_for_loop10_npmz#TestFlowControl.test_for_loop10_npm1  s    >2r   c                 :    | j                  t        dgdg|       y Nr%   r   rm   )rj   r8   rn   s     r   test_while_loop1z TestFlowControl.test_while_loop14      )B4!EBr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_while_loop1_npmz$TestFlowControl.test_while_loop1_npm7      N3r   c                 :    | j                  t        dgdg|       y r   )rj   r;   rn   s     r   test_while_loop2z TestFlowControl.test_while_loop2:  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_while_loop2_npmz$TestFlowControl.test_while_loop2_npm=  r   r   c                 :    | j                  t        dgdg|       y r   )rj   r=   rn   s     r   test_while_loop3z TestFlowControl.test_while_loop3@  s    )B4"UCr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_while_loop3_npmz$TestFlowControl.test_while_loop3_npmC  r   r   c                 :    | j                  t        dgdg|       y r   )rj   r?   rn   s     r   test_while_loop4z TestFlowControl.test_while_loop4F  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_while_loop4_npmz$TestFlowControl.test_while_loop4_npmI  r   r   c                 >    | j                  t        g dg d|       y )N)r   r   r%   rm   )rj   rA   rn   s     r   test_while_loop5z TestFlowControl.test_while_loop5L  s    ):zOr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_while_loop5_npmz$TestFlowControl.test_while_loop5_npmO  r   r   c                 >    | j                  t        g dg d|       y N)r   r   r&   rm   )rj   rD   rn   s     r   test_ifelse1zTestFlowControl.test_ifelse1R      oz:UKr   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_ifelse1_npmz TestFlowControl.test_ifelse1_npmU      /r   c                 >    | j                  t        g dg d|       y r   )rj   rG   rn   s     r   test_ifelse2zTestFlowControl.test_ifelse2X  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_ifelse2_npmz TestFlowControl.test_ifelse2_npm[  r   r   c                 >    | j                  t        g dg d|       y r   )rj   rI   rn   s     r   test_ifelse3zTestFlowControl.test_ifelse3^  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_ifelse3_npmz TestFlowControl.test_ifelse3_npma  r   r   c                 >    | j                  t        g dg d|       y r   )rj   rL   rn   s     r   test_ifelse4zTestFlowControl.test_ifelse4d  r   r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_ifelse4_npmz TestFlowControl.test_ifelse4_npmg  r   r   c                 >    | j                  t        g dg d|       y r   )rj   rN   rn   s     r   test_ternary_ifelse1z$TestFlowControl.test_ternary_ifelse1j  s    -z:! 	 	#r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_ternary_ifelse1_npmz(TestFlowControl.test_ternary_ifelse1_npmn  s    !!!7r   c                 :    | j                  t        dgdg|       y r   )rj   rQ   rn   s     r   test_double_infinite_loopz)TestFlowControl.test_double_infinite_loopq  s!    *RD1#! 	 	#r   c                 0    | j                  t               y rq   )r   rr   rs   s    r   test_double_infinite_loop_npmz-TestFlowControl.test_double_infinite_loop_npmu  s    &&^&<r   N)/__name__
__module____qualname__enable_pyobj_flagsrj   ro   rt   rw   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   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   rW   rW      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   rW   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~  sT     IDJJt 779KD%

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:
                    ...
        rF      	   r   rF   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_nodes4  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_descendentsJ  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   rF   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_orderb  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)r0  list	topo_sortrandomshuffle)r)  expectedr   r_   s     r   check_topo_sortz3TestCFGraph.test_topo_sort.<locals>.check_topo_sortr  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_sortq  s\    
	$ NN(+;*=>()+;<	>!(/?K	Mr   c                     | j                  t        |      t        |             t        |      D ]0  }| j                  t        ||         t        ||         d|       2 y )Nzmismatch for r]   r  )r_   gotr:  r   s       r   check_dominatorszTestCFGraph.check_dominators  sS    fX&673KD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 rS   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_   rF  r   rE  s   `  @r   test_dominators_looplessz$TestCFGraph.test_dominators_loopless  s    	1.."D$=$=$?@A<<>DA3KQGQGQG A 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  rH  r@  r  r  r_   r   rE  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 rS   r>  rC  s     r   rF  z6TestCFGraph.test_post_dominators_loopless.<locals>.eq_  rG  r   r   r   r   r   r   r   r   )r   r   post_dominatorsr   rI  s   `  @r   test_post_dominators_looplessz)TestCFGraph.test_post_dominators_loopless  s    	1.."D$=$=$?@A$$&DAr7ORHRHRDM A 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  rP  r@  r  rL  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  rF   r  r  r  )r  rP  r@  r  rL  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 rS   )dominator_treer]   )graphr:  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  rF   r  r  r  r   setr   r   r  r  r  r  r_   r[  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 rS   )immediate_dominatorsr]   )rY  r:  idomsr_   s      r   r[  z4TestCFGraph.test_immediate_dominators.<locals>.check2  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  rF   r  )r   r   r   r  r  r  r  r^  s   ` r   test_immediate_dominatorsz%TestCFGraph.test_immediate_dominators1  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 rS   )dominance_frontierr]   )rY  r:  dfr_   s      r   r[  z2TestCFGraph.test_dominance_frontier.<locals>.checkH  s!    ))+BR*r   r   r   rd  r   r   r%   r   r   r  r  r  r   r  r   r  r  r  r	  r  r
  r  r  r  rF   r  r\  r^  s   ` r   test_dominance_frontierz#TestCFGraph.test_dominance_frontierG  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^  sl    .."D$=$=$?@AVAJJL1Ar7; A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   rF   )r  r]   r  rl  r  r  r   s     r   test_backbone_loopszTestCFGraph.test_backbone_loopsd  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%   rF   )rF   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_loopsl  s   .."D$=$=$?.."$AS^Q/$ !	*K8122*/!5-t4,<	>+/"6-t4,.>?+/"6-Bx8,r2h7+DQZZ-r2 ,'DQZZ-x8 ($DQZZ-/?@ %DQZZ-x8  !	*RD1wwy}b)-t4

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

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

+\:		*K8#DQZZ-r2 $DQZZ-v6  r   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   r3   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   Nrq  ) r   r   r   __doc__r   r   r   r   r  r  r  r  r!  r$  r*  r.  r2  r<  r@  rJ  rM  rQ  rS  rU  r_  re  rj  rm  ro  r|  r  r  r!   r   r   r   r   y  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 rS   )r   run)r_   bccfas      r   r  zTestRealCodeDomFront.cfa5  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_namedblocks:  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srh   blknos                r   r  z&TestRealCodeDomFront._scan_namedblocksM  s    
 3>>+;<+;a+;<=D{{m+[[!5dhh!?@==(c&kl+D #FF12J&*q.9I,I J1/a/! !K --?@@E(-K%  # =s   Cc                    d }| j                  |      \  }}|j                  j                         }t        dk  r| j	                  |d   ||d             |j                  j                         }| j                  ||d             | j                  ||d             t        dk  r| j	                  |d   h||d             y 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>.foog  s6    AAQQ  Hr   rF   r%   B0B1AC)r  rY  rb  r
   r]   rh  assertFalser_   r  r  blkptsrc  domfronts         r   	test_loopzTestRealCodeDomFront.test_loopf  s    		 2237V		..0
 wVD\5+>?99//1&+./&+./wfTl^XfTl-CD r   c                 6   d }| j                  |      \  }}|j                  j                         }| j                  |d   |d          t        dk  r| j                  |d   ||d             |j                  j                         }| j                  ||d             | j                  ||d             t        dk  r| j                  |d   h||d	             t        dk  r | j                  |d   |d
   h||d             | j                  |d
   h||d             | j                  |d   h||d             t        dk  rU| j                  |d   h||d             | j                  |d   h||d
             | j                  |d   h||d             y y )Nc                     t          t        rAt         t        r(t         t
        rt         n| rt         t         t        r(t         t        rAt         y rS   )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  C0r  Gr  r  FD1ED2)r  rY  rb  r]   r
   rh  r  r  s         r   test_loop_nested_and_breakz/TestRealCodeDomFront.test_loop_nested_and_break  s   	 2237V		..0vd|4 wVD\5+>?99//1&+./&+./wfTl^XfTl-CD wfTlF3K8%fTl35&+(>?&+(>?wfTl^XfSk-BCfTl^XfSk-BCfTl^XfTl-CD 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&   r2   rF   )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  Br  r  Dr  r  r  r  rY  rb  r]   rh  r  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&   r2   )
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A1r  r  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                 p   d }| j                  |      \  }}|j                  j                         }t        dk\  r| j	                  d|       n| j	                  |d   |       | j                  |d   ||d             | j                  |d   ||d             |j                  j                         }t        dk  r| j	                  |d   |       | j                  ||d             | j                  ||d             | j                  |d   h||d             | j                  |d   h||d             y )Nc                  8    t          	 t        rt         y t         rS   )r  r  r  r  r  r!   r   r   r  z4TestRealCodeDomFront.test_infinite_loop.<locals>.foo  s    	 r   r  r  r  r  r  r  )r  rY  rb  r
   assertNotInr]   rh  r  r  s         r   test_infinite_loopz'TestRealCodeDomFront.test_infinite_loop  s1   	 2237V		..0 S&)VC[%0eF3K&89eF3K&8999//1wVC[(3&+./&+./&+(=>&+(=>r   N)r   r   r   r  r  r  r  r  r  r  r  r  r!   r   r   r  r  )  s4    

 &2E6'ER(?T#?J?r   r  __main__).rZ   unittestnumbar   numba.core.controlflowr   r   
numba.corer   numba.core.bytecoder   r   r	   numba.core.utilsr
   numba.tests.supportr   r   rr   r   r   r#   r'   r*   r,   r.   r0   r4   r6   r8   r;   r=   r?   rA   rD   rG   rI   rL   rN   rQ   rU   rW   r   r  r   mainr!   r   r   <module>r     s       ?  P P & ( D1	
$ ]=h ]=@m
"( m
"`l?8 l?^ zHMMO r   