o
    3ήcq3                     @   sT   d dl Z d dlZd dlmZmZmZ G dd dZdd Zdd Z	G d	d
 d
Z
dS )    N)check_planarity_recursiveget_counterexampleget_counterexample_recursivec                   @   s   e Zd ZdZed/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'd( Zd)d* Zd+d, Zd-d. ZdS )0TestLRPlanaritya`  Nose Unit tests for the :mod:`networkx.algorithms.planarity` module.

    Tests three things:
    1. Check that the result is correct
        (returns planar if and only if the graph is actually planar)
    2. In case a counter example is returned: Check if it is correct
    3. In case an embedding is returned: Check if its actually an embedding
    Nc                 C   s   t | d\}}t| d\}}|dur*|rd}nd}||ks"J |||ks*J ||r8t| | t| | dS t| | t| | dS )a,  Raises an exception if the lr_planarity check returns a wrong result

        Parameters
        ----------
        G : NetworkX graph
        is_planar : bool
            The expected result of the planarity check.
            If set to None only counter example or embedding are verified.

        TNz/Wrong planarity check result. Should be planar.z3Wrong planarity check result. Should be non-planar.)nxcheck_planarityr   check_embeddingcheck_counterexample)G	is_planaris_planar_lrresultis_planar_lr_rec
result_recmsg r   O/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/tests/test_planarity.pycheck_graph   s   

zTestLRPlanarity.check_graphc                 C       g d}| j t|dd d S )N)      r      r      r      r      )r   r   r      )r!   r   r   r   r   r!   r!   r   Tr   r   r   Graphselfer   r   r   test_simple_planar_graph:   s   z(TestLRPlanarity.test_simple_planar_graphc                 C   r   )N))r   r   r   r   )r   r   )r   r   )r!   r!   r   r   r   r    )r   r!   r"   r   r   r!   r#   Tr%   r&   r(   r   r   r   test_planar_with_selfloopJ   s   z)TestLRPlanarity.test_planar_with_selfloopc                 C   s   | j tdddd d S )Nr   Fr%   )r   r   complete_bipartite_graphr)   r   r   r   	test_k3_3\   s   zTestLRPlanarity.test_k3_3c                 C   s   | j tddd d S Nr!   Fr%   )r   r   complete_graphr1   r   r   r   test_k5_   s   zTestLRPlanarity.test_k5c                 C   r   )N)r   r   r   r   r#   r!   r   )r   r   Tr%   r&   r(   r   r   r   test_multiple_components_planarb   s   z/TestLRPlanarity.test_multiple_components_planarc                 C   s*   t d}|g d | j|dd d S )Nr!   )r   r      )r:   r   Fr%   )r   r4   add_edges_fromr   r)   r
   r   r   r   #test_multiple_components_non_planarf   s   
z3TestLRPlanarity.test_multiple_components_non_planarc                 C   s6   t d}tdD ]}||| q	| j|dd d S r3   )r   r4   rangeadd_edger   )r)   r
   ir   r   r   test_non_planar_with_selfloopm   s   
z-TestLRPlanarity.test_non_planar_with_selfloopc                 C   r   )N)
r    )r   r   r   r   r   r   r   r.   )r   r   r#   r   r   r   Fr%   r&   r(   r   r   r   test_non_planar1t   s   z TestLRPlanarity.test_non_planar1c                 C   s$   ddg}t |}| j|dd d S )Nr   r,   Tr%   r   r'   r   r)   r*   r
   r   r   r   	test_loop      
zTestLRPlanarity.test_loopc                 C   s0   ddg}t |}|dd | j|dd d S )Nr   r   r   r   Tr%   )r   r'   remove_edger   rG   r   r   r   	test_comp   s   
zTestLRPlanarity.test_compc                 C   s$   g d}t |}| j|dd d S )N)r   r-   r   r   r    rB   )r   r:   )r   
   r      r   r"   rC   )r   r   )r   	   )r   rM   )r   rO   r   r#   r   rD   r$   r   r9   )r   rP   r   rM   )r:   rM   )rP   rM   )rM   rO   Tr%   rF   rG   r   r   r   test_goldner_harary   s   
