o
    3ήc.>                     @   sH   d Z ddlZddlZddlmZmZmZ G dd dZG dd dZ	dS )zHUnit tests for the :mod:`networkx.algorithms.minors.contraction` module.    N)arbitrary_elementedges_equalnodes_equalc                   @   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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 )'TestQuotientz)Unit tests for computing quotient graphs.c                    sD   t ddd  fdd}t d}t  |}t ||s J dS )zTests that the quotient graph of the complete *n*-partite graph
        under the "same neighbors" node relation is the complete graph on *n*
        nodes.

                 c                    (   |  | vo| |  vo |   | kS N uvGr   X/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/minors/tests/test_contraction.pysame_neighbors      (zNTestQuotient.test_quotient_graph_complete_multipartite.<locals>.same_neighborsN)nxcomplete_multipartite_graphcomplete_graphquotient_graphis_isomorphicselfr   expectedactualr   r   r   )test_quotient_graph_complete_multipartite   s
   
z6TestQuotient.test_quotient_graph_complete_multipartitec                    sB   t dd  fdd}t d}t  |}t ||sJ dS )zTests that the quotient graph of the complete bipartite graph under
        the "same neighbors" node relation is `K_2`.

        r   r   c                    r	   r
   r   r   r   r   r   r   '   r   zKTestQuotient.test_quotient_graph_complete_bipartite.<locals>.same_neighborsN)r   complete_bipartite_graphr   r   r   r   r   r   r   &test_quotient_graph_complete_bipartite   s
   
z3TestQuotient.test_quotient_graph_complete_bipartitec                 C   s^   t d}dd }dd }t |||}t  }|g d |dd t ||s-J d	S )
zYTests for specifying an alternate edge relation for the quotient
        graph.

           c                 S   s   | |kS r
   r   r   r   r   r   identity7   s   z@TestQuotient.test_quotient_graph_edge_relation.<locals>.identityc                 S   s   t | d t |d kS )Nr   )r   )bcr   r   r   same_parity:   s   zCTestQuotient.test_quotient_graph_edge_relation.<locals>.same_parity)r   r   )r   r   )r   r      r   N)r   
path_graphr   Graphadd_edges_fromadd_edger   )r   r   r!   r$   r   r   r   r   r   !test_quotient_graph_edge_relation0   s   
z.TestQuotient.test_quotient_graph_edge_relationc                    sf   t  }|g d tt |}t ||}|jd   fdd}t ||}t ||s1J dS )zThis tests that the condensation of a graph can be viewed as the
        quotient graph under the "in the same connected component" equivalence
        relation.

        )r&   r   r   r   )r      )r      )r   r   )r   r   r   r    )r       )r1   r    )r1      )r2      )r2   	   )r2   
   )r3   r4   )r4   r2   )r5   r1   )r.   r   )r.   r   )r.   r1   )r/   r1   )r/   r.   mappingc                    s    |   | kS r
   r   r   component_ofr   r   same_componenti   s   zBTestQuotient.test_condensation_as_quotient.<locals>.same_componentN)	r   DiGraphr)   liststrongly_connected_componentscondensationgraphr   r   )r   r   sccCr9   Qr   r7   r   test_condensation_as_quotientC   s   
z*TestQuotient.test_condensation_as_quotientc                 C   s   t d}ddhddhddhg}t j||dd	}t|g d
s!J t| ddgs,J |D ]#}|j| d dks;J |j| d dksFJ |j| d dksQJ q.d S Nr1   r   r&   r   r   r   r    Trelabelr   r&   r   r   r&   r,   nedgesnnodesdensityr   r'   r   r   r   edgesnodesr   r   	partitionMnr   r   r   	test_patho   s   
zTestQuotient.test_pathc                 C   s   t d}ddgddgddgd}t j||d	d
}t|g ds"J t| ddgs-J |D ]#}|j| d dks<J |j| d dksGJ |j| d dksRJ q/d S Nr1   r   r&   r   r   r   r    r   r   r   TrD   rF   rG   r,   rH   rI   rJ   rK   rN   r   r   r   .test_path__partition_provided_as_dict_of_listsz      
z;TestQuotient.test_path__partition_provided_as_dict_of_listsc                 C   s   t d}dddd}t j||dd}t|g dsJ t| dd	gs'J |D ]#}|j| d
 dks6J |j| d dksAJ |j| d dksLJ q)d S )Nr1   rG   r-   r0   rT   TrD   rF   r,   rH   r&   rI   r   rJ   rK   rN   r   r   r   /test_path__partition_provided_as_dict_of_tuples   s   
