001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.random;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020:
021: import org.apache.commons.math.stat.Frequency;
022:
023: /**
024: * Test cases for the AbstractRandomGenerator class
025: *
026: * @version $Revision:$ $Date$
027: */
028:
029: public class AbstractRandomGeneratorTest extends RandomDataTest {
030:
031: protected TestRandomGenerator testGenerator = new TestRandomGenerator();
032:
033: public AbstractRandomGeneratorTest(String name) {
034: super (name);
035: randomData = new RandomDataImpl(testGenerator);
036: }
037:
038: public static Test suite() {
039: TestSuite suite = new TestSuite(
040: AbstractRandomGeneratorTest.class);
041: suite.setName("AbstractRandomGenerator Tests");
042: return suite;
043: }
044:
045: public void testNextInt() {
046: try {
047: int x = testGenerator.nextInt(-1);
048: fail("IllegalArgumentException expected");
049: } catch (IllegalArgumentException ex) {
050: ;
051: }
052: Frequency freq = new Frequency();
053: int value = 0;
054: for (int i = 0; i < smallSampleSize; i++) {
055: value = testGenerator.nextInt(4);
056: assertTrue("nextInt range", (value >= 0) && (value <= 3));
057: freq.addValue(value);
058: }
059: long[] observed = new long[4];
060: for (int i = 0; i < 4; i++) {
061: observed[i] = freq.getCount(i);
062: }
063:
064: /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
065: * Change to 11.34 for alpha = .01
066: */
067: assertTrue(
068: "chi-square test -- will fail about 1 in 1000 times",
069: testStatistic.chiSquare(expected, observed) < 16.27);
070: }
071:
072: public void testNextLong() {
073: long q1 = Long.MAX_VALUE / 4;
074: long q2 = 2 * q1;
075: long q3 = 3 * q1;
076:
077: Frequency freq = new Frequency();
078: long val = 0;
079: int value = 0;
080: for (int i = 0; i < smallSampleSize; i++) {
081: val = testGenerator.nextLong();
082: if (val < q1) {
083: value = 0;
084: } else if (val < q2) {
085: value = 1;
086: } else if (val < q3) {
087: value = 2;
088: } else {
089: value = 3;
090: }
091: freq.addValue(value);
092: }
093: long[] observed = new long[4];
094: for (int i = 0; i < 4; i++) {
095: observed[i] = freq.getCount(i);
096: }
097:
098: /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
099: * Change to 11.34 for alpha = .01
100: */
101: assertTrue(
102: "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(
121: "chi-square test -- will fail about 1 in 1000 times",
122: testStatistic.chiSquare(expected, observed) < 10.828);
123: }
124:
125: public void testNextFloat() {
126: Frequency freq = new Frequency();
127: float val = 0;
128: int value = 0;
129: for (int i = 0; i < smallSampleSize; i++) {
130: val = testGenerator.nextFloat();
131: if (val < 0.25) {
132: value = 0;
133: } else if (val < 0.5) {
134: value = 1;
135: } else if (val < 0.75) {
136: value = 2;
137: } else {
138: value = 3;
139: }
140: freq.addValue(value);
141: }
142: long[] observed = new long[4];
143: for (int i = 0; i < 4; i++) {
144: observed[i] = freq.getCount(i);
145: }
146:
147: /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
148: * Change to 11.34 for alpha = .01
149: */
150: assertTrue(
151: "chi-square test -- will fail about 1 in 1000 times",
152: testStatistic.chiSquare(expected, observed) < 16.27);
153: }
154: }
|