1 package civitas.common.tests;
2
3 import java.util.function.Function;
4
5 import org.mockito.invocation.InvocationOnMock;
6
7 import civitas.util.CivitasBigInteger;
8
9 public class TestUtil {
10
11 static int n = 0;
12
13 public static void fakeRandomToArray(final InvocationOnMock invocation, final CivitasBigInteger random) {
14 byte[] array = invocation.getArgument(0);
15 java.util.Arrays.fill(array, (byte) 0);
16 byte[] aBytes = random.toByteArray();
17 int diff = aBytes.length - array.length;
18 for (int i = 0; i < array.length; i++) {
19 array[i] = aBytes[i + diff];
20 }
21 }
22
23 public static <T> T construct(final Class<T> klass, final Object... params) {
24 Class<?>[] types = new Class<?>[params.length];
25
26 for (int i = 0; i < types.length; i++) {
27 Class<?> cl = params[i].getClass();
28 types[i] = cl;
29 }
30 try {
31 return klass.getDeclaredConstructor(types).newInstance(params);
32 } catch (Exception e) {
33 throw new Error(e);
34 }
35 }
36
37 public static Function<CivitasBigInteger, CivitasBigInteger> pick(final int mod) {
38 n = 0;
39 return s -> {
40 if (n < 8 && mod == n++ % 2) {
41 return s;
42 } else {
43 return null;
44 }
45 };
46 }
47 }