1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test cases for the RandomAdaptor class
24   *
25   * @version $Revision: 796543 $ $Date: 2009-07-21 17:32:38 -0400 (Tue, 21 Jul 2009) $
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       * "Constant" generator to test Adaptor delegation.
65       * "Powered by Eclipse ;-)"
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 }