Coverage Report - net.sourceforge.combean.test.graph.alg.traversal.TestDFSNodeStackVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
TestDFSNodeStackVisitor
92%
24/26
100%
2/2
1,125
TestDFSNodeStackVisitor$DFSNodeStackVisitorWithPrematureEnd
91%
10/11
0%
0/2
1,125
 
 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.02.2005
 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  
  * @author schickin
 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  
          * constructor 
 53  
          */
 54  3
         public DFSNodeStackVisitorWithPrematureEnd(int maxIter) {
 55  3
             super();
 56  3
             this.maxIter = maxIter;
 57  3
             this.iterCount = 0;
 58  3
         }
 59  
         
 60  
         /* (non-Javadoc)
 61  
          * @see net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor#visitNode(net.sourceforge.combean.interfaces.graph.Node)
 62  
          */
 63  
         public void visitNode(Node v) {
 64  15
              super.visitNode(v);
 65  15
              this.iterCount++;
 66  15
         }
 67  
         
 68  
         /* (non-Javadoc)
 69  
          * @see net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor#readyToTerminate()
 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  
      * @see TestCase#setUp()
 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  
      * @see TestCase#tearDown()
 93  
      */
 94  
     protected void tearDown() throws Exception {
 95  3
         super.tearDown();
 96  3
     }
 97  
 
 98  
     /**
 99  
      * Constructor for TestDFSNodeStackVisitor.
 100  
      * @param name
 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  
 }