U
    ?h                     @   sF   d Z ddlmZmZ ddlZddlZdd Zdd ZG dd	 d	Z	dS )
z1Unit tests for the chain decomposition functions.    )cycleisliceNc                 c   s6   t | }t| }| D ]}tt||V  t| qdS )zYields cyclic permutations of the given sequence.

    For example::

        >>> list(cycles("abc"))
        [('a', 'b', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b')]

    N)lenr   tupler   next)seqnZ
cycled_seqx r
   W/var/www/html/venv/lib/python3.8/site-packages/networkx/algorithms/tests/test_chains.pycycles	   s
    	r   c                    s"   t   t fddt| D S )zDecide whether two sequences are equal up to cyclic permutations.

    For example::

        >>> cyclic_equals("xyz", "zxy")
        True
        >>> cyclic_equals("xyz", "zyx")
        False

    c                 3   s   | ]}|t  kV  qd S )N)r   ).0r	   seq2r
   r   	<genexpr>&   s     z cyclic_equals.<locals>.<genexpr>)r   anyr   )Zseq1r   r
   r   r   cyclic_equals   s    r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestChainDecompositionz0Unit tests for the chain decomposition function.c                 C   sJ   t tdd |D }|D ] }t||r, qFt||r qFq| d d S )Nc                 S   s   g | ]}t t|qS r
   )r   reversed)r   er
   r
   r   
<listcomp>0   s     z>TestChainDecomposition.assertContainsChain.<locals>.<listcomp>zchain not found)listr   r   Zfail)selfchainexpectedZreversed_chain	candidater
   r
   r   assertContainsChain,   s    

z*TestChainDecomposition.assertContainsChainc                 C   s|   ddddddddd	d
ddddg}t |}d
ddgddgddgdddgdddgg}tt j|dd}t|t|ksxtd S )N      )r      r       )r       )r#      )r$      )r%      )r#   	   )r'   
   )r   r    )r   r"   )r   r#   )r#   r(   )r$   r&   )r    r   )r   r   )r"   r    r#   r    )r(   r'   )r'   r#   )r&   r%   )r%   r$   r   root)nxGraphr   chain_decompositionr   AssertionError)r   edgesGr   chainsr
   r
   r   test_decomposition9   s0    
z)TestChainDecomposition.test_decompositionc                 C   s`   t dd}tt j|dd}dddgddd	gg}t|t|ksFt|D ]}| || qJd S )
Nr    r   r*   r   r   r   r   r   r!   r"   r#   r)   )r,   barbell_graphr   r.   r   r/   r   )r   r1   r2   r   r   r
   r
   r   test_barbell_graph\   s    z)TestChainDecomposition.test_barbell_graphc                 C   s   t dd}t dd}tttdd}t j||dd t ||}tt |}ddd	gd
ddgdddgdddgg}t	|t	|kst
|D ]}| || qdS )z4Test for a graph with multiple connected components.r    r   r$   abcdefFcopyr4   r   r5   r!   r6   r)   abr>   cr@   r=   dr   r   frE   rC   Nr,   r7   dictziprangeZrelabel_nodesunionr   r.   r   r/   r   r   r1   Hmappingr2   r   r   r
   r
   r   test_disconnected_graphe   s    z.TestChainDecomposition.test_disconnected_graphc                 C   s   t dd}t dd}tttdd}t j||dd t ||}tt j|dd}d	d
dgdddgg}t	|t	|kst
|D ]}| || qdS )z4Test for a single component of a disconnected graph.r    r   r$   r9   Fr:   r=   r*   r<   r?   rA   rB   rD   rF   NrG   rL   r
   r
   r   !test_disconnected_graph_root_nodew   s    z8TestChainDecomposition.test_disconnected_graph_root_nodec              	   C   sB   t  }|dddg tt j t j|dd W 5 Q R X dS )z2Test chain decomposition when root is not in graphr   r   r    r$   r*   N)r,   r-   Zadd_nodes_frompytestZraisesZNodeNotFoundZhas_bridges)r   r1   r
   r
   r   &test_chain_decomposition_root_not_in_G   s    z=TestChainDecomposition.test_chain_decomposition_root_not_in_GN)
__name__
__module____qualname____doc__r   r3   r8   rO   rP   rR   r
   r
   r
   r   r   )   s   #	r   )
rV   	itertoolsr   r   rQ   Znetworkxr,   r   r   r   r
   r
   r
   r   <module>   s   