z#TestLRPlanarity.test_goldner_hararyc                 C       t g d}| j|dd d S )N)r   r   r   r   r   r6   Tr%   )r   
MultiGraphr   r<   r   r   r   test_planar_multigraph      z&TestLRPlanarity.test_planar_multigraphc                 C   s2   t t d}|dgd  | j|dd d S )Nr!   r   Fr%   )r   rT   r4   r;   r   r<   r   r   r   test_non_planar_multigraph   s   z*TestLRPlanarity.test_non_planar_multigraphc                 C   rS   )N)r   r   r"   )r   r   )r   r   rL   )r   r   Tr%   )r   DiGraphr   r<   r   r   r   test_planar_digraph   rV   z#TestLRPlanarity.test_planar_digraphc                 C   s:   t t d}|dd |dd | j|dd d S )Nr!   r   r   r   Fr%   )r   rX   r4   rJ   r   r<   r   r   r   test_non_planar_digraph   s   z'TestLRPlanarity.test_non_planar_digraphc                 C   s$   t  }|d | j|dd d S )Nr   Tr%   )r   r'   add_noder   r<   r   r   r   test_single_component   rI   z%TestLRPlanarity.test_single_componentc                 C   rS   )N)
)r   rM   r      r   r^   )r   rO   r   r:   )r:   r^   )r   r   r   r   )r   rM   rB   Tr%   r   OrderedGraphr   r<   r   r   r   test_graph1   s   zTestLRPlanarity.test_graph1c                 C   rS   )N)r   )r   r^   )r   r^   r#   rQ   rB   )r   r   rC   r7   )r   r^   )r   r:   r`   )r   rP   r]   r   )r   r   )r   r:   Fr%   rb   r<   r   r   r   test_graph2   s   zTestLRPlanarity.test_graph2c                 C   rS   )N)ra   )r   rO   r   )r:   rP   )r   rO   rB   r_   rN   r.   r$   r-   )r   r   )r!   rO   )r!   r^   Fr%   rb   r<   r   r   r   test_graph3   s   zTestLRPlanarity.test_graph3c                 C   L   t tj t }|d t| W d    d S 1 sw   Y  d S Nr   )pytestraisesr   NetworkXExceptionr'   r[   r   r<   r   r   r   test_counterexample_planar
  
   

"z*TestLRPlanarity.test_counterexample_planarc                 C   rg   rh   )ri   rj   r   rk   r'   r[   r   r<   r   r   r   $test_counterexample_planar_recursive  rm   z4TestLRPlanarity.test_counterexample_planar_recursive)N)__name__
__module____qualname____doc__staticmethodr   r+   r/   r2   r5   r8   r=   rA   rE   rH   rK   rR   rU   rW   rY   rZ   r\   rd   re   rf   rl   rn   r   r   r   r   r      s2    	$"r   c                 C   s   t |tjstd|  t| jt|jksJ dt }| jD ] }|d |d krC||d |d f ||d |d f q#|t|jksOJ ddS )a;  Raises an exception if the combinatorial embedding is not correct

    Parameters
    ----------
    G : NetworkX graph
    embedding : a dict mapping nodes to a list of edges
        This specifies the ordering of the outgoing edges from a node for
        a combinatorial embedding

    Notes
    -----
    Checks the following things:
        - The type of the embedding is correct
        - The nodes and edges match the original graph
        - Every half edge has its matching opposite half edge
        - No intersections of edges (checked by Euler's formula)
    z-Bad embedding. Not of type nx.PlanarEmbeddingz4Bad embedding. Nodes don't match the original graph.r   r   z4Bad embedding. Edges don't match the original graph.N)	
isinstancer   PlanarEmbeddingrk   check_structuresetnodesedgesadd)r
   	embeddingg_edgesedger   r   r   r     s&   


