1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31 class ArrayConverterEndianTest {
32
33
34
35
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
48 final ByteBuffer bb = ByteBuffer.wrap(
49 Arrays.copyOf(seedBytes, Conversions.longSizeFromByteSize(bytes) * Long.BYTES))
50 .order(ByteOrder.LITTLE_ENDIAN);
51
52
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
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
70 Assertions.assertArrayEquals(expectedLong, new IntArray2LongArray().convert(expectedInt));
71 Assertions.assertArrayEquals(expectedLong, (long[]) NativeSeedType.LONG_ARRAY.convert(expectedInt, expectedLong.length));
72
73
74 Assertions.assertArrayEquals(expectedInt, (int[]) NativeSeedType.INT_ARRAY.convert(expectedLong, expectedInt.length));
75 }
76 }