001/**
002 * This is part of HW0: Environment Setup and Java Introduction for CSE 331.
003 */
004package hw3.test;
005import hw3.*;
006
007import org.junit.Test;
008import org.junit.BeforeClass;
009import static org.junit.Assert.*;
010
011/**
012 * FibonacciTest is a glassbox test of the Fibonacci class.
013 *
014 * Recall that the Fibonacci sequence is a recursive
015 * sequence where the first two terms of the sequence are 1 and all subsequent
016 * terms are the sum of the previous two terms.
017 *
018 * Thus, the Fibonacci sequence starts out as 1, 1, 2, 3, 5, 8, 13...
019 * The first 1 in the sequence is considered the "0th" term,
020 * so the indices that <code>hw3.Fibonacci</code> uses are 0-based.
021 *
022 * @see hw3.Fibonacci
023 *
024 * @author mbolin
025 */
026@SuppressWarnings("nullness")
027public class FibonacciTest {
028
029    private static Fibonacci fib = null;
030
031    @BeforeClass
032    public static void setupBeforeTests() throws Exception {
033        fib = new Fibonacci();
034    }
035
036    /**
037     * Tests that Fibonacci throws an IllegalArgumentException
038     * for a negative number.
039     */
040    @Test(expected=IllegalArgumentException.class)
041    public void expectedIllegalArgumentException() {
042        fib.getFibTerm(-1);
043    }
044
045
046    /**
047     * Tests that Fibonacci throws no IllegalArgumentException
048     * for zero or for a positive number.
049     */
050    @Test
051    public void testThrowsIllegalArgumentException() {
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    @Test
072    public void testBaseCase() {
073        assertEquals("getFibTerm(0)", 1, fib.getFibTerm(0));
074        assertEquals("getFibTerm(1)", 1, fib.getFibTerm(1));
075    }
076
077    /** Tests inductive cases of the Fibonacci sequence */
078    @Test
079    public void testInductiveCase() {
080        int[][] cases = new int[][] {
081                { 2, 2 },
082                { 3, 3 },
083                { 4, 5 },
084                { 5, 8 },
085                { 6, 13 },
086                { 7, 21 }
087            };
088        for (int i = 0; i < cases.length; i++) {
089            assertEquals("getFibTerm(" + cases[i][0] + ")",
090                         cases[i][1], fib.getFibTerm(cases[i][0]));
091        }
092    }
093
094}