    1   /*
    2    * Created on Jun 20, 2004
    3    *
    4    */
    5   package baseCode.dataStructure.graph;
    6
    7   import java.util.ArrayList;
    8   import java.util.Collections;
    9   import java.util.Iterator;
    10  import java.util.List;
    11
    12  import junit.framework.TestCase;
    13  import baseCode.dataStructure.graph.DirectedGraph;
    14  import baseCode.dataStructure.graph.DirectedGraphNode;
    15
    16  /***
    17   *
    18   * <p>
    19   * Copyright (c) Columbia University
    20   *
    21   * @author Paul Pavlidis
    22   * @version $Id: TestDirectedGraph.java,v 1.1 2005/03/17 13:58:43 pavlidis Exp $
    23   */
    24  public class TestDirectedGraph extends TestCase {
    25     DirectedGraph testGraph;
    26     DirectedGraph testGraphCycle; // has a cycle.
    27
    28     /***
    29      * Constructor for TestDirectedGraph.
    30      *
    31      * @param arg0
    32      */
    33     public TestDirectedGraph( String arg0 ) {
    34        super( arg0 );
    35        testGraph = new DirectedGraph();
    36
    37        testGraph.addNode( "b", "bee." );
    38        testGraph.addNode( "a", "aaa." );
    39        testGraph.addNode( "c", "cee." );
    40        testGraph.addNode( "d", "dee." );
    41        testGraph.addNode( "f", "eff." );
    42        testGraph.addNode( "e", "eee." );
    43
    44        testGraph.addParentTo( "b", "a" );
    45        testGraph.addParentTo( "c", "a" );
    46        testGraph.addChildTo( "a", "c" ); // redundant
    47        testGraph.addChildTo( "a", "b" ); // redundant
    48
    49        testGraph.addChildTo( "c", "d" ); // top down
    50        testGraph.addChildTo( "c", "e" ); // top down
    51        testGraph.addParentTo( "f", "c" ); // bottom up
    52
    53        testGraphCycle = new DirectedGraph();
    54
    55        testGraphCycle.addNode( "b", "bee." );
    56        testGraphCycle.addNode( "a", "aaa." );
    57        testGraphCycle.addNode( "c", "cee." );
    58        testGraphCycle.addNode( "d", "dee." );
    59        testGraphCycle.addNode( "f", "eff." );
    60        testGraphCycle.addNode( "e", "eee." );
    61
    62        testGraphCycle.addParentTo( "b", "a" );
    63        testGraphCycle.addParentTo( "c", "a" );
    64        testGraphCycle.addChildTo( "a", "c" ); // redundant
    65        testGraphCycle.addChildTo( "a", "b" ); // redundant
    66
    67        testGraphCycle.addChildTo( "c", "d" ); // top down
    68        testGraphCycle.addChildTo( "c", "e" ); // top down
    69        testGraphCycle.addParentTo( "f", "c" ); // bottom up
    70        testGraphCycle.addParentTo( "f", "e" ); // cycle
    71     }
    72
    73     /*
    74      * @see TestCase#setUp()
    75      */
    76     protected void setUp() throws Exception {
    77        super.setUp();
    78     }
    79
    80     /*
    81      * @see TestCase#tearDown()
    82      */
    83     protected void tearDown() throws Exception {
    84        super.tearDown();
    85     }
    86
    87     public void testToString() {
    88        String expectedReturn = "aaa.\n\tbee.\n\tcee.\n\t\tdee.\n\t\teee.\n\t\teff.\n";
    89        String actualReturn = testGraph.toString();
    90        assertEquals( "return", expectedReturn, actualReturn );
    91     }
    92
    93     public void testTopoSort() {
    94        testGraph.topoSort();
    95        List nodes = new ArrayList( testGraph.getItems().values() );
    96        Collections.sort( nodes );
    97        StringBuffer buf = new StringBuffer();
    98        for ( Iterator it = nodes.iterator(); it.hasNext(); ) {
    99           buf.append( it.next().toString() );
    100       }
    101       String actualReturn = buf.toString();
    102       String expectedReturn = "aaa.bee.cee.dee.eee.eff.";
    103
    104       assertEquals( "return", expectedReturn, actualReturn );
    105    }
    106
    107    public void testGetChildren() {
    108       DirectedGraphNode n = ( DirectedGraphNode ) testGraph.get( "c" );
    109       String actualReturn = n.getChildGraph().toString();
    110       String expectedReturn = "cee.\n\tdee.\n\teee.\n\teff.\n";
    111       assertEquals( "return", expectedReturn, actualReturn );
    112    }
    113
    114 }