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 public boolean nextBoolean() {
71 return false;
72 }
73
74 public void nextBytes(byte[] bytes) {
75 }
76
77 public double nextDouble() {
78 return 0;
79 }
80
81 public float nextFloat() {
82 return 0;
83 }
84
85 public double nextGaussian() {
86 return 0;
87 }
88
89 public int nextInt() {
90 return 0;
91 }
92
93 public int nextInt(int n) {
94 return 0;
95 }
96
97 public long nextLong() {
98 return 0;
99 }
100
101 public void setSeed(int seed) {
102 }
103
104 public void setSeed(int[] seed) {
105 }
106
107 public void setSeed(long seed) {
108 }
109
110 }
111 }