a
    äze_!  ã                   @   sV   d dl mZ d dlZd dlZd dlZd dlmZ G dd„ dejƒZe	dkrRe 
¡  dS )é    )ÚchainN)Úassert_python_okc                   @   s”   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#S )$ÚTestTopologicalSortc                    sp   dd„ }t  |¡}|  t||ƒƒt|ƒ¡ t  |¡}t| ¡ ƒ‰ |D ]&}‡ fdd„|D ƒ}|  t|ƒ|¡ qDd S )Nc                 s   s@   |   ¡  |  ¡ r<|  ¡ }|D ]}|  |¡ qtt|ƒƒV  qd S ©N)ÚprepareÚ	is_activeÚ	get_readyÚdoneÚtupleÚsorted)ÚtsÚnodesÚnode© r   ú(/usr/lib/python3.9/test/test_graphlib.pyÚstatic_order_with_groups
   s    zATestTopologicalSort._test_graph.<locals>.static_order_with_groupsc                    s   h | ]}t ˆ ƒ’qS r   )Únext)Ú.0Úelement©Úitr   r   Ú	<setcomp>   ó    z2TestTopologicalSort._test_graph.<locals>.<setcomp>)ÚgraphlibÚTopologicalSorterÚassertEqualÚlistÚiterÚstatic_orderÚset)ÚselfÚgraphZexpectedr   r   ÚgroupZtsgroupr   r   r   Ú_test_graph	   s    

zTestTopologicalSort._test_graphc           	      C   s˜   t  ¡ }| ¡ D ]\}}|j|g|¢R Ž  qz| ¡  W nX t jy } z>|j\}}|  d t	t
|ƒ¡d t	t
|d ƒ¡¡ W Y d }~nd }~0 0 ‚ d S )Nú é   )r   r   ÚitemsÚaddr   Z
CycleErrorÚargsZassertInÚjoinÚmapÚstr)	r    r!   Úcycler   r   Ú	dependsonÚeÚmsgÚseqr   r   r   Ú_assert_cycle   s    
>z!TestTopologicalSort._assert_cyclec                 C   s(  |   dhddhddhddhddhdœg d¢¡ |   di id	g¡ |   d
d„ tdƒ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%   é	   é
   r2   r3   ))r4   r6   r5   )r3   r2   )r%   r7   r8   é   ©r9   c                 S   s   i | ]}||d  h“qS r:   r   ©r   Úxr   r   r   Ú
<dictcomp>3   r   z9TestTopologicalSort.test_simple_cases.<locals>.<dictcomp>r8   c                 S   s   g | ]
}|f‘qS r   r   r;   r   r   r   Ú
<listcomp>3   r   z9TestTopologicalSort.test_simple_cases.<locals>.<listcomp>éÿÿÿÿé   é   é   é   é   )r%   r4   r@   r6   r2   rA   rB   rC   ))r9   rD   )r6   rC   )r@   rB   )r4   rA   )r%   r2   r%   é   r7   )
r   r9   r%   r4   r@   r6   rE   r5   r3   r7   ))r7   )r@   )r4   r3   )r9   r6   r5   )rE   ©r%   ©r   )r   r9   r%   r4   ))r9   r4   rF   rG   )r   r9   r%   r4   r@   r6   rE   ))r9   r4   rE   )r%   r6   )r   r@   )r#   Úrange©r    r   r   r   Útest_simple_cases*   s>     þ$ÿ$þöó þz%TestTopologicalSort.test_simple_casesc                 C   s>   |   dhdhdhdœddg¡ |   tƒ tƒ tƒ dœdg¡ d S )Nr%   r@   rE   )r9   r4   r6   )r%   r@   rE   )r#   r   rI   r   r   r   Útest_no_dependenciesR   s    z(TestTopologicalSort.test_no_dependenciesc                 C   sh   |   dhdhg d¢dœddg¡ t ¡ }| dd¡ | dd¡ | dd¡ |  g | ¡ ¢ddg¡ d S )Nr%   r@   )r%   r@   r@   r@   r@   r@   )r9   r4   r   )r%   r@   )r   r9   r4   r9   )r#   r   r   r'   r   r   ©r    r   r   r   r   Útest_the_node_multiple_timesW   s     z0TestTopologicalSort.test_the_node_multiple_timesc                 C   s<   dd„ t dƒD ƒ}t d|i¡}|  t| ¡ ƒg d¢¡ d S )Nc                 s   s   | ]}d | d V  qdS )r%   r9   Nr   r;   r   r   r   Ú	<genexpr>c   r   z@TestTopologicalSort.test_graph_with_iterables.<locals>.<genexpr>r6   r   )r9   r4   r6   r5   r7   r   )rH   r   r   r   r   r   )r    r-   r   r   r   r   Útest_graph_with_iterablesb   s    z-TestTopologicalSort.test_graph_with_iterablesc                 C   sj   t  ¡ }| dd¡ | dd¡ | dd¡ | dd¡ t  dh d£i¡}|  g | ¡ ¢g | ¡ ¢¡ d S )Nr9   r%   r4   r@   r6   >   r%   r4   r@   r6   )r   r   r'   r   r   )r    r   Úts2r   r   r   Ú1test_add_dependencies_for_same_node_incrementallyg   s    zETestTopologicalSort.test_add_dependencies_for_same_node_incrementallyc                 C   s   |   i g ¡ d S r   )r#   rI   r   r   r   Ú
test_emptyr   s    zTestTopologicalSort.test_emptyc              	   C   sÆ   |   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 )Nr9   r%   )r9   r%   )r9   r%   r9   r4   )r9   r%   r4   )r9   r4   r%   r9   r@   rE   )r9   r%   r4   r6   r@   r6   r5   )r9   r%   r4   r@   rE   r5   )r9   r%   r4   r@   )r1   rI   r   r   r   Ú
test_cycleu   s    &*zTestTopologicalSort.test_cyclec                 C   sª   t  ¡ }|  td¡ | ¡  W d   ƒ n1 s20    Y  |  td¡ | d¡ W d   ƒ n1 sh0    Y  |  td¡ | ¡  W d   ƒ n1 sœ0    Y  d S )Nz prepare\(\) must be called firstr4   )r   r   ÚassertRaisesRegexÚ
ValueErrorr   r	   r   rL   r   r   r   Útest_calls_before_prepareƒ   s    &(z-TestTopologicalSort.test_calls_before_preparec                 C   sH   t  ¡ }| ¡  |  td¡ | ¡  W d   ƒ n1 s:0    Y  d S )Nz!cannot prepare\(\) more than once)r   r   r   rT   rU   rL   r   r   r   Útest_prepare_multiple_times   s    z/TestTopologicalSort.test_prepare_multiple_timesc                 C   s¦   t  ¡ }| dddd¡ | ddd¡ | ¡  | ¡  |  td¡ | d¡ W d   ƒ n1 sb0    Y  |  td¡ | d¡ W d   ƒ n1 s˜0    Y  d S )Nr9   r%   r4   r@   znode 2 was not passed outz#node 24 was not added using add\(\)é   )r   r   r'   r   r   rT   rU   r	   rL   r   r   r   Útest_invalid_nodes_in_done“   s    (z.TestTopologicalSort.test_invalid_nodes_in_donec                 C   sÖ   t  ¡ }| dddd¡ | dd¡ | ¡  |  | ¡ d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ |  | ¡ d¡ | d¡ | d¡ |  | ¡ d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ |  | ¡ ¡ d S )	Nr9   r%   r4   r@   )r4   r@   r   rF   r:   )	r   r   r'   r   r   r   r	   ÚassertFalser   rL   r   r   r   Ú	test_doneŸ   s     



