package net.thingly.dice;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for Dice.
 */
public class DiceTest extends TestCase {

	private int iter = 100000;

	/**
	 * Create the test case
	 *
	 * @param testName
	 *            name of the test case
	 */
	public DiceTest(String testName) {
		super(testName);
	}

	/**
	 * @return the suite of tests being tested
	 */
	public static Test suite() {
		return new TestSuite(DiceTest.class);
	}

	/**
	 * Rigourous Test :-)
	 */
	public void testRoll() {
		// Hashtable<Integer, Double> probs = Prob.prob(1, 6);
		int[] results = new int[6];
		for (int i = 0; i < iter; i++) {
			int r = Dice.roll();
			assertTrue(r >= 1 && r <= 6);
			results[r - 1]++;
		}

		double expected = ((double) iter) / 6.0;
		for (int i = 0; i < results.length; i++) {
			double pct = ((double) results[i]) / iter;
			// not super-scientific, but i'm satisfied if we're within a half a
			// percent for each possible outcome
			assertTrue(Math.abs(pct - (expected / ((double) iter))) <= .005);
		}
	}

	/**
	 * Rigourous Test :-)
	 */
	public void testRollN() {
		assertTrue(true);
	}

	/**
	 * Rigourous Test :-)
	 */
	public void testRollND() {
		assertTrue(true);
	}
}

@@ -20,16 +20,80 @@
 .dice;%0A%0A
+import java.util.Hashtable;%0A%0Aimport net.thingly.dice.util.Prob;%0A
 import j
@@ -928,19 +928,24 @@
 %09double 
-pct
+observed
  = ((dou
@@ -1107,23 +1107,27 @@
 abs(
-pct
+observed
  - 
-(
 expected
  / (
@@ -1126,28 +1126,20 @@
 cted
+)
  / 
-((double) iter)))
+expected
  %3C= 
@@ -1219,110 +1219,1175 @@
 %7B%0A%09%09
-assertTrue(true);%0A%09%7D%0A%0A%09/**%0A%09 * Rigourous Test :-)%0A%09 */%0A%09public void testRollND() %7B%0A%09%09assertTrue(true);
+int%5B%5D results = new int%5B6%5D;%0A%09%09int%5B%5D rolls = Dice.roll(iter);%0A%09%09for (int i = 0; i %3C iter; i++) %7B%0A%09%09%09results%5Brolls%5Bi%5D - 1%5D++;%0A%09%09%7D%0A%0A%09%09double expected = ((double) iter) / 6.0;%0A%09%09for (int i = 0; i %3C results.length; i++) %7B%0A%09%09%09double observed = ((double) results%5Bi%5D) / iter;%0A%09%09%09// not super-scientific, but i'm satisfied if we're within a half a%0A%09%09%09// percent for each possible outcome%0A%09%09%09assertTrue(Math.abs(observed - expected) / expected %3C= .005);%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Rigourous Test :-)%0A%09 */%0A%09public void testRollND() %7B%0A%09%09Hashtable%3CInteger, Integer%3E results = new Hashtable%3CInteger, Integer%3E();%0A%09%09for (int i = 0; i %3C iter; i++) %7B%0A%09%09%09int%5B%5D r = Dice.roll(4, 12);%0A%09%09%09assertTrue(r.length == 4);%0A%09%09%09int key = 0;%0A%09%09%09for (int j = 0; j %3C r.length; i++) %7B%0A%09%09%09%09assertTrue(r%5Bj%5D %3E= 1 && r%5Bj%5D %3C= 12);%0A%09%09%09%09key += r%5Bj%5D;%0A%09%09%09%7D%0A%09%09%09int value = results.getOrDefault(key, 0);%0A%09%09%09results.put(key, value + 1);%0A%09%09%7D%0A%0A%09%09Hashtable%3CInteger, Double%3E probs = Prob.prob(4, 12);%0A%09%09for (int key : probs.keySet()) %7B%0A%09%09%09double observed = ((double) results.getOrDefault(key, 0))%0A%09%09%09%09%09/ ((double) iter);%0A%09%09%09double expected = probs.get(key);%0A%09%09%09assertTrue((Math.abs(observed - expected) / expected) %3C= .005);%0A%09%09%7D
 %0A%09%7D%0A

complete unit tests