| 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.NumberNode; |
| 26 | |
import net.sourceforge.combean.graph.alg.traversal.DFSNodeStackVisitor; |
| 27 | |
import net.sourceforge.combean.graph.alg.traversal.RecursiveDFSImpl; |
| 28 | |
import net.sourceforge.combean.interfaces.graph.Graph; |
| 29 | |
import net.sourceforge.combean.interfaces.graph.Node; |
| 30 | |
import net.sourceforge.combean.interfaces.graph.alg.traverse.DepthFirstSearch; |
| 31 | |
import net.sourceforge.combean.interfaces.graph.containers.NodeStack; |
| 32 | |
import net.sourceforge.combean.samples.simplegraphs.DirectedCircle; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class TestDFSNodeStackVisitor extends TestCase { |
| 39 | |
|
| 40 | 3 | private DFSNodeStackVisitor stackVis = null; |
| 41 | 3 | private DepthFirstSearch dfsAlg = null; |
| 42 | |
|
| 43 | |
private static final int CIRCLESIZE = 5; |
| 44 | 3 | private Graph circle = null; |
| 45 | |
|
| 46 | |
private class DFSNodeStackVisitorWithPrematureEnd |
| 47 | |
extends DFSNodeStackVisitor { |
| 48 | 3 | private int maxIter = 0; |
| 49 | 3 | private int iterCount = 0; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 3 | public DFSNodeStackVisitorWithPrematureEnd(int maxIter) { |
| 55 | 3 | super(); |
| 56 | 3 | this.maxIter = maxIter; |
| 57 | 3 | this.iterCount = 0; |
| 58 | 3 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public void visitNode(Node v) { |
| 64 | 15 | super.visitNode(v); |
| 65 | 15 | this.iterCount++; |
| 66 | 15 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public boolean readyToTerminate() { |
| 72 | 0 | return this.iterCount == this.maxIter; |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
public static void main(String[] args) { |
| 77 | 0 | junit.textui.TestRunner.run(TestDFSNodeStackVisitor.class); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
protected void setUp() throws Exception { |
| 84 | 3 | super.setUp(); |
| 85 | |
|
| 86 | 3 | this.circle = new DirectedCircle(CIRCLESIZE); |
| 87 | 3 | this.dfsAlg = new RecursiveDFSImpl(); |
| 88 | 3 | this.dfsAlg.setGraph(this.circle); |
| 89 | 3 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
protected void tearDown() throws Exception { |
| 95 | 3 | super.tearDown(); |
| 96 | 3 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public TestDFSNodeStackVisitor(String name) { |
| 103 | 3 | super(name); |
| 104 | 3 | } |
| 105 | |
|
| 106 | |
public final void testStackVisitorOnCircleGraph() { |
| 107 | 3 | this.stackVis = new DFSNodeStackVisitorWithPrematureEnd(CIRCLESIZE-1); |
| 108 | 3 | this.dfsAlg.setVisitor(this.stackVis); |
| 109 | 3 | this.dfsAlg.setLocalStartNode(new NumberNode(0)); |
| 110 | |
|
| 111 | 3 | this.dfsAlg.run(); |
| 112 | |
|
| 113 | 3 | NodeStack S = this.stackVis.getStack(); |
| 114 | 3 | int nodeCount = CIRCLESIZE-1; |
| 115 | 18 | while (!S.isEmpty()) { |
| 116 | 15 | NumberNode v = (NumberNode) S.pop(); |
| 117 | 15 | assertEquals("nodes shall be renumbered in reverse order by stack", |
| 118 | |
nodeCount--, v.getNodeNum()); |
| 119 | 15 | } |
| 120 | 3 | assertEquals("the stack shall contain all nodes up to node 0", |
| 121 | |
-1, nodeCount); |
| 122 | 3 | } |
| 123 | |
|
| 124 | |
} |