o
    HXîh_!  ã                   @   sZ   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r+e 
¡  dS 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|ƒ|¡ q"d S )Nc                 s   sL    |   ¡  |  ¡ r$|  ¡ }|D ]}|  |¡ qtt|ƒƒV  |  ¡ s	d S d S ©N)ÚprepareÚ	is_activeÚ	get_readyÚdoneÚtupleÚsorted)ÚtsÚnodesÚnode© r   ú</opt/python-3.10.19/usr/lib/python3.10/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>   s    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 ‚  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$   é	   é
   r1   r2   ))r3   r5   r4   )r2   r1   )r$   r6   r7   é   ©r8   c                 S   s   i | ]}||d  h“qS r9   r   ©r   Úxr   r   r   Ú
<dictcomp>3   s    z9TestTopologicalSort.test_simple_cases.<locals>.<dictcomp>r7   c                 S   s   g | ]}|f‘qS r   r   r:   r   r   r   Ú
<listcomp>3   s    z9TestTopologicalSort.test_simple_cases.<locals>.<listcomp>éÿÿÿÿé   é   é   é   é   )r$   r3   r?   r5   r1   r@   rA   rB   ))r8   rC   )r5   rB   )r?   rA   )r3   r@   )r$   r1   r$   é   r6   )
r   r8   r$   r3   r?   r5   rD   r4   r2   r6   ))r6   )r?   )r3   r2   )r8   r5   r4   )rD   ©r$   ©r   )r   r8   r$   r3   ))r8   r3   rE   rF   )r   r8   r$   r3   r?   r5   rD   ))r8   r3   rD   )r$   r5   )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?   rD   )r8   r3   r5   )r$   r?   rD   )r"   r   rH   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?   )r8   r3   r   )r$   r?   )r   r8   r3   r8   )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$   r8   Nr   r:   r   r   r   Ú	<genexpr>c   s   € z@TestTopologicalSort.test_graph_with_iterables.<locals>.<genexpr>r5   r   )r8   r3   r5   r4   r6   r   )rG   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 )Nr8   r$   r3   r?   r5   >   r$   r3   r?   r5   )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"   rH   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 )Nr8   r$   )r8   r$   )r8   r$   r8   r3   )r8   r$   r3   )r8   r3   r$   r8   r?   rD   )r8   r$   r3   r5   r?   r5   r4   )r8   r$   r3   r?   rD   r4   )r8   r$   r3   r?   )r0   rH   r   r   r   Ú
test_cycleu   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 firstr3   )r   r   ÚassertRaisesRegexÚ
ValueErrorr   r	   r   rK   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   rS   rT   rK   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 )Nr8   r$   r3   r?   znode 2 was not passed outz#node 24 was not added using add\(\)é   )r   r   r&   r   r   rS   rT   r	   rK   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 )	Nr8   r$   r3   r?   )r3   r?   r   rE   r9   )	r   r   r&   r   r   r   r	   ÚassertFalser   rK   r   r   r   Ú	test_doneŸ   s    



zTestTopologicalSort.test_donec                 C   sš   t  ¡ }| dd¡ | ¡  |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  | ¡ ¡ |  | ¡ d¡ |  | ¡ ¡ | d¡ |  	| ¡ ¡ d S )Nr8   r$   rE   r9   )
r   r   r&   r   Z
assertTruer   r   r   r	   rY   rK   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 )Nr8   )r   r   ZassertRaisesÚ	TypeErrorr&   ÚdictrK   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_groupsr3   r$   r8   r   r?   r5   rD   r4   )r   r   r&   r   r   )r   r_   r   rO   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   rh   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"   r0   rI   rJ   rL   rN   rP   rQ   rR   rU   rV   rX   rZ   r[   r^   r`   rj   r   r   r   r   r      s$    (
r   Ú__main__)Ú	itertoolsr   r   ra   ZunittestZtest.support.script_helperr   ZTestCaser   rk   Úmainr   r   r   r   Ú<module>   s     uÿ