1 25 40 package org.jgrapht.alg; 41 42 import java.util.*; 43 44 import junit.framework.*; 45 46 import org.jgrapht.*; 47 import org.jgrapht.graph.*; 48 49 50 55 public class CycleDetectorTest 56 extends TestCase 57 { 58 59 61 private static final String V1 = "v1"; 62 private static final String V2 = "v2"; 63 private static final String V3 = "v3"; 64 private static final String V4 = "v4"; 65 private static final String V5 = "v5"; 66 private static final String V6 = "v6"; 67 68 70 75 public void createGraph(Graph<String , DefaultEdge> g) 76 { 77 g.addVertex(V1); 78 g.addVertex(V2); 79 g.addVertex(V3); 80 g.addVertex(V4); 81 g.addVertex(V5); 82 g.addVertex(V6); 83 84 g.addEdge(V1, V2); 85 g.addEdge(V2, V3); 86 g.addEdge(V3, V4); 87 g.addEdge(V4, V1); 88 g.addEdge(V4, V5); 89 g.addEdge(V5, V6); 90 g.addEdge(V1, V6); 91 } 92 93 96 public void testDirectedWithCycle() 97 { 98 DirectedGraph<String , DefaultEdge> g = 99 new DefaultDirectedGraph<String , DefaultEdge>( 100 DefaultEdge.class); 101 createGraph(g); 102 103 Set<String > cyclicSet = new HashSet<String >(); 104 cyclicSet.add(V1); 105 cyclicSet.add(V2); 106 cyclicSet.add(V3); 107 cyclicSet.add(V4); 108 109 Set<String > acyclicSet = new HashSet<String >(); 110 acyclicSet.add(V5); 111 acyclicSet.add(V6); 112 113 runTest(g, cyclicSet, acyclicSet); 114 } 115 116 119 @SuppressWarnings ("unchecked") 120 public void testDirectedWithoutCycle() 121 { 122 DirectedGraph<String , DefaultEdge> g = 123 new DefaultDirectedGraph<String , DefaultEdge>( 124 DefaultEdge.class); 125 createGraph(g); 126 g.removeVertex(V2); 127 128 Set<String > cyclicSet = Collections.EMPTY_SET; Set<String > acyclicSet = g.vertexSet(); 132 133 runTest(g, cyclicSet, acyclicSet); 134 } 135 136 private void runTest( 137 DirectedGraph<String , DefaultEdge> g, 138 Set<String > cyclicSet, 139 Set<String > acyclicSet) 140 { 141 CycleDetector<String , DefaultEdge> detector = 142 new CycleDetector<String , DefaultEdge>(g); 143 144 Set emptySet = Collections.EMPTY_SET; 145 146 assertEquals(!cyclicSet.isEmpty(), detector.detectCycles()); 147 148 assertEquals(cyclicSet, detector.findCycles()); 149 150 for (String v : cyclicSet) { 151 assertEquals(true, detector.detectCyclesContainingVertex(v)); 152 assertEquals(cyclicSet, detector.findCyclesContainingVertex(v)); 153 } 154 155 for (String v : acyclicSet) { 156 assertEquals(false, detector.detectCyclesContainingVertex(v)); 157 assertEquals(emptySet, detector.findCyclesContainingVertex(v)); 158 } 159 } 160 } 161 | Popular Tags |