-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathTestRandomly.java
More file actions
320 lines (290 loc) · 11.5 KB
/
TestRandomly.java
File metadata and controls
320 lines (290 loc) · 11.5 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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));
int i = 0;
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
|| i++ < NR_MIN_RUNS);
assertTrue(encounteredEmptySubset, "Empty subset was not encountered");
assertTrue(encounteredOriginalSet, "Original set was not encountered");
assertTrue(encounteredStrictSubsetNonEmpty, "Strict subset was not encountered");
}
@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);
assertTrue(encounteredInteger, "Integer was not encountered");
assertTrue(encounteredAscii, "Ascii was not encountered");
assertTrue(encounteredNonAscii, "Non ascii was not encountered");
assertTrue(encounteredSpace, "Space was not encountered");
}
@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;
int i = 0;
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
|| i++ < NR_MIN_RUNS);
assertTrue(encounteredZero, "Zero was not encountered");
assertTrue(encounteredPositive, "Positive was not encountered");
assertTrue(encounteredNegative, "Negative was not encountered");
assertTrue(encounteredInfinity, "Infinity was not encountered");
}
@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);
assertTrue(encounteredPositive, "Positive integer was not encountered");
assertTrue(encounteredNegative, "Negative integer was not encountered");
}
@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);
assertTrue(encounteredZero, "Zero was not encountered");
assertTrue(encounteredMaxValue, "Max value was not encountered");
}
@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);
assertTrue(encounteredAllZeroes, "All zeroes were not encountered");
assertTrue(encounteredMax, "Max value was not encountered");
assertTrue(encounteredZeroLength, "Zero length was not encountered");
}
@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;
}
@Test
public void testGetPercentage() {
for (int i = 0; i < NR_MIN_RUNS; i++) {
double percentage = Randomly.getPercentage();
assertTrue(percentage >= 0.0);
assertTrue(percentage <= 1.0);
}
}
@Test
public void testGetChar() {
Randomly r = new Randomly();
boolean encounteredAlphabetic = false;
boolean encounteredNumeric = false;
boolean encounteredSpecial = false;
int i = 0;
do {
String c = r.getChar();
assertEquals(1, c.length());
if (Character.isAlphabetic(c.charAt(0))) {
encounteredAlphabetic = true;
} else if (Character.isDigit(c.charAt(0))) {
encounteredNumeric = true;
} else {
encounteredSpecial = true;
}
} while (!encounteredAlphabetic || !encounteredNumeric || !encounteredSpecial || i++ < NR_MIN_RUNS);
assertTrue(encounteredAlphabetic, "Never encounter an alphabetic character.");
assertTrue(encounteredNumeric, "Never encounter a numeric character.");
assertTrue(encounteredSpecial, "Never encounter a special character.");
}
@Test
public void testGetAlphabeticChar() {
Randomly r = new Randomly();
for (int i = 0; i < NR_MIN_RUNS; i++) {
String c = r.getAlphabeticChar();
assertEquals(1, c.length());
assertTrue(Character.isAlphabetic(c.charAt(0)));
}
}
@Test
public void testGetBooleanWithSmallProbability() {
int trueCount = 0;
int totalRuns = NR_MIN_RUNS;
for (int i = 0; i < totalRuns; i++) {
if (Randomly.getBooleanWithSmallProbability()) {
trueCount++;
}
}
double trueRatio = (double) trueCount / totalRuns;
assertTrue(trueRatio > 0.005);
assertTrue(trueRatio < 0.015);
}
}