package org.mitre.synthea.helpers;

import static org.junit.Assert.*;

import java.util.Random;

import org.junit.Assert;
import org.junit.Test;

public class RandomCollectionTest {
  
  @SuppressWarnings("serial")
  private class Fixed extends Random {
    // nextDouble returns value between 0.0 (inclusive) and 1.0 (exclusive)
    private final double[] values = {0.0, 0.5, 0.999};
    private int index = 0;
    
    @Override
    public double nextDouble() {
      double returnValue = values[index];
      index++;
      if (index >= values.length) {
        index = 0;
      }
      return returnValue;
    }
  }

  @Test
  public void testZeroWeights() {
    RandomCollection<String> rc = new RandomCollection<String>();
    rc.add(0.0, "white");
    rc.add(1.0, "black");
    rc.add(0.0, "asian");
    
    Random random = new Random();
    for (int i=0; i < 10; i++) {
      String randomString = rc.next(random);
      assertEquals("black", randomString);
    }
  }
  
  @Test
  public void testFixed() {
    Fixed fixed = new Fixed();
    double[] expected = {0.0, 0.5, 0.999, 0.0, 0.5, 0.999, 0.0, 0.5, 0.999};
    for (int i=0; i < expected.length; i++) {
      Assert.assertTrue(fixed.nextDouble() == expected[i]);
    }
  }
  
  @Test
  public void testFixedCounts() {
    RandomCollection<String> rc = new RandomCollection<String>();
    rc.add(0.33, "white");
    rc.add(0.33, "black");
    rc.add(0.33, "asian");
    
    int white = 0;
    int black = 0;
    int asian = 0;
    
    Fixed fixed = new Fixed();
    for (int i=0; i < 9; i++) {
      String notRandomString = rc.next(fixed);
      switch (notRandomString) {
        case "white":
          white++;
          break;
        case "black":
          black++;
          break;
        case "asian":
          asian++;
          break;
      }
    }
    Assert.assertTrue(3 == white);
    Assert.assertTrue(3 == black);
    Assert.assertTrue(3 == asian);
  }

}

@@ -33,43 +33,8 @@
 s;%0A%0A
-import static org.junit.Assert.*;%0A%0A
 impo
@@ -54,16 +54,16 @@
 Random;%0A
+
 %0Aimport 
@@ -814,25 +814,27 @@
   for (int i
-=
+ = 
 0; i %3C 10; i
@@ -890,16 +890,23 @@
 ;%0A      
+Assert.
 assertEq
@@ -1101,25 +1101,27 @@
   for (int i
-=
+ = 
 0; i %3C expec
@@ -1513,17 +1513,19 @@
 r (int i
-=
+ = 
 0; i %3C 9
@@ -1751,16 +1751,16 @@
 asian%22:%0A
-
         
@@ -1783,24 +1783,58 @@
      break;%0A
+        default:%0A          break;%0A
       %7D%0A    

Fix checkstyle warnings.