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