
    <eA!                     ~    d dl Z d dlZd dlZd dlmZ  G d dej                  Zedk    r ej                     dS dS )    N)assert_python_okc                   t    e 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S )TestTopologicalSortc                 ~   d }t          j        |          }|                     t           ||                    t          |                     t          j        |          }t	          |                                          |D ]3}fd|D             }|                     t          |          |           4d S )Nc              3     K   |                                   |                                 rb|                                 }|D ]}|                     |           t	          t          |                    V  |                                 `d S d S N)prepare	is_active	get_readydonetuplesorted)tsnodesnodes      )/usr/lib/python3.11/test/test_graphlib.pystatic_order_with_groupszATestTopologicalSort._test_graph.<locals>.static_order_with_groups	   s      JJLLL,,.. +! " "DGGDMMMMF5MM*****	 ,,.. + + + + +    c                 .    h | ]}t                    S  )next).0elementits     r   	<setcomp>z2TestTopologicalSort._test_graph.<locals>.<setcomp>   s    555GtBxx555r   )graphlibTopologicalSorterassertEquallistiterstatic_orderset)selfgraphexpectedr   r   grouptsgroupr   s          @r   _test_graphzTestTopologicalSort._test_graph   s    	+ 	+ 	+ '..66r::;;T(^^LLL'.. "//##$$ 	2 	2E5555u555GSZZ1111	2 	2r   c                    t          j                    }|                                D ]\  }} |j        |g|R   	 |                                  # t           j        $ rz}|j        \  }}|                     d                    t          t          |                    d                    t          t          |dz                                 Y d }~d S d }~ww xY w)N    )r   r   itemsaddr	   
CycleErrorargsassertInjoinmapstr)	r#   r$   cycler   r   	dependsone_seqs	            r   _assert_cyclez!TestTopologicalSort._assert_cycle   s    '))${{}} 	% 	%OD)BF4$)$$$$$	JJLLL
 	 " 	R 	R 	RVFAsMM#((3sE??33SXXc#sQw>O>O5P5PQQQQQQQQQ	Rs   A C!A/CCc                 J   |                      dhddhddhddhddhdg d           |                      di id	g           |                      d
 t          d          D             d t          ddd          D                        |                      dhdhdhdhdhdhdhdhdg d           |                      ddgdgddgdgdgdgdgdgdgg d
g d           |                      ddgg dgg dg d           |                      ddgg dgg dgdgg dg d           d S )N               )r+   	   
   r;   r<   ))r=   r?   r>   )r<   r;   )r+   r@   rA      rB   c                     i | ]	}||d z   h
S rC   r   r   xs     r   
<dictcomp>z9TestTopologicalSort.test_simple_cases.<locals>.<dictcomp>2   s     +++AQQ+++r   rA   c                     g | ]}|fS r   r   rE   s     r   
<listcomp>z9TestTopologicalSort.test_simple_cases.<locals>.<listcomp>2   s    -N-N-Nqqd-N-N-Nr                  )r+   r=   rK   r?   r;   rL   rM   rN   ))rB   rO   )r?   rN   )rK   rM   )r=   rL   )r+   r;   r+      r@   )
r   rB   r+   r=   rK   r?   rP   r>   r<   r@   ))r@   )rK   )r=   r<   )rB   r?   r>   )rP   r+   r   )r   rB   r+   r=   ))rB   r=   rQ   rR   )r   rB   r+   r=   rK   r?   rP   ))rB   r=   rP   )r+   r?   )r   rK   )r(   ranger#   s    r   test_simple_casesz%TestTopologicalSort.test_simple_cases)   s   "ar1gAq6q!fEE,,,	
 	
 	

 	!R4&)))++r+++-N-NE"b"<M<M-N-N-N	
 	
 	
 	"B4bTPRtTT999	
 	
 	

 	q63q6333333  >==	
 	
 	
  	aVsr::<P<P<PQQQA21#"!!DD'''	
 	
 	
 	
 	
r   c                     |                      dhdhdhdddg           |                      t                      t                      t                      ddg           d S )Nr+   rK   rP   )rB   r=   r?   )r+   rK   rP   )r(   r"   rT   s    r   test_no_dependenciesz(TestTopologicalSort.test_no_dependenciesQ   sa    aSaSaS11Iy3IJJJSUUsuu77)EEEEEr   c                 H   |                      dhdhg ddddg           t          j                    }|                    dd           |                    dd           |                    dd           |                     g |                                ddg           d S )Nr+   rK   )r+   rK   rK   rK   rK   rK   )rB   r=   r   )r+   rK   )r   rB   r=   rB   )r(   r   r   r-   r   r!   r#   r   s     r   test_the_node_multiple_timesz0TestTopologicalSort.test_the_node_multiple_timesV   s    aSaS-?-?-?@@69BUVVV '))
q!
q!
q!-2??,,-1v66666r   c                     d t          d          D             }t          j        d|i          }|                     t	          |                                          g d           d S )Nc              3   &   K   | ]}d |z  dz   V  dS )r+   rB   Nr   rE   s     r   	<genexpr>z@TestTopologicalSort.test_graph_with_iterables.<locals>.<genexpr>b   s*      111QUQY111111r   r?   r   )rB   r=   r?   r>   r@   r   )rS   r   r   r   r   r!   )r#   r5   r   s      r   test_graph_with_iterablesz-TestTopologicalSort.test_graph_with_iterablesa   sd    11a111	'I77boo//002D2D2DEEEEEr   c                    t          j                    }|                    dd           |                    dd           |                    dd           |                    dd           t          j        dh di          }|                     g |                                g |                                           d S )NrB   r+   r=   rK   r?   >   r+   r=   rK   r?   )r   r   r-   r   r!   )r#   r   ts2s      r   1test_add_dependencies_for_same_node_incrementallyzETestTopologicalSort.test_add_dependencies_for_same_node_incrementallyf   s    '))
q!
q!
q!
q!(!\\\):;;-2??,,-/D1A1A1C1C/DEEEEEr   c                 2    |                      i g            d S r   )r(   rT   s    r   
test_emptyzTestTopologicalSort.test_emptyq   s    R     r   c           	         |                      ddhiddg           |                      dhdhdg d           |                      dhdhdhdg d           |                      dhdhdhdhd	hd
g d           |                      dhdhdhdhdhd	hdg d           |                      dhdhddhdhdddg           d S )NrB   r+   )rB   r+   )rB   r+   rB   r=   )rB   r+   r=   )rB   r=   r+   rB   rK   rP   )rB   r+   r=   r?   rK   r?   r>   )rB   r+   r=   rK   rP   r>   )rB   r+   r=   rK   )r9   rT   s    r   
test_cyclezTestTopologicalSort.test_cyclet   s   As8aV,,,ss++YYY777sss33\\\BBBsssssCC\\\RRRssssssKKYYYWWWss1v1#>>AGGGGGr   c                    t          j                    }|                     t          d          5  |                                 d d d            n# 1 swxY w Y   |                     t          d          5  |                    d           d d d            n# 1 swxY w Y   |                     t          d          5  |                                 d d d            d S # 1 swxY w Y   d S )Nz prepare\(\) must be called firstr=   )r   r   assertRaisesRegex
ValueErrorr   r   r
   rY   s     r   test_calls_before_preparez-TestTopologicalSort.test_calls_before_prepare   s   '))##J0STT 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	##J0STT 	 	GGAJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	##J0STT 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s5   AAA6BBB>C  C$'C$c                     t          j                    }|                                 |                     t          d          5  |                                 d d d            d S # 1 swxY w Y   d S )Nz!cannot prepare\(\) more than once)r   r   r	   rg   rh   rY   s     r   test_prepare_multiple_timesz/TestTopologicalSort.test_prepare_multiple_times   s    '))


##J0TUU 	 	JJLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A%%A),A)c                    t          j                    }|                    dddd           |                    ddd           |                                 |                                 |                     t          d          5  |                    d           d d d            n# 1 swxY w Y   |                     t          d          5  |                    d           d d d            d S # 1 swxY w Y   d S )NrB   r+   r=   rK   znode 2 was not passed outz#node 24 was not added using add\(\)   )r   r   r-   r	   r   rg   rh   r   rY   s     r   test_invalid_nodes_in_donez.TestTopologicalSort.test_invalid_nodes_in_done   sZ   '))
q!Q
q!Q



##J0KLL 	 	GGAJJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	##J0VWW 	 	GGBKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s$   B((B,/B,C11C58C5c                    t          j                    }|                    dddd           |                    dd           |                                 |                     |                                d           |                     |                                d           |                    d           |                     |                                d           |                     |                                d           |                    d           |                    d           |                     |                                d           |                     |                                d           |                    d           |                     |                                d           |                     |                                           d S )	NrB   r+   r=   rK   )r=   rK   r   rQ   rC   )	r   r   r-   r	   r   r   r   assertFalser
   rY   s     r   	test_donezTestTopologicalSort.test_done   sv   '))
q!Q
q!


000,,,



...,,,







...,,,



,,,(((((r   c                    t          j                    }|                    dd           |                                 |                     |                                           |                     |                                d           |                     |                                           |                    d           |                     |                                           |                     |                                d           |                     |                                           |                    d           | 	                    |                                           d S )NrB   r+   rQ   rC   )
r   r   r-   r	   
assertTruer
   r   r   r   rp   rY   s     r   test_is_activez"TestTopologicalSort.test_is_active   s   '))
q!


'''...'''



'''...'''



(((((r   c                 X   t          j                    }|                     t          |j        t                      d           |                     t          |j        dt                                 |                     t          |j        t                      t                                 d S )NrB   )r   r   assertRaises	TypeErrorr-   dictrY   s     r   test_not_hashable_nodesz+TestTopologicalSort.test_not_hashable_nodes   sz    ')))RVTVVQ777)RVQ777)RVTVVTVV<<<<<r   c                    d }t          j                    }|                    ddd           |                    dd           |                    dd           |                    dd	           |                    dd	           t          j                    }|                    dd           |                    ddd           |                    dd	           |                    dd	           |                    dd           |                     t	           ||                    t	           ||                               d S )
Nc              3      K   |                                   |                                 rE|                                 } | j        |  t	          |          V  |                                 Cd S d S r   )r	   r
   r   r   r"   )r   r   s     r   
get_groupsz^TestTopologicalSort.test_order_of_insertion_does_not_matter_between_groups.<locals>.get_groups   su      JJLLL,,.. !%jj    ,,.. ! ! ! ! !r   r=   r+   rB   r   rK   r?   rP   r>   )r   r   r-   r   r   )r#   r|   r   r`   s       r   6test_order_of_insertion_does_not_matter_between_groupszJTestTopologicalSort.test_order_of_insertion_does_not_matter_between_groups   s   	! 	! 	! '))
q!Q
q!
q!
q!
q!(**11a111jjnn--tJJsOO/D/DEEEEEr   c                     d } |d          } |d          }|                      |d           |                      |d           |                     ||           d S )Nc                     d}t           j                                        }d|d<   t          |           |d<   t	          d|fi |}|S )Nax  if 1:
                import graphlib
                ts = graphlib.TopologicalSorter()
                ts.add('blech', 'bluch', 'hola')
                ts.add('abcd', 'blech', 'bluch', 'a', 'b')
                ts.add('a', 'a string', 'something', 'b')
                ts.add('bluch', 'hola', 'abcde', 'a', 'b')
                print(list(ts.static_order()))
                T
__cleanenvPYTHONHASHSEEDz-c)osenvironcopyr3   r   )seedcodeenvouts       r   check_order_with_hash_seedzlTestTopologicalSort.test_static_order_does_not_change_with_the_hash_seed.<locals>.check_order_with_hash_seed   sU    D *//##C !%C$'IIC !"45555CJr   i  iz   )assertNotEqualr   )r#   r   run1run2s       r   4test_static_order_does_not_change_with_the_hash_seedzHTestTopologicalSort.test_static_order_does_not_change_with_the_hash_seed   s{    	 	 	$ *)$//))%00D"%%%D"%%%t$$$$$r   N)__name__
__module____qualname__r(   r9   rU   rW   rZ   r^   ra   rc   re   ri   rk   rn   rq   rt   ry   r}   r   r   r   r   r   r      s#       2 2 2*
 
 
&
 &
 &
PF F F
	7 	7 	7F F F
	F 	F 	F! ! !H H H    
 
 
) ) ),) ) )= = =F F F0% % % % %r   r   __main__)	r   r   unittesttest.support.script_helperr   TestCaser   r   mainr   r   r   <module>r      s     				  7 7 7 7 7 7r% r% r% r% r%(+ r% r% r%h zHMOOOOO r   