Created
November 4, 2014 12:30
-
-
Save LukasKalbertodt/b53934dec12626fc51fd to your computer and use it in GitHub Desktop.
PrimeTest
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
import AlgoTools.IO; | |
import java.util.Scanner; | |
import java.io.*; | |
public class PrimeTest { | |
private static String m_program; | |
private static String m_yesString; | |
private static String m_noString; | |
public static void main(String[] args) throws Exception { | |
// Check command line parameter | |
if(args.length < 3) { | |
IO.println("To few arguments! Usage:"); | |
IO.println("java PrimeTest program yesString noString"); | |
IO.println(""); | |
IO.println("program: \tProgram to test"); | |
IO.println("yesString:\tString that is found in the output of your " | |
+ "program if the input is a prime"); | |
IO.println("noString: \tString that is found in the output of your " | |
+ "program if the input is NOT a prime"); | |
return; | |
} | |
m_program = args[0]; | |
m_yesString = args[1]; | |
m_noString = args[2]; | |
IO.println("Will test application '" + m_program | |
+ "' with yesString '" + m_yesString | |
+ "' and noString '" + m_noString + "'"); | |
IO.println("----------------------------"); | |
IO.println(); | |
for(int i = 1; i < 9001; ++i) { | |
if(i % 100 == 0) { | |
IO.println(">>> Testing " + i); | |
} | |
if(testNumber(i)) { | |
break; | |
} | |
} | |
} | |
private static boolean testNumber(int number) throws Exception { | |
ProcessBuilder builder = new ProcessBuilder("java", m_program); | |
Process process = builder.start(); | |
OutputStream stdin = process.getOutputStream(); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)); | |
writer.write("" + number); | |
writer.flush(); | |
writer.close(); | |
InputStream stdout = process.getInputStream(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); | |
Scanner scanner = new Scanner(stdout); | |
String all = ""; | |
while (scanner.hasNextLine()) { | |
all += scanner.nextLine(); | |
} | |
boolean progSays = true; | |
if(all.indexOf(m_yesString) == -1) { | |
// shouldn't be a prime | |
if(all.indexOf(m_noString) == -1) { | |
IO.println("yesString and noString was found in output! " | |
+ "Fix that!"); | |
System.exit(-1); | |
} | |
progSays = false; | |
} | |
if(all.indexOf(m_noString) == -1) { | |
IO.println("Neither yesString nor noString was found in output! " | |
+ "Fix that!"); | |
System.exit(-1); | |
} | |
if(progSays != isPrime(number)) { | |
IO.println("Error detected! Number: " + number); | |
IO.println("ProgOutput -> "); | |
IO.println(all); | |
return false; | |
} | |
return true; | |
} | |
private static boolean isPrime(int number) { | |
// early return Mr. Haldenwang :* | |
if(number == 2) { | |
return true; | |
} | |
if(number == 1 || number % 2 == 0) { | |
return false; | |
} | |
for(int i = 3; i <= Math.sqrt(number); ++i) { | |
if(number % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment