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 org.hipparchus.exception.NullArgumentException;
25  import org.junit.jupiter.api.Test;
26  
27  import java.util.Random;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  
33  /**
34   * Test cases for the RandomAdaptor class.
35   */
36  class RandomAdaptorTest {
37  
38      @Test
39      void testAdaptor() {
40          ConstantGenerator generator = new ConstantGenerator();
41          Random random = RandomAdaptor.of(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          assertFalse(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      @SuppressWarnings("unused")
63      @Test
64      void testNullGenerator(){
65          assertThrows(NullArgumentException.class, () -> {
66              RandomGenerator nullGenerator = null;
67              Random random = new RandomAdaptor(nullGenerator);
68          });
69      }
70  
71      @SuppressWarnings("unused")
72      @Test
73      void testNullGenerator2(){
74          assertThrows(NullArgumentException.class, () -> {
75              RandomGenerator nullGenerator = null;
76              Random random = RandomAdaptor.of(nullGenerator);
77          });
78      }
79  
80      /*
81       * "Constant" generator to test Adaptor delegation.
82       * "Powered by Eclipse ;-)"
83       *
84       */
85      public static class ConstantGenerator implements RandomGenerator {
86  
87          private final double value;
88  
89          public ConstantGenerator() {
90              value = 0;
91          }
92  
93          public ConstantGenerator(double value) {
94              this.value = value;
95          }
96  
97          @Override
98          public boolean nextBoolean() {
99              return false;
100         }
101 
102         @Override
103         public void nextBytes(byte[] bytes) {
104         }
105 
106         @Override
107         public void nextBytes(byte[] bytes, int offset, int len) {
108         }
109 
110         @Override
111         public double nextDouble() {
112             return value;
113         }
114 
115         @Override
116         public float nextFloat() {
117             return (float) value;
118         }
119 
120         @Override
121         public double nextGaussian() {
122             return value;
123         }
124 
125         @Override
126         public int nextInt() {
127             return (int) value;
128         }
129 
130         @Override
131         public int nextInt(int n) {
132             return (int) value;
133         }
134 
135         @Override
136         public long nextLong() {
137             return (int) value;
138         }
139 
140         @Override
141         public long nextLong(long n) {
142             return (int) value;
143         }
144 
145         @Override
146         public void setSeed(int seed) {
147         }
148 
149         @Override
150         public void setSeed(int[] seed) {
151         }
152 
153         @Override
154         public void setSeed(long seed) {
155         }
156 
157     }
158 }