zTestTopologicalSort.test_donec                 C   sš   t  ¡ }| dd¡ | ¡  |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  	| ¡ ¡ d S )Nr9   r%   rF   r:   )
r   r   r'   r   Z
assertTruer   r   r   r	   rZ   rL   r   r   r   Útest_is_activeµ   s    

z"TestTopologicalSort.test_is_activec                 C   sJ   t  ¡ }|  t|jtƒ d¡ |  t|jdtƒ ¡ |  t|jtƒ tƒ ¡ d S )Nr9   )r   r   ZassertRaisesÚ	TypeErrorr'   ÚdictrL   r   r   r   Útest_not_hashable_nodesÄ   s    z+TestTopologicalSort.test_not_hashable_nodesc                 C   s´   dd„ }t  ¡ }| ddd¡ | dd¡ | dd¡ | d	d
¡ | dd
¡ t  ¡ }| dd¡ | ddd¡ | dd
¡ | d	d
¡ | dd¡ |  t||ƒƒt||ƒƒ¡ d S )Nc                 s   s2   |   ¡  |  ¡ r.|  ¡ }| j|Ž  t|ƒV  qd S r   )r   r   r   r	   r   )r   r   r   r   r   Ú
get_groupsË   s
    
z^TestTopologicalSort.test_order_of_insertion_does_not_matter_between_groups.<locals>.get_groupsr4   r%   r9   r   r@   r6   rE   r5   )r   r   r'   r   r   )r    r`   r   rP   r   r   r   Ú6test_order_of_insertion_does_not_matter_between_groupsÊ   s    zJTestTopologicalSort.test_order_of_insertion_does_not_matter_between_groupsc                 C   s@   dd„ }|dƒ}|dƒ}|   |d¡ |   |d¡ |  ||¡ d S )Nc                 S   s8   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()))
                TZ
__cleanenvZPYTHONHASHSEEDz-c)ÚosÚenvironÚcopyr+   r   )ÚseedÚcodeÚenvÚoutr   r   r   Úcheck_order_with_hash_seedã   s    	
zlTestTopologicalSort.test_static_order_does_not_change_with_the_hash_seed.<locals>.check_order_with_hash_seediÒ  i·z  Ú )ZassertNotEqualr   )r    ri   Zrun1Zrun2r   r   r   Ú4test_static_order_does_not_change_with_the_hash_seedâ   s    zHTestTopologicalSort.test_static_order_does_not_change_with_the_hash_seedN)Ú__name__Ú
__module__Ú__qualname__r#   r1   rJ   rK   rM   rO   rQ   rR   rS   rV   rW   rY   r[   r\   r_   ra   rk   r   r   r   r   r      s"   (
r   Ú__main__)Ú	itertoolsr   r   rb   ZunittestZtest.support.script_helperr   ZTestCaser   rl   Úmainr   r   r   r   Ú<module>   s    u