-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestRandomly.java
More file actions
237 lines (216 loc) · 8.21 KB
/
TestRandomly.java
File metadata and controls
237 lines (216 loc) · 8.21 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package sqlancer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
public class TestRandomly {
private static final int NR_MIN_RUNS = 100000;
@Test // test that every option is picked
public void testFromOptions() {
Integer[] options = { 1, 2, 3 };
List<Integer> remainingOptions = new ArrayList<>(Arrays.asList(options));
while (!remainingOptions.isEmpty()) {
Integer pickedOption = Randomly.fromOptions(options);
remainingOptions.remove(pickedOption);
}
assertTrue(remainingOptions.isEmpty());
}
@Test
public void testSubset() {
boolean encounteredEmptySubset = false;
boolean encounteredOriginalSet = false;
boolean encounteredStrictSubsetNonEmpty = false;
Integer[] options = { 1, 2, 3 };
List<Integer> optionList = new ArrayList<>(Arrays.asList(options));
do {
List<Integer> subset = Randomly.subset(optionList);
assertEquals(optionList.size(), 3); // check that the original set hasn't been modified
assertTrue(optionList.containsAll(subset));
if (subset.isEmpty()) {
encounteredEmptySubset = true;
} else if (subset.size() == optionList.size()) {
encounteredOriginalSet = true;
} else {
encounteredStrictSubsetNonEmpty = true;
}
} while (!encounteredEmptySubset || !encounteredOriginalSet || !encounteredStrictSubsetNonEmpty);
}
@Test
public void testString() {
boolean encounteredInteger = false;
boolean encounteredAscii = false;
boolean encounteredNonAscii = false;
boolean encounteredSpace = false;
Randomly r = new Randomly();
int i = 0;
do {
String s = r.getString();
for (Character c : s.toCharArray()) {
if (Character.isAlphabetic(c)) {
encounteredAscii = true;
} else if (Character.isDigit(c)) {
encounteredInteger = true;
} else if (Character.isSpaceChar(c)) {
encounteredSpace = true;
} else {
encounteredNonAscii = true;
}
}
} while (!encounteredInteger || !encounteredAscii || !encounteredNonAscii || !encounteredSpace
|| i++ < NR_MIN_RUNS);
}
@Test // TODO: also generate and check for NaN
public void testDouble() {
Randomly r = new Randomly();
boolean encounteredZero = false;
boolean encounteredPositive = false;
boolean encounteredNegative = false;
boolean encounteredInfinity = false;
do {
double doubleVal = r.getDouble();
if (doubleVal == 0) {
encounteredZero = true;
} else if (Double.isInfinite(doubleVal)) {
encounteredInfinity = true;
} else if (doubleVal > 0) {
encounteredPositive = true;
} else if (doubleVal < 0) {
encounteredNegative = true;
} else {
fail(String.valueOf(doubleVal));
}
} while (!encounteredZero || !encounteredPositive || !encounteredNegative || !encounteredInfinity);
}
@Test
public void testFiniteDouble() {
Randomly r = new Randomly();
for (int i = 0; i < NR_MIN_RUNS; i++) {
assertFalse(Double.isInfinite(r.getFiniteDouble()));
}
}
@Test
public void testNonZeroInteger() {
Randomly r = new Randomly();
boolean encounteredPositive = false;
boolean encounteredNegative = false;
int i = 0;
do {
long nonZeroInt = r.getNonZeroInteger();
assertNotEquals(0, nonZeroInt);
if (nonZeroInt > 0) {
encounteredPositive = true;
} else {
encounteredNegative = true;
}
} while (!encounteredPositive || !encounteredNegative || i++ < NR_MIN_RUNS);
}
@Test
public void testPositiveInteger() {
Randomly r = new Randomly();
boolean encounteredZero = false;
boolean encounteredMaxValue = false;
int i = 0;
do {
long positiveInt = r.getPositiveInteger();
assertTrue(positiveInt >= 0);
if (positiveInt == 0) {
encounteredZero = true;
} else if (positiveInt == Long.MAX_VALUE) {
encounteredMaxValue = true;
}
} while (!encounteredZero || !encounteredMaxValue || i++ < NR_MIN_RUNS);
}
@Test
public void testBytes() {
Randomly r = new Randomly();
boolean encounteredAllZeroes = false;
boolean encounteredMax = false;
boolean encounteredZeroLength = false;
int i = 0;
do {
byte[] bytes = r.getBytes();
if (bytes.length == 0) {
encounteredZeroLength = true;
} else if (bytes[0] == 0) {
encounteredAllZeroes = true;
} else if (bytes[0] == Byte.MAX_VALUE) {
encounteredMax = true;
}
} while (!encounteredAllZeroes || !encounteredMax || !encounteredZeroLength || i++ < NR_MIN_RUNS);
}
@Test
public void testNonCachedInteger() {
assertEquals(0, Randomly.getNotCachedInteger(0, 1));
assertEquals(0, Randomly.getNotCachedInteger(0, 0));
assertThrows(Exception.class, () -> Randomly.getNotCachedInteger(5, 0));
}
@Test
public void testInteger() {
Randomly r = new Randomly();
// TODO: we should throw an exception instead
assertEquals(0, r.getInteger(0, 0));
assertEquals(0, r.getInteger(0, 1));
}
@Test
public void testLong() {
Randomly r = new Randomly();
// TODO: we should throw an exception instead
assertEquals(0, r.getLong(0, 0));
assertEquals(0, r.getLong(0, 1));
}
@Test
public void testLong2() {
Randomly r = new Randomly();
for (int i = 0; i < NR_MIN_RUNS; i++) {
long val = r.getLong(-1, Long.MAX_VALUE);
assertTrue(val >= -1);
assertTrue(val < Long.MAX_VALUE);
}
}
@Test // check that when given a seed, each thread computes a consistent result
public void testSeed() {
int seed = 123;
Randomly r = new Randomly(seed);
List<String> values = getRandomValueList(r);
List<String> nonSeedList = getRandomValueList(new Randomly());
List<List<String>> otherThreadResults = new ArrayList<>();
assertNotEquals(values, nonSeedList);
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int i = 0; i < 1000; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
otherThreadResults.add(getRandomValueList(new Randomly(seed)));
}
});
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
for (List<String> otherThreadResult : otherThreadResults) {
assertEquals(values, otherThreadResult);
}
}
private List<String> getRandomValueList(Randomly r) {
List<String> values = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
values.add(String.valueOf(r.getDouble()));
values.add(String.valueOf(r.getInteger()));
values.add(String.valueOf(r.getString()));
values.add(String.valueOf(Randomly.getBoolean()));
}
return values;
}
}