1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import util.RandomArrList;
import java.io.*; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List;
public class CompareCode {
private static final String TEST_CASES_FOLDER = "./";
public static void main(String[] args) throws IOException { int numCase = 1;
generateTestCases(numCase);
for (int i = 1; i <= numCase; i++) { String inputFilePath = TEST_CASES_FOLDER + "testcase" + i + ".txt"; String outputAFilePath = TEST_CASES_FOLDER + "outputA" + i + ".txt"; String outputBFilePath = TEST_CASES_FOLDER + "outputB" + i + ".txt";
runAlgorithm(A.class, inputFilePath, outputAFilePath); runAlgorithm(B.class, inputFilePath, outputBFilePath);
if (!compareOutputs(outputAFilePath, outputBFilePath)) { System.setOut(System.err); System.out.println("Output mismatch for testcase" + i); System.out.println("A: " + outputAFilePath); System.out.println("B: " + outputBFilePath); System.setOut(System.out); System.out.println("============================================"); System.out.println(); } } }
private static void runAlgorithm(Class<?> clazz, String inputFilePath, String outputFilePath) { try { Method method = clazz.getMethod("main", String[].class);
List<String> inputFileContent = Files.readAllLines(Paths.get(inputFilePath));
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(String.join(System.lineSeparator(), inputFileContent).getBytes(StandardCharsets.UTF_8)); System.setIn(byteArrayInputStream);
PrintStream fileOut = new PrintStream(new FileOutputStream(outputFilePath)); System.setOut(fileOut);
method.invoke(null, new Object[]{new String[]{}});
System.setIn(System.in); System.setOut(System.out); } catch (Exception e) { e.printStackTrace(); } }
private static boolean compareOutputs(String outputFilePath1, String outputFilePath2) throws IOException { byte[] bytes1 = Files.readAllBytes(Paths.get(outputFilePath1)); byte[] bytes2 = Files.readAllBytes(Paths.get(outputFilePath2)); String content1 = new String(bytes1, StandardCharsets.UTF_8); String content2 = new String(bytes2, StandardCharsets.UTF_8);
return content1.equals(content2); }
private static void generateTestCases1(int numTestCases) throws IOException { for (int i = 1; i <= numTestCases; i++) { String filePath = TEST_CASES_FOLDER + "testcase" + i + ".txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { int n = RandomArrList.getRandNum(3, 10); writer.write(n + "\n"); int[] randomArr = RandomArrList.getRandomArr(n, 1, n); for (int j = 0; j < randomArr.length; j++) { writer.write(randomArr[j] + " "); } writer.write("\n"); } } }
private static void generateTestCases(int numTestCases) throws IOException {
for (int i = 1; i <= numTestCases; i++) { String filePath = TEST_CASES_FOLDER + "testcase" + i + ".txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { int t = RandomArrList.getRandNum(1, 10); writer.write(t + "\n"); for (int j = 0; j < t; j++) { int n = RandomArrList.getRandNum(1, 15); int m = RandomArrList.getRandNum(1, 15); writer.write(n + " " + m); writer.write("\n"); ArrayList<Integer> randomList = RandomArrList.getRandomList(n, 1, 10); for (Integer num : randomList) { writer.write(num + " "); } writer.write("\n"); } } } } }
|