/** * Test suite for a simple Fibonacci number calculator; does not * use a test harness. * * @author Wayne Eberly * */ public class FibTest1 { public static void main(String[] args) { /* * Check evaluation of method at inputs 0-10 */ System.out.println("Test Suite for a Simple Fibonacci Method"); System.out.println(""); System.out.println("Testing evaluation at 0"); checkValue(0, 0); System.out.println(""); System.out.println("Testing evaluation at 1"); checkValue(1, 1); System.out.println(""); System.out.println("Testing evaluation at 2"); checkValue(2, 1); System.out.println(""); System.out.println("Testing evaluation at 3"); checkValue(3, 2); System.out.println(""); System.out.println("Testing evaluation at 4"); checkValue(4, 3); System.out.println(""); System.out.println("Testing evaluation at 5"); checkValue(5, 5); System.out.println(""); System.out.println("Testing evaluation at 6"); checkValue(6, 8); System.out.println(""); System.out.println("Testing evaluation at 7"); checkValue(7, 13); System.out.println(""); System.out.println("Testing evaluation at 8"); checkValue(8, 21); System.out.println(""); System.out.println("Testing evaluation at 9"); checkValue(9, 34); System.out.println(""); System.out.println("Testing evaluation at 10"); checkValue(10, 55); System.out.println(""); /* * Check evaluation when a negative input is supplied */ System.out.println("Testing evaluation at -1"); checkException(-1); } /* * The following method is not really necessary; one could simply * refer to "Fibonacci1.fib" instead of "fib" throughout the main * method. It is included as a convenience to simplify the modification * of this test suite to test other classes (like Fibonacci2) that * also provide a "fib" method */ private static long fib (int i ) { return Fibonacci1.fib(i); } /* * This method checks to see whether the Fibonacci method * returns the second input value when the first is supplied * as its input and displays an appropriate report. */ private static void checkValue ( int i, long v) { try { if ( fib(i) == v ) { System.out.println("SUCCESS: Expected Value was Generated."); } else { System.out.println("FAILURE: Expected Value was Not Generated."); }; } catch (IllegalArgumentException ex) { System.out.println("FAILURE: Unexpected Exception was Thrown."); }; } /* * This final method checks to see whether the Fibonacci method * throws an exception (as expected) and displays an appropriate * report */ private static void checkException ( int i ) { try { fib(i); System.out.println("FAILURE: Expected exception was not Thown."); } catch (IllegalArgumentException ex) { System.out.println("SUCCESS: Expected exception was Thrown."); }; } }