public class StackTester {

	private static int testPassCount = 0;
	private static int testCount = 0;

	public static void main(String[] args) {
		IntegerStack myStack = new IntegerStack();
		int result;

		displayResults(myStack.isEmpty()==true, "isEmpty test1");

		myStack.push(4);
		displayResults(myStack.isEmpty()==false, "isEmpty test2");
		
		result = myStack.top();
		displayResults(result==4, "top test1");

		myStack.push(7);
		myStack.push(2);
		result = myStack.top();
		displayResults(result==2, "top test2");

		result = myStack.pop();
		displayResults(result==2, "pop test1");
		result = myStack.top();
		displayResults(result==7, "top test2");

		myStack.push(8);
		myStack.push(1);
		myStack.push(2);
		myStack.push(6);
		
		myStack.pop();
		myStack.pop();
		myStack.pop();
		
		myStack.pop();
		myStack.pop();
		myStack.pop();
		
		displayResults(myStack.isEmpty()==true, "isEmpty test3");
		
		myStack.pop();
		
		displayResults(myStack.isEmpty()==true, "isEmpty test4");
		
		myStack.push(3);
		
		displayResults(myStack.isEmpty()==false, "isEmpty test5");
		displayResults(myStack.top()==3, "top is now 3");
		// TODO: Add more tests
		
		sumTest();

		System.out.println("Passed " + testPassCount + "/" + testCount + " tests");
	}
	
	public static void sumTest() {
		IntegerStack s0 = new IntegerStack();
		IntegerStack s1 = new IntegerStack();
		
		s1.push(7);
		s1.push(1);
		s1.push(3);
		s1.push(4);
		
		int result = 0;
		int expected = 0;
		int top = 0;
		
		result = sumElements(s0);
		displayResults(result==expected, "sum elements of empty stack");
		
		top = s1.top();
		displayResults(top == 4, "top is 4");
		
		result = sumElements(s1);
		expected = 7 + 1 + 3 + 4;
		displayResults(result==expected, "sum elements of s1");
		
		top = s1.top();
		displayResults(top == 4, "top is 4");
	}
	
	public static int sumElements(IntegerStack s) {
		int result = 0;
		IntegerStack temp = new IntegerStack();
		
		while (!s.isEmpty()) {
			int val = s.pop();
			result += val;
			temp.push(val);
		}
		
		while (!temp.isEmpty()) {
			s.push(temp.pop());
		}
		
		return result;
	}

	public static void displayResults (boolean passed, String testName) {
		/* There is some magic going on here getting the line number
		* Borrowed from:
		* http://blog.taragana.com/index.php/archive/core-java-how-to-get-java-source-code-line-number-file-name-in-code/
		*/

		testCount++;
		if (passed) {
			System.out.println ("Passed test: " + testName);
			testPassCount++;
		} else {
			System.out.println ("Failed test: " + testName + " at line "
					+ Thread.currentThread().getStackTrace()[2].getLineNumber());
		}
    }
}