import java.io.File; import java.util.Scanner; /** * The Test class is the main class used to test and grade the CPS420 assignment. *
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/W24/A/Tests/"; /** * Main test program calculates score for program. *
It first tests the getHamiltonian 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) { int result; int score=0; // Run test files score += 1 - testFile("toosmall", 0); System.out.println("Score so far: " + score); score += 2 - testFile("disconnected", 0); System.out.println("Score so far: " + score); score += 4 - testFile("nohamiltonian", 0); System.out.println("Score so far: " + score); score += testFile("small", 1); System.out.println("Score so far: " + score); score += testFile("hashamiltonian", 5); System.out.println("Score so far: " + score); score += testFile("large", 6); System.out.println("Score so far: " + score); score += testFile("verylarge", 5); System.out.println("Score so far: " + score); score += testFile("onethousand", 1); System.out.println("Score so far: " + score + " out of 30."); // Test random graphs // Will be added for grading System.out.println("\n======= Test Random ======="); System.out.println("The final 5 marks will be calculated \nby running additional tests on five random graphs."); // Print final score } /** * Reads graphs described in a test file and tests the getHamiltonian method on these graphs. * @param filename name of test file. The directory containing that file is hard coded in testDirPath * @param expected number of graphs in file which have a Hamiltonian circult * @return The number of valid Hamiltonian circuits found for the graphs in the file. */ public static int testFile(String filename, int expected) { String filepath = testDirPath + filename; System.out.println("\n======= Test " + filepath +" ======="); try { input = new Scanner(new File(filepath)); } catch (Exception ex) { System.out.println(ex); System.exit(0); } int found = 0; Graph graph = null; while (input.hasNext()) { // Read and print input Graph 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); } found += testGraph(graph); } System.out.println("\nHamiltonian circuits expected: " + expected + ", found: " + found); return found; } /** * Tests the getHamiltonian method for a graph and prints the results. * @param graph graph on which the getHamiltonian method will be tested * @return 1 if a valid Hamiltonian circuit has been found and 0 otherwise */ public static int testGraph(Graph graph) { // Print graph System.out.println("Graph has " + graph.getTotalVertices() + " vertices, and " + graph.getTotalEdges() + " edges."); // Try to find a Hamiltonian circuit Walk hamiltonian = Hamilton.getHamiltonian(graph); if (hamiltonian == null) { System.out.println("Graph has no Hamiltonian circuit"); return 0; } System.out.println("The following circuit was found:\n" + hamiltonian); if (isValidHamiltonian(graph,hamiltonian)) { System.out.println("Valid Hamiltonian Circuit"); return 1; } else { System.out.println("Invalid Hamiltonian Circuit"); return 0; } } /** * Verifies whether a walk is a valid Hamiltonian circuit for a graph. * @param graph original graph * @param walk potential Hamiltonian circuit for graph * @return true iff walk is a valid Hamiltonian circuit for graph, and false otherwise. */ public static boolean isValidHamiltonian(Graph graph, Walk walk) { // First check if the walk is a circuit if (!walk.isClosed()) { System.out.println("Error: This walk is not a circuit"); return false; } // Then check if the circuit has the correct number of edges and vertices int totalVertices = graph.getTotalVertices(); int walklength = walk.getLength(); if (walklength < totalVertices) { System.out.println("Error: Some vertices have not been visited"); return false; } if (walklength > totalVertices) { System.out.println("Error: Too many edges in circuit"); return false; } // Now check whether the circuit is a subgraph of the graph // and goes through each vertex exactly once graph.resetVisitation(); int i, v1, v2; v1 = walk.getVertex(0); graph.visitV(v1); for (i=1; i