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.core;
18  
19  import org.junit.Test;
20  import org.junit.Assert;
21  
22  /**
23   * Tests for {@link BaseProvider}.
24   *
25   * This class should only contain unit tests that cover code paths not
26   * exercised elsewhere.  Those code paths are most probably only taken
27   * in case of a wrong implementation (and would therefore fail other
28   * tests too).
29   */
30  public class BaseProviderTest {
31      @Test(expected=UnsupportedOperationException.class)
32      public void testMissingGetStateInternal() {
33          new DummyGenerator().saveState();
34      }
35  
36      @Test(expected=UnsupportedOperationException.class)
37      public void testMissingSetStateInternal() {
38          new DummyGenerator().restoreState(new RandomProviderDefaultState(new byte[1]));
39      }
40  
41      @Test
42      public void testFillStateInt() {
43          final int[] state = new int[10];
44          final int[] seed = {1, 2, 3};
45  
46          for (int i = 0; i < state.length; i++) {
47              Assert.assertEquals(0, state[i]);
48          }
49  
50          new DummyGenerator().fillState(state, seed);
51          for (int i = 0; i < seed.length; i++) {
52              Assert.assertEquals(seed[i], state[i]);
53          }
54          for (int i = seed.length; i < state.length; i++) {
55              Assert.assertNotEquals(0, state[i]);
56          }
57      }
58  
59      @Test
60      public void testFillStateLong() {
61          final long[] state = new long[10];
62          final long[] seed = {1, 2, 3};
63  
64          for (int i = 0; i < state.length; i++) {
65              Assert.assertEquals(0, state[i]);
66          }
67  
68          new DummyGenerator().fillState(state, seed);
69          for (int i = 0; i < seed.length; i++) {
70              Assert.assertEquals(seed[i], state[i]);
71          }
72          for (int i = seed.length; i < state.length; i++) {
73              Assert.assertNotEquals(0, state[i]);
74          }
75      }
76  
77      /**
78       * Dummy class for checking the behaviour of
79       * <ul>
80       *  <li>an incomplete implementation</li>
81       *  <li>{@code fillState} methods with "protected" access</li>
82       * </ul>
83       */
84      class DummyGenerator extends org.apache.commons.rng.core.source32.IntProvider {
85          /** {@inheritDoc} */
86          @Override
87          public int next() {
88              return 4; // https://www.xkcd.com/221/
89          }
90  
91          // Missing overrides of "setStateInternal" and "getStateInternal".
92  
93          /** {@inheritDoc} */
94          @Override
95          public void fillState(int[] state, int[] seed) {
96              super.fillState(state, seed);
97          }
98  
99          /** {@inheritDoc} */
100         @Override
101         public void fillState(long[] state, long[] seed) {
102             super.fillState(state, seed);
103         }
104     }
105 }