Coverage Report - net.sourceforge.combean.test.graph.alg.traversal.TestFindPathByTraversalAlg
 
Classes in this File Line Coverage Branch Coverage Complexity
TestFindPathByTraversalAlg
97%
67/69
100%
4/4
1,25
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 20.08.2005
 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  
  * @author schickin
 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  
      * @see TestCase#setUp()
 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  
      * @see TestCase#tearDown()
 62  
      */
 63  
     protected void tearDown() throws Exception {
 64  12
         super.tearDown();
 65  12
     }
 66  
 
 67  
     /**
 68  
      * Constructor for TestFindPathByTraversalAlg.
 69  
      * @param name
 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  
 }