package Fibonacci2; import org.junit.*; import static org.junit.Assert.*; /** * Test suite for a simple Fibonacci number calculator; requires * Junit 4.x. * * @author Wayne Eberly */ public class FibTest { /* * Simple Tests */ private void simpleTest( int i ) throws NumericalOverflowException { System.out.print("Testing value at "); System.out.printf("%2d", i); System.out.println(""); assertEquals(Fibonacci.fib(i), SFibonacci.fib(i)); }; /* * Initial tests check that the method returns the * expected values at inputs 0-10 */ @Test public void testZero () throws NumericalOverflowException { simpleTest(0); } @Test public void testOne () throws NumericalOverflowException { simpleTest(1); } @Test public void testTwo () throws NumericalOverflowException { simpleTest(2); } @Test public void testThree () throws NumericalOverflowException { simpleTest(3); } @Test public void testFour () throws NumericalOverflowException { simpleTest(4); } @Test public void testFive () throws NumericalOverflowException { simpleTest(5); } @Test public void testSix () throws NumericalOverflowException { simpleTest(6); } @Test public void testSeven () throws NumericalOverflowException { simpleTest(7); } @Test public void testEight () throws NumericalOverflowException { simpleTest(8); } @Test public void testNine () throws NumericalOverflowException { simpleTest(9); }; @Test public void testTen () throws NumericalOverflowException { simpleTest(10); } /* * This next test checks whether the method throws * the expected exception when on input -1 */ @Test (expected = IllegalArgumentException.class) public void testNegative () throws NumericalOverflowException { System.out.println("Testing evaluation at -1"); Fibonacci.fib(-1); } /* * The final set of tests check whether the method * behaves as expected in the region where * numerical overflow would be expected to * occur */ private void longTest ( int i, String s ) throws NumericalOverflowException { System.out.print("Testing evaluation at "); System.out.printf("%2d", i); System.out.println(""); Long value = new Long(Fibonacci.fib(i)); Long target = new Long(s); assertTrue(value.equals(target)); } @Test public void TestNinety () throws NumericalOverflowException { longTest(90, "2880067194370816120"); } @Test (expected = NumericalOverflowException.class) public void TestNinetyFive () throws NumericalOverflowException { System.out.println("Testing evaluation at 95:"); Fibonacci.fib(95); } }