z<TestQuotient.test_path__partition_provided_as_dict_of_tuplesc                 C   s   t d}ddhddhddhd}t j||d	d
}t|g ds"J t| ddgs-J |D ]#}|j| d dks<J |j| d dksGJ |j| d dksRJ q/d S rS   rK   rN   r   r   r   -test_path__partition_provided_as_dict_of_sets   rV   z:TestQuotient.test_path__partition_provided_as_dict_of_setsc                 C   s   t t d}ddhddhddhg}t j||dd	}t|g d
s$J t| ddgs/J |D ]#}|j| d dks>J |j| d dksIJ |j| d dksTJ q1d S rC   )r   
MultiGraphr'   r   r   r   rL   rM   rN   r   r   r   test_multigraph_path   s   z!TestQuotient.test_multigraph_pathc                 C      t  }t |td ddhddhddhg}t j||dd	}t|g d
s(J t| ddgs3J |D ]#}|j| d dksBJ |j| d dksMJ |j| d dksXJ q5d S Nr1   r   r&   r   r   r   r    TrD   rF   rG   r,   rH   rI   rJ   g      ?)	r   r:   add_pathranger   r   r   rL   rM   rN   r   r   r   test_directed_path      zTestQuotient.test_directed_pathc                 C   r[   r\   )	r   MultiDiGraphr]   r^   r   r   r   rL   rM   rN   r   r   r   test_directed_multigraph_path   r`   z*TestQuotient.test_directed_multigraph_pathc                 C   s^   t tj td}h dddhddhg}t|| W d    d S 1 s(w   Y  d S )Nr1      r   r&   r   r   r   r   r    )pytestraisesr   NetworkXExceptionr'   r   )r   r   rO   r   r   r   test_overlapping_blocks   s
   
"z$TestQuotient.test_overlapping_blocksc                 C   s   t d}tdD ]}|d || |d  d< q	ddhddhddhg}t j||d	d
}t|g ds4J t| ddgs?J |d d d dksKJ |d d d dksWJ |D ]#}|j| d dksfJ |j| d dksqJ |j| d dks|J qYd S )Nr1   r    r&   weightr   r   r   r   TrD   rF   rG   r,   rH   rI   rJ   )r   r'   r^   r   r   r   rL   rM   )r   r   irO   rP   rQ   r   r   r   test_weighted_path   s   
zTestQuotient.test_weighted_pathc                 C   s   t dd}h dh dg}t j||dd}t|ddgsJ t| dgs)J |D ]#}|j| d	 dks8J |j| d
 dksCJ |j| d dksNJ q+d S )Nr   r   rc      r   r   r    TrD   r&   rG   rH   rI   rJ   )r   barbell_graphr   r   r   rL   rM   rN   r   r   r   test_barbell   s   zTestQuotient.test_barbellc                 C   s   t dd}|dd h dh dg}t j||dd}t|ddgs%J t| d	gs/J |d d d
 dks;J |D ]#}|j| d dksJJ |j| d dksUJ |j| d dks`J q=d S )Nr   r   r    rc   rk   TrD   r&   rG   rh   r   rH   rI   rJ   )r   rl   r*   r   r   r   rL   rM   rN   r   r   r   test_barbell_plus   s   zTestQuotient.test_barbell_plusc                 C   s   t d}ddgddgddgg}t j||dd	}t| g d
s#J t| ddgs.J | D ]#}|j| d dks?J |j| d dksJJ |j| d dksUJ q2d S )Nr1   r   r&   r   r   r   r    TrD   rF   rG   r,   rH   rI   rJ         ?r   r'   r   r   rM   r   rL   rN   r   r   r   test_blockmodel   s   
zTestQuotient.test_blockmodelc                 C   s   t t d}ddgddgddgg}t j||t  dd	}t| g d
s)J t| ddgs4J | D ]#}|j| d dksEJ |j| d dksPJ |j| d dks[J q8d S )Nr1   r   r&   r   r   r   r    T)create_usingrE   rF   rG   r,   rH   rI   rJ   ro   )r   rY   r'   r   r   rM   r   rL   rN   r   r   r   test_multigraph_blockmodel   s   z'TestQuotient.test_multigraph_blockmodelc                 C   s   t d}g }t j||dd}t| g sJ t| g s!J ddgddgdgg}t j||dd}t| g d	s>J t| d
gsHJ d S )Nr1   TrD   r   r&   r   r   r    rF   rG   rp   )r   r   rO   Hr   r   r   (test_quotient_graph_incomplete_partition  s   
z5TestQuotient.test_quotient_graph_incomplete_partitionN)__name__
__module____qualname____doc__r   r   r+   rB   rR   rU   rW   rX   rZ   r_   rb   rg   rj   rm   rn   rq   rs   ru   r   r   r   r   r      s(    ,r   c                   @   sx   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d Zdd Zdd Zdd Zdd ZdS )TestContractionz3Unit tests for node and edge contraction functions.c                 C   sB   t d}t |dd}t d}|dd t ||sJ dS )z2Tests for node contraction in an undirected graph.r   r   r&   r   Nr   cycle_graphcontracted_nodesr*   r   r   r   r   r   r   r   r    test_undirected_node_contraction  s
   

