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