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.Assert;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.Parameterized;
23 import org.junit.runners.Parameterized.Parameters;
24
25 import java.lang.reflect.Array;
26
27
28
29
30
31
32 @RunWith(value = Parameterized.class)
33 public class NativeSeedTypeParametricTest {
34
35 private static final Object[] SUPPORTED_NATIVE_TYPES = {
36 Integer.class,
37 Long.class,
38 int[].class,
39 long[].class
40 };
41
42 private static final Object[] SUPPORTED_SEEDS = {
43 Integer.valueOf(1),
44 Long.valueOf(2),
45 new int[] {3, 4, 5},
46 new long[] {6, 7, 8},
47 new byte[] {9, 10, 11},
48 };
49
50 private static final Object[] UNSUPPORTED_SEEDS = {
51 null,
52 Double.valueOf(Math.PI),
53 };
54
55
56 private final NativeSeedType nativeSeedType;
57
58 private final Class<?> type;
59
60
61
62
63
64
65 public NativeSeedTypeParametricTest(Class<?> type) {
66 this.type = type;
67 nativeSeedType = findNativeSeedType(type);
68 }
69
70
71
72
73
74
75 @Parameters
76 public static Object[] getTypes() {
77
78
79 Assert.assertEquals("Incorrect number of enum values for the supported native types",
80 SUPPORTED_NATIVE_TYPES.length, NativeSeedType.values().length);
81
82 return SUPPORTED_NATIVE_TYPES;
83 }
84
85
86
87
88
89
90
91 private static NativeSeedType findNativeSeedType(Class<?> type) {
92 for (final NativeSeedType nativeSeedType : NativeSeedType.values()) {
93 if (type.equals(nativeSeedType.getType())) {
94 return nativeSeedType;
95 }
96 }
97 throw new AssertionError("No enum matching the type: " + type);
98 }
99
100
101
102
103 @Test
104 public void testCreateSeed() {
105 final int size = 3;
106 final Object seed = nativeSeedType.createSeed(size);
107 Assert.assertNotNull(seed);
108 Assert.assertEquals("Seed was not the correct class", type, seed.getClass());
109 if (type.isArray()) {
110 Assert.assertEquals("Seed was not created the correct length", size, Array.getLength(seed));
111 }
112 }
113
114
115
116
117 @Test
118 public void testConvertSeedToBytes() {
119 final int size = 3;
120 final Object seed = nativeSeedType.createSeed(size);
121 Assert.assertNotNull("Null seed", seed);
122
123 final byte[] bytes = NativeSeedType.convertSeedToBytes(seed);
124 Assert.assertNotNull("Null byte[] seed", bytes);
125
126 final Object seed2 = nativeSeedType.convertSeed(bytes, size);
127 if (type.isArray()) {
128
129 Assert.assertArrayEquals("byte[] seed was not converted back",
130 new Object[] {seed}, new Object[] {seed2});
131 } else {
132 Assert.assertEquals("byte[] seed was not converted back", seed, seed2);
133 }
134 }
135
136
137
138
139 @Test
140 public void testConvertSupportedSeed() {
141
142 final int size = 3;
143 for (final Object input : SUPPORTED_SEEDS) {
144 final Object seed = nativeSeedType.convertSeed(input, size);
145 final String msg = input.getClass() + " input seed was not converted";
146 Assert.assertNotNull(msg, seed);
147 Assert.assertEquals(msg, type, seed.getClass());
148 }
149 }
150
151
152
153
154 @Test
155 public void testCannotConvertUnsupportedSeed() {
156 final int size = 3;
157 for (final Object input : UNSUPPORTED_SEEDS) {
158 try {
159 nativeSeedType.convertSeed(input, size);
160 Assert.fail(input.getClass() + " input seed was not rejected as unsupported");
161 } catch (UnsupportedOperationException ex) {
162
163 }
164 }
165 }
166 }