-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTestHard.java
More file actions
296 lines (277 loc) · 11 KB
/
Copy pathParserTestHard.java
File metadata and controls
296 lines (277 loc) · 11 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
import java.util.Random;
/**
* @author Niyaz Nigmatullin
*/
public class ParserTestHard {
static final Random RNG = new Random(58L);
public static void main(String[] args) {
checkAssert();
testExpression("10", new Expression3() {
public int evaluate(int x, int y, int z) {
return 10;
}
});
testExpression("x", new Expression3() {
public int evaluate(int x, int y, int z) {
return x;
}
});
testExpression("y", new Expression3() {
public int evaluate(int x, int y, int z) {
return y;
}
});
testExpression("z", new Expression3() {
public int evaluate(int x, int y, int z) {
return z;
}
});
testExpression("x+2", new Expression3() {
public int evaluate(int x, int y, int z) {
return x + 2;
}
});
testExpression("2-y", new Expression3() {
public int evaluate(int x, int y, int z) {
return 2 - y;
}
});
testExpression(" 3* z ", new Expression3() {
public int evaluate(int x, int y, int z) {
return 3 * z;
}
});
testExpression("x/ - 2", new Expression3() {
public int evaluate(int x, int y, int z) {
return -x / 2;
}
});
testExpression("x*y+(z-1 )/10", new Expression3() {
public int evaluate(int x, int y, int z) {
return x * y + (z - 1) / 10;
}
});
testExpression("-(-(-\t\t-5 + 16 *x*y) + 1 * z) -(((-11)))", new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return -(-(5 + 16 * x * y) + z) + 11;
}
});
testExpression("" + Integer.MIN_VALUE, new Expression3() {
public int evaluate(int x, int y, int z) {
return Integer.MIN_VALUE;
}
});
testExpression("x--y--z", new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return x + y + z;
}
});
testExpression("((2+2))-0/(--2)*555", new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return 4;
}
});
testExpression("x-x+y-y+z-(z)", new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return 0;
}
});
{
String s = "";
for (int i = 0; i < 1000; i++) s += "(";
s += "x + y + (-10*-z)";
for (int i = 0; i < 1000; i++) s += ")";
testExpression(s, new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return x + y + 10 * z;
}
});
}
testExpression("2*" + Integer.MAX_VALUE, new Expression3() {
@Override
public int evaluate(int x, int y, int z) {
return 2 * Integer.MAX_VALUE;
}
});
System.out.println("Testing random tests #1");
final int TESTS1 = 2000;
for (int it = 0; it < TESTS1; it++) {
if (it % 100 == 0) {
System.out.println("Completed " + it + " out of " + TESTS1);
}
genTests1(it / 5 + 2);
}
System.out.println("Testing random tests #2");
final int TESTS2 = 777;
for (int it = 0; it < TESTS2; it++) {
if (it % 100 == 0) {
System.out.println("Completed " + it + " out of " + TESTS2);
}
genTests2(it / 50 + 1);
}
System.out.println("OK");
}
private static void genTests1(int depth) {
int x = RNG.nextInt();
int y = RNG.nextInt();
int z = RNG.nextInt();
Test test = generate(new int[]{x, y, z}, depth);
tryTest(x, y, z, test);
}
private static void genTests2(int coefficient) {
int x = RNG.nextInt();
int y = RNG.nextInt();
int z = RNG.nextInt();
Test test = Generator.genExpression(1, coefficient, new int[]{x, y, z});
tryTest(x, y, z, test);
}
private static void tryTest(int x, int y, int z, Test test) {
try {
try {
int answer = ExpressionParser.parse(test.expr).evaluate(x, y, z);
assertTrue(test.answer != null, "division by zero error expected");
assertEquals(String.format("f(%d, %d, %d)", x, y, z), answer, test.answer);
} catch (ArithmeticException e) {
assertTrue(test.answer == null, "no division by zero in this expression");
}
} catch (Throwable e) {
System.out.println("Failed test: " + test.expr);
throw e;
}
}
static Test generate(int[] vars, int depth) {
if (depth == 0) {
if (RNG.nextBoolean()) {
int id = RNG.nextInt(3);
return new Test("xyz".charAt(id) + "", vars[id]);
} else {
int val = RNG.nextInt();
return new Test(val + "", val);
}
}
int operator = RNG.nextInt(6);
if (operator == 5) {
Test t1 = generate(vars, RNG.nextInt(depth));
return new Test("(" + t1.expr + ")", t1.answer);
} else if (operator == 0) {
Test t1 = generate(vars, RNG.nextInt(depth));
return new Test("-(" + t1.expr + ")", t1.answer == null ? null : -t1.answer);
} else {
Test t1 = generate(vars, RNG.nextInt(depth));
Test t2 = generate(vars, RNG.nextInt(depth));
if (operator == 1) {
return new Test("(" + t1.expr + ") + (" + t2.expr + ")",
t1.answer == null || t2.answer == null ? null : t1.answer + t2.answer);
} else if (operator == 2) {
return new Test("(" + t1.expr + ") - (" + t2.expr + ")",
t1.answer == null || t2.answer == null ? null : t1.answer - t2.answer);
} else if (operator == 3) {
return new Test("(" + t1.expr + ") * (" + t2.expr + ")",
t1.answer == null || t2.answer == null ? null : t1.answer * t2.answer);
} else {
return new Test("(" + t1.expr + ") / (" + t2.expr + ")",
t1.answer == null || t2.answer == null || t2.answer.intValue() == 0 ? null : t1.answer / t2.answer);
}
}
}
static class Generator {
static Test genExpression(int depth, int coefficient, int[] vars) {
if (makeNewBranch(depth, coefficient)) {
Test t1 = genExpression(depth + 1, coefficient, vars);
Test t2 = genSummand(depth, coefficient, vars);
if (RNG.nextBoolean()) {
return new Test(t1.expr + " + " + t2.expr,
t1.answer == null || t2.answer == null ? null : t1.answer + t2.answer);
} else {
return new Test(t1.expr + " - " + t2.expr,
t1.answer == null || t2.answer == null ? null : t1.answer - t2.answer);
}
} else {
return genSummand(depth, coefficient, vars);
}
}
static Test genSummand(int depth, int coefficient, int[] vars) {
if (makeNewBranch(depth, coefficient)) {
Test t1 = genSummand(depth + 1, coefficient, vars);
Test t2 = genFactor(depth, coefficient, vars);
if (RNG.nextBoolean()) {
return new Test(t1.expr + " * " + t2.expr,
t1.answer == null || t2.answer == null ? null : t1.answer * t2.answer);
} else {
return new Test(t1.expr + " / " + t2.expr,
t1.answer == null || t2.answer == null || t2.answer.intValue() == 0 ? null : t1.answer / t2.answer);
}
} else {
return genFactor(depth, coefficient, vars);
}
}
static Test genFactor(int depth, int coefficient, int[] vars) {
if (makeNewBranch(depth, coefficient)) {
Test t = genFactor(depth + 1, coefficient, vars);
if (RNG.nextBoolean()) {
return new Test("- " + t.expr, t.answer == null ? null : -t.answer);
} else {
return new Test("~ " + t.expr, t.answer == null ? null : ~t.answer);
}
} else {
return genValue(depth, coefficient, vars);
}
}
static Test genValue(int depth, int coefficient, int[] vars) {
if (makeNewBranch(depth, coefficient)) {
Test t = genExpression(depth + 1, coefficient, vars);
if (RNG.nextBoolean()) {
return new Test("( " + t.expr + " )", t.answer == null ? null : t.answer);
} else {
return new Test("abs( " + t.expr + " )", t.answer == null ? null : Math.abs(t.answer));
}
} else if (RNG.nextBoolean()) {
int value = RNG.nextInt();
return new Test("" + value, value);
} else {
int id = RNG.nextInt(3);
return new Test("xyz".charAt(id) + "", vars[id]);
}
}
static boolean makeNewBranch(int depth, int coefficient) {
return RNG.nextInt(depth + coefficient) < coefficient;
}
}
static class Test {
String expr;
Integer answer;
Test(String expr, Integer answer) {
this.expr = expr;
this.answer = answer;
}
}
private static void testExpression(String description, Expression3 expected) {
System.out.println("Testing: " + description);
Expression3 actual = ExpressionParser.parse(description);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
assertEquals(String.format("f(%d, %d, %d)", i, j, k), actual.evaluate(i, j, k), expected.evaluate(i, j, k));
}
}
}
}
private static void assertTrue(boolean condition, String message) {
assert condition : message;
}
private static void assertEquals(String message, int actual, int expected) {
assertTrue(actual == expected, String.format("%s: Expected %d, found %d", message, expected, actual));
}
private static void checkAssert() {
boolean assertsEnabled = false;
assert assertsEnabled = true;
if (!assertsEnabled) {
throw new AssertionError("You should enable assertions by running 'java -ea ParserTestHard'");
}
}
}