import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Scanner;

import junit.framework.TestCase;


/**
 * JUnit test suite mimics the test script I will use to test the Hardy program.
 * If you don't ouptut your code to bin, then you should change bin to . in the line:
 * 			p = Runtime.getRuntime().exec("java -cp bin Hardy");
 * below.
 * 
 * @author Matt Boutell.
 *         Created Apr 17, 2008.
 */
public class HardyJUnitTests extends TestCase {

	public void test1() {
		assertEquals("1729 = 1^3 + 12^3 = 9^3 + 10^3", callLikeScript(1));
	}

	public void test5() {
		assertEquals("32832 = 4^3 + 32^3 = 18^3 + 30^3", callLikeScript(5));
	}
	
	public void test30() {
		assertEquals("515375 = 15^3 + 80^3 = 54^3 + 71^3", callLikeScript(30));
	}
	
	public void test100() {
		assertEquals("4673088 = 25^3 + 167^3 = 64^3 + 164^3", callLikeScript(100));
	}
	
	public void test500() {
		assertEquals("106243219 = 307^3 + 426^3 = 363^3 + 388^3", callLikeScript(500));
	}

	/**
	 * This simulates running the process just like the test script will call it:
	 * 1. Run the program
	 * 2. Pass n to stdin (console input), then  
	 * 3. Store the result from stdout (console output) as a string
	 * 4. Check it against the known answer.
	 * 
	 * @param n Which Hardy number to find.
	 * @return The result string.
	 */
	private String callLikeScript(int n) {
		Process p = null;
		try {
			p = Runtime.getRuntime().exec("java -cp bin Hardy");
		} catch (IOException exception) {
			exception.printStackTrace();
		}
		PrintWriter processOut=new PrintWriter(p.getOutputStream());
		processOut.println(n);
		processOut.flush();
		processOut.close();
		
		Scanner in=new Scanner(p.getInputStream());
		String answer;
		while(!in.hasNextLine()) {/* Wait for student output */}
		answer =in.nextLine();
		in.close();
		return answer;
	}
}