/**
 * CS-20  Asg4
 * PairOfDiceTest.java
 * Purpose: Unit test of the PairOfDice class.
 *
 * @version 1.3 3/13/05
 * @author Ed Parrish
 */
public final class PairOfDiceTest {
    private static int numErrors;

    /**
     * The main method begins execution of the tests.
     *
     * @param args not used
     */
    public static void main(final String[] args) {
        testPairOfDice();
        testPairOfDice1();
        testRoll();
        testGetSum();
        if (numErrors == 0) {
            System.out.println("*** All tests passed ***");
        }
        System.exit(0);
    }

    /**
     * Convenience method to test for assertions.
     *
     * @param condition The test condition that must be true to pass.
     * @param message The reason for the failure.
     */
    public static void assertTrue(boolean condition, String message) {
        if (!condition) {
            //throw new RuntimeException(message);
            System.out.println("Error: " + message);
            numErrors++;
        }
    }

    /**
     * Test method: PairOfDice()
     */
    public static void testPairOfDice() {
        System.out.println("Testing PairOfDice()");
        PairOfDice pod = new PairOfDice();
        assertTrue(pod.getValue1() >= 1,
            "PairOfDice() makes die1 with value less than 1: "
            + pod.getValue1());
        assertTrue(pod.getValue1() <= 6,
            "PairOfDice() die1 created with more than 6 sides: "
            + pod.getValue1());
        assertTrue(pod.getValue2() >= 1,
            "PairOfDice() makes die2 with value less than 1: "
            + pod.getValue1());
        assertTrue(pod.getValue2() <= 6,
            "PairOfDice() die2 created with more than 6 sides: "
            + pod.getValue2());
    }

    /**
     * Test method: PairOfDice(int)
     */
    public static void testPairOfDice1() {
        System.out.println("Testing PairOfDice(int)");
        PairOfDice pod = new PairOfDice(10);
        assertTrue(pod.getValue1() >= 1,
            "PairOfDice(int) makes die1 with value less than 1: "
            + pod.getValue1());
        assertTrue(pod.getValue2() >= 1,
            "PairOfDice(int) makes die2 with value less than 1: "
            + pod.getValue1());
        for (int i = 1; i < 20; i++) {
            pod = new PairOfDice(i);
            assertTrue(pod.getValue1() <= i,
                "PairOfDice(int) die1 created with more than " + i + "sides: "
                + pod.getValue1());
            assertTrue(pod.getValue2() <= i,
                "PairOfDice(int) die2 created with more than " + i + "sides: "
                + pod.getValue2());
        }
    }

    /**
     * Test method: roll()
     */
    public static void testRoll() {
        System.out.println("Testing roll()");
        final int NUM_CHECKS = 100;
        int sameCount = 0;
        PairOfDice pod = new PairOfDice();
        for (int i = 1; i < NUM_CHECKS; i++) {
            int previousSum = pod.getSum();
            pod.roll();
            if (previousSum == pod.getSum()) sameCount++;
        }
        assertTrue(sameCount < NUM_CHECKS / 4.0,
            "roll() produces many repeated values: " + sameCount);
    }

    /**
     * Test method: getSum()
     */
    public static void testGetSum() {
        System.out.println("Testing getSum()");
        PairOfDice pod = new PairOfDice();
        for (int i = 1; i < 100; i++) {
            pod.roll();
            assertTrue(pod.getValue1() + pod.getValue2() == pod.getSum(),
                "PairOfDice getSum() != getValue1() + getValue2(): "
                + pod.getSum());
        }
    }
}

