package labInsertionSort; import java.io.File; import java.net.URL; import java.io.BufferedReader; import java.io.FileReader; import java.io.PrintStream; import java.io.ByteArrayOutputStream; public class TestAll { public static void main(String[] args) { processInputFiles(); } public static void processInputFiles() { try { // Get the inputs and outputs directories URL input_url = TestAll.class.getResource("inputs"); URL output_url = TestAll.class.getResource("outputs"); if (input_url == null) { System.out.println("Cannot find folder \"inputs\""); return; } if (output_url == null) { System.out.println("Cannot find folder \"outputs\""); return; } // Get the names of all the files in either directory File input_folder = new File(input_url.getPath()); File[] input_files = input_folder.listFiles(); File output_folder = new File(output_url.getPath()); if (input_files == null) { System.out.printf("No files in folder: %s\n", input_folder.getPath()); return; } // No need to check if output files exist, can still run inputs // Capture the print statements from the assignment into printstream ByteArrayOutputStream captured_bytestream = new ByteArrayOutputStream(); PrintStream captured_printstream = new PrintStream(captured_bytestream); // Replace System.out with a local variable to capture prints PrintStream stdout = System.out; System.setOut(captured_printstream); Integer total_tests = 0, passed_tests = 0; for (File input_file : input_files) { if (!input_file.isFile()) { continue; } // Print which test is currently being run stdout.println("Processing file: " + input_file.getName()); // Run the assignment on this input file Assignment.run(input_file.getAbsolutePath()); // Ensure all the output from assignment goes to local variable System.out.flush(); // Get the output from assignment into a String String assignment_output = captured_bytestream.toString(); captured_bytestream.reset(); // Get the file contents for comparison File output_file = new File(output_folder, input_file.getName()); // Check if a corresponding output file exists if (!output_file.exists()) { // Print the output to the console without grading stdout.println("No corresponding output, result shown:"); stdout.println(assignment_output); continue; } // Read the contents of the corresponding output file total_tests++; BufferedReader buffered_reader = new BufferedReader(new FileReader(output_file.getAbsolutePath())); StringBuilder string_builder = new StringBuilder(); String line; while ((line = buffered_reader.readLine()) != null) { string_builder.append(line).append(System.lineSeparator()); } buffered_reader.close(); String correct_output = string_builder.toString(); if (assignment_output.equals(correct_output)) { // This test passes passed_tests++; stdout.println("Test Pass"); } else { // This test does not pass stdout.println("Test Fail:"); stdout.println("Expected:"); stdout.println(correct_output); stdout.println("Actual:"); stdout.println(assignment_output); } // Indicate this test is done stdout.println("====="); } stdout.printf("Result: %d/%d Tests Passed\n", passed_tests, total_tests); System.setOut(stdout); } catch (Exception e) { e.printStackTrace(); } } }