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.sampling;
18  
19  import java.util.concurrent.ThreadLocalRandom;
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.RepeatedTest;
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.ValueSource;
26  
27  /**
28   * Tests the default methods in the {@link ObjectSampler} interface.
29   */
30  class ObjectSamplerTest {
31      @Test
32      void testSamplesUnlimitedSize() {
33          final ObjectSampler<Double> s = RandomAssert.createRNG()::nextDouble;
34          Assertions.assertEquals(Long.MAX_VALUE, s.samples().spliterator().estimateSize());
35      }
36  
37      @RepeatedTest(value = 3)
38      void testSamples() {
39          final UniformRandomProvider[] rngs = RandomAssert.createRNG(2);
40          final ObjectSampler<Double> s1 = rngs[0]::nextDouble;
41          final ObjectSampler<Double> s2 = rngs[1]::nextDouble;
42          final int count = ThreadLocalRandom.current().nextInt(3, 13);
43          Assertions.assertArrayEquals(createSamples(s1, count),
44                                       s2.samples().limit(count).toArray());
45      }
46  
47      @ParameterizedTest
48      @ValueSource(ints = {0, 1, 2, 5, 13})
49      void testSamples(int streamSize) {
50          final UniformRandomProvider[] rngs = RandomAssert.createRNG(2);
51          final ObjectSampler<Double> s1 = rngs[0]::nextDouble;
52          final ObjectSampler<Double> s2 = rngs[1]::nextDouble;
53          Assertions.assertArrayEquals(createSamples(s1, streamSize),
54                                       s2.samples(streamSize).toArray());
55      }
56  
57      /**
58       * Creates an array of samples.
59       *
60       * @param sampler Source of samples.
61       * @param count Number of samples.
62       * @return the samples
63       */
64      private static Double[] createSamples(ObjectSampler<Double> sampler, int count) {
65          final Double[] data = new Double[count];
66          for (int i = 0; i < count; i++) {
67              // Explicit boxing
68              data[i] = Double.valueOf(sampler.sample());
69          }
70          return data;
71      }
72  }