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: 764749 $ $Date: 2009-04-14 07:51:40 -0400 (Tue, 14 Apr 2009) $
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      @Override
46      public void testNextInt() {
47          try {
48              testGenerator.nextInt(-1);
49              fail("IllegalArgumentException expected");
50          } catch (IllegalArgumentException ex) {
51              // ignored
52          }
53          Frequency freq = new Frequency();
54          int value = 0;
55          for (int i=0; i<smallSampleSize; i++) {
56              value = testGenerator.nextInt(4);
57              assertTrue("nextInt range",(value >= 0) && (value <= 3));
58              freq.addValue(value);  
59          }
60          long[] observed = new long[4];
61          for (int i=0; i<4; i++) {
62              observed[i] = freq.getCount(i);
63          } 
64          
65          /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
66           * Change to 11.34 for alpha = .01
67           */
68          assertTrue("chi-square test -- will fail about 1 in 1000 times",
69                  testStatistic.chiSquare(expected,observed) < 16.27);    
70      }
71      
72      @Override
73      public void testNextLong() {
74          long q1 = Long.MAX_VALUE/4;
75          long q2 = 2 *  q1;
76          long q3 = 3 * q1;
77          
78          Frequency freq = new Frequency();
79          long val = 0;
80          int value = 0;
81          for (int i=0; i<smallSampleSize; i++) {
82              val = testGenerator.nextLong();
83              if (val < q1) {
84                  value = 0;
85              } else if (val < q2) {
86                  value = 1;
87              } else if (val < q3) {
88                  value = 2;
89              } else {
90                  value = 3;
91              }
92              freq.addValue(value);  
93          }
94          long[] observed = new long[4];
95          for (int i=0; i<4; i++) {
96              observed[i] = freq.getCount(i);
97          } 
98          
99          /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
100          * Change to 11.34 for alpha = .01
101          */
102         assertTrue("chi-square test -- will fail about 1 in 1000 times",
103                 testStatistic.chiSquare(expected,observed) < 16.27);    
104     }
105     
106     public void testNextBoolean() {
107         long halfSampleSize = smallSampleSize / 2; 
108         double[] expected = {halfSampleSize, halfSampleSize};
109         long[] observed = new long[2];
110         for (int i=0; i<smallSampleSize; i++) {
111             if (testGenerator.nextBoolean()) {
112                 observed[0]++;
113             } else {
114                 observed[1]++;
115             }
116         }
117         /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
118          * Change to 6.635 for alpha = .01
119          */
120         assertTrue("chi-square test -- will fail about 1 in 1000 times",
121                 testStatistic.chiSquare(expected,observed) < 10.828);    
122     }
123     
124     public void testNextFloat() {
125         Frequency freq = new Frequency();
126         float val = 0;
127         int value = 0;
128         for (int i=0; i<smallSampleSize; i++) {
129             val = testGenerator.nextFloat();
130             if (val < 0.25) {
131                 value = 0;
132             } else if (val < 0.5) {
133                 value = 1;
134             } else if (val < 0.75) {
135                 value = 2;
136             } else {
137                 value = 3;
138             }
139             freq.addValue(value);  
140         }
141         long[] observed = new long[4];
142         for (int i=0; i<4; i++) {
143             observed[i] = freq.getCount(i);
144         } 
145         
146         /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
147          * Change to 11.34 for alpha = .01
148          */
149         assertTrue("chi-square test -- will fail about 1 in 1000 times",
150                 testStatistic.chiSquare(expected,observed) < 16.27);    
151     }
152 }