001    /**
002     * This is part of CSE 331 Problem Set 0.
003     */
004    package ps0.test;
005    import ps0.*;
006    
007    import junit.framework.TestCase;
008    
009    /**
010     * FibonacciTest is a glassbox test of the Fibonacci class.
011     *
012     * Recall that the Fibonacci sequence is a recursive
013     * sequence where the first two terms of the sequence are 1 and all subsequent
014     * terms are the sum of the previous two terms.
015     *
016     * Thus, the Fibonacci sequence starts out as 1, 1, 2, 3, 5, 8, 13...
017     * The first 1 in the sequence is considered the "0th" term,
018     * so the indices that <code>ps0.Fibonacci</code> uses are 0-based.
019     *
020     * @see ps0.Fibonacci
021     *
022     * @author mbolin
023     */
024    public class FibonacciTest extends TestCase {
025    
026        private Fibonacci fib = null;
027    
028        protected void setUp() throws Exception {
029            fib = new Fibonacci();
030        }
031    
032        /**
033         * Tests to see that Fibonacci throws an IllegalArgumentException
034         * for a negative number but not for zero or for a positive number.
035         */
036        public void testThrowsIllegalArgumentException() {
037            boolean throwsIllegalArgumentException;
038    
039            // test -1
040            throwsIllegalArgumentException = false;
041            try {
042                fib.getFibTerm(-1);
043            } catch (IllegalArgumentException ex) {
044                throwsIllegalArgumentException = true;
045            } catch (Exception ex) {
046                fail("Threw exception other than IllegalArgumentException for a negative number: "
047                        + ex);
048            }
049            assertTrue(
050                    "Did not throw IllegalArgumentException for a negative number.",
051                    throwsIllegalArgumentException);
052    
053            // test 0
054            try {
055                fib.getFibTerm(0);
056            } catch (IllegalArgumentException ex) {
057                fail("Threw IllegalArgumentException for 0 but 0 is nonnegative: "
058                        + ex);
059            }
060    
061            // test 1
062            try {
063                fib.getFibTerm(1);
064            } catch (IllegalArgumentException ex) {
065                fail("Threw IllegalArgumentException for 1 but 1 is nonnegative: "
066                        + ex);
067            }
068        }
069    
070        /** Tests to see that Fibonacci returns the correct value for the base cases, n=0 and n=1 */
071        public void testBaseCase() {
072            assertEquals("getFibTerm(0) should return 1", 1, fib.getFibTerm(0));
073            assertEquals("getFibTerm(1) should return 1", 1, fib.getFibTerm(1));
074        }
075    
076        /** Tests inductive cases of the Fibonacci sequence */
077        public void testInductiveCase() {
078            int[][] cases = new int[][] {
079                    { 2, 2 },
080                    { 3, 3 },
081                    { 4, 5 },
082                    { 5, 8 },
083                    { 6, 13 },
084                    { 7, 21 }
085                };
086            for (int i = 0; i < cases.length; i++) {
087                assertEquals("getFibTerm(" + cases[i][0] + ") should return "
088                        + cases[i][1], cases[i][1], fib.getFibTerm(cases[i][0]));
089            }
090        }
091    
092    }