View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.random;
23  
24  import java.util.Random;
25  
26  import org.hipparchus.exception.NullArgumentException;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for the RandomAdaptor class.
32   */
33  public class RandomAdaptorTest {
34  
35      @Test
36      public void testAdaptor() {
37          ConstantGenerator generator = new ConstantGenerator();
38          Random random = RandomAdaptor.of(generator);
39          checkConstant(random);
40          RandomAdaptor randomAdaptor = new RandomAdaptor(generator);
41          checkConstant(randomAdaptor);
42      }
43  
44      private void checkConstant(Random random) {
45          byte[] bytes = new byte[] {0};
46          random.nextBytes(bytes);
47          Assert.assertEquals(0, bytes[0]);
48          Assert.assertFalse(random.nextBoolean());
49          Assert.assertEquals(0, random.nextDouble(), 0);
50          Assert.assertEquals(0, random.nextFloat(), 0);
51          Assert.assertEquals(0, random.nextGaussian(), 0);
52          Assert.assertEquals(0, random.nextInt());
53          Assert.assertEquals(0, random.nextInt(1));
54          Assert.assertEquals(0, random.nextLong());
55          random.setSeed(100);
56          Assert.assertEquals(0, random.nextDouble(), 0);
57      }
58  
59      @SuppressWarnings("unused")
60      @Test(expected=NullArgumentException.class)
61      public void testNullGenerator(){
62         RandomGenerator nullGenerator = null;
63         Random random = new RandomAdaptor(nullGenerator);
64      }
65  
66      @SuppressWarnings("unused")
67      @Test(expected=NullArgumentException.class)
68      public void testNullGenerator2(){
69         RandomGenerator nullGenerator = null;
70         Random random = RandomAdaptor.of(nullGenerator);
71      }
72  
73      /*
74       * "Constant" generator to test Adaptor delegation.
75       * "Powered by Eclipse ;-)"
76       *
77       */
78      public static class ConstantGenerator implements RandomGenerator {
79  
80          private final double value;
81  
82          public ConstantGenerator() {
83              value = 0;
84          }
85  
86          public ConstantGenerator(double value) {
87              this.value = value;
88          }
89  
90          @Override
91          public boolean nextBoolean() {
92              return false;
93          }
94  
95          @Override
96          public void nextBytes(byte[] bytes) {
97          }
98  
99          @Override
100         public void nextBytes(byte[] bytes, int offset, int len) {
101         }
102 
103         @Override
104         public double nextDouble() {
105             return value;
106         }
107 
108         @Override
109         public float nextFloat() {
110             return (float) value;
111         }
112 
113         @Override
114         public double nextGaussian() {
115             return value;
116         }
117 
118         @Override
119         public int nextInt() {
120             return (int) value;
121         }
122 
123         @Override
124         public int nextInt(int n) {
125             return (int) value;
126         }
127 
128         @Override
129         public long nextLong() {
130             return (int) value;
131         }
132 
133         @Override
134         public long nextLong(long n) {
135             return (int) value;
136         }
137 
138         @Override
139         public void setSeed(int seed) {
140         }
141 
142         @Override
143         public void setSeed(int[] seed) {
144         }
145 
146         @Override
147         public void setSeed(long seed) {
148         }
149 
150     }
151 }