@@ -9,16 +9,47 @@ class DirectedGraphSpec extends Specification {
99 graph. apply DirectedGraphPlugin
1010 }
1111
12- def ' can get outedges and adjacent edges' () {
12+ def ' can get out edges' () {
1313 setup :
14- def expected = [] as LinkedHashSet
14+ def expected = [] as LinkedHashSet<DirectedEdge >
15+ graph. with {
16+ expected << edge(' step1' , ' step2' )
17+ expected << edge(' step1' , ' step3' )
18+ edge ' step4' , ' step1'
19+ edge ' step4' , ' step3'
20+ }
21+
22+ when :
23+ def out = graph. outEdges ' step1'
24+
25+ then :
26+ out == expected
27+ }
28+
29+ def ' can get out degree' () {
30+ setup :
31+ graph. with {
32+ edge(' step1' , ' step2' )
33+ edge(' step1' , ' step3' )
34+ }
35+
36+ when :
37+ def out = graph. outDegree ' step1'
38+
39+ then :
40+ out == 2
41+ }
42+
43+ def ' adjacentEdges are outEdges' () {
44+ setup :
45+ def expected = [] as LinkedHashSet<DirectedEdge >
1546 graph. with {
16- vertex ' step1'
1747 expected << edge(' step1' , ' step2' )
1848 expected << edge(' step1' , ' step3' )
1949 edge ' step4' , ' step1'
2050 edge ' step4' , ' step3'
2151 }
52+
2253 when :
2354 def out = graph. outEdges ' step1'
2455 def adjacent = graph. adjacentEdges ' step1'
@@ -28,6 +59,37 @@ class DirectedGraphSpec extends Specification {
2859 adjacent == expected
2960 }
3061
62+ def ' can get inEdges' () {
63+ setup :
64+ def expected = [] as LinkedHashSet<DirectedEdge >
65+ graph. with {
66+ expected << edge(' step2' , ' step1' )
67+ expected << edge(' step3' , ' step1' )
68+ edge ' step1' , ' step4'
69+ edge ' step3' , ' step4'
70+ }
71+
72+ when :
73+ def inEdges = graph. inEdges ' step1'
74+
75+ then :
76+ inEdges == expected
77+ }
78+
79+ def ' can get in degree' () {
80+ setup :
81+ graph. with {
82+ edge(' step2' , ' step1' )
83+ edge(' step3' , ' step1' )
84+ }
85+
86+ when :
87+ def inEdges = graph. inDegree ' step1'
88+
89+ then :
90+ inEdges == 2
91+ }
92+
3193 def ' traverse is directed' () {
3294 setup :
3395 graph. with {
@@ -62,4 +124,41 @@ class DirectedGraphSpec extends Specification {
62124 preorderList == [' step1' , ' step3' , ' step5' , ' step2' , ' step4' ]
63125 postorderList == [' step2' , ' step5' , ' step3' , ' step4' , ' step1' ]
64126 }
127+
128+ def ' can get topological sort' () {
129+ setup :
130+ graph. with {
131+ vertex(' A' , [connectsTo :[' B' , ' D' , ' E' ]])
132+ vertex(' B' , [connectsTo :[' D' , ' C' ]])
133+ vertex(' C' , [connectsTo :' D' ])
134+ vertex(' D' , [connectsTo :' G' ])
135+ vertex(' G' , [connectsTo :' F' ])
136+ }
137+
138+ when :
139+ def sort = graph. reversePostOrderSort()
140+
141+ then :
142+ sort == [' A' , ' E' , ' B' , ' C' , ' D' , ' G' , ' F' ]
143+ }
144+
145+ def ' can perform reversePostOrder' () {
146+ setup :
147+ graph. with {
148+ vertex(' A' , [connectsTo :[' B' , ' D' , ' E' ]])
149+ vertex(' B' , [connectsTo :[' D' , ' C' ]])
150+ vertex(' C' , [connectsTo :' D' ])
151+ vertex(' D' , [connectsTo :' G' ])
152+ vertex(' G' , [connectsTo :' F' ])
153+ }
154+ def rpo = []
155+
156+ when :
157+ graph. reversePostOrder { vertex ->
158+ rpo << vertex. name
159+ }
160+
161+ then :
162+ rpo == [' A' , ' E' , ' B' , ' C' , ' D' , ' G' , ' F' ]
163+ }
65164}
0 commit comments