
    dafA!                         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                          yy)    N)assert_python_okc                   r    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y)TestTopologicalSortc                 b   d }t        j                  |      }| j                  t         ||            t        |             t        j                  |      }t	        |j                               }|D ]5  }|D ch c]  }t        |       }}| j                  t        |      |       7 y c c}w )Nc              3      K   | j                          | j                         rP| j                         }|D ]  }| j                  |        t	        t        |             | j                         rOy y wN)prepare	is_active	get_readydonetuplesorted)tsnodesnodes      -/root/Python-3.12.4/Lib/test/test_graphlib.pystatic_order_with_groupszATestTopologicalSort._test_graph.<locals>.static_order_with_groups	   sW     JJL,,.! "DGGDM"F5M**	 ,,.s   A/A42A4)graphlibTopologicalSorterassertEquallistiterstatic_ordernextset)	selfgraphexpectedr   r   itgroupelementtsgroups	            r   _test_graphzTestTopologicalSort._test_graph   s    	+ ''.6r:;T(^L''. "//#$ 	2E/45GtBx5G5SZ1	25s   :B,c                    t        j                         }|j                         D ]  \  }} |j                  |g|   	 |j	                           # t         j
                  $ rh}|j                  \  }}| j                  dj                  t        t        |            dj                  t        t        |dz                     Y d }~y 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)BFF4$)$	%	JJL
 	 "" 	RVVFAsMM#((3sE?3SXXc#sQw>O5PQQ	Rs   A C%ACCc                 0   | j                  dhddhddhddhddhdg d       | j                  di id	g       | j                  t        d
      D ci c]	  }||dz   h c}t        d
dd      D cg c]  }|f c}       | j                  dhdhdhdhdhdhdhdhdg d       | j                  ddgdgddgdgdgdgdgdgdgg d
g d       | j                  ddgg dgg dg d       | j                  ddgg dgg dgdgg dg d       y c c}w c c}w )N               )r&   	   
   r6   r7   ))r8   r:   r9   )r7   r6   )r&   r;   r<      r=   r<                  )r&   r8   r@   r:   r6   rA   rB   rC   ))r=   rD   )r:   rC   )r@   rB   )r8   rA   )r&   r6   r&      r;   )
r   r=   r&   r8   r@   r:   rE   r9   r7   r;   ))r;   )r@   )r8   r7   )r=   r:   r9   )rE   r&   r   )r   r=   r&   r8   ))r=   r8   rF   rG   )r   r=   r&   r8   r@   r:   rE   ))r=   r8   rE   )r&   r:   )r   r@   )r#   range)r   xs     r   test_simple_casesz%TestTopologicalSort.test_simple_cases)   sj   "ar1gAq6q!fE,	

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

 	q63q6333333 >	
  	aVsr:<PQA21#"!!D'	
5 ,-Ns   D0
D
c                     | j                  dhdhdhdddg       | j                  t               t               t               ddg       y )Nr&   r@   rE   )r=   r8   r:   )r&   r@   rE   )r#   r   r   s    r   test_no_dependenciesz(TestTopologicalSort.test_no_dependenciesQ   sC    aSaSaS1Iy3IJSUsu7)E    c                    | j                  dhdhg ddddg       t        j                         }|j                  dd       |j                  dd       |j                  dd       | j	                  g |j                         ddg       y )Nr&   r@   )r&   r@   r@   r@   r@   r@   )r=   r8   r   )r&   r@   )r   r=   r8   r=   )r#   r   r   r(   r   r   r   r   s     r   test_the_node_multiple_timesz0TestTopologicalSort.test_the_node_multiple_timesV   s{    aSaS-?@69BUV '')
q!
q!
q!-2??,-1v6rN   c                     d t        d      D        }t        j                  d|i      }| j                  t	        |j                               g d       y )Nc              3   ,   K   | ]  }d |z  dz     yw)r&   r=   N ).0rI   s     r   	<genexpr>z@TestTopologicalSort.test_graph_with_iterables.<locals>.<genexpr>b   s     11QUQY1s   r:   r   )r=   r8   r:   r9   r;   r   )rH   r   r   r   r   r   )r   r0   r   s      r   test_graph_with_iterablesz-TestTopologicalSort.test_graph_with_iterablesa   sA    1a1	''I7boo/02DErN   c                 R   t        j                         }|j                  dd       |j                  dd       |j                  dd       |j                  dd       t        j                  dh di      }| j                  g |j	                         g |j	                                y )Nr=   r&   r8   r@   r:   >   r&   r8   r@   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1A1A1C/DErN   c                 (    | j                  i g        y r   )r#   rL   s    r   
test_emptyzTestTopologicalSort.test_emptyq   s    R rN   c           	      V   | j                  ddhiddg       | j                  dhdhdg d       | j                  dhdhdhdg d       | j                  dhdhdhdhd	hd
g d       | j                  dhdhdhdhdhd	hdg d       | j                  dhdhddhdhdddg       y )Nr=   r&   )r=   r&   )r=   r&   r=   r8   )r=   r&   r8   )r=   r8   r&   r=   r@   rE   )r=   r&   r8   r:   r@   r:   r9   )r=   r&   r8   r@   rE   r9   )r=   r&   r8   r@   )r4   rL   s    r   
test_cyclezTestTopologicalSort.test_cyclet   s    As8aV,ss+Y7sss3\BsssssC\RssssssKYWss1v1#>AGrN   c                    t        j                         }| j                  t        d      5  |j	                          d d d        | j                  t        d      5  |j                  d       d d d        | j                  t        d      5  |j                          d d d        y # 1 sw Y   ixY w# 1 sw Y   ExY w# 1 sw Y   y xY w)Nz prepare\(\) must be called firstr8   )r   r   assertRaisesRegex
ValueErrorr   r   r
   rP   s     r   test_calls_before_preparez-TestTopologicalSort.test_calls_before_prepare   s    '')##J0ST 	LLN	##J0ST 	GGAJ	##J0ST 	LLN	 			 		 		 	s#   B$B0
