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.source64;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  /**
23   * The tests the caching of calls to {@link LongProvider#nextLong()} are used as
24   * the source for {@link LongProvider#nextInt()} and
25   * {@link LongProvider#nextBoolean()}.
26   */
27  public class LongProviderTest {
28      /**
29       * A simple class to return a fixed value as the source for
30       * {@link LongProvider#next()}.
31       */
32      static final class FixedLongProvider extends LongProvider {
33          /** The value. */
34          private long value;
35  
36          /**
37           * @param value the value
38           */
39          public FixedLongProvider(long value) {
40              this.value = value;
41          }
42  
43          @Override
44          public long next() {
45              return value;
46          }
47      }
48  
49      /**
50       * A simple class to flip the bits in a number as the source for
51       * {@link LongProvider#next()}.
52       */
53      static final class FlipLongProvider extends LongProvider {
54          /** The value. */
55          private long value;
56  
57          /**
58           * @param value the value
59           */
60          public FlipLongProvider(long value) {
61              // Flip the bits so the first call to next() returns to the same state
62              this.value = ~value;
63          }
64  
65          @Override
66          public long next() {
67              // Flip the bits
68              value = ~value;
69              return value;
70          }
71      }
72  
73      /**
74       * This test ensures that the call to {@link LongProvider#nextInt()} returns the
75       * upper and then lower 32-bits from {@link LongProvider#nextLong()}.
76       */
77      @Test
78      public void testNextInt() {
79          final int MAX = 5;
80          for (int i = 0; i < MAX; i++) {
81              for (int j = 0; j < MAX; j++) {
82                  // Pack into upper then lower bits
83                  final long value = (((long) i) << 32) | (j & 0xffffffffL);
84                  final LongProvider provider = new FixedLongProvider(value);
85                  Assert.assertEquals("1st call not the upper 32-bits", i, provider.nextInt());
86                  Assert.assertEquals("2nd call not the lower 32-bits", j, provider.nextInt());
87                  Assert.assertEquals("3rd call not the upper 32-bits", i, provider.nextInt());
88                  Assert.assertEquals("4th call not the lower 32-bits", j, provider.nextInt());
89              }
90          }
91      }
92  
93      /**
94       * This test ensures that the call to {@link LongProvider#nextBoolean()} returns
95       * each of the bits from a call to {@link LongProvider#nextLong()}.
96       *
97       * <p>The order should be from the least-significant bit.
98       */
99      @Test
100     public void testNextBoolean() {
101         for (int i = 0; i < Long.SIZE; i++) {
102             // Set only a single bit in the source
103             final long value = 1L << i;
104             final LongProvider provider = new FlipLongProvider(value);
105             // Test the result for a single pass over the long
106             for (int j = 0; j < Long.SIZE; j++) {
107                 final boolean expected = (i == j);
108                 Assert.assertEquals("Pass 1, bit " + j, expected, provider.nextBoolean());
109             }
110             // The second pass should use the opposite bits
111             for (int j = 0; j < Long.SIZE; j++) {
112                 final boolean expected = (i != j);
113                 Assert.assertEquals("Pass 2, bit " + j, expected, provider.nextBoolean());
114             }
115         }
116     }
117 }