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 public void testNextInt() {
46 try {
47 testGenerator.nextInt(-1);
48 fail("IllegalArgumentException expected");
49 } catch (IllegalArgumentException ex) {
50 ;
51 }
52 Frequency freq = new Frequency();
53 int value = 0;
54 for (int i=0; i<smallSampleSize; i++) {
55 value = testGenerator.nextInt(4);
56 assertTrue("nextInt range",(value >= 0) && (value <= 3));
57 freq.addValue(value);
58 }
59 long[] observed = new long[4];
60 for (int i=0; i<4; i++) {
61 observed[i] = freq.getCount(i);
62 }
63
64
65
66
67 assertTrue("chi-square test -- will fail about 1 in 1000 times",
68 testStatistic.chiSquare(expected,observed) < 16.27);
69 }
70
71 public void testNextLong() {
72 long q1 = Long.MAX_VALUE/4;
73 long q2 = 2 * q1;
74 long q3 = 3 * q1;
75
76 Frequency freq = new Frequency();
77 long val = 0;
78 int value = 0;
79 for (int i=0; i<smallSampleSize; i++) {
80 val = testGenerator.nextLong();
81 if (val < q1) {
82 value = 0;
83 } else if (val < q2) {
84 value = 1;
85 } else if (val < q3) {
86 value = 2;
87 } else {
88 value = 3;
89 }
90 freq.addValue(value);
91 }
92 long[] observed = new long[4];
93 for (int i=0; i<4; i++) {
94 observed[i] = freq.getCount(i);
95 }
96
97
98
99
100 assertTrue("chi-square test -- will fail about 1 in 1000 times",
101 testStatistic.chiSquare(expected,observed) < 16.27);
102 }
103
104 public void testNextBoolean() {
105 long halfSampleSize = smallSampleSize / 2;
106 double[] expected = {halfSampleSize, halfSampleSize};
107 long[] observed = new long[2];
108 for (int i=0; i<smallSampleSize; i++) {
109 if (testGenerator.nextBoolean()) {
110 observed[0]++;
111 } else {
112 observed[1]++;
113 }
114 }
115
116
117
118 assertTrue("chi-square test -- will fail about 1 in 1000 times",
119 testStatistic.chiSquare(expected,observed) < 10.828);
120 }
121
122 public void testNextFloat() {
123 Frequency freq = new Frequency();
124 float val = 0;
125 int value = 0;
126 for (int i=0; i<smallSampleSize; i++) {
127 val = testGenerator.nextFloat();
128 if (val < 0.25) {
129 value = 0;
130 } else if (val < 0.5) {
131 value = 1;
132 } else if (val < 0.75) {
133 value = 2;
134 } else {
135 value = 3;
136 }
137 freq.addValue(value);
138 }
139 long[] observed = new long[4];
140 for (int i=0; i<4; i++) {
141 observed[i] = freq.getCount(i);
142 }
143
144
145
146
147 assertTrue("chi-square test -- will fail about 1 in 1000 times",
148 testStatistic.chiSquare(expected,observed) < 16.27);
149 }
150 }