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.sampling.RandomAssert;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Test for the {@link RejectionInversionZipfSampler}. The tests hit edge cases for the sampler.
26   */
27  class RejectionInversionZipfSamplerTest {
28      /**
29       * Test the constructor with a bad number of elements.
30       */
31      @Test
32      void testConstructorThrowsWithZeroNumberOfElements() {
33          final UniformRandomProvider rng = RandomAssert.seededRNG();
34          final int numberOfElements = 0;
35          final double exponent = 1;
36          Assertions.assertThrows(IllegalArgumentException.class,
37              () -> RejectionInversionZipfSampler.of(rng, numberOfElements, exponent));
38      }
39  
40      /**
41       * Test the constructor with a bad exponent.
42       */
43      @Test
44      void testConstructorThrowsWithNegativeExponent() {
45          final UniformRandomProvider rng = RandomAssert.seededRNG();
46          final int numberOfElements = 1;
47          final double exponent = Math.nextDown(0);
48          Assertions.assertThrows(IllegalArgumentException.class,
49              () -> RejectionInversionZipfSampler.of(rng, numberOfElements, exponent));
50      }
51  
52      /**
53       * Test the SharedStateSampler implementation.
54       */
55      @Test
56      void testSharedStateSampler() {
57          testSharedStateSampler(7, 1.23);
58      }
59  
60      /**
61       * Special case: Test the SharedStateSampler implementation with a zero exponent.
62       */
63      @Test
64      void testSharedStateSamplerWithZeroExponent() {
65          testSharedStateSampler(7, 0);
66      }
67  
68      /**
69       * Test the SharedStateSampler implementation for the specified parameters.
70       *
71       * @param numberOfElements Number of elements
72       * @param exponent Exponent
73       */
74      private static void testSharedStateSampler(int numberOfElements, double exponent) {
75          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
76          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
77          // Use instance constructor not factory constructor to exercise 1.X public API
78          final RejectionInversionZipfSampler sampler1 =
79              new RejectionInversionZipfSampler(rng1, numberOfElements, exponent);
80          final SharedStateDiscreteSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
81          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
82      }
83  
84      /**
85       * Test the toString method. This is added to ensure coverage as the factory constructor
86       * used in other tests does not create an instance of the wrapper class.
87       */
88      @Test
89      void testToString() {
90          final UniformRandomProvider rng = RandomAssert.seededRNG();
91          Assertions.assertTrue(new RejectionInversionZipfSampler(rng, 10, 2.0).toString()
92                  .toLowerCase().contains("zipf"));
93      }
94  }