o
    ˷e                     @   s>   d Z ddlmZm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   s8    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n
cycled_seqx r   \/var/www/ideatree/venv/lib/python3.10/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   )seq1r   r   r   r   cyclic_equals   s   r   c                   @   s8   e Zd Z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   sN   t tdd |D }|D ]}t||r d S t||r d S q| d d S )Nc                 S   s   g | ]}t t|qS r   )r   reversed)r   er   r   r   
<listcomp>.   s    z>TestChainDecomposition.assertContainsChain.<locals>.<listcomp>zchain not found)listr   r   fail)selfchainexpectedreversed_chain	candidater   r   r   assertContainsChain*   s   

z*TestChainDecomposition.assertContainsChainc                 C   s^   g d}t |}g dddgddgg dg dg}tt j|d	d
}t|t|ks-J 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$   )r0   )r,   r+   )r+   r'   )r1   )r*   r)   )r)   r(   r"   root)nxGraphr   chain_decompositionr   )r   edgesGr   chainsr   r   r   test_decomposition7   s   
z)TestChainDecomposition.test_decompositionc                 C   s\   t dd}tt j|dd}g dg dg}t|t|ks!J |D ]}| || q#d S )Nr$   r   r3   )r   r"   r!   )r#   r   r%   )r&   r'   r2   )r5   barbell_graphr   r7   r   r    )r   r9   r:   r   r   r   r   r   test_barbell_graphZ   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 |}g dg dg d	g d
g}t	|t	|ksBJ |D ]}| 
|| qDdS )z4Test for a graph with multiple connected components.r$   r   r(   abcdefFcopyr<   r=   )ab)rE   c)rF   rD   )dr   )r   f)rI   rH   Nr5   r>   dictziprangerelabel_nodesunionr   r7   r   r    r   r9   Hmappingr:   r   r   r   r   r   test_disconnected_graphc   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}g d	g d
g}t	|t	|ks>J |D ]}| 
|| q@dS )z4Test for a single component of a disconnected graph.r$   r   r(   r@   FrA   rD   r3   rC   rG   NrJ   rP   r   r   r   !test_disconnected_graph_root_nodeu   s   z8TestChainDecomposition.test_disconnected_graph_root_nodeN)	__name__
__module____qualname____doc__r    r;   r?   rS   rT   r   r   r   r   r   '   s    #	r   )	rX   	itertoolsr   r   networkxr5   r   r   r   r   r   r   r   <module>   s    