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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.random;
18
import junit.framework.Test;
19
import junit.framework.TestSuite;
21
import org.apache.commons.math.stat.Frequency;
25
* Test cases for the AbstractRandomGenerator class
27
* @version $Revision: 764749 $ $Date: 2009-04-14 07:51:40 -0400 (Tue, 14 Apr 2009) $
30
public class AbstractRandomGeneratorTest extends RandomDataTest {
32
protected TestRandomGenerator testGenerator = new TestRandomGenerator();
34
public AbstractRandomGeneratorTest(String name) {
36
randomData = new RandomDataImpl(testGenerator);
39
public static Test suite() {
40
TestSuite suite = new TestSuite(AbstractRandomGeneratorTest.class);
41
suite.setName("AbstractRandomGenerator Tests");
46
public void testNextInt() {
48
testGenerator.nextInt(-1);
49
fail("IllegalArgumentException expected");
50
} catch (IllegalArgumentException ex) {
53
Frequency freq = new Frequency();
55
for (int i=0; i<smallSampleSize; i++) {
56
value = testGenerator.nextInt(4);
57
assertTrue("nextInt range",(value >= 0) && (value <= 3));
60
long[] observed = new long[4];
61
for (int i=0; i<4; i++) {
62
observed[i] = freq.getCount(i);
65
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
66
* Change to 11.34 for alpha = .01
68
assertTrue("chi-square test -- will fail about 1 in 1000 times",
69
testStatistic.chiSquare(expected,observed) < 16.27);
73
public void testNextLong() {
74
long q1 = Long.MAX_VALUE/4;
78
Frequency freq = new Frequency();
81
for (int i=0; i<smallSampleSize; i++) {
82
val = testGenerator.nextLong();
85
} else if (val < q2) {
87
} else if (val < q3) {
94
long[] observed = new long[4];
95
for (int i=0; i<4; i++) {
96
observed[i] = freq.getCount(i);
99
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
100
* Change to 11.34 for alpha = .01
102
assertTrue("chi-square test -- will fail about 1 in 1000 times",
103
testStatistic.chiSquare(expected,observed) < 16.27);
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()) {
117
/* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
118
* Change to 6.635 for alpha = .01
120
assertTrue("chi-square test -- will fail about 1 in 1000 times",
121
testStatistic.chiSquare(expected,observed) < 10.828);
124
public void testNextFloat() {
125
Frequency freq = new Frequency();
128
for (int i=0; i<smallSampleSize; i++) {
129
val = testGenerator.nextFloat();
132
} else if (val < 0.5) {
134
} else if (val < 0.75) {
139
freq.addValue(value);
141
long[] observed = new long[4];
142
for (int i=0; i<4; i++) {
143
observed[i] = freq.getCount(i);
146
/* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
147
* Change to 11.34 for alpha = .01
149
assertTrue("chi-square test -- will fail about 1 in 1000 times",
150
testStatistic.chiSquare(expected,observed) < 16.27);