/
TwoCmresTest.java
133 lines (114 loc) · 4.54 KB
/
TwoCmresTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.rng.core.source64;
import org.apache.commons.rng.core.RandomAssert;
import org.apache.commons.rng.core.source64.TwoCmres.Cmres;
import org.junit.jupiter.api.Assertions;
import org.junit.Test;
import java.util.ArrayList;
public class TwoCmresTest {
@Test
public void testAsymmetric() {
final int index1 = 2;
final int index2 = 5;
final int seed = -123456789;
final TwoCmres rng1 = new TwoCmres(seed, index1, index2);
final TwoCmres rng2 = new TwoCmres(seed, index2, index1);
// Try a few values.
final int n = 1000;
for (int[] i = {0}; i[0] < n; i[0]++) {
Assertions.assertNotEquals(rng1.nextLong(), rng2.nextLong(), () -> "i=" + i[0]);
}
}
/**
* This test targets the seeding procedure to verify any bit of the input seed contributes
* to the output. Note: The seeding routine creates 2 16-bit integers from the 32-bit seed,
* thus a change of any single bit should make a different output.
*/
@Test
public void testSeedingWithASingleBitProducesDifferentOutputFromZeroSeed() {
final int n = 100;
// Output with a zero seed
final long[] values = new long[n];
final TwoCmres rng = new TwoCmres(0);
for (int i = 0; i < n; i++) {
values[i] = rng.nextLong();
}
// Seed with a single bit
for (int bit = 0; bit < 32; bit++) {
final int seed = 1 << bit;
RandomAssert.assertNotEquals(values, new TwoCmres(seed));
}
}
@Test
public void testSubcycleGeneratorsMustBeDifferent() {
final int max = TwoCmres.numberOfSubcycleGenerators();
for (int i = 0; i < max; i++) {
try {
new TwoCmres(-97845, i, i);
Assertions.fail("Exception expected");
} catch (IllegalArgumentException e) {
// Expected.
}
}
}
@Test
public void testSubcycleGeneratorsIndex() {
final int seed = 246810;
// Valid indices are between 0 (included) and max (excluded).
final int max = TwoCmres.numberOfSubcycleGenerators();
for (int i = 0; i < max; i++) {
for (int j = 0; j < max; j++) {
if (i != j) { // Subcycle generators must be different.
// Can be instantiated.
new TwoCmres(seed, i, j);
}
}
}
for (int wrongIndex : new int[] {-1, max}) {
try {
new TwoCmres(seed, wrongIndex, 1);
Assertions.fail("Exception expected for index=" + wrongIndex);
} catch (IndexOutOfBoundsException e) {
// Expected.
}
try {
new TwoCmres(seed, 1, wrongIndex);
Assertions.fail("Exception expected for index=" + wrongIndex);
} catch (IndexOutOfBoundsException e) {
// Expected.
}
}
}
@Test
public void testCmresFactoryThrowsWithDuplicateMultiplier() {
ArrayList<Cmres> list = new ArrayList<Cmres>();
final long multiply = 0;
final int rotate = 3;
final int start = 5;
list.add(new Cmres(multiply, rotate, start));
long nextMultiply = multiply + 1;
try {
Cmres.Factory.checkUnique(list, nextMultiply);
} catch (IllegalStateException ex) {
Assertions.fail("The next multiply should be unique: " + nextMultiply);
}
list.add(new Cmres(nextMultiply, rotate, start));
// This should throw as the list now contains the multiply value
Assertions.assertThrows(IllegalStateException.class, () -> Cmres.Factory.checkUnique(list, nextMultiply));
}
}