r   c                 C   s  t |}|D ]}|||r||| qt|}t|dkr[| }||vr)q|j| }|dkrUt|| }t	|}t	|}|
| |
| || ||| t|dks t|dkrqt t d|sot ddS t|dkrt t dd|st ddS t d)zRaises an exception if the counterexample is wrong.

    Parameters
    ----------
    G : NetworkX graph
    subdivision_nodes : set
        A set of nodes inducing a subgraph as a counterexample
    r   r   r!   zBad counter example.r   r   N)r   r'   has_edgerJ   listlenpopdegreeiternextappendremove_noder?   is_isomorphicr4   rk   r0   )r
   	sub_graphucontractcontract_noder   	neighborsvr   r   r   r	   C  s:   








r	   c                   @   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edd ZdS )TestPlanarEmbeddingClassc                 C   s6   |  d}| }ddgdgdgd}||ksJ d S )Nr   r   r   r   )r   r   r   )get_star_embeddingget_data)r)   r{   datadata_cmpr   r   r   test_get_dataw  s   
z&TestPlanarEmbeddingClass.test_get_datac                 C   sZ   t tj t }|dd |dd |  W d    d S 1 s&w   Y  d S Nr   r   )ri   rj   r   rk   ru   r?   rv   r)   r{   r   r   r   test_missing_edge_orientation}  s   
"z6TestPlanarEmbeddingClass.test_missing_edge_orientationc                 C   sf   t tj# t }|dd |dd |dd |  W d    d S 1 s,w   Y  d S Nr   r   r   )ri   rj   r   rk   ru   add_half_edge_firstr?   rv   r   r   r   r   test_invalid_edge_orientation  s   
"z6TestPlanarEmbeddingClass.test_invalid_edge_orientationc                 C   sN   t tj t }|dd |  W d    d S 1 s w   Y  d S r   )ri   rj   r   rk   ru   r   rv   r   r   r   r   test_missing_half_edge  s
   
"z/TestPlanarEmbeddingClass.test_missing_half_edgec                 C   sr   t tj) t }tdD ]}tdD ]}||kr!||| qq|  W d    d S 1 s2w   Y  d S )Nr!   )ri   rj   r   rk   ru   r>   r   rv   )r)   r{   r@   jr   r   r   !test_not_fulfilling_euler_formula  s   
"z:TestPlanarEmbeddingClass.test_not_fulfilling_euler_formulac                 C   sH   t tj t }|ddd W d    d S 1 sw   Y  d S r   )ri   rj   r   rk   ru   add_half_edge_cwr   r   r   r   test_missing_reference  s   "z/TestPlanarEmbeddingClass.test_missing_referencec                 C   s   t  }|dd d S r   )r   ru   connect_componentsr   r   r   r   test_connect_components  s   z0TestPlanarEmbeddingClass.test_connect_componentsc                 C   s@   t  }|dd |dd |dd}|ddgksJ d S r   )r   ru   r   traverse_face)r)   r{   facer   r   r   test_successful_face_traversal  s
   z7TestPlanarEmbeddingClass.test_successful_face_traversalc                 C   sj   t tj% t }|jddddd |jddddd |dd W d    d S 1 s.w   Y  d S )Nr   r   r   )ccwcw)ri   rj   r   rk   ru   r?   r   r   r   r   r    test_unsuccessful_face_traversal  s   "z9TestPlanarEmbeddingClass.test_unsuccessful_face_traversalc                 C   s4   t  }td| D ]}|d| ||d q	|S )Nr   r   )r   ru   r>   r   )nr{   r@   r   r   r   r     s
   z+TestPlanarEmbeddingClass.get_star_embeddingN)ro   rp   rq   r   r   r   r   r   r   r   r   r   rs   r   r   r   r   r   r   v  s    	r   )ri   networkxr   networkx.algorithms.planarityr   r   r   r   r   r	   r   r   r   r   r   <module>   s      *3