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