1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
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
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
66
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
100
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
118
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
147
148
149 assertTrue("chi-square test -- will fail about 1 in 1000 times",
150 testStatistic.chiSquare(expected,observed) < 16.27);
151 }
152 }