-
-
Save da9dac/d1d14ae23eedf077a3fbf59829668895 to your computer and use it in GitHub Desktop.
Java TestCase Generator + Answer Comparator for ProblemSolving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Java TestCase Generator + Answer Comparator for ProblemSolving | |
* @author sinam7 (muwon777@naver.com) | |
* <p> | |
* To use this generator, You need two java file first: | |
* 1. Code you wrote -> default: in class Main | |
* 2. AC Code -> default: in class Solution | |
* (!) the code must use stdio (System.in, System.out) | |
* <p> | |
* Then, make your code's System.in to our custom InputStreamReader. | |
* @see Line 50~58 | |
* <p> | |
* Change the code inside these methods below: | |
* 1. private static void generateTestCase() -> write a code to generate testcase. | |
* 2. private static void runCorrectCode(InputStreamReader in) -> to run correct code's psvm. | |
* 3. private static void runTestCode(InputStreamReader in) -> to run test code's psvm. | |
*/ | |
import java.io.*; | |
import java.util.StringTokenizer; | |
public class TestcaseGenerator { | |
private static void generateTestCase() { | |
/* | |
Example: 2 <= N <= 1000000, 1 <= M <= N, Input: "N M" | |
for (int i = 2; i <= 1000000; i++) { | |
for (int j = 1; j <= i; j++) { | |
// run code with string input. | |
// if you want to find the first WA testcase, code it like below. | |
// WA will return false, AC will return true. | |
// else, remove if statement. | |
if (!start(i + " " + j + "\n")){ | |
return; | |
} | |
} | |
} | |
*/ | |
// Code below... | |
for (int i = 2; i <= 1000000; i++) { | |
for (int j = 1; j <= i; j++) { | |
if (!start(i + " " + j + "\n")){ | |
return; | |
} | |
} | |
} | |
// Code above... | |
} | |
private static void runCorrectCode(InputStreamReader in) throws IOException { | |
Solution.sc.init(in); // Change code if needed -> Change InputStream from 'System.in' to parameter 'in' | |
Solution.solve(); // your psvm here | |
} | |
private static void runTestCode(InputStreamReader in) throws IOException { | |
Main.sc.init(in); // Change code if needed -> Change InputStream from 'System.in' to parameter 'in' | |
Main.solve(); // your psvm here | |
} | |
/* | |
* Example of sc inner static class. | |
*/ | |
@SuppressWarnings("unused") | |
static class sc { | |
private static BufferedReader br; | |
private static StringTokenizer st; | |
static void init(InputStreamReader in) { | |
br = new BufferedReader(in); | |
st = new StringTokenizer(""); | |
} | |
static String next() { | |
while (!st.hasMoreTokens()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException ignored) { | |
} | |
} | |
return st.nextToken(); | |
} | |
static int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
generateTestCase(); | |
} | |
public static boolean start(String tc) { | |
final PrintStream defaultSysout = System.out; | |
InputStreamReader in1 = new InputStreamReader(new ByteArrayInputStream(tc.getBytes())); | |
InputStreamReader in2 = new InputStreamReader(new ByteArrayInputStream(tc.getBytes())); | |
String correctOutput = run(() -> { | |
try { | |
runCorrectCode(in1); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
}); | |
String testOutput = run(() -> { | |
try { | |
runTestCode(in2); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
}); | |
// customize here if you want file output. | |
System.setOut(defaultSysout); | |
if (!correctOutput.equals(testOutput)) { | |
System.out.println("===================="); | |
System.out.println("Found Wrong result"); | |
System.out.println("Input:"); | |
System.out.println(tc); | |
System.out.println("Correct Output:"); | |
System.out.println(correctOutput); | |
System.out.println("Test Output:"); | |
System.out.println(testOutput); | |
return false; | |
} | |
return true; | |
} | |
private static String run(Runnable runnable) { | |
CustomOutputStream outputStream = setUp(); | |
runnable.run(); | |
return outputStream.toString(); | |
} | |
private static CustomOutputStream setUp() { | |
CustomOutputStream outputStream = new CustomOutputStream(); | |
PrintStream printStream = new PrintStream(outputStream); | |
System.setOut(printStream); | |
return outputStream; | |
} | |
private static class CustomOutputStream extends OutputStream { | |
private StringBuilder string = new StringBuilder(); | |
@Override | |
public void write(int b) { | |
this.string.append((char) b); | |
} | |
@Override | |
public String toString() { | |
return this.string.toString(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment