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.source32;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  /**
23   * The tests the caching of calls to {@link IntProvider#nextInt()} are used as
24   * the source for {@link IntProvider#nextInt()} and
25   * {@link IntProvider#nextBoolean()}.
26   */
27  public class IntProviderTest {
28      /**
29       * A simple class to flip the bits in a number as the source for
30       * {@link IntProvider#next()}.
31       */
32      static final class FlipIntProvider extends IntProvider {
33          /** The value. */
34          private int value;
35  
36          /**
37           * @param value the value
38           */
39          public FlipIntProvider(int value) {
40              // Flip the bits so the first call to next() returns to the same state
41              this.value = ~value;
42          }
43  
44          @Override
45          public int next() {
46              // Flip the bits
47              value = ~value;
48              return value;
49          }
50      }
51  
52      /**
53       * This test ensures that the call to {@link IntProvider#nextBoolean()} returns
54       * each of the bits from a call to {@link IntProvider#nextInt()}.
55       *
56       * <p>The order should be from the least-significant bit.
57       */
58      @Test
59      public void testNextBoolean() {
60          for (int i = 0; i < Integer.SIZE; i++) {
61              // Set only a single bit in the source
62              final int value = 1 << i;
63              final IntProvider provider = new FlipIntProvider(value);
64              // Test the result for a single pass over the long
65              for (int j = 0; j < Integer.SIZE; j++) {
66                  final boolean expected = (i == j);
67                  Assert.assertEquals("Pass 1, bit " + j, expected, provider.nextBoolean());
68              }
69              // The second pass should use the opposite bits
70              for (int j = 0; j < Integer.SIZE; j++) {
71                  final boolean expected = (i != j);
72                  Assert.assertEquals("Pass 2, bit " + j, expected, provider.nextBoolean());
73              }
74          }
75      }
76  }