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.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
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  public 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              super();
42              this.outputs = outputs.clone();
43              this.index = 0;
44          }
45  
46          @Override
47          public void nextBytes(byte[] bytes) {
48              Objects.requireNonNull(bytes, "bytes");
49              if (index + bytes.length > outputs.length) {
50                  throw new IllegalStateException("Not enough outputs given in MockRandom");
51              }
52              System.arraycopy(outputs, index, bytes, 0, bytes.length);
53              index += bytes.length;
54          }
55      }
56  
57      @ParameterizedTest
58      @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 32})
59      public void testNext(int cacheSize) {
60          MockRandom random = new MockRandom(new byte[]{
61                  0x11, 0x12, 0x13, 0x25,
62                  (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0xff,
63                  0x55, 0x44, 0x12, 0x34,
64                  0x56, 0x78, 0x00, 0x00,
65                  0x00, 0x00, 0x00, 0x00,
66                  0x00, 0x00, 0x00, 0x00,
67                  0x00, 0x00, 0x00, 0x00,
68                  0x00, 0x00, 0x00, 0x00,
69          });
70  
71          CachedRandomBits arb = new CachedRandomBits(cacheSize, random);
72  
73          assertThrows(IllegalArgumentException.class, () -> arb.nextBits(0));
74          assertThrows(IllegalArgumentException.class, () -> arb.nextBits(33));
75  
76          assertEquals(0x11, arb.nextBits(8));
77          assertEquals(0x12, arb.nextBits(8));
78          assertEquals(0x1325, arb.nextBits(16));
79  
80          assertEquals(0xabcdefff, arb.nextBits(32));
81  
82          assertEquals(0x5, arb.nextBits(4));
83          assertEquals(0x1, arb.nextBits(1));
84          assertEquals(0x0, arb.nextBits(1));
85          assertEquals(0x1, arb.nextBits(2));
86  
87          assertEquals(0x4, arb.nextBits(6));
88  
89          assertEquals(0x40000000 | (0x12345600 >> 2) | 0x38, arb.nextBits(32));
90  
91          assertEquals(1, arb.nextBits(1));
92          assertEquals(0, arb.nextBits(1));
93          assertEquals(0, arb.nextBits(9));
94          assertEquals(0, arb.nextBits(31));
95      }
96  }