1+ import random
12import unittest
2- from igraph import *
3+
4+ from igraph import Graph , Matrix
35
46
57class DirectedUndirectedTests (unittest .TestCase ):
@@ -37,7 +39,7 @@ def testToUndirected(self):
3739 graph2 .es ["weight" ] == [7 , 3 , 11 ] or graph2 .es ["weight" ] == [3 , 7 , 11 ]
3840 )
3941
40- def testToDirected (self ):
42+ def testToDirectedNoModeArg (self ):
4143 graph = Graph ([(0 , 1 ), (0 , 2 ), (2 , 3 ), (2 , 4 )], directed = False )
4244 graph .to_directed ()
4345 self .assertTrue (graph .is_directed ())
@@ -47,6 +49,51 @@ def testToDirected(self):
4749 == [(0 , 1 ), (0 , 2 ), (1 , 0 ), (2 , 0 ), (2 , 3 ), (2 , 4 ), (3 , 2 ), (4 , 2 )]
4850 )
4951
52+ def testToDirectedMutual (self ):
53+ graph = Graph ([(0 , 1 ), (0 , 2 ), (2 , 3 ), (2 , 4 )], directed = False )
54+ graph .to_directed ("mutual" )
55+ self .assertTrue (graph .is_directed ())
56+ self .assertTrue (graph .vcount () == 5 )
57+ self .assertTrue (
58+ sorted (graph .get_edgelist ())
59+ == [(0 , 1 ), (0 , 2 ), (1 , 0 ), (2 , 0 ), (2 , 3 ), (2 , 4 ), (3 , 2 ), (4 , 2 )]
60+ )
61+
62+ def testToDirectedAcyclic (self ):
63+ graph = Graph ([(0 , 1 ), (2 , 0 ), (3 , 0 ), (3 , 0 ), (4 , 2 )], directed = False )
64+ graph .to_directed ("acyclic" )
65+ self .assertTrue (graph .is_directed ())
66+ self .assertTrue (graph .vcount () == 5 )
67+ print (graph .get_edgelist ())
68+ self .assertTrue (
69+ sorted (graph .get_edgelist ())
70+ == [(0 , 1 ), (0 , 2 ), (0 , 3 ), (0 , 3 ), (2 , 4 )]
71+ )
72+
73+ def testToDirectedRandom (self ):
74+ random .seed (0 )
75+
76+ graph = Graph .Ring (200 , directed = False )
77+ graph .to_directed ("random" )
78+
79+ self .assertTrue (graph .is_directed ())
80+ self .assertTrue (graph .vcount () == 200 )
81+ edgelist1 = sorted (graph .get_edgelist ())
82+
83+ graph = Graph .Ring (200 , directed = False )
84+ graph .to_directed ("random" )
85+
86+ self .assertTrue (graph .is_directed ())
87+ self .assertTrue (graph .vcount () == 200 )
88+ edgelist2 = sorted (graph .get_edgelist ())
89+
90+ self .assertTrue (edgelist1 != edgelist2 )
91+
92+ def testToDirectedInvalidMode (self ):
93+ graph = Graph ([(0 , 1 ), (0 , 2 ), (2 , 3 ), (2 , 4 )], directed = False )
94+ with self .assertRaises (ValueError ):
95+ graph .to_directed ("no-such-mode" )
96+
5097
5198class GraphRepresentationTests (unittest .TestCase ):
5299 def testGetAdjacency (self ):
0 commit comments