B<$B-0B9<Cc                     t        j                         }|j                          | j                  t        d      5  |j                          d d d        y # 1 sw Y   y xY w)Nz!cannot prepare\(\) more than once)r   r   r	   r`   ra   rP   s     r   test_prepare_multiple_timesz/TestTopologicalSort.test_prepare_multiple_times   sH    '')


##J0TU 	JJL	 	 	s   AAc                    t        j                         }|j                  dddd       |j                  ddd       |j                          |j	                          | j                  t        d      5  |j                  d       d d d        | j                  t        d      5  |j                  d       d d d        y # 1 sw Y   :xY w# 1 sw Y   y xY w)Nr=   r&   r8   r@   znode 2 was not passed outz#node 24 was not added using add\(\)   )r   r   r(   r	   r   r`   ra   r   rP   s     r   test_invalid_nodes_in_donez.TestTopologicalSort.test_invalid_nodes_in_done   s    '')
q!Q
q!Q



##J0KL 	GGAJ	##J0VW 	GGBK	 		 		 	s   2B="C	=C	Cc                    t        j                         }|j                  dddd       |j                  dd       |j                          | j	                  |j                         d       | j	                  |j                         d       |j                  d       | j	                  |j                         d       | j	                  |j                         d       |j                  d       |j                  d       | j	                  |j                         d       | j	                  |j                         d       |j                  d       | j	                  |j                         d       | j                  |j                                y )	Nr=   r&   r8   r@   )r8   r@   rT   rF   r>   )	r   r   r(   r	   r   r   r   assertFalser
   rP   s     r   	test_donezTestTopologicalSort.test_done   s   '')
q!Q
q!


0,

.,



.,

,(rN   c                 j   t        j                         }|j                  dd       |j                          | j	                  |j                                | j                  |j                         d       | j	                  |j                                |j                  d       | j	                  |j                                | j                  |j                         d       | j	                  |j                                |j                  d       | j                  |j                                y )Nr=   r&   rF   r>   )
r   r   r(   r	   
assertTruer
   r   r   r   ri   rP   s     r   test_is_activez"TestTopologicalSort.test_is_active   s    '')
q!


'.'

'.'

(rN   c                 8   t        j                         }| j                  t        |j                  t               d       | j                  t        |j                  dt                      | j                  t        |j                  t               t                      y )Nr=   )r   r   assertRaises	TypeErrorr(   dictrP   s     r   test_not_hashable_nodesz+TestTopologicalSort.test_not_hashable_nodes   s`    ''))RVVTVQ7)RVVQ7)RVVTVTV<rN   c                 &   d }t        j                         }|j                  ddd       |j                  dd       |j                  dd       |j                  dd	       |j                  dd	       t        j                         }|j                  dd       |j                  ddd       |j                  dd	       |j                  dd	       |j                  dd       | j                  t	         ||            t	         ||                   y )
Nc              3      K   | j                          | j                         r>| j                         } | j                  |  t	        |       | j                         r=y y w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   sE     JJL,,.%j  ,,.s   AA" A"r8   r&   r=   r   r@   r:   rE   r9   )r   r   r(   r   r   )r   ru   r   rY   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jn-tJsO/DErN   c                     d } |d      } |d      }| j                  |d       | j                  |d       | j                  ||       y )Nc                     d}t         j                  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copyr.   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   sL    D **//#C !%C$'IC !"455CJrN   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   sM    	$ *$/)%0D"%D"%t$rN   N)__name__
__module____qualname__r#   r4   rJ   rM   rQ   rW   rZ   r\   r^   rb   rd   rg   rj   rm   rr   rv   r   rT   rN   r   r   r      s]    2*
&
PF
	7F
	F!H
),)=F0%rN   r   __main__)	r   r{   unittesttest.support.script_helperr   TestCaser   r   mainrT   rN   r   <module>r      s@     	  7r%(++ r%h zHMMO rN   