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 import java.util.Random;
21
22
23
24
25
26
27
28 public class RandomAdaptorTest extends RandomDataTest {
29
30 public RandomAdaptorTest(String name) {
31 super(name);
32 }
33
34 public static Test suite() {
35 TestSuite suite = new TestSuite(RandomAdaptorTest.class);
36 suite.setName("RandomAdaptor Tests");
37 return suite;
38 }
39
40 public void testAdaptor() {
41 ConstantGenerator generator = new ConstantGenerator();
42 Random random = RandomAdaptor.createAdaptor(generator);
43 checkConstant(random);
44 RandomAdaptor randomAdaptor = new RandomAdaptor(generator);
45 checkConstant(randomAdaptor);
46 }
47
48 private void checkConstant(Random random) {
49 byte[] bytes = new byte[] {0};
50 random.nextBytes(bytes);
51 assertEquals(0, bytes[0]);
52 assertEquals(false, random.nextBoolean());
53 assertEquals(0, random.nextDouble(), 0);
54 assertEquals(0, random.nextFloat(), 0);
55 assertEquals(0, random.nextGaussian(), 0);
56 assertEquals(0, random.nextInt());
57 assertEquals(0, random.nextInt(1));
58 assertEquals(0, random.nextLong());
59 random.setSeed(100);
60 assertEquals(0, random.nextDouble(), 0);
61 }
62
63
64
65
66
67
68 private static class ConstantGenerator implements RandomGenerator {
69
70 private static final long serialVersionUID = 5936262220824971138L;
71
72 public boolean nextBoolean() {
73 return false;
74 }
75
76 public void nextBytes(byte[] bytes) {
77 }
78
79 public double nextDouble() {
80 return 0;
81 }
82
83 public float nextFloat() {
84 return 0;
85 }
86
87 public double nextGaussian() {
88 return 0;
89 }
90
91 public int nextInt() {
92 return 0;
93 }
94
95 public int nextInt(int n) {
96 return 0;
97 }
98
99 public long nextLong() {
100 return 0;
101 }
102
103 public void setSeed(long seed) {
104 }
105 }
106 }