@@ -153,3 +153,184 @@ pub mod error {
153153 }
154154 }
155155}
156+
157+ #[ cfg( test) ]
158+ mod dfs_test {
159+ use std:: path:: PathBuf ;
160+
161+ use hamcrest2:: prelude:: * ;
162+ use hamcrest2:: { assert_that, ok} ;
163+ use petgraph:: { algo:: is_cyclic_directed, graph:: NodeIndex } ;
164+
165+ use crate :: graph:: CachedStableGraph ;
166+ use crate :: { dfs, IncludePosition } ;
167+
168+ #[ test]
169+ fn test_graph_dfs ( ) {
170+ {
171+ let mut graph = CachedStableGraph :: new ( ) ;
172+
173+ let idx0 = graph. add_node ( & PathBuf :: from ( "0" ) ) ;
174+ let idx1 = graph. add_node ( & PathBuf :: from ( "1" ) ) ;
175+ let idx2 = graph. add_node ( & PathBuf :: from ( "2" ) ) ;
176+ let idx3 = graph. add_node ( & PathBuf :: from ( "3" ) ) ;
177+
178+ graph. add_edge ( idx0, idx1, IncludePosition { line : 2 , start : 0 , end : 0 } ) ;
179+ graph. add_edge ( idx0, idx2, IncludePosition { line : 3 , start : 0 , end : 0 } ) ;
180+ graph. add_edge ( idx1, idx3, IncludePosition { line : 5 , start : 0 , end : 0 } ) ;
181+
182+ let dfs = dfs:: Dfs :: new ( & graph, idx0) ;
183+
184+ let mut collection = Vec :: new ( ) ;
185+
186+ for i in dfs {
187+ assert_that ! ( & i, ok( ) ) ;
188+ collection. push ( i. unwrap ( ) ) ;
189+ }
190+
191+ let nodes: Vec < NodeIndex > = collection. iter ( ) . map ( |n| n. 0 ) . collect ( ) ;
192+ let parents: Vec < Option < NodeIndex > > = collection. iter ( ) . map ( |n| n. 1 ) . collect ( ) ;
193+ // 0
194+ // / \
195+ // 1 2
196+ // /
197+ // 3
198+ let expected_nodes = vec ! [ idx0, idx1, idx3, idx2] ;
199+
200+ assert_eq ! ( expected_nodes, nodes) ;
201+
202+ let expected_parents = vec ! [ None , Some ( idx0) , Some ( idx1) , Some ( idx0) ] ;
203+
204+ assert_eq ! ( expected_parents, parents) ;
205+
206+ assert ! ( !is_cyclic_directed( & graph. graph) ) ;
207+ }
208+ {
209+ let mut graph = CachedStableGraph :: new ( ) ;
210+
211+ let idx0 = graph. add_node ( & PathBuf :: from ( "0" ) ) ;
212+ let idx1 = graph. add_node ( & PathBuf :: from ( "1" ) ) ;
213+ let idx2 = graph. add_node ( & PathBuf :: from ( "2" ) ) ;
214+ let idx3 = graph. add_node ( & PathBuf :: from ( "3" ) ) ;
215+ let idx4 = graph. add_node ( & PathBuf :: from ( "4" ) ) ;
216+ let idx5 = graph. add_node ( & PathBuf :: from ( "5" ) ) ;
217+ let idx6 = graph. add_node ( & PathBuf :: from ( "6" ) ) ;
218+ let idx7 = graph. add_node ( & PathBuf :: from ( "7" ) ) ;
219+
220+ graph. add_edge ( idx0, idx1, IncludePosition { line : 2 , start : 0 , end : 0 } ) ;
221+ graph. add_edge ( idx0, idx2, IncludePosition { line : 3 , start : 0 , end : 0 } ) ;
222+ graph. add_edge ( idx1, idx3, IncludePosition { line : 5 , start : 0 , end : 0 } ) ;
223+ graph. add_edge ( idx1, idx4, IncludePosition { line : 6 , start : 0 , end : 0 } ) ;
224+ graph. add_edge ( idx2, idx4, IncludePosition { line : 5 , start : 0 , end : 0 } ) ;
225+ graph. add_edge ( idx2, idx5, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
226+ graph. add_edge ( idx3, idx6, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
227+ graph. add_edge ( idx4, idx6, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
228+ graph. add_edge ( idx6, idx7, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
229+
230+ let dfs = dfs:: Dfs :: new ( & graph, idx0) ;
231+
232+ let mut collection = Vec :: new ( ) ;
233+
234+ for i in dfs {
235+ assert_that ! ( & i, ok( ) ) ;
236+ collection. push ( i. unwrap ( ) ) ;
237+ }
238+
239+ let nodes: Vec < NodeIndex > = collection. iter ( ) . map ( |n| n. 0 ) . collect ( ) ;
240+ let parents: Vec < Option < NodeIndex > > = collection. iter ( ) . map ( |n| n. 1 ) . collect ( ) ;
241+ // 0
242+ // / \
243+ // 1 2
244+ // / \ / \
245+ // 3 4 5
246+ // \ /
247+ // 6 - 7
248+ let expected_nodes = vec ! [ idx0, idx1, idx3, idx6, idx7, idx4, idx6, idx7, idx2, idx5, idx4, idx6, idx7] ;
249+
250+ assert_eq ! ( expected_nodes, nodes) ;
251+
252+ let expected_parents = vec ! [
253+ None ,
254+ Some ( idx0) ,
255+ Some ( idx1) ,
256+ Some ( idx3) ,
257+ Some ( idx6) ,
258+ Some ( idx1) ,
259+ Some ( idx4) ,
260+ Some ( idx6) ,
261+ Some ( idx0) ,
262+ Some ( idx2) ,
263+ Some ( idx2) ,
264+ Some ( idx4) ,
265+ Some ( idx6) ,
266+ ] ;
267+
268+ assert_eq ! ( expected_parents, parents) ;
269+
270+ assert ! ( !is_cyclic_directed( & graph. graph) ) ;
271+ }
272+ }
273+
274+ #[ test]
275+ fn test_graph_dfs_cycle ( ) {
276+ {
277+ let mut graph = CachedStableGraph :: new ( ) ;
278+
279+ let idx0 = graph. add_node ( & PathBuf :: from ( "0" ) ) ;
280+ let idx1 = graph. add_node ( & PathBuf :: from ( "1" ) ) ;
281+ let idx2 = graph. add_node ( & PathBuf :: from ( "2" ) ) ;
282+ let idx3 = graph. add_node ( & PathBuf :: from ( "3" ) ) ;
283+ let idx4 = graph. add_node ( & PathBuf :: from ( "4" ) ) ;
284+ let idx5 = graph. add_node ( & PathBuf :: from ( "5" ) ) ;
285+ let idx6 = graph. add_node ( & PathBuf :: from ( "6" ) ) ;
286+ let idx7 = graph. add_node ( & PathBuf :: from ( "7" ) ) ;
287+
288+ graph. add_edge ( idx0, idx1, IncludePosition { line : 2 , start : 0 , end : 0 } ) ;
289+ graph. add_edge ( idx0, idx2, IncludePosition { line : 3 , start : 0 , end : 0 } ) ;
290+ graph. add_edge ( idx1, idx3, IncludePosition { line : 5 , start : 0 , end : 0 } ) ;
291+ graph. add_edge ( idx1, idx4, IncludePosition { line : 6 , start : 0 , end : 0 } ) ;
292+ graph. add_edge ( idx2, idx4, IncludePosition { line : 5 , start : 0 , end : 0 } ) ;
293+ graph. add_edge ( idx2, idx5, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
294+ graph. add_edge ( idx3, idx6, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
295+ graph. add_edge ( idx4, idx6, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
296+ graph. add_edge ( idx6, idx7, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
297+ graph. add_edge ( idx7, idx4, IncludePosition { line : 4 , start : 0 , end : 0 } ) ;
298+
299+ let mut dfs = dfs:: Dfs :: new ( & graph, idx0) ;
300+
301+ for _ in 0 ..5 {
302+ if let Some ( i) = dfs. next ( ) {
303+ assert_that ! ( & i, ok( ) ) ;
304+ }
305+ }
306+
307+ // 0
308+ // / \
309+ // 1 2
310+ // / \ / \
311+ // 3 4 5
312+ // \ / \
313+ // 6 - 7
314+
315+ assert ! ( is_cyclic_directed( & graph. graph) ) ;
316+
317+ let next = dfs. next ( ) . unwrap ( ) ;
318+ assert_that ! ( next, err( ) ) ;
319+ }
320+ {
321+ let mut graph = CachedStableGraph :: new ( ) ;
322+
323+ let idx0 = graph. add_node ( & PathBuf :: from ( "0" ) ) ;
324+ let idx1 = graph. add_node ( & PathBuf :: from ( "1" ) ) ;
325+
326+ graph. add_edge ( idx0, idx1, IncludePosition { line : 2 , start : 0 , end : 0 } ) ;
327+ graph. add_edge ( idx1, idx0, IncludePosition { line : 2 , start : 0 , end : 0 } ) ;
328+
329+ let mut dfs = dfs:: Dfs :: new ( & graph, idx1) ;
330+
331+ println ! ( "{:?}" , dfs. next( ) ) ;
332+ println ! ( "{:?}" , dfs. next( ) ) ;
333+ println ! ( "{:?}" , dfs. next( ) ) ;
334+ }
335+ }
336+ }
0 commit comments