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    *      https://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.lang3;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.Objects;
23  import java.util.Random;
24  
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.ValueSource;
27  
28  /**
29   * Tests {@link CachedRandomBits}.
30   */
31  class CachedRandomBitsTest {
32  
33      /** MockRandom mocks a Random class nextBytes to use a specific list of outputs */
34      private static class MockRandom extends Random {
35  
36          private static final long serialVersionUID = 1L;
37          private final byte[] outputs;
38          private int index;
39  
40          MockRandom(final byte[] outputs) {
41              this.outputs = outputs.clone();
42              this.index = 0;
43          }
44  
45          @Override
46          public void nextBytes(final byte[] bytes) {
47              Objects.requireNonNull(bytes, "bytes");
48              if (index + bytes.length > outputs.length) {
49                  throw new IllegalStateException("Not enough outputs given in MockRandom");
50              }
51              System.arraycopy(outputs, index, bytes, 0, bytes.length);
52              index += bytes.length;
53          }
54      }
55  
56      @ParameterizedTest
57      @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 32})
58      void testNext(final int cacheSize) {
59          final MockRandom random = new MockRandom(new byte[]{
60                  0x11, 0x12, 0x13, 0x25,
61                  (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0xff,
62                  0x55, 0x44, 0x12, 0x34,
63                  0x56, 0x78, 0x00, 0x00,
64                  0x00, 0x00, 0x00, 0x00,
65                  0x00, 0x00, 0x00, 0x00,
66                  0x00, 0x00, 0x00, 0x00,
67                  0x00, 0x00, 0x00, 0x00,
68          });
69  
70          final CachedRandomBits arb = new CachedRandomBits(cacheSize, random);
71  
72          assertIllegalArgumentException(() -> arb.nextBits(0));
73          assertIllegalArgumentException(() -> arb.nextBits(33));
74  
75          assertEquals(0x11, arb.nextBits(8));
76          assertEquals(0x12, arb.nextBits(8));
77          assertEquals(0x1325, arb.nextBits(16));
78  
79          assertEquals(0xabcdefff, arb.nextBits(32));
80  
81          assertEquals(0x5, arb.nextBits(4));
82          assertEquals(0x1, arb.nextBits(1));
83          assertEquals(0x0, arb.nextBits(1));
84          assertEquals(0x1, arb.nextBits(2));
85  
86          assertEquals(0x4, arb.nextBits(6));
87  
88          assertEquals(0x40000000 | 0x12345600 >> 2 | 0x38, arb.nextBits(32));
89  
90          assertEquals(1, arb.nextBits(1));
91          assertEquals(0, arb.nextBits(1));
92          assertEquals(0, arb.nextBits(9));
93          assertEquals(0, arb.nextBits(31));
94      }
95  }