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 SplittableUniformRandomProviderStreamTest extends BaseRandomProviderStreamTest {
26  
27      /**
28       * Dummy class for checking the behavior of the SplittableUniformRandomProvider.
29       */
30      private static class DummyGenerator implements SplittableUniformRandomProvider {
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          @Override
40          public SplittableUniformRandomProvider split(UniformRandomProvider source) {
41              throw new UnsupportedOperationException("The split method should not be invoked");
42          }
43      }
44  
45      @Override
46      UniformRandomProvider create() {
47          return DummyGenerator.INSTANCE;
48      }
49  
50      @Override
51      UniformRandomProvider createInts(int[] values) {
52          return new DummyGenerator() {
53              private int i;
54              @Override
55              public int nextInt() {
56                  return values[i++];
57              }
58          };
59      }
60  
61      @Override
62      UniformRandomProvider createInts(int[] values, int origin, int bound) {
63          return new DummyGenerator() {
64              private int i;
65              @Override
66              public int nextInt(int o, int b) {
67                  Assertions.assertEquals(origin, o, "origin");
68                  Assertions.assertEquals(bound, b, "bound");
69                  return values[i++];
70              }
71          };
72      }
73  
74      @Override
75      UniformRandomProvider createLongs(long[] values) {
76          return new DummyGenerator() {
77              private int i;
78              @Override
79              public long nextLong() {
80                  return values[i++];
81              }
82          };
83      }
84  
85      @Override
86      UniformRandomProvider createLongs(long[] values, long origin, long bound) {
87          return new DummyGenerator() {
88              private int i;
89              @Override
90              public long nextLong(long o, long b) {
91                  Assertions.assertEquals(origin, o, "origin");
92                  Assertions.assertEquals(bound, b, "bound");
93                  return values[i++];
94              }
95          };
96      }
97  
98      @Override
99      UniformRandomProvider createDoubles(double[] values) {
100         return new DummyGenerator() {
101             private int i;
102             @Override
103             public double nextDouble() {
104                 return values[i++];
105             }
106         };
107     }
108 
109     @Override
110     UniformRandomProvider createDoubles(double[] values, double origin, double bound) {
111         return new DummyGenerator() {
112             private int i;
113             @Override
114             public double nextDouble(double o, double b) {
115                 Assertions.assertEquals(origin, o, "origin");
116                 Assertions.assertEquals(bound, b, "bound");
117                 return values[i++];
118             }
119         };
120     }
121 
122     @Override
123     int getCharacteristics() {
124         // Since this is splittable it supports sized and sub-sized.
125         // Add non-null although this may not be relevant for primitive streams.
126         return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL | Spliterator.IMMUTABLE;
127     }
128 }