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 org.apache.commons.rng.UniformRandomProvider;
20  import org.apache.commons.rng.simple.RandomSource;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * Test for the {@link SamplerBase}. The class is deprecated but is public. The methods
26   * should be tested to ensure correct functionality.
27   */
28  public class SamplerBaseTest {
29      /**
30       * Extends the {@link SamplerBase} to allow access to methods.
31       */
32      @SuppressWarnings("deprecation")
33      private static class SimpleSampler extends SamplerBase {
34          SimpleSampler(UniformRandomProvider rng) {
35              super(rng);
36          }
37  
38          @Override
39          public double nextDouble() {
40              return super.nextDouble();
41          }
42  
43          @Override
44          public int nextInt() {
45              return super.nextInt();
46          }
47  
48          @Override
49          public int nextInt(int max) {
50              return super.nextInt(max);
51          }
52  
53          @Override
54          public long nextLong() {
55              return super.nextLong();
56          }
57      }
58  
59      @Test
60      public void testNextMethods() {
61          final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
62          final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
63          final SimpleSampler sampler = new SimpleSampler(rng2);
64          final int n = 256;
65          for (int i = 0; i < 3; i++) {
66              Assert.assertEquals(rng1.nextDouble(), sampler.nextDouble(), 0);
67              Assert.assertEquals(rng1.nextInt(), sampler.nextInt());
68              Assert.assertEquals(rng1.nextInt(n), sampler.nextInt(n));
69              Assert.assertEquals(rng1.nextLong(), sampler.nextLong());
70          }
71      }
72  
73      @Test
74      public void testToString() {
75          final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
76          final SimpleSampler sampler = new SimpleSampler(rng);
77          Assert.assertTrue(sampler.toString().contains("rng"));
78      }
79  }