| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package net.sourceforge.combean.test.graph.alg.traversal; |
| 23 | |
|
| 24 | |
import junit.framework.TestCase; |
| 25 | |
import net.sourceforge.combean.graph.alg.traversal.FindPathByTraversalAlg; |
| 26 | |
import net.sourceforge.combean.interfaces.graph.Node; |
| 27 | |
import net.sourceforge.combean.interfaces.graph.NodeIterator; |
| 28 | |
import net.sourceforge.combean.interfaces.graph.containers.FixedPath; |
| 29 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalNumberedGraphProp; |
| 30 | |
import net.sourceforge.combean.test.helpers.factories.GraphFixtureFactory; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class TestFindPathByTraversalAlg extends TestCase { |
| 37 | |
|
| 38 | |
private static final int NUMNODES = 9; |
| 39 | 12 | GlobalNumberedGraphProp path = null; |
| 40 | 12 | GlobalNumberedGraphProp cycle = null; |
| 41 | |
|
| 42 | 12 | private FindPathByTraversalAlg findPathAlg = null; |
| 43 | |
|
| 44 | |
public static void main(String[] args) { |
| 45 | 0 | junit.textui.TestRunner.run(TestFindPathByTraversalAlg.class); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
protected void setUp() throws Exception { |
| 52 | 12 | super.setUp(); |
| 53 | |
|
| 54 | 12 | this.path = GraphFixtureFactory.createDirectedPath(NUMNODES); |
| 55 | 12 | this.cycle = GraphFixtureFactory.createDirectedCycle(NUMNODES); |
| 56 | |
|
| 57 | 12 | this.findPathAlg = new FindPathByTraversalAlg(); |
| 58 | 12 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
protected void tearDown() throws Exception { |
| 64 | 12 | super.tearDown(); |
| 65 | 12 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public TestFindPathByTraversalAlg(String name) { |
| 72 | 12 | super(name); |
| 73 | 12 | } |
| 74 | |
|
| 75 | |
public final void testUnreachableNode() { |
| 76 | 3 | this.findPathAlg.setGraph(this.path); |
| 77 | 3 | this.findPathAlg.setUseOutgoingEdgesOnly(true); |
| 78 | 3 | Node s = this.path.getNode(1); |
| 79 | 3 | Node t = this.path.getNode(0); |
| 80 | 3 | this.findPathAlg.setStartNode(s); |
| 81 | 3 | this.findPathAlg.setTargetNode(t); |
| 82 | |
|
| 83 | 3 | this.findPathAlg.run(); |
| 84 | |
|
| 85 | 3 | FixedPath p = this.findPathAlg.getPathTo(t); |
| 86 | 3 | assertNull("no path may have been found", p); |
| 87 | 3 | } |
| 88 | |
|
| 89 | |
public final void testFindPathInDirectedPath() { |
| 90 | 3 | this.findPathAlg.setGraph(this.path); |
| 91 | 3 | this.findPathAlg.setUseOutgoingEdgesOnly(true); |
| 92 | 3 | Node s = this.path.getNode(1); |
| 93 | 3 | Node t = this.path.getNode(NUMNODES-2); |
| 94 | 3 | this.findPathAlg.setStartNode(s); |
| 95 | 3 | this.findPathAlg.setTargetNode(t); |
| 96 | |
|
| 97 | 3 | this.findPathAlg.run(); |
| 98 | |
|
| 99 | 3 | FixedPath p = this.findPathAlg.getPathTo(t); |
| 100 | 3 | assertNotNull(p); |
| 101 | |
|
| 102 | 3 | NodeIterator itNodesInPath = p.getNodeIterator(); |
| 103 | 3 | int currNodeNum = 1; |
| 104 | 24 | while (itNodesInPath.hasNext()) { |
| 105 | 21 | Node v = itNodesInPath.next(); |
| 106 | 21 | assertEquals(currNodeNum++, this.path.getNodeNumber(v)); |
| 107 | 21 | } |
| 108 | 3 | assertEquals(NUMNODES-1, currNodeNum); |
| 109 | 3 | } |
| 110 | |
|
| 111 | |
public final void testFindPathInUndirectedPath() { |
| 112 | 3 | this.findPathAlg.setGraph(this.path); |
| 113 | 3 | this.findPathAlg.setUseOutgoingEdgesOnly(false); |
| 114 | 3 | Node s = this.path.getNode(NUMNODES-2); |
| 115 | 3 | Node t = this.path.getNode(1); |
| 116 | 3 | this.findPathAlg.setStartNode(s); |
| 117 | 3 | this.findPathAlg.setTargetNode(t); |
| 118 | |
|
| 119 | 3 | this.findPathAlg.run(); |
| 120 | |
|
| 121 | 3 | FixedPath p = this.findPathAlg.getPathTo(t); |
| 122 | 3 | assertNotNull(p); |
| 123 | |
|
| 124 | 3 | NodeIterator itNodesInPath = p.getNodeIterator(); |
| 125 | 3 | int currNodeNum = NUMNODES-2; |
| 126 | 24 | while (itNodesInPath.hasNext()) { |
| 127 | 21 | Node v = itNodesInPath.next(); |
| 128 | 21 | assertEquals(currNodeNum--, this.path.getNodeNumber(v)); |
| 129 | 21 | } |
| 130 | 3 | assertEquals(0, currNodeNum); |
| 131 | 3 | } |
| 132 | |
|
| 133 | |
public final void testFindPathInCycle() { |
| 134 | 3 | this.findPathAlg.setGraph(this.cycle); |
| 135 | 3 | this.findPathAlg.setUseOutgoingEdgesOnly(false); |
| 136 | 3 | Node s = this.cycle.getNode(1); |
| 137 | 3 | Node t = this.cycle.getNode(NUMNODES-2); |
| 138 | 3 | this.findPathAlg.setStartNode(s); |
| 139 | 3 | this.findPathAlg.setTargetNode(t); |
| 140 | |
|
| 141 | 3 | this.findPathAlg.run(); |
| 142 | |
|
| 143 | 3 | FixedPath p = this.findPathAlg.getPathTo(t); |
| 144 | 3 | assertNotNull(p); |
| 145 | 3 | assertEquals(4, p.getNumNodes()); |
| 146 | 3 | } |
| 147 | |
} |