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=IllegalStateException.class)
32      public void testWrongStateSize() {
33          new DummyGenerator().restoreState(new RandomProviderDefaultState(new byte[1]));
34      }
35  
36      @Test
37      public void testFillStateInt() {
38          final int[] state = new int[10];
39          final int[] seed = {1, 2, 3};
40  
41          for (int i = 0; i < state.length; i++) {
42              Assert.assertEquals(0, state[i]);
43          }
44  
45          new DummyGenerator().fillState(state, seed);
46          for (int i = 0; i < seed.length; i++) {
47              Assert.assertEquals(seed[i], state[i]);
48          }
49          for (int i = seed.length; i < state.length; i++) {
50              Assert.assertNotEquals(0, state[i]);
51          }
52      }
53  
54      @Test
55      public void testFillStateLong() {
56          final long[] state = new long[10];
57          final long[] seed = {1, 2, 3};
58  
59          for (int i = 0; i < state.length; i++) {
60              Assert.assertEquals(0, state[i]);
61          }
62  
63          new DummyGenerator().fillState(state, seed);
64          for (int i = 0; i < seed.length; i++) {
65              Assert.assertEquals(seed[i], state[i]);
66          }
67          for (int i = seed.length; i < state.length; i++) {
68              Assert.assertNotEquals(0, state[i]);
69          }
70      }
71  
72      /**
73       * Dummy class for checking the behaviour of
74       * <ul>
75       *  <li>an incomplete implementation</li>
76       *  <li>{@code fillState} methods with "protected" access</li>
77       * </ul>
78       */
79      class DummyGenerator extends org.apache.commons.rng.core.source32.IntProvider {
80          /** {@inheritDoc} */
81          @Override
82          public int next() {
83              return 4; // https://www.xkcd.com/221/
84          }
85  
86          // Missing overrides of "setStateInternal" and "getStateInternal".
87  
88          /** {@inheritDoc} */
89          @Override
90          public void fillState(int[] state, int[] seed) {
91              super.fillState(state, seed);
92          }
93  
94          /** {@inheritDoc} */
95          @Override
96          public void fillState(long[] state, long[] seed) {
97              super.fillState(state, seed);
98          }
99      }
100 }