public void testComplexWithShallowCycle() throws GraphConstraintException { // +-----------+ // | | // v | // a --> b --> c // | | // | v // +---> d Dag dag = new Dag(); try { dag.addEdge("a", "b"); dag.addEdge("b", "c"); dag.addEdge("b", "d"); dag.addEdge("a", "d"); dag.addEdge("c", "a"); fail("Cycle should be detected"); } catch (CycleDetectedException e) { assertCycle(new String[] {"c", "a", "b", "c"}, e.getCycle()); // Ensure that edge was removed assertFalse("Edge 'c' -> 'a' should have been removed.", dag.hasEdge("c", "a")); } }
public void testSimpleSelfCycle() throws GraphConstraintException { // a --> b <---+ // | | // | | // +-----+ Dag dag = new Dag(); try { dag.addEdge("a", "b"); dag.addEdge("b", "b"); fail("Cycle should be detected"); } catch (CycleDetectedException e) { assertCycle(new String[] {"b", "b"}, e.getCycle()); // Ensure that edge was removed assertFalse("Edge 'b' -> 'b' should have been removed.", dag.hasEdge("b", "b")); } }
public void testComplexWithDeepCycle() throws GraphConstraintException { // f --> g --> h // | // | // a --> b --> c --> d // ^ | // | v // +---------- e final Dag dag = new Dag(); try { dag.addEdge("a", "b"); dag.addEdge("b", "c"); dag.addEdge("b", "f"); dag.addEdge("f", "g"); dag.addEdge("g", "h"); dag.addEdge("c", "d"); dag.addEdge("d", "e"); dag.addEdge("e", "b"); fail("Cycle should be detected"); } catch (CycleDetectedException e) { assertCycle(new String[] {"e", "b", "c", "d", "e"}, e.getCycle()); // Ensure that edge was removed assertFalse("Edge 'e' -> 'b' should have been removed.", dag.hasEdge("e", "b")); } }