/**
 * 
 */
package test.blog.distrib;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;

import org.junit.Test;

import blog.distrib.Categorical;

/**
 * @author cgioia
 * @since Jun 17, 2014
 */
public class TestCategorical implements TestDistributions {
  private final double ERROR = 10e-5;

  /** Categorical. "Albert" => 0.5, "Bob" => 0.2, "Craig" => 0.3. */
  public void testCategorical1(Categorical cat) {
    assertEquals(0.0, cat.getProb("Andy"), ERROR);
    assertEquals(0.5, cat.getProb("Albert"), ERROR);
    assertEquals(0.2, cat.getProb("Bob"), ERROR);
    assertEquals(0.3, cat.getProb("Craig"), ERROR);
    assertEquals(0.0, cat.getProb(null), ERROR);

    assertEquals(Double.NEGATIVE_INFINITY, cat.getLogProb("Andy"), ERROR);
    assertEquals(Math.log(0.5), cat.getLogProb("Albert"), ERROR);
    assertEquals(Math.log(0.2), cat.getLogProb("Bob"), ERROR);
    assertEquals(Math.log(0.3), cat.getLogProb("Craig"), ERROR);
    assertEquals(Double.NEGATIVE_INFINITY, cat.getLogProb(null), ERROR);
  }

  @Test
  public void testProbabilityViaConstructor() {
    // not needed. will be removed
  }

  @Test
  public void testProbabilityViaSetParams() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Bob", 0.2);
    map.put("Craig", 0.3);
    map.put("Albert", 0.5);
    cat.setParams(new Object[] { map });
    testCategorical1(cat);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testInsufficientArguments() {
    Categorical cat = new Categorical();
    cat.setParams(new Object[] { null });
    cat.sampleVal();
  }

  @Test(expected = IllegalArgumentException.class)
  public void testIncorrectArguments() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Bob", 0.2);
    map.put("Craig", -0.01);
    cat.setParams(new Object[] { map });
    cat.sampleVal();
  }

  @Test(expected = IllegalArgumentException.class)
  public void testProbabilitySumTooSmall() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Albert", 0.0);
    map.put("Bob", 0.0);
    cat.setParams(new Object[] { map });
    cat.sampleVal();
  }

  @Test
  public void testNormalization() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Bob", 6.0);
    map.put("Craig", 9.0);
    map.put("Albert", 15.0);
    cat.setParams(new Object[] { map });
    testCategorical1(cat);
  }

  @Test
  public void testDoubleSet() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Albert", 0.5);
    map.put("Bob", 0.2);
    map.put("Craig", 0.3);
    cat.setParams(new Object[] { null });
    cat.setParams(new Object[] { map });
    testCategorical1(cat);
  }

  @Test
  public void testSetParamsIntegerArguments() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Albert", 15);
    map.put("Bob", 6);
    map.put("Craig", 9);
    cat.setParams(new Object[] { map });
    testCategorical1(cat);
  }

  @Test
  public void testGetProbIntegerArguments() {
    // not needed
  }

  @Test
  public void testGetFiniteSupport() {
    Categorical cat = new Categorical();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Bob", 0.2);
    map.put("Craig", 0.3);
    map.put("Albert", 0.5);
    map.put("Andy", 0.0);
    cat.setParams(new Object[] { map });

    Object[] list = cat.getFiniteSupport();
    assertEquals(3, list.length);
    assertEquals(list[0], "Bob");
    assertEquals(list[1], "Craig");
    assertEquals(list[2], "Albert");
  }

}

@@ -82,59 +82,167 @@
 ls;%0A
-%0Aimport java.util.HashMap;%0A%0Aimport org.junit.Test;%0A
+import static org.junit.Assert.assertTrue;%0A%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0A%0Aimport org.junit.Test;%0A%0Aimport scala.actors.threadpool.Arrays;
 %0Aimp
@@ -3781,70 +3781,149 @@
     
-assertEquals(3, list.length);%0A    assertEquals(list%5B0%5D, 
+HashSet%3CObject%3E values = new HashSet%3CObject%3E(Arrays.asList(list));%0A    assertEquals(3, values.size());%0A    assertTrue(values.contains(
 %22Bob%22)
+)
 ;%0A  
@@ -3934,32 +3934,37 @@
 sert
-Equals(list%5B1%5D, 
+True(values.contains(
 %22Craig%22)
 ;%0A  
@@ -3959,16 +3959,17 @@
 %22Craig%22)
+)
 ;%0A    as
@@ -3976,24 +3976,29 @@
 sert
-Equals(list%5B2%5D, 
+True(values.contains(
 %22Alb
@@ -4002,16 +4002,17 @@
 Albert%22)
+)
 ;%0A  %7D%0A%0A%7D

fix the test in TestCategorical