1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.math.random;
17 import junit.framework.Test;
18 import junit.framework.TestSuite;
19
20 import org.apache.commons.math.stat.Frequency;
21
22
23 /**
24 * Test cases for the AbstractRandomGenerator class
25 *
26 * @version $Revision:$ $Date$
27 */
28
29 public class AbstractRandomGeneratorTest extends RandomDataTest {
30
31 protected TestRandomGenerator testGenerator = new TestRandomGenerator();
32
33 public AbstractRandomGeneratorTest(String name) {
34 super(name);
35 randomData = new RandomDataImpl(testGenerator);
36 }
37
38 public static Test suite() {
39 TestSuite suite = new TestSuite(AbstractRandomGeneratorTest.class);
40 suite.setName("AbstractRandomGenerator Tests");
41 return suite;
42 }
43
44 public void testNextInt() {
45 try {
46 int x = testGenerator.nextInt(-1);
47 fail("IllegalArgumentException expected");
48 } catch (IllegalArgumentException ex) {
49 ;
50 }
51 Frequency freq = new Frequency();
52 int value = 0;
53 for (int i=0; i<smallSampleSize; i++) {
54 value = testGenerator.nextInt(4);
55 assertTrue("nextInt range",(value >= 0) && (value <= 3));
56 freq.addValue(value);
57 }
58 long[] observed = new long[4];
59 for (int i=0; i<4; i++) {
60 observed[i] = freq.getCount(i);
61 }
62
63
64
65
66 assertTrue("chi-square test -- will fail about 1 in 1000 times",
67 testStatistic.chiSquare(expected,observed) < 16.27);
68 }
69
70 public void testNextLong() {
71 long q1 = Long.MAX_VALUE/4;
72 long q2 = 2 * q1;
73 long q3 = 3 * q1;
74
75 Frequency freq = new Frequency();
76 long val = 0;
77 int value = 0;
78 for (int i=0; i<smallSampleSize; i++) {
79 val = testGenerator.nextLong();
80 if (val < q1) {
81 value = 0;
82 } else if (val < q2) {
83 value = 1;
84 } else if (val < q3) {
85 value = 2;
86 } else {
87 value = 3;
88 }
89 freq.addValue(value);
90 }
91 long[] observed = new long[4];
92 for (int i=0; i<4; i++) {
93 observed[i] = freq.getCount(i);
94 }
95
96
97
98
99 assertTrue("chi-square test -- will fail about 1 in 1000 times",
100 testStatistic.chiSquare(expected,observed) < 16.27);
101 }
102
103 public void testNextBoolean() {
104 long halfSampleSize = smallSampleSize / 2;
105 double[] expected = {halfSampleSize, halfSampleSize};
106 long[] observed = new long[2];
107 for (int i=0; i<smallSampleSize; i++) {
108 if (testGenerator.nextBoolean()) {
109 observed[0]++;
110 } else {
111 observed[1]++;
112 }
113 }
114
115
116
117 assertTrue("chi-square test -- will fail about 1 in 1000 times",
118 testStatistic.chiSquare(expected,observed) < 10.828);
119 }
120
121 public void testNextFloat() {
122 Frequency freq = new Frequency();
123 float val = 0;
124 int value = 0;
125 for (int i=0; i<smallSampleSize; i++) {
126 val = testGenerator.nextFloat();
127 if (val < 0.25) {
128 value = 0;
129 } else if (val < 0.5) {
130 value = 1;
131 } else if (val < 0.75) {
132 value = 2;
133 } else {
134 value = 3;
135 }
136 freq.addValue(value);
137 }
138 long[] observed = new long[4];
139 for (int i=0; i<4; i++) {
140 observed[i] = freq.getCount(i);
141 }
142
143
144
145
146 assertTrue("chi-square test -- will fail about 1 in 1000 times",
147 testStatistic.chiSquare(expected,observed) < 16.27);
148 }
149 }