1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.core.source64;
18
19 import org.apache.commons.rng.core.RandomAssert;
20 import org.apache.commons.rng.core.source64.TwoCmres.Cmres;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.api.Test;
23
24 import java.util.ArrayList;
25
26
27 class TwoCmresTest {
28 @Test
29 void testAsymmetric() {
30 final int index1 = 2;
31 final int index2 = 5;
32 final int seed = -123456789;
33
34 final TwoCmres rng1 = new TwoCmres(seed, index1, index2);
35 final TwoCmres rng2 = new TwoCmres(seed, index2, index1);
36
37
38 final int n = 1000;
39 for (int[] i = {0}; i[0] < n; i[0]++) {
40 Assertions.assertNotEquals(rng1.nextLong(), rng2.nextLong(), () -> "i=" + i[0]);
41 }
42 }
43
44
45
46
47
48
49 @Test
50 void testSeedingWithASingleBitProducesDifferentOutputFromZeroSeed() {
51 final int n = 100;
52
53
54 final long[] values = new long[n];
55 final TwoCmres rng = new TwoCmres(0);
56 for (int i = 0; i < n; i++) {
57 values[i] = rng.nextLong();
58 }
59
60
61 for (int bit = 0; bit < 32; bit++) {
62 final int seed = 1 << bit;
63 RandomAssert.assertNotEquals(values, new TwoCmres(seed));
64 }
65 }
66
67 @Test
68 void testSubcycleGeneratorsMustBeDifferent() {
69 final int max = TwoCmres.numberOfSubcycleGenerators();
70 for (int i = 0; i < max; i++) {
71 final int subCycle = i;
72 Assertions.assertThrows(IllegalArgumentException.class,
73 () -> new TwoCmres(-97845, subCycle, subCycle));
74 }
75 }
76
77 @Test
78 void testSubcycleGeneratorsIndex() {
79 final int seed = 246810;
80
81
82 final int max = TwoCmres.numberOfSubcycleGenerators();
83
84 for (int i = 0; i < max; i++) {
85 for (int j = 0; j < max; j++) {
86 if (i != j) {
87
88 new TwoCmres(seed, i, j);
89 }
90 }
91 }
92
93 for (int wrongIndex : new int[] {-1, max}) {
94 Assertions.assertThrows(IndexOutOfBoundsException.class,
95 () -> new TwoCmres(seed, wrongIndex, 1),
96 () -> "Exception expected for index i = " + wrongIndex);
97 Assertions.assertThrows(IndexOutOfBoundsException.class,
98 () -> new TwoCmres(seed, 1, wrongIndex),
99 () -> "Exception expected for index j = " + wrongIndex);
100 }
101 }
102
103 @Test
104 void testCmresFactoryThrowsWithDuplicateMultiplier() {
105 final ArrayList<Cmres> list = new ArrayList<>();
106 final long multiply = 0;
107 final int rotate = 3;
108 final int start = 5;
109
110 list.add(new Cmres(multiply, rotate, start));
111
112 long nextMultiply = multiply + 1;
113 Assertions.assertDoesNotThrow(
114 () -> Cmres.Factory.checkUnique(list, nextMultiply),
115 () -> "The next multiply should be unique: " + nextMultiply);
116
117 list.add(new Cmres(nextMultiply, rotate, start));
118
119 Assertions.assertThrows(IllegalStateException.class, () -> Cmres.Factory.checkUnique(list, nextMultiply));
120 }
121 }