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.Assert;
22 import org.junit.Test;
23
24 import java.util.ArrayList;
25
26
27 public class TwoCmresTest {
28 @Test
29 public 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 < n; i++) {
40 Assert.assertNotEquals("i=" + i, rng1.nextLong(), rng2.nextLong());
41 }
42 }
43
44
45
46
47
48
49 @Test
50 public 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 public void testSubcycleGeneratorsMustBeDifferent() {
69 final int max = TwoCmres.numberOfSubcycleGenerators();
70 for (int i = 0; i < max; i++) {
71 try {
72 new TwoCmres(-97845, i, i);
73 Assert.fail("Exception expected");
74 } catch (IllegalArgumentException e) {
75
76 }
77 }
78 }
79
80 @Test
81 public void testSubcycleGeneratorsIndex() {
82 final int seed = 246810;
83
84
85 final int max = TwoCmres.numberOfSubcycleGenerators();
86
87 for (int i = 0; i < max; i++) {
88 for (int j = 0; j < max; j++) {
89 if (i != j) {
90
91 new TwoCmres(seed, i, j);
92 }
93 }
94 }
95
96 for (int wrongIndex : new int[] {-1, max}) {
97 try {
98 new TwoCmres(seed, wrongIndex, 1);
99 Assert.fail("Exception expected for index=" + wrongIndex);
100 } catch (IndexOutOfBoundsException e) {
101
102 }
103
104 try {
105 new TwoCmres(seed, 1, wrongIndex);
106 Assert.fail("Exception expected for index=" + wrongIndex);
107 } catch (IndexOutOfBoundsException e) {
108
109 }
110 }
111 }
112
113 @Test(expected = IllegalStateException.class)
114 public void testCmresFactoryThrowsWithDuplicateMultiplier() {
115 ArrayList<Cmres> list = new ArrayList<Cmres>();
116 final long multiply = 0;
117 final int rotate = 3;
118 final int start = 5;
119
120 list.add(new Cmres(multiply, rotate, start));
121
122 long nextMultiply = multiply + 1;
123 try {
124 Cmres.Factory.checkUnique(list, nextMultiply);
125 } catch (IllegalStateException ex) {
126 Assert.fail("The next multiply should be unique: " + nextMultiply);
127 }
128
129 list.add(new Cmres(nextMultiply, rotate, start));
130
131 Cmres.Factory.checkUnique(list, nextMultiply);
132 }
133 }