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.simple.internal;
18  
19  import java.nio.ByteBuffer;
20  import java.nio.ByteOrder;
21  import java.util.Arrays;
22  import java.util.concurrent.ThreadLocalRandom;
23  import java.util.stream.IntStream;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.MethodSource;
27  
28  /**
29   * Tests the endian conversion of all array converters.
30   */
31  class ArrayConverterEndianTest {
32      /**
33       * Gets the lengths for the byte[] seeds.
34       *
35       * @return the lengths
36       */
37      static IntStream getLengths() {
38          return IntStream.rangeClosed(0, 16);
39      }
40  
41      @ParameterizedTest
42      @MethodSource(value = {"getLengths"})
43      void testLittleEndian(int bytes) {
44          final byte[] seedBytes = new byte[bytes];
45          ThreadLocalRandom.current().nextBytes(seedBytes);
46  
47          // Reference implementation using a ByteBuffer
48          final ByteBuffer bb = ByteBuffer.wrap(
49              Arrays.copyOf(seedBytes, Conversions.longSizeFromByteSize(bytes) * Long.BYTES))
50              .order(ByteOrder.LITTLE_ENDIAN);
51  
52          // byte[] -> int[]
53          final int[] expectedInt = new int[Conversions.intSizeFromByteSize(bytes)];
54          for (int i = 0; i < expectedInt.length; i++) {
55              expectedInt[i] = bb.getInt();
56          }
57          Assertions.assertArrayEquals(expectedInt, new ByteArray2IntArray().convert(seedBytes));
58          Assertions.assertArrayEquals(expectedInt, (int[]) NativeSeedType.INT_ARRAY.convert(seedBytes, expectedInt.length));
59  
60          // byte[] -> long[]
61          bb.clear();
62          final long[] expectedLong = new long[Conversions.longSizeFromByteSize(bytes)];
63          for (int i = 0; i < expectedLong.length; i++) {
64              expectedLong[i] = bb.getLong();
65          }
66          Assertions.assertArrayEquals(expectedLong, new ByteArray2LongArray().convert(seedBytes));
67          Assertions.assertArrayEquals(expectedLong, (long[]) NativeSeedType.LONG_ARRAY.convert(seedBytes, expectedLong.length));
68  
69          // int[] -> long[]
70          Assertions.assertArrayEquals(expectedLong, new IntArray2LongArray().convert(expectedInt));
71          Assertions.assertArrayEquals(expectedLong, (long[]) NativeSeedType.LONG_ARRAY.convert(expectedInt, expectedLong.length));
72  
73          // long[] -> int[]
74          Assertions.assertArrayEquals(expectedInt, (int[]) NativeSeedType.INT_ARRAY.convert(expectedLong, expectedInt.length));
75      }
76  }