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  
18  package org.apache.commons.rng.sampling;
19  
20  import org.junit.Assert;
21  
22  import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
23  import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
24  
25  /**
26   * Utility class for testing random samplers.
27   */
28  public final class RandomAssert {
29      /** Number of samples to generate to test for equal sequences. */
30      private static final int SAMPLES = 10;
31  
32      /**
33       * Defines a generic sampler.
34       *
35       * @param <T> the type of the sample
36       */
37      public interface Sampler<T> {
38          /**
39           * Creates a sample.
40           *
41           * @return a sample.
42           */
43          T sample();
44      }
45  
46      /**
47       * Class contains only static methods.
48       */
49      private RandomAssert() {}
50  
51      /**
52       * Exercise the {@link ContinuousSampler} interface, and
53       * ensure that the two samplers produce the same sequence.
54       *
55       * @param sampler1 First sampler.
56       * @param sampler2 Second sampler.
57       */
58      public static void assertProduceSameSequence(ContinuousSampler sampler1,
59                                                   ContinuousSampler sampler2) {
60          for (int i = 0; i < SAMPLES; i++) {
61              Assert.assertEquals(sampler1.sample(), sampler2.sample(), 0.0);
62          }
63      }
64  
65      /**
66       * Exercise the {@link DiscreteSampler} interface, and
67       * ensure that the two samplers produce the same sequence.
68       *
69       * @param sampler1 First sampler.
70       * @param sampler2 Second sampler.
71       */
72      public static void assertProduceSameSequence(DiscreteSampler sampler1,
73                                                   DiscreteSampler sampler2) {
74          for (int i = 0; i < SAMPLES; i++) {
75              Assert.assertEquals(sampler1.sample(), sampler2.sample());
76          }
77      }
78  
79      /**
80       * Exercise the {@link Sampler} interface, and
81       * ensure that the two samplers produce the same sequence.
82       *
83       * <p>Arrays are tested using {@link Assert#assertArrayEquals(Object[], Object[])}
84       * which handles primitive arrays using exact equality and objects using
85       * {@link Object#equals(Object)}. Otherwise {@link Assert#assertEquals(Object, Object)}
86       * is used which makes use of {@link Object#equals(Object)}.</p>
87       *
88       * <p>This should be used to test samplers of any type by wrapping the sample method
89       * to an anonymous {@link Sampler} class.</p>
90       *
91       * @param sampler1 First sampler.
92       * @param sampler2 Second sampler.
93       */
94      public static <T> void assertProduceSameSequence(Sampler<T> sampler1,
95                                                       Sampler<T> sampler2) {
96          for (int i = 0; i < SAMPLES; i++) {
97              final T value1 = sampler1.sample();
98              final T value2 = sampler2.sample();
99              if (isArray(value1) && isArray(value2)) {
100                 // JUnit assertArrayEquals will handle nested primitive arrays
101                 Assert.assertArrayEquals(new Object[] {value1}, new Object[] {value2});
102             } else {
103                 Assert.assertEquals(value1, value2);
104             }
105         }
106     }
107 
108     /**
109      * Checks if the object is an array.
110      *
111      * @param object Object.
112      * @return true if an array
113      */
114     private static boolean isArray(Object object) {
115         return object != null && object.getClass().isArray();
116     }
117 }