1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Test cases for the RandomAdaptor class
23   *
24   * @version $Revision:$ $Date$
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       * "Constant" generator to test Adaptor delegation.
64       * "Powered by Eclipse ;-)"
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 }