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    *      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.rng;
18  
19  import java.util.Spliterator;
20  import org.junit.jupiter.api.Assertions;
21  
22  /**
23   * Tests for default stream method implementations in {@link UniformRandomProvider}.
24   */
25  class UniformRandomProviderStreamTest extends BaseRandomProviderStreamTest {
26  
27      /**
28       * Dummy class for checking the behavior of the UniformRandomProvider.
29       */
30      private static class DummyGenerator implements UniformRandomProvider {
31          /** An instance. */
32          static final DummyGenerator INSTANCE = new DummyGenerator();
33  
34          @Override
35          public long nextLong() {
36              throw new UnsupportedOperationException("The nextLong method should not be invoked");
37          }
38      }
39  
40      @Override
41      UniformRandomProvider create() {
42          return DummyGenerator.INSTANCE;
43      }
44  
45      @Override
46      UniformRandomProvider createInts(int[] values) {
47          return new DummyGenerator() {
48              private int i;
49              @Override
50              public int nextInt() {
51                  return values[i++];
52              }
53          };
54      }
55  
56      @Override
57      UniformRandomProvider createInts(int[] values, int origin, int bound) {
58          return new DummyGenerator() {
59              private int i;
60              @Override
61              public int nextInt(int o, int b) {
62                  Assertions.assertEquals(origin, o, "origin");
63                  Assertions.assertEquals(bound, b, "bound");
64                  return values[i++];
65              }
66          };
67      }
68  
69      @Override
70      UniformRandomProvider createLongs(long[] values) {
71          return new DummyGenerator() {
72              private int i;
73              @Override
74              public long nextLong() {
75                  return values[i++];
76              }
77          };
78      }
79  
80      @Override
81      UniformRandomProvider createLongs(long[] values, long origin, long bound) {
82          return new DummyGenerator() {
83              private int i;
84              @Override
85              public long nextLong(long o, long b) {
86                  Assertions.assertEquals(origin, o, "origin");
87                  Assertions.assertEquals(bound, b, "bound");
88                  return values[i++];
89              }
90          };
91      }
92  
93      @Override
94      UniformRandomProvider createDoubles(double[] values) {
95          return new DummyGenerator() {
96              private int i;
97              @Override
98              public double nextDouble() {
99                  return values[i++];
100             }
101         };
102     }
103 
104     @Override
105     UniformRandomProvider createDoubles(double[] values, double origin, double bound) {
106         return new DummyGenerator() {
107             private int i;
108             @Override
109             public double nextDouble(double o, double b) {
110                 Assertions.assertEquals(origin, o, "origin");
111                 Assertions.assertEquals(bound, b, "bound");
112                 return values[i++];
113             }
114         };
115     }
116 
117     @Override
118     int getCharacteristics() {
119         // The current stream produced by the generate method only returns immutable
120         return Spliterator.IMMUTABLE;
121     }
122 }