001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.random;
018 import junit.framework.Test;
019 import junit.framework.TestSuite;
020
021 import org.apache.commons.math.stat.Frequency;
022
023
024 /**
025 * Test cases for the AbstractRandomGenerator class
026 *
027 * @version $Revision: 764749 $ $Date: 2009-04-14 07:51:40 -0400 (Tue, 14 Apr 2009) $
028 */
029
030 public class AbstractRandomGeneratorTest extends RandomDataTest {
031
032 protected TestRandomGenerator testGenerator = new TestRandomGenerator();
033
034 public AbstractRandomGeneratorTest(String name) {
035 super(name);
036 randomData = new RandomDataImpl(testGenerator);
037 }
038
039 public static Test suite() {
040 TestSuite suite = new TestSuite(AbstractRandomGeneratorTest.class);
041 suite.setName("AbstractRandomGenerator Tests");
042 return suite;
043 }
044
045 @Override
046 public void testNextInt() {
047 try {
048 testGenerator.nextInt(-1);
049 fail("IllegalArgumentException expected");
050 } catch (IllegalArgumentException ex) {
051 // ignored
052 }
053 Frequency freq = new Frequency();
054 int value = 0;
055 for (int i=0; i<smallSampleSize; i++) {
056 value = testGenerator.nextInt(4);
057 assertTrue("nextInt range",(value >= 0) && (value <= 3));
058 freq.addValue(value);
059 }
060 long[] observed = new long[4];
061 for (int i=0; i<4; i++) {
062 observed[i] = freq.getCount(i);
063 }
064
065 /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
066 * Change to 11.34 for alpha = .01
067 */
068 assertTrue("chi-square test -- will fail about 1 in 1000 times",
069 testStatistic.chiSquare(expected,observed) < 16.27);
070 }
071
072 @Override
073 public void testNextLong() {
074 long q1 = Long.MAX_VALUE/4;
075 long q2 = 2 * q1;
076 long q3 = 3 * q1;
077
078 Frequency freq = new Frequency();
079 long val = 0;
080 int value = 0;
081 for (int i=0; i<smallSampleSize; i++) {
082 val = testGenerator.nextLong();
083 if (val < q1) {
084 value = 0;
085 } else if (val < q2) {
086 value = 1;
087 } else if (val < q3) {
088 value = 2;
089 } else {
090 value = 3;
091 }
092 freq.addValue(value);
093 }
094 long[] observed = new long[4];
095 for (int i=0; i<4; i++) {
096 observed[i] = freq.getCount(i);
097 }
098
099 /* 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 }