o
    3ήcr                     @   sP   d Z ddlmZ ddlZddlZddlmZmZ G dd dZ	G dd dZ
dS )	zBUnit tests for the :mod:`~networkx.algorithms.tree.coding` module.    )productN)edges_equalnodes_equalc                   @   sP   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d Z
dd ZdS )TestPruferSequenceuN   Unit tests for the Prüfer sequence encoding and decoding
    functions.

    c                 C   sF   t tj td}t| W d    d S 1 sw   Y  d S )N   )pytestraisesnxNotATreecycle_graphto_prufer_sequenceselfG r   Q/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/tree/tests/test_coding.pytest_nontree   s   
"zTestPruferSequence.test_nontreec                 C   @   t tj tt  W d    d S 1 sw   Y  d S N)r   r   r	   NetworkXPointlessConceptr   
null_graphr   r   r   r   test_null_graph      "z"TestPruferSequence.test_null_graphc                 C   r   r   )r   r   r	   r   r   trivial_graphr   r   r   r   test_trivial_graph   r   z%TestPruferSequence.test_trivial_graphc                 C   sL   t t ttjd}t| W d    d S 1 sw   Y  d S )Nabc)r   r   KeyErrorr	   Graphutilspairwiser   )r   Tr   r   r   test_bad_integer_labels   s   "z*TestPruferSequence.test_bad_integer_labelsc                 C   s,   t g d}t |}|g dksJ dS )u_   Tests for encoding a tree as a Prüfer sequence using the
        iterative strategy.

        r   r      r   )   r   )r      )r(      r   r   r   r(   N)r	   r   r   )r   treesequencer   r   r   test_encoding"   s   
z TestPruferSequence.test_encodingc                 C   sN   g d}t |}tt|ttdsJ g d}tt| |s%J dS )u2   Tests for decoding a tree from a Prüfer sequence.r*      r#   Nr	   from_prufer_sequencer   listranger   edgesr   r,   r+   r3   r   r   r   test_decoding,   s
   
z TestPruferSequence.test_decodingc                 C   sN   g d}t |}tt|ttdsJ g d}tt| |s%J d S )N)r'   r(   r   r&   r   r      )r   r&   )r   r(   r%   )r'   r(   r'   r)   r   r.   r      r/   r4   r   r   r   test_decoding25   s
   
z!TestPruferSequence.test_decoding2c                 C   s   t dD ]$}t t |}tt|t|sJ tt| t| s)J qtt	dddD ]}t t |}t||ksDJ q2dS )z<Tests that the encoding and decoding functions are inverses.r(   r'   )repeatN)
r	   nonisomorphic_treesr0   r   r   r1   r   r3   r   r2   )r   r!   T2seqseq2r   r   r   test_inverse=   s    zTestPruferSequence.test_inverseN)__name__
__module____qualname____doc__r   r   r   r"   r-   r5   r<   rB   r   r   r   r   r   
   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 )TestNestedTuplez@Unit tests for the nested tuple encoding and decoding functions.c                 C   H   t tj td}t|d W d    d S 1 sw   Y  d S )Nr   r   )r   r   r	   r
   r   to_nested_tupler   r   r   r   r   L      
"zTestNestedTuple.test_nontreec                 C   rH   )Nr'   bogus)r   r   r	   NodeNotFound
path_graphrI   r   r   r   r   test_unknown_rootQ   rJ   z!TestNestedTuple.test_unknown_rootc                 C   s.   t dd}d}t |d}t||sJ d S )Nr'   r;   r   r   rP   r   )r	   full_rary_treerI   r   )r   r!   expectedactualr   r   r   r-   V   s   zTestNestedTuple.test_encodingc                 C   sZ   t  }|g d |ddg |ddg d}t j||dd}d	}||ks+J d S )
N)r7   r   r'   r$   r&   r(   )r&   r)   r9   r:   r   T)canonical_form)r   rP   rP   )r	   r   add_edges_fromrI   )r   r!   rootrS   rR   r   r   r   test_canonical_form\   s   z#TestNestedTuple.test_canonical_formc                 C   s.   d}t dd}t |}t ||sJ d S )NrO   r'   r;   )r	   rQ   from_nested_tupleis_isomorphic)r   balancedrR   rS   r   r   r   r5   f   s   
zTestNestedTuple.test_decodingc                 C   sN   d}t j|dd}g d}tt|ttdsJ tt| |s%J d S )NrO   T)sensible_relabeling)r7   rT   r%   rU   r8   )r'   r.   r;   )r	   rZ   r   r1   r2   r   r3   )r   r\   r!   r3   r   r   r   test_sensible_relabelingl   s
   z(TestNestedTuple.test_sensible_relabelingN)
rC   rD   rE   rF   r   rN   r-   rY   r5   r^   r   r   r   r   rG   I   s    
rG   )rF   	itertoolsr   r   networkxr	   networkx.utilsr   r   r   rG   r   r   r   r   <module>   s    ?