o
    °ó®g   ã                   @   sF   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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                 C   sT   dd„ }t  |¡}|  t||ƒƒt|ƒ¡ t  |¡}|  t| ¡ ƒtt|Ž ƒ¡ d S )Nc                 s   sD    |   ¡  |  ¡ r |  ¡ }|D ]}|  |¡ q|V  |  ¡ s	d S d S ©N)ÚprepareÚ	is_activeÚ	get_readyÚdone)ÚtsÚnodesÚnode© r   úH/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_graphlib.pyÚstatic_order_with_groups
   s   €üzATestTopologicalSort._test_graph.<locals>.static_order_with_groups)ÚgraphlibÚTopologicalSorterÚassertEqualÚlistÚstatic_orderr   )ÚselfÚgraphZexpectedr   r
   r   r   r   Ú_test_graph	   s
   

 zTestTopologicalSort._test_graphc           	      C   s”   t  ¡ }| ¡ D ]\}}|j|g|¢R Ž  qz| ¡  W ‚  t jyI } z |j\}}|  d t	t
|ƒ¡d t	t
|d ƒ¡¡ W Y d }~d S d }~ww )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   
ü
6€þ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   é	   é
   r&   r'   ))r(   r*   r)   )r&   r'   )r   r,   r+   é   ©r-   c                 S   s   i | ]}||d  h“qS r.   r   ©Ú.0Úxr   r   r   Ú
<dictcomp>-   s    z9TestTopologicalSort.test_simple_cases.<locals>.<dictcomp>r,   c                 S   s   g | ]}|f‘qS r   r   r/   r   r   r   Ú
<listcomp>-   s    z9TestTopologicalSort.test_simple_cases.<locals>.<listcomp>éÿÿÿÿé   é   é   é   é   )r   r(   r5   r*   r&   r6   r7   r8   ))r-   r9   )r*   r8   )r5   r7   )r(   r6   )r   r&   r   é   r+   )
r   r-   r   r(   r5   r*   r:   r)   r'   r+   ))r+   )r5   )r(   r'   )r-   r*   r)   )r:   ©r   ©r   )r   r-   r   r(   ))r-   r(   r;   r<   )r   r-   r   r(   r5   r*   r:   ))r-   r(   r:   )r   r*   )r   r5   )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   r5   r:   )r-   r(   r*   )r   r5   r:   )r   Úsetr>   r   r   r   Útest_no_dependenciesL   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   r5   )r   r5   r5   r5   r5   r5   )r-   r(   r   )r   r5   r-   )r   r   r   r   r   r   ©r   r
   r   r   r   Útest_the_node_multiple_timesQ   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   r-   Nr   r/   r   r   r   Ú	<genexpr>]   s   € z@TestTopologicalSort.test_graph_with_iterables.<locals>.<genexpr>r*   r   )r-   r(   r*   r)   r+   r   )r=   r   r   r   r   r   )r   r!   r
   r   r   r   Útest_graph_with_iterables\   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 )Nr-   r   r(   r5   r*   >   r   r(   r5   r*   )r   r   r   r   r   )r   r
   Úts2r   r   r   Ú1test_add_dependencies_for_same_node_incrementallya   s    zETestTopologicalSort.test_add_dependencies_for_same_node_incrementallyc                 C   s   |   i g ¡ d S r   )r   r>   r   r   r   Ú
test_emptyl   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 )Nr-   r   )r-   r   )r-   r   r-   r(   )r-   r   r(   )r-   r(   r   r-   r5   r:   )r-   r   r(   r*   r5   r*   r)   )r-   r   r(   r5   r:   r)   )r-   r   r(   r5   )r%   r>   r   r   r   Ú
test_cycleo   s   &*(zTestTopologicalSort.test_cyclec                 C   s¬   t  ¡ }|  td¡ | ¡  W d   ƒ n1 sw   Y  |  td¡ | d¡ W d   ƒ n1 s4w   Y  |  td¡ | ¡  W d   ƒ d S 1 sOw   Y  d S )Nz prepare\(\) must be called firstr(   )r   r   ÚassertRaisesRegexÚ
ValueErrorr   r	   r   rB   r   r   r   Útest_calls_before_prepare}   s   
ÿÿ
"ÿz-TestTopologicalSort.test_calls_before_preparec                 C   sJ   t  ¡ }| ¡  |  td¡ | ¡  W d   ƒ d S 1 sw   Y  d S )Nz!cannot prepare\(\) more than once)r   r   r   rJ   rK   rB   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 s1w   Y  |  td¡ | d¡ W d   ƒ d S 1 sMw   Y  d S )Nr-   r   r(   r5   znode 2 was not passed outz#node 24 was not added using add\(\)é   )r   r   r   r   r   rJ   rK   r	   rB   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 )	Nr-   r   r(   r5   )r(   r5   r   r;   r.   )	r   r   r   r   r   r   r	   ÚassertFalser   rB   r   r   r   Ú	test_done™   s    



zTestTopologicalSort.test_donec                 C   sš   t  ¡ }| dd¡ | ¡  |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  	| ¡ ¡ d S )Nr-   r   r;   r.   )
r   r   r   r   Z
assertTruer   r   r   r	   rP   rB   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 )Nr-   )r   r   ZassertRaisesÚ	TypeErrorr   ÚdictrB   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   s>    |   ¡  |  ¡ r|  ¡ }| j|Ž  t|ƒV  |  ¡ s	d S 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_groupsr(   r   r-   r   r5   r*   r:   r)   )r   r   r   r   r   )r   rV   r
   rF   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   r_   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   r%   r?   rA   rC   rE   rG   rH   rI   rL   rM   rO   rQ   rR   rU   rW   ra   r   r   r   r   r      s$    (
r   )	Ú	itertoolsr   r   rX   ZunittestZtest.support.script_helperr   ZTestCaser   r   r   r   r   Ú<module>   s    