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 org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.Assumptions;
21 import org.junit.jupiter.params.ParameterizedTest;
22 import org.junit.jupiter.params.provider.EnumSource;
23 import org.junit.jupiter.api.Test;
24
25 import java.lang.reflect.Array;
26 import java.util.Arrays;
27 import java.util.Set;
28 import java.util.function.Supplier;
29 import java.util.stream.Collectors;
30
31
32
33
34
35
36 class NativeSeedTypeParametricTest {
37
38 private static final Object[] SUPPORTED_NATIVE_TYPES = {
39 Integer.class,
40 Long.class,
41 int[].class,
42 long[].class
43 };
44
45 private static final Object[] SUPPORTED_SEEDS = {
46 Integer.valueOf(1),
47 Long.valueOf(2),
48 new int[] {3, 4, 5},
49 new long[] {6, 7, 8},
50 new byte[] {9, 10, 11},
51 };
52
53 private static final Object[] UNSUPPORTED_SEEDS = {
54 null,
55 Double.valueOf(Math.PI),
56 };
57
58
59
60
61
62 @Test
63 void testNativeSeedTypeEnum() {
64 Set<Class<?>> supported = Arrays.stream(SUPPORTED_NATIVE_TYPES)
65 .map(o -> (Class<?>) o)
66 .collect(Collectors.toSet());
67 Assertions.assertEquals(SUPPORTED_NATIVE_TYPES.length, supported.size(),
68 "Class type of supported seeds should be unique");
69
70 final NativeSeedType[] values = NativeSeedType.values();
71 Assertions.assertEquals(SUPPORTED_NATIVE_TYPES.length, values.length,
72 "Incorrect number of enum values for the supported native types");
73
74
75 Arrays.stream(values).map(NativeSeedType::getType).forEach(supported::remove);
76 Assertions.assertEquals(0, supported.size());
77 }
78
79
80
81
82
83
84 @ParameterizedTest
85 @EnumSource
86 void testCreateSeed(NativeSeedType nativeSeedType) {
87 final int size = 3;
88 final Object seed = nativeSeedType.createSeed(size);
89 Assertions.assertNotNull(seed);
90 final Class<?> type = nativeSeedType.getType();
91 Assertions.assertEquals(type, seed.getClass(), "Seed was not the correct class");
92 if (type.isArray()) {
93 Assertions.assertEquals(size, Array.getLength(seed), "Seed was not created the correct length");
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106 @ParameterizedTest
107 @EnumSource
108 void testCreateArraySeedWithNonZeroRangeThrows(NativeSeedType nativeSeedType) {
109 Assumptions.assumeTrue(nativeSeedType.getType().isArray(), "Not an array type");
110
111
112
113
114
115 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> nativeSeedType.createSeed(0, 0, 1));
116 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> nativeSeedType.createSeed(1, -1, 1));
117 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> nativeSeedType.createSeed(1, 1, 2));
118 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> nativeSeedType.createSeed(1, 2, 3));
119 }
120
121
122
123
124
125
126 @ParameterizedTest
127 @EnumSource
128 void testConvertSeedToBytes(NativeSeedType nativeSeedType) {
129 final int size = 3;
130 final Object seed = nativeSeedType.createSeed(size);
131 Assertions.assertNotNull(seed, "Null seed");
132
133 final byte[] bytes = NativeSeedType.convertSeedToBytes(seed);
134 Assertions.assertNotNull(bytes, "Null byte[] seed");
135
136 final Object seed2 = nativeSeedType.convertSeed(bytes, size);
137 if (nativeSeedType.getType().isArray()) {
138
139 Assertions.assertArrayEquals(new Object[] {seed}, new Object[] {seed2},
140 "byte[] seed was not converted back");
141 } else {
142 Assertions.assertEquals(seed, seed2, "byte[] seed was not converted back");
143 }
144 }
145
146
147
148
149
150
151 @ParameterizedTest
152 @EnumSource
153 void testConvertSupportedSeed(NativeSeedType nativeSeedType) {
154
155 final int size = 3;
156 for (final Object input : SUPPORTED_SEEDS) {
157 final Object seed = nativeSeedType.convertSeed(input, size);
158 final Supplier<String> msg = () -> input.getClass() + " input seed was not converted";
159 Assertions.assertNotNull(seed, msg);
160 Assertions.assertEquals(nativeSeedType.getType(), seed.getClass(), msg);
161 }
162 }
163
164
165
166
167
168
169 @ParameterizedTest
170 @EnumSource
171 void testCannotConvertUnsupportedSeed(NativeSeedType nativeSeedType) {
172 final int size = 3;
173 for (final Object input : UNSUPPORTED_SEEDS) {
174 Assertions.assertThrows(UnsupportedOperationException.class,
175 () -> nativeSeedType.convertSeed(input, size));
176 }
177 }
178 }