-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericParserTest.java
More file actions
408 lines (382 loc) · 16.1 KB
/
Copy pathGenericParserTest.java
File metadata and controls
408 lines (382 loc) · 16.1 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.*;
/**
* Created by Niyaz Nigmatullin on 4/21/14.
*/
public class GenericParserTest {
static final Random RNG = new Random(58L);
public static void main(String[] args) {
{
System.err.println("check TL");
Test t = new Generator(0).genExpression(1, 10);
int toAdd = 777777;
int[] pair = new int[t.expr.length()];
List<Integer> stack = new ArrayList<>();
int count = 0;
int[] id = new int[t.expr.length()];
for (int i = 0; i < pair.length; i++) {
char c = t.expr.charAt(i);
if (c == '(') {
stack.add(i);
id[i] = count;
++count;
} else if (c == ')') {
int j = stack.remove(stack.size() - 1);
pair[i] = j;
pair[j] = i;
id[j] = id[i];
}
}
int[] d = new int[count];
int sum = 0;
for (int i = 0; i < count; i++) {
d[i] = RNG.nextInt(toAdd / count) + 1;
sum += d[i];
}
double mul = 1. * toAdd / sum;
for (int i = 0; i < count; i++) {
d[i] = (int) Math.round(mul * d[i]);
}
StringBuilder sb = new StringBuilder();
char[] charArray = t.expr.toCharArray();
for (int index = 0; index < charArray.length; index++) {
char c = charArray[index];
if (c == '(') {
for (int i = 0; i < d[id[index]]; i++) {
sb.append('(');
}
} else if (c == ')') {
for (int i = 0; i < d[id[index]]; i++) {
sb.append(')');
}
}
sb.append(c);
}
t.expr = sb.toString();
if (!checkTest(t)) return;
}
for (int type = 0; type < 3; type++) {
Generator g = new Generator(type);
System.err.println("Testing type: " + type);
for (int i = 0; i < 30; i++) {
System.err.println("[" + i + "]");
if (!checkTest(g.genExpression(1, i / 10 + 3))) return;
}
}
}
static boolean checkTest(Test test) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
String sType = new String[]{"-i", "-d", "-bi"}[test.type];
try {
GenericParser.main(new String[]{sType, test.expr});
} catch (Throwable e) {
System.err.println("throwable object has been thrown on test:");
System.err.println(test.expr);
e.printStackTrace();
return false;
}
Object[][] ans = test.answer;
Scanner scanner = new Scanner(new ByteArrayInputStream(out.toByteArray()));
for (int i = 0; i < ans.length; i++) {
for (int j = 0; j < ans[i].length; j++) {
if (!scanner.hasNext()) {
System.err.println("not enough tokens in output for test:");
System.err.println(sType + " " + test.expr);
return false;
}
String token = scanner.next();
int xValue = i - 100;
int yValue = j - 100;
if (ans[i][j] == null) {
if (!token.equals("error")) {
System.err.println(String.format("error message expected, x = %d, y = %d, on test:\n", xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
} else if (ans[i][j] instanceof Integer) {
int x;
try {
x = Integer.parseInt(token);
} catch (NumberFormatException e) {
System.err.println("couldn't parse int: " + token);
System.err.println(String.format("x = %d, y = %d\n", xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
if ((Integer) ans[i][j] != x) {
System.err.println(String.format("expected %d, found %d, x = %d, y = %d, on test:", (Integer) ans[i][j], x, xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
} else if (ans[i][j] instanceof Double) {
double x;
try {
x = Double.parseDouble(token);
} catch (NumberFormatException e) {
System.err.println("couldn't parse double: " + token);
System.err.println(String.format("x = %d, y = %d\n", xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
double value = (Double) ans[i][j];
boolean bad = true;
if (value == x) {
bad = false;
} else if (Double.isNaN(value)) {
if (Double.isNaN(x)) {
bad = false;
}
} else if (Double.isInfinite(value)) {
if (x != value) {
bad = false;
}
} else if (Double.isNaN(x) || Double.isInfinite(x)) {
} else if (Math.abs(x - value) / Math.max(1., Math.abs(x)) < 1e-4) {
bad = false;
}
if (bad) {
System.err.println(String.format("expected %f, found %f, x = %d, y = %d, on test:", (Double) ans[i][j], x, xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
} else {
BigInteger x;
try {
x = new BigInteger(token);
} catch (NumberFormatException e) {
System.err.println("couldn't parse BigInteger: " + token);
System.err.println(String.format("x = %d, y = %d\n", xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
if (!ans[i][j].equals(x)) {
System.err.println(String.format("expected %d, found %d, x = %d, y = %d, on test:", (BigInteger) ans[i][j], x, xValue, yValue));
System.err.println(sType + " " + test.expr);
return false;
}
}
}
}
return true;
}
static class Generator {
static Object[][] add(Object[][] x, Object[][] y) {
Object[][] ret = new Object[x.length][];
for (int i = 0; i < x.length; i++) {
ret[i] = new Object[x[i].length];
for (int j = 0; j < x[i].length; j++) {
Object a = x[i][j];
Object b = y[i][j];
if (a == null || b == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
ret[i][j] = (Integer) a + (Integer) b;
} else if (a instanceof Double) {
ret[i][j] = (Double) a + (Double) b;
} else {
ret[i][j] = ((BigInteger) a).add((BigInteger) b);
}
}
}
return ret;
}
static Object[][] subtract(Object[][] x, Object[][] y) {
Object[][] ret = new Object[x.length][];
for (int i = 0; i < x.length; i++) {
ret[i] = new Object[x[i].length];
for (int j = 0; j < x[i].length; j++) {
Object a = x[i][j];
Object b = y[i][j];
if (a == null || b == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
ret[i][j] = (Integer) a - (Integer) b;
} else if (a instanceof Double) {
ret[i][j] = (Double) a - (Double) b;
} else {
ret[i][j] = ((BigInteger) a).subtract((BigInteger) b);
}
}
}
return ret;
}
static Object[][] multiply(Object[][] x, Object[][] y) {
Object[][] ret = new Object[x.length][];
for (int i = 0; i < x.length; i++) {
ret[i] = new Object[x[i].length];
for (int j = 0; j < x[i].length; j++) {
Object a = x[i][j];
Object b = y[i][j];
if (a == null || b == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
ret[i][j] = (Integer) a * (Integer) b;
} else if (a instanceof Double) {
ret[i][j] = (Double) a * (Double) b;
} else {
ret[i][j] = ((BigInteger) a).multiply((BigInteger) b);
}
}
}
return ret;
}
static Object[][] divide(Object[][] x, Object[][] y) {
Object[][] ret = new Object[x.length][];
for (int i = 0; i < x.length; i++) {
ret[i] = new Object[x[i].length];
for (int j = 0; j < x[i].length; j++) {
Object a = x[i][j];
Object b = y[i][j];
if (a == null || b == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
Integer bInteger = (Integer) b;
if (bInteger == 0)
ret[i][j] = null;
else
ret[i][j] = (Integer) a / bInteger;
} else if (a instanceof Double) {
ret[i][j] = (Double) a / (Double) b;
} else {
BigInteger val = (BigInteger) b;
ret[i][j] = val.signum() == 0 ? null : ((BigInteger) a).divide(val);
}
}
}
return ret;
}
static Object[][] negate(Object[][] z) {
Object[][] ret = new Object[z.length][];
for (int i = 0; i < z.length; i++) {
ret[i] = new Object[z[i].length];
for (int j = 0; j < z[i].length; j++) {
Object a = z[i][j];
if (a == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
ret[i][j] = -(Integer) a;
} else if (a instanceof Double) {
ret[i][j] = -(Double) a;
} else {
ret[i][j] = ((BigInteger) a).negate();
}
}
}
return ret;
}
static Object[][] abs(Object[][] z) {
Object[][] ret = new Object[z.length][];
for (int i = 0; i < z.length; i++) {
ret[i] = new Object[z[i].length];
for (int j = 0; j < z[i].length; j++) {
Object a = z[i][j];
if (a == null) {
ret[i][j] = null;
} else if (a instanceof Integer) {
ret[i][j] = Math.abs((Integer) a);
} else if (a instanceof Double) {
ret[i][j] = Math.abs((Double) a);
} else {
ret[i][j] = ((BigInteger) a).abs();
}
}
}
return ret;
}
int type;
public Generator(int type) {
this.type = type;
}
Test genExpression(int depth, int coefficient) {
if (makeNewBranch(depth, coefficient)) {
Test t1 = genExpression(depth + 1, coefficient);
Test t2 = genSummand(depth, coefficient);
if (RNG.nextBoolean()) {
return new Test(t1.expr + " + " + t2.expr, add(t1.answer, t2.answer), type);
} else {
return new Test(t1.expr + " - " + t2.expr, subtract(t1.answer, t2.answer), type);
}
} else {
return genSummand(depth, coefficient);
}
}
Test genSummand(int depth, int coefficient) {
if (makeNewBranch(depth, coefficient)) {
Test t1 = genSummand(depth + 1, coefficient);
Test t2 = genFactor(depth, coefficient);
if (RNG.nextBoolean()) {
return new Test(t1.expr + " * " + t2.expr, multiply(t1.answer, t2.answer), type);
} else {
return new Test(t1.expr + " / " + t2.expr, divide(t1.answer, t2.answer), type);
}
} else {
return genFactor(depth, coefficient);
}
}
Test genFactor(int depth, int coefficient) {
if (makeNewBranch(depth, coefficient)) {
Test t = genFactor(depth + 1, coefficient);
return new Test("- " + t.expr, negate(t.answer), type);
} else {
return genValue(depth, coefficient);
}
}
Test genValue(int depth, int coefficient) {
if (makeNewBranch(depth, coefficient)) {
Test t = genExpression(depth + 1, coefficient);
if (RNG.nextBoolean()) {
return new Test("( " + t.expr + " )", t.answer, type);
} else {
return new Test("abs( " + t.expr + " )", abs(t.answer), type);
}
} else if (RNG.nextBoolean()) {
Object z = type == 0 ? RNG.nextInt() : type == 1 ? (RNG.nextDouble() * Math.pow(10, RNG.nextInt(41) - 20)) : randomBigInteger();
Object[][] ret = new Object[201][201];
for (int i = 0; i <= 200; i++) {
Arrays.fill(ret[i], z);
}
return new Test("" + (z.toString()), ret, type);
} else {
int id = RNG.nextInt(2);
Object[][] ret = new Object[201][201];
for (int i = 0; i <= 200; i++) {
for (int j = 0; j <= 200; j++) {
int val = (id == 0 ? i : j) - 100;
if (type == 0) {
ret[i][j] = val;
} else if (type == 1) {
ret[i][j] = (double) val;
} else {
ret[i][j] = BigInteger.valueOf(val);
}
}
}
return new Test("xy".charAt(id) + "", ret, type);
}
}
boolean makeNewBranch(int depth, int coefficient) {
return RNG.nextInt(depth + coefficient) < coefficient;
}
static BigInteger randomBigInteger() {
StringBuilder sb = new StringBuilder();
if (RNG.nextBoolean()) sb.append('-');
int len = RNG.nextInt(50) + 1;
for (int i = 0; i < len; i++) sb.append(RNG.nextInt(10));
return new BigInteger(sb.toString());
}
}
static class Test {
String expr;
Object[][] answer;
int type;
Test(String expr, Object[][] answer, int type) {
this.expr = expr;
this.answer = answer;
this.type = type;
}
}
}