package graph; import graph.Graph.CycleException; import junit.framework.TestCase; public class GraphTest extends TestCase { public void testAddAndRemove() throws CycleException { Graph g = new Graph(); g.addEdge(1,2); g.addEdge(2,3); g.addEdge(2,4); g.addEdge(4,5); assertEquals(true, g.containsVertex(5)); assertEquals(true, g.containsEdge(1,2)); assertEquals(false, g.containsEdge(2,1)); g.visitDepthFirst(new Visitor() { public void visit(Integer i) { System.out.println(i); } }); g.addEdge(3,1); // create a cycle try { g.visitDepthFirst(new Visitor() { public void visit(Integer i) { System.out.println(i); } }); fail(); // should get an exception due to cycle } catch (CycleException cfe) { System.out.println(cfe); } g.removeEdge(3,1); // take out the cycle g.removeEdge(1,2); g.removeVertex(2); assertEquals(false, g.containsEdge(1,2)); assertEquals(false, g.edgesTo(4).hasNext()); g.removeEdge(4,5); assertEquals(false, g.containsEdge(4,5)); assertEquals(true, g.containsVertex(5)); for (int i : g) System.out.println(i); } }