| 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 java.util.HashSet; |
| 25 | |
|
| 26 | |
import junit.framework.TestCase; |
| 27 | |
import net.sourceforge.combean.graph.NumberNode; |
| 28 | |
import net.sourceforge.combean.graph.alg.traversal.AbstractGraphTraversalAlg; |
| 29 | |
import net.sourceforge.combean.graph.alg.traversal.NodeCountVisitor; |
| 30 | |
import net.sourceforge.combean.graph.containers.SetAsNodeSet; |
| 31 | |
import net.sourceforge.combean.graph.except.UnsupportedGraphProperty; |
| 32 | |
import net.sourceforge.combean.interfaces.graph.Graph; |
| 33 | |
import net.sourceforge.combean.interfaces.graph.Node; |
| 34 | |
import net.sourceforge.combean.interfaces.graph.containers.NodeSet; |
| 35 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalNumberedGraphProp; |
| 36 | |
import net.sourceforge.combean.samples.simplegraphs.IsolatedNodes; |
| 37 | |
import net.sourceforge.combean.samples.simplegraphs.Path; |
| 38 | |
import net.sourceforge.combean.test.helpers.factories.GraphFixtureFactory; |
| 39 | |
import net.sourceforge.combean.test.helpers.stubs.GraphWithoutProperties; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public abstract class AbstractTestGraphTraversalAlg extends TestCase { |
| 46 | |
|
| 47 | |
private static final int NUMNODES = 5; |
| 48 | 30 | private Path path = null; |
| 49 | 30 | private IsolatedNodes isolated = null; |
| 50 | 30 | private AbstractGraphTraversalAlg traversalAlg = null; |
| 51 | 30 | private GlobalNumberedGraphProp directedPath = null; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public AbstractTestGraphTraversalAlg(String name) { |
| 59 | 30 | super(name); |
| 60 | 30 | } |
| 61 | |
|
| 62 | |
protected void setUp() throws Exception { |
| 63 | 30 | super.setUp(); |
| 64 | |
|
| 65 | 30 | this.path = new Path(NUMNODES); |
| 66 | 30 | this.isolated = new IsolatedNodes(NUMNODES); |
| 67 | 30 | this.directedPath = GraphFixtureFactory.createDirectedPath(NUMNODES); |
| 68 | |
|
| 69 | 30 | this.traversalAlg = createTraversalAlg(); |
| 70 | |
|
| 71 | 30 | NodeSet nodeSet = new SetAsNodeSet(new HashSet<Node>(NUMNODES)); |
| 72 | 30 | this.traversalAlg.setVisitedNodes(nodeSet); |
| 73 | 30 | } |
| 74 | |
|
| 75 | |
protected void tearDown() throws Exception { |
| 76 | 30 | super.tearDown(); |
| 77 | 30 | } |
| 78 | |
|
| 79 | |
protected abstract AbstractGraphTraversalAlg createTraversalAlg(); |
| 80 | |
|
| 81 | |
public void testGetAndSetGraph() { |
| 82 | 6 | Graph g = new GraphWithoutProperties(); |
| 83 | |
try { |
| 84 | 6 | this.traversalAlg.setGraph(g); |
| 85 | 0 | fail("graph set without the necessary properties"); |
| 86 | |
} |
| 87 | 6 | catch (UnsupportedGraphProperty e) { |
| 88 | |
|
| 89 | 0 | } |
| 90 | |
|
| 91 | 6 | this.traversalAlg.setGraph(this.path); |
| 92 | 6 | assertEquals("graph not correctly retrieved", this.traversalAlg.getGraph(), |
| 93 | |
this.path); |
| 94 | 6 | } |
| 95 | |
|
| 96 | |
public final void testGlobalTraversalWithNodeCountVisitor() { |
| 97 | 6 | this.traversalAlg.setGraph(this.path); |
| 98 | 6 | NodeCountVisitor vis = new NodeCountVisitor(); |
| 99 | 6 | this.traversalAlg.setVisitor(vis); |
| 100 | 6 | this.traversalAlg.run(); |
| 101 | 6 | assertEquals("wrong number of nodes counted", NUMNODES, vis.getNodeCount()); |
| 102 | 6 | } |
| 103 | |
|
| 104 | |
public final void testLocalTraversalWithNodeCountVisitor() { |
| 105 | 6 | this.traversalAlg.setGraph(this.path); |
| 106 | 6 | NumberNode v = new NumberNode(0); |
| 107 | 6 | this.traversalAlg.setLocalStartNode(v); |
| 108 | 6 | NodeCountVisitor vis = new NodeCountVisitor(); |
| 109 | 6 | this.traversalAlg.setVisitor(vis); |
| 110 | 6 | this.traversalAlg.run(); |
| 111 | 6 | assertEquals("wrong number of nodes counted", NUMNODES, vis.getNodeCount()); |
| 112 | 6 | } |
| 113 | |
|
| 114 | |
public final void testLocalTraversalOfDirectedGraphWithNodeCountVisitor() { |
| 115 | 6 | this.traversalAlg.setGraph(this.directedPath); |
| 116 | 6 | this.traversalAlg.setUseOnlyOutgoingEdges(true); |
| 117 | 6 | Node v = this.directedPath.getNode(1); |
| 118 | 6 | this.traversalAlg.setLocalStartNode(v); |
| 119 | 6 | NodeCountVisitor vis = new NodeCountVisitor(); |
| 120 | 6 | this.traversalAlg.setVisitor(vis); |
| 121 | 6 | this.traversalAlg.run(); |
| 122 | 6 | assertEquals("wrong number of nodes counted", NUMNODES-1, vis.getNodeCount()); |
| 123 | |
|
| 124 | 6 | this.traversalAlg.setUseOnlyOutgoingEdges(false); |
| 125 | 6 | this.traversalAlg.run(); |
| 126 | 6 | assertEquals("wrong number of nodes counted", NUMNODES, vis.getNodeCount()); |
| 127 | 6 | } |
| 128 | |
|
| 129 | |
public final void testGlobalTraversalWithEmptyGraph() { |
| 130 | 6 | this.traversalAlg.setGraph(this.isolated); |
| 131 | 6 | NodeCountVisitor vis = new NodeCountVisitor(); |
| 132 | 6 | this.traversalAlg.setVisitor(vis); |
| 133 | 6 | this.traversalAlg.run(); |
| 134 | 6 | assertEquals("wrong number of nodes counted", NUMNODES, vis.getNodeCount()); |
| 135 | 6 | } |
| 136 | |
} |