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/W24/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",true,2); testFile("tree1",true,1); testFile("tree4",true,1); testFile("disconnected2",false,1); testFile("disconnected3",false,1); testFile("disconnected4",false,1); testFile("disconnected5",false,2); testFile("disconnected7",false,2); testFile("fullgraph2",true,1); testFile("fullgraph5",true,1); testFile("graph2-1",false,2); testFile("graph2-2",true,1); testFile("graph5-1",false,2); testFile("graph5-2",true,1); testFile("graph5-3",false,2); testFile("graph5-4",true,1); testFile("graph10",true,2); testFile("graph10-1",true,2); testFile("graph10-3",false,2); testFile("graph10-4",true,2); testFile("tree10-1",true,2); testFile("tree10-2",true,2); testFile("tree10-2",true,2); testFile("graph15",true,2); testFile("graph20",true,2); } /** * 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 expected true if graph is connected and false otherwise * @param points points for correct answer */ public static void testFile(String filename, boolean expected, int points) { String filepath = testDirPath + filename; boolean connected; System.out.print("===== Test " + filename + " out of " + points ); 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 isConnected on graph connected = graph.isConnected(); if (connected == expected) { score += points; System.out.println("\t- pass. Score so far=" + score); } else { System.out.println("\t- fail. Score so far=" + score); } } }