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