    1   /*
    2    * Copyright 2005 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.random;
    17  import junit.framework.Test;
    18  import junit.framework.TestSuite;
    19
    20  import org.apache.commons.math.stat.Frequency;
    21
    22
    23  /***
    24   * Test cases for the AbstractRandomGenerator class
    25   *
    26   * @version $Revision:$ $Date$
    27   */
    28
    29  public class AbstractRandomGeneratorTest extends RandomDataTest {
    30
    31      protected TestRandomGenerator testGenerator = new TestRandomGenerator();
    32
    33      public AbstractRandomGeneratorTest(String name) {
    34          super(name);
    35          randomData = new RandomDataImpl(testGenerator);
    36      }
    37
    38      public static Test suite() {
    39          TestSuite suite = new TestSuite(AbstractRandomGeneratorTest.class);
    40          suite.setName("AbstractRandomGenerator Tests");
    41          return suite;
    42      }
    43
    44      public void testNextInt() {
    45          try {
    46              int x = testGenerator.nextInt(-1);
    47              fail("IllegalArgumentException expected");
    48          } catch (IllegalArgumentException ex) {
    49              ;
    50          }
    51          Frequency freq = new Frequency();
    52          int value = 0;
    53          for (int i=0; i<smallSampleSize; i++) {
    54              value = testGenerator.nextInt(4);
    55              assertTrue("nextInt range",(value >= 0) && (value <= 3));
    56              freq.addValue(value);
    57          }
    58          long[] observed = new long[4];
    59          for (int i=0; i<4; i++) {
    60              observed[i] = freq.getCount(i);
    61          }
    62
    63          /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
    64           * Change to 11.34 for alpha = .01
    65           */
    66          assertTrue("chi-square test -- will fail about 1 in 1000 times",
    67                  testStatistic.chiSquare(expected,observed) < 16.27);
    68      }
    69
    70      public void testNextLong() {
    71          long q1 = Long.MAX_VALUE/4;
    72          long q2 = 2 *  q1;
    73          long q3 = 3 * q1;
    74
    75          Frequency freq = new Frequency();
    76          long val = 0;
    77          int value = 0;
    78          for (int i=0; i<smallSampleSize; i++) {
    79              val = testGenerator.nextLong();
    80              if (val < q1) {
    81                  value = 0;
    82              } else if (val < q2) {
    83                  value = 1;
    84              } else if (val < q3) {
    85                  value = 2;
    86              } else {
    87                  value = 3;
    88              }
    89              freq.addValue(value);
    90          }
    91          long[] observed = new long[4];
    92          for (int i=0; i<4; i++) {
    93              observed[i] = freq.getCount(i);
    94          }
    95
    96          /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
    97           * Change to 11.34 for alpha = .01
    98           */
    99          assertTrue("chi-square test -- will fail about 1 in 1000 times",
    100                 testStatistic.chiSquare(expected,observed) < 16.27);
    101     }
    102
    103     public void testNextBoolean() {
    104         long halfSampleSize = smallSampleSize / 2;
    105         double[] expected = {halfSampleSize, halfSampleSize};
    106         long[] observed = new long[2];
    107         for (int i=0; i<smallSampleSize; i++) {
    108             if (testGenerator.nextBoolean()) {
    109                 observed[0]++;
    110             } else {
    111                 observed[1]++;
    112             }
    113         }
    114         /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
    115          * Change to 6.635 for alpha = .01
    116          */
    117         assertTrue("chi-square test -- will fail about 1 in 1000 times",
    118                 testStatistic.chiSquare(expected,observed) < 10.828);
    119     }
    120
    121     public void testNextFloat() {
    122         Frequency freq = new Frequency();
    123         float val = 0;
    124         int value = 0;
    125         for (int i=0; i<smallSampleSize; i++) {
    126             val = testGenerator.nextFloat();
    127             if (val < 0.25) {
    128                 value = 0;
    129             } else if (val < 0.5) {
    130                 value = 1;
    131             } else if (val < 0.75) {
    132                 value = 2;
    133             } else {
    134                 value = 3;
    135             }
    136             freq.addValue(value);
    137         }
    138         long[] observed = new long[4];
    139         for (int i=0; i<4; i++) {
    140             observed[i] = freq.getCount(i);
    141         }
    142
    143         /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
    144          * Change to 11.34 for alpha = .01
    145          */
    146         assertTrue("chi-square test -- will fail about 1 in 1000 times",
    147                 testStatistic.chiSquare(expected,observed) < 16.27);
    148     }
    149 }