package org.intermine.web.logic;

import java.math.BigDecimal;
import java.util.HashMap;

import org.apache.commons.math.distribution.HypergeometricDistributionImpl;

import junit.framework.TestCase;

public class WidgetUtilTest extends TestCase
{
    private final int REFERENCE_SIZE = 5;
    private HashMap<String, BigDecimal> resultsMap = new HashMap();
    private String[] id = new String[REFERENCE_SIZE];
    private int[] taggedSample = new int[REFERENCE_SIZE];
    private int[] taggedPopulation = new int[REFERENCE_SIZE];
    private BigDecimal[] expectedResults = new BigDecimal[REFERENCE_SIZE];
    private int bagsize = 3;
    private int total = 100;
    public  WidgetUtilTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {

        // these numbers are generated via this website:
        // http://www.quantitativeskills.com/sisa/distributions/hypghlp.htm

        id[1] = "overrepresented";
        taggedSample[1] = 3;
        taggedPopulation[1] = 20;
        expectedResults[1] = new BigDecimal(0.00705009276437847977325734660780653939582407474517822265625);

        id[2] = "notEnriched";
        taggedSample[2] = 1;
        taggedPopulation[2] = 10;
        expectedResults[2] = new BigDecimal(0.273469387755102155690423160194768570363521575927734375);

        id[3] = "underrepresented";
        taggedSample[3] = 1;
        taggedPopulation[3] = 50;
        expectedResults[3] = new BigDecimal(0.8787878787878777853137535203131847083568572998046875);

        id[4] = "one";
        taggedSample[4] = 3;
        taggedPopulation[4] = 100;
        expectedResults[4] = new BigDecimal(1);

        HypergeometricDistributionImpl h = new HypergeometricDistributionImpl(total,bagsize,bagsize);

        for (int i = 1; i <= 4; i++) {
            h.setNumberOfSuccesses(taggedPopulation[i]);
            BigDecimal p = new BigDecimal(h.upperCumulativeProbability(taggedSample[i]));
            resultsMap.put(id[i], p);
        }
    }

    public void testHypergeometric() throws Exception {
        for (int i = 0; i < 4; i++) {
            assertEquals(expectedResults[i], resultsMap.get(id[i]));
        }
    }
}

@@ -626,17 +626,19 @@
 gsize = 
-3
+100
 ;%0A    pr
@@ -654,24 +654,25 @@
  total = 100
+0
 ;%0A    public
@@ -785,166 +785,21 @@
     
-// these numbers are generated via this website:%0A        // http://www.quantitativeskills.com/sisa/distributions/hypghlp.htm%0A%0A        id%5B1%5D = %22overrepresented
+id%5B1%5D = %22half
 %22;%0A 
@@ -819,25 +819,26 @@
 Sample%5B1%5D = 
-3
+50
 ;%0A        ta
@@ -857,17 +857,18 @@
 on%5B1%5D = 
-2
+50
 0;%0A     
@@ -912,66 +912,60 @@
 l(0.
-0070500927643784797732573466078065393958240747451782226562
+5419460460463944029285698888998012989759445190429687
 5);%0A
@@ -1109,59 +1109,54 @@
 l(0.
-273469387755102155690423160194768570363521575927734
+6530722852079744455977561301551759243011474609
 375)
@@ -1179,22 +1179,14 @@
  = %22
-underrepresent
+enrich
 ed%22;
@@ -1208,25 +1208,26 @@
 Sample%5B3%5D = 
-1
+25
 ;%0A        ta
@@ -1242,25 +1242,26 @@
 lation%5B3%5D = 
-5
+10
 0;%0A        e
@@ -1301,57 +1301,72 @@
 l(0.
-878787878787877785313753520
+0000031931122567
 31
+4
 31
-847083568572998046
+9696078761534607792782480828464031219482421
 875)

change values in test to make a little more interesting