Please send questions to
st10@humboldt.edu .
/* Cs132AdjListGraphTest.java 1.0 */
/* by Sharon Tuttle */
/* last modified: 5-2-03 */
public class Cs132AdjListGraphTest extends JPFalt
{
public static void main(String[] args)
{
new Cs132AdjListGraphTest();
}
/** Test suite for the class Cs132AdjListGraph */
/*--------------------------------------------------------------------
Define a Cs132AdjListGraph object and print the member data values
--------------------------------------------------------------------*/
void TestCs132AdjListGraph()
{
println("\nDefine Cs132AdjListGraph object");
// empty graph
Cs132Graph myGraph = new Cs132AdjListGraph(20);
println("new, empty graph:");
println(myGraph);
println("");
// graph with 1 vertex, no edges
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
try
{
myGraph.addVertex(vertA);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
println("Graph with 1 vertex, with key A:");
println(myGraph);
println("");
// graph with 2 vertices, no edges
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
try
{
myGraph.addVertex(vertB);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
println("Graph with 2 vertices, with keys A,B:");
println(myGraph);
println("");
// graph with 2 vertices, 1 edge
myGraph.addEdge(vertA, vertB);
println("Graph with 2 vertices, with keys A,B, and 1 edge btwn:");
println(myGraph);
println("");
// add 2 more vertices, 2 more edges
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
println("Graph with 4 vertices, with keys A,B, C, D, and 3 edges");
println(" btwn AB, BC, CD:");
println(myGraph);
println("");
/*
// remove 1 edge
myGraph.removeEdge(vertB, vertC);
println("Graph with 4 vertices, keys A,B,C,D, and 2 edges");
println(" btwn AB, CD");
println(myGraph);
println("");
*/
}
/*---------------------------------------------------------------
Test method for isEmpty()
-----------------------------------------------------------------*/
void isEmptyTest()
{
Cs132Graph myGraph = new Cs132AdjListGraph(20);
testHeader("isEmpty");
expected(true);
actual(myGraph.isEmpty());
// graph with 1 vertex, no edges
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
try
{
myGraph.addVertex(vertA);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(false);
actual(myGraph.isEmpty());
myGraph.removeVertex(vertA);
expected(true);
actual(myGraph.isEmpty());
}
/*---------------------------------------------------------------
Test method for vertexCount()
-----------------------------------------------------------------*/
void vertexCountTest()
{
Cs132Graph myGraph = new Cs132AdjListGraph(20);
testHeader("vertexCount");
expected(0);
actual(myGraph.vertexCount());
// graph with 1 vertex, no edges
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
try
{
myGraph.addVertex(vertA);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(1);
actual(myGraph.vertexCount());
myGraph.removeVertex(vertA);
expected(0);
actual(myGraph.vertexCount());
}
/*---------------------------------------------------------------
Test method for edgeCount()
-----------------------------------------------------------------*/
void edgeCountTest()
{
Cs132Graph myGraph = new Cs132AdjListGraph(20);
testHeader("edgeCount");
expected(0);
actual(myGraph.edgeCount());
// graph with 1 vertex, no edges
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
try
{
myGraph.addVertex(vertA);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(0);
actual(myGraph.edgeCount());
// graph with 4 vertices, 3 edges
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertB);
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
myGraph.addEdge(vertA, vertB);
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
expected(3);
actual(myGraph.edgeCount());
/*
myGraph.removeVertex(vertA);
expected(2);
actual(myGraph.edgeCount());
*/
}
/*---------------------------------------------------------------
Test method for containsEdge()
-----------------------------------------------------------------*/
void containsEdgeTest()
{
Cs132Graph myGraph = new Cs132AdjListGraph(20);
testHeader("containsEdge");
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
expected(false);
actual(myGraph.containsEdge(vertA, vertB));
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertA);
myGraph.addVertex(vertB);
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(false);
actual(myGraph.containsEdge(vertA, vertB));
myGraph.addEdge(vertA, vertB);
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
expected(true);
actual(myGraph.containsEdge(vertA, vertB));
expected(true);
actual(myGraph.containsEdge(vertB, vertA));
expected(false);
actual(myGraph.containsEdge(vertC, vertA));
/*
myGraph.removeEdge(vertA, vertB);
expected(false);
actual(myGraph.containsEdge(vertB, vertA));
*/
}
/*---------------------------------------------------------------
Test method for getIndex()
-----------------------------------------------------------------*/
void getIndexTest()
{
Cs132AdjListGraph myGraph = new Cs132AdjListGraph(20);
testHeader("getIndex");
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
expected(-1);
actual(myGraph.getIndex(vertA));
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertA);
myGraph.addVertex(vertB);
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(0);
actual(myGraph.getIndex(vertA));
expected(1);
actual(myGraph.getIndex(vertB));
expected(2);
actual(myGraph.getIndex(vertC));
expected(3);
actual(myGraph.getIndex(vertD));
myGraph.addEdge(vertA, vertB);
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
/*
myGraph.removeVertex(vertA);
expected(-1);
actual(myGraph.getVertex(vertA));
expected(0);
actual(myGraph.getVertex(vertB));
expected(1);
actual(myGraph.getVertex(vertC));
expected(2);
actual(myGraph.getVertex(vertD));
*/
}
/*---------------------------------------------------------------
Test method for degree()
-----------------------------------------------------------------*/
void degreeTest()
{
Cs132AdjListGraph myGraph = new Cs132AdjListGraph(20);
testHeader("degree");
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
expected(-1);
actual(myGraph.degree(vertA));
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertA);
myGraph.addVertex(vertB);
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected(0);
actual(myGraph.degree(vertA));
expected(0);
actual(myGraph.degree(vertB));
expected(0);
actual(myGraph.degree(vertC));
expected(0);
actual(myGraph.degree(vertD));
myGraph.addEdge(vertA, vertB);
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
expected(1);
actual(myGraph.degree(vertA));
myGraph.addEdge(vertA, vertC);
myGraph.addEdge(vertA, vertD);
println(myGraph);
expected(3);
actual(myGraph.degree(vertA));
expected(3);
actual(myGraph.degree(vertC));
expected(2);
actual(myGraph.degree(vertB));
/*
myGraph.removeVertex(vertA);
expected(-1);
actual(myGraph.getVertex(vertA));
expected(0);
actual(myGraph.getVertex(vertB));
expected(1);
actual(myGraph.getVertex(vertC));
expected(2);
actual(myGraph.getVertex(vertD));
*/
}
/*---------------------------------------------------------------
Test method for adjacentVerts()
-----------------------------------------------------------------*/
void adjacentVertsTest()
{
Cs132AdjListGraph myGraph = new Cs132AdjListGraph(20);
testHeader("adjacentVerts");
Cs132Vertex vertA = new Cs132SimpleVertex("A", new Integer(4));
Cs132Vertex vertB = new Cs132SimpleVertex("B", new Integer(6));
expected("" + null);
actual("" + myGraph.adjacentVerts(vertA));
Cs132Vertex vertC = new Cs132SimpleVertex("C", new Integer(10));
Cs132Vertex vertD = new Cs132SimpleVertex("D", new Integer(12));
try
{
myGraph.addVertex(vertA);
myGraph.addVertex(vertB);
myGraph.addVertex(vertC);
myGraph.addVertex(vertD);
}
catch (Exception ex)
{
println("Unexpected exception: " + ex);
}
expected("" + null);
actual("" + myGraph.adjacentVerts(vertA));
expected("" + null);
actual("" + myGraph.adjacentVerts(vertB));
expected("" + null);
actual("" + myGraph.adjacentVerts(vertC));
expected("" + null);
actual("" + myGraph.adjacentVerts(vertD));
myGraph.addEdge(vertA, vertB);
myGraph.addEdge(vertB, vertC);
myGraph.addEdge(vertC, vertD);
println(myGraph);
expected("just B");
Cs132Vertex looky[] = myGraph.adjacentVerts(vertA);
actual("vertices adjacent to A");
for (int i = 0; i < looky.length; i++)
println(looky[i]);
expected("A, C");
looky = myGraph.adjacentVerts(vertB);
actual("vertices adjacent to B");
for (int i = 0; i < looky.length; i++)
println(looky[i]);
myGraph.addEdge(vertA, vertC);
myGraph.addEdge(vertA, vertD);
println(myGraph);
expected("B, C, D");
looky = myGraph.adjacentVerts(vertA);
actual("vertices adjacent to A");
for (int i = 0; i < looky.length; i++)
println(looky[i]);
expected("A, C");
looky = myGraph.adjacentVerts(vertD);
actual("vertices adjacent to D");
for (int i = 0; i < looky.length; i++)
println(looky[i]);
/*
myGraph.removeVertex(vertA);
expected(-1);
actual(myGraph.getVertex(vertA));
expected(0);
actual(myGraph.getVertex(vertB));
expected(1);
actual(myGraph.getVertex(vertC));
expected(2);
actual(myGraph.getVertex(vertD));
*/
}
}