z0TestContraction.test_undirected_node_contractionc                 C   sZ   t t d}t |dd}t t d}|dd |dd t ||s+J dS )z/Tests for node contraction in a directed graph.r   r   r&   r   Nr   r:   r|   r}   r*   r   r~   r   r   r   test_directed_node_contraction  s   z.TestContraction.test_directed_node_contractionc                 C   sV   t d}t j|dddd}t d}|dd t ||s!J t ||s)J dS )zUTests for node contraction in an undirected graph
        by making changes in place.r   r   r&   Fcopyr   Nr{   r~   r   r   r   (test_undirected_node_contraction_no_copy#  s   

z8TestContraction.test_undirected_node_contraction_no_copyc                 C   sn   t t d}t j|dddd}t t d}|dd |dd t ||s-J t ||s5J dS )zRTests for node contraction in a directed graph
        by making changes in place.r   r   r&   Fr   r   Nr   r~   r   r   r   &test_directed_node_contraction_no_copy-  s   z6TestContraction.test_directed_node_contraction_no_copyc                 C   s   t jdt  d}|dd |dd |dd t |dd}t  }|dd |dd |dd |dd |dd t|j|jsMJ dS )z5Tests that using a MultiGraph creates multiple edges.r   rr   r   r&   r   Nr   r'   rY   r*   r}   r   rL   r~   r   r   r   test_create_multigraph8  s   z&TestContraction.test_create_multigraphc                 C   s   t jdt  d}|ddd |ddd |ddd t |dd}t  }|ddd |ddd |ddd |ddd |ddd t|j|jsUJ dS )z1Tests that multiedge keys are reset in new graph.r   r   r   r&   r    r   Nr   r~   r   r   r   test_multigraph_keysG  s   z$TestContraction.test_multigraph_keysc                 C   s   t d}d|jd d< d|jd d< t |dd}t d}t |d	dd
}|dd dddii}|jd td|d t 	||sHJ |j|jksPJ dS )z6Tests that node contraction preserves node attributes.r   barr   fooxyzzyr&   bazr   r   r,   )r   contractionN)
r   r|   rM   r}   r   relabel_nodesr*   updatedictr   )r   r   r   r   cdictr   r   r   test_node_attributesV  s   

z$TestContraction.test_node_attributesc                 C   s   t ddg}d|d d d< d|d d d< t |}t |dd}|jd d dks.J |jd d	 d d dks=J t |dd}t|dd
ksOJ dS )z6Tests that node contraction preserves edge attributes.)src1dest)src2r   zsrc1-->destr   r   valuezsrc2-->destr   r   r   N)r   r:   ra   r}   rL   len)r   r   rt   r   r   r   test_edge_attributesg  s   
z$TestContraction.test_edge_attributesc                 C   s:   t d}t j|dddd}t d}t ||sJ dS )z9Tests for node contraction without preserving self-loops.r   r   r&   F)
self_loopsr   N)r   r|   r}   r   r   r~   r   r   r   test_without_self_loopsy  s   

z'TestContraction.test_without_self_loopsc                 C   s   t d}|dd t |dd}t g d}|dd |dd t|j|js.J t |dd}t g d}|dd |dd t|j|jsQJ dS )z5Tests for node contraction when nodes have selfloops.r   r   r&   )r   r   r   )r&   r   r   N)r   r|   r*   r}   r   r   rL   r~   r   r   r   test_contract_selfloop_graph  s   
z,TestContraction.test_contract_selfloop_graphc                 C   @   t d}t |d}t d}|dd t ||sJ dS )z2Tests for edge contraction in an undirected graph.r   rG   r   r   Nr   r|   contracted_edger   r*   r   r~   r   r   r    test_undirected_edge_contraction  
   

z0TestContraction.test_undirected_edge_contractionc                 C   r   )z*Tests for edge contraction in a multigraphr   )r   r&   r   r   r   Nr   r~   r   r   r    test_multigraph_edge_contraction  r   z0TestContraction.test_multigraph_edge_contractionc                 C   sF   t t td}t|d W d   dS 1 sw   Y  dS )z\Tests that attempting to contract a non-existent edge raises an
        exception.

        r   r%   N)rd   re   
ValueErrorr   r|   r   )r   r   r   r   r   test_nonexistent_edge  s   
"z%TestContraction.test_nonexistent_edgeN)rv   rw   rx   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz     s    	
rz   )
ry   rd   networkxr   networkx.utilsr   r   r   r   rz   r   r   r   r   <module>   s      	