import java.io.File;
import java.util.Scanner;
/**
* The Test class is the main class used to test and grade the CPS420 Lab 6.
*
DO NOT MODIFY THIS CLASS.
* @author Sophie Quigley
*/
public class Test {
/**
* Input stream.
*/
private static Scanner input = null;
/**
* Path of directory containing test files.
*/
public static final String testDirPath = "/misc/cps420/public_html/doc/W25/Lab6/Tests/";
/**
* Graph being tested
*/
private static Graph graph = null;
/**
* Score calculated
*/
private static int score = 0;
/**
* Main test program calculates score for program.
*
It first tests the isConnected method on graphs in test files.
*
Then it tests it on randomly generated undirected simple graphs.
* @param args the command line arguments
*/
public static void main(String[] args) {
// Run test files
testFile("tree0","",1,true,1);
testFile("tree1","0",1,true,1);
testFile("tree4","0 1 2 3",1,true,1);
testFile("disconnected2","0",1,false,1);
testFile("disconnected3","0 1",1,false,1);
testFile("disconnected4","0 1",1,false,1);
testFile("disconnected5","0 1 2",1,false,1);
testFile("disconnected7","0 1 2 3 6 5",2,false,1);
testFile("fullgraph2","0 1",1,true,1);
testFile("fullgraph5","0 1 2 3 4",1,true,1);
testFile("graph2-1","0",2,false,1);
testFile("graph2-2","0 1",1,true,1);
testFile("graph5-1","0",2,false,1);
testFile("graph5-2","0 1 2 3 4",1,true,1);
testFile("graph5-3","0 1",2,false,1);
testFile("graph5-4","0 1 2 3 4",1,true,1);
testFile("graph10","0 1 3 2 4 5 6 9 8 7",2,true,1);
testFile("graph10-1","0 1 3 2 4 5 6 9 8 7",2,true,1);
testFile("graph10-3","0 1 5 6 7",2,false,1);
testFile("graph10-4","0 1 3 2 9 8 4 5 6 7",2,true,1);
testFile("tree10-1","0 1 3 4 8 9 2 5 6 7",2,true,1);
testFile("tree10-2","0 1 3 4 8 9 2 5 6 7",2,true,1);
testFile("graph15","0 3 9 1 2 4 5 7 10 13 8 6 12 11 14",2,true,1);
testFile("graph20","0 3 9 1 2 4 5 7 10 13 8 6 12 11 14 17 15 16 18 19",2,true,1);
System.out.println("Final score:" + score);
}
/**
* Reads graph described in a test file and test whether that graph is connected
* @param filename name of test file. The directory containing that file is hard coded in testDirPath
* @param expectedDFS string of the visit expected to be created by DFSvisit
* @param DFSpoints points for correct answer to DFSvisit
* @param expected true if graph is connected and false otherwise
* @param connectedpoints points for correct answer to isConnected
*/
public static void testFile(String filename, String expectedDFS, int DFSpoints, boolean expected, int connectedpoints) {
String filepath = testDirPath + filename;
boolean connected;
Walk visit;
int points;
System.out.print("===== Test " + filename + " -");
try {
input = new Scanner(new File(filepath));
} catch (Exception ex) {
System.out.println(ex);
System.exit(0);
}
try {
graph = new Graph(input);
} catch (InstantiationException ex) {
System.out.println(ex);
System.exit(0);
} catch (Exception ex) {
System.out.println(ex);
System.exit(0);
}
// Test DFSvisit on graph starting at vertex 0.
visit = new Walk(graph.getTotalVertices());
graph.DFSvisit(0,visit);
graph.resetVisitation();
if (visit.toString().compareTo(expectedDFS) == 0) points = DFSpoints;
else points = 0;
System.out.print(" DFSvisit:"+points + "/" + DFSpoints);
score += points;
// System.out.println(". Score so far=" + score);
// System.out.println(visit.toString());
// Test isConnected on graph
connected = graph.isConnected();
if (connected == expected) points = connectedpoints;
else points = 0;
System.out.print(" isConnected:"+points + "/" + connectedpoints);
score += points;
System.out.println(". Score so far=" + score);
}
}