MtasFunctionParserTest.java
13 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package mtas.parser;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Random;
import mtas.parser.function.MtasFunctionParser;
import mtas.parser.function.ParseException;
import mtas.parser.function.util.MtasFunctionParserFunction;
import mtas.parser.function.util.MtasFunctionParserFunctionResponse;
import mtas.parser.function.util.MtasFunctionParserFunctionResponseDouble;
import mtas.parser.function.util.MtasFunctionParserFunctionResponseLong;
public class MtasFunctionParserTest {
Random generator = new Random();
@org.junit.Test
public void test() {
basicTests();
}
private void testFunction(MtasFunctionParserFunction pf, long[] args, int n,
MtasFunctionParserFunctionResponse r) {
assertEquals(pf + "\tn:" + n + "\targs:" + Arrays.toString(args),
pf.getResponse(args, n), r);
}
private void basicTests() {
basicTest1();
basicTest2();
basicTest3();
basicTest4();
basicTest5();
basicTest6();
basicTest7();
basicTest8();
basicTest9();
basicTest10();
}
private long[] getArgs(int n, int min, int max) {
long[] args = new long[n];
for (int i = 0; i < n; i++) {
args[i] = min + generator.nextInt((1 + max - min));
}
return args;
}
private int getN(int min, int max) {
return min + generator.nextInt((1 + max - min));
}
private void basicTest1() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
for (int i = 0; i < 1000; i++) {
int n = getN(0, 10000);
int k = generator.nextInt(10);
function = "$q" + k;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(1 + k + generator.nextInt(20), -1000, 1000);
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseLong(args[k], true));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest2() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
function = "$n";
p = new MtasFunctionParser(new BufferedReader(new StringReader(function)));
try {
pf = p.parse();
for (int i = 0; i < 1000; i++) {
int n = getN(0, 10000);
int k = generator.nextInt(10);
args = getArgs(k + generator.nextInt(20), -1000, 1000);
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseLong(n, true));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest3() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
for (int i = 0; i < 1000; i++) {
int n = getN(0, 10);
int i1 = generator.nextInt(100) - 50;
int o0 = generator.nextInt(4);
int k1 = generator.nextInt(10);
int o1 = generator.nextInt(4);
int k2 = generator.nextInt(10);
int o2 = generator.nextInt(4);
int k3 = generator.nextInt(10);
int o3 = generator.nextInt(4);
int k4 = generator.nextInt(10);
int o4 = generator.nextInt(4);
int k5 = generator.nextInt(10);
int o5 = generator.nextInt(4);
int k6 = generator.nextInt(3);
int o6 = generator.nextInt(4);
function = i1 + " " + getOperator(o0) + " $q" + k1 + " "
+ getOperator(o1) + " $q" + k2 + " " + getOperator(o2) + " $q" + k3
+ " " + getOperator(o3) + " $q" + k4 + " " + getOperator(o4) + " $q"
+ k5 + " " + getOperator(o5) + " $n " + getOperator(o6) + " $q"
+ k6;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), -10, 10);
Object answer = null;
try {
answer = compute(o0, i1, args[k1]);
answer = answer instanceof Double
? compute(o1, (double) answer, args[k2])
: compute(o1, (int) answer, args[k2]);
answer = answer instanceof Double
? compute(o2, (double) answer, args[k3])
: compute(o2, (int) answer, args[k3]);
answer = answer instanceof Double
? compute(o3, (double) answer, args[k4])
: compute(o3, (int) answer, args[k4]);
answer = answer instanceof Double
? compute(o4, (double) answer, args[k5])
: compute(o4, (int) answer, args[k5]);
answer = answer instanceof Double
? compute(o5, (double) answer, n)
: compute(o5, (int) answer, n);
answer = answer instanceof Double
? compute(o6, (double) answer, args[k6])
: compute(o6, (int) answer, args[k6]);
if (answer instanceof Double) {
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble((double) answer,
true));
} else {
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseLong((int) answer,
true));
}
} catch (IOException | IllegalArgumentException e) {
if (answer != null && answer instanceof Double) {
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble((double) answer,
false));
} else {
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble((int) 0, false));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest4() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
int n = getN(0, 10000);
int k1 = generator.nextInt(10);
function = "100/$q" + k1;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), 100, 1000);
double answer = 100.0 / args[k1];
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble(answer, true));
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest5() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
function = "$n+100/$q0";
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = new long[] { 0 };
testFunction(pf, args, 10,
new MtasFunctionParserFunctionResponseDouble(0, false));
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest6() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
for (int i = 0; i < 1000; i++) {
int n = getN(0, 10000);
int k = generator.nextInt(10);
function = "$n+1.3+2.6/$q" + k;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), -1000, 1000);
double answer = (args[k] != 0) ? (n + 1.3 + 2.6) / args[k] : 0;
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble(answer, args[k] != 0));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest7() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
for (int i = 0; i < 1000; i++) {
int n = getN(0, 10000);
int k1 = generator.nextInt(10);
int k2 = generator.nextInt(10);
int k3 = generator.nextInt(10);
function = "$n * ($q" + k1 + "+$q" + k2 + ")/$q" + k3;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), -1000, 1000);
double answer = (args[k3] != 0)
? (double) (n * (args[k1] + args[k2])) / args[k3] : 0;
testFunction(pf, args, n, new MtasFunctionParserFunctionResponseDouble(
answer, args[k3] != 0));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest8() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
for (int i = 0; i < 100000; i++) {
int n = getN(0, 10000);
int k1 = generator.nextInt(10);
int k2 = generator.nextInt(10);
int k3 = generator.nextInt(10);
int k4 = generator.nextInt(10);
function = "1+(($q" + k1 + "+$q" + k2 + ")/($q" + k3 + "+$q" + k4
+ "))-$n";
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), -1000, 1000);
double answer = (args[k3] + args[k4] != 0)
? ((double) ((args[k1] + args[k2]))
/ (double) ((args[k3] + args[k4]))) + 1 - n
: 0;
testFunction(pf, args, n, new MtasFunctionParserFunctionResponseDouble(
answer, (args[k3] + args[k4]) != 0));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest9() {
String function = null;
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
int n = getN(0, 100);
int k1 = generator.nextInt(10);
function = "$n^$q" + k1;
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), 0, 2);
long answer = n ^ args[k1];
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseLong(answer, true));
} catch (ParseException e) {
e.printStackTrace();
}
}
private void basicTest10() {
String function = "(1+2)/3";
MtasFunctionParser p;
MtasFunctionParserFunction pf;
long[] args = null;
try {
int n = getN(0, 100);
int k1 = generator.nextInt(10);
int k2 = generator.nextInt(10);
int k3 = generator.nextInt(10);
function = "("+k1+" + "+k2+")/($q0 + 1 + "+k3+" - 2)";
p = new MtasFunctionParser(
new BufferedReader(new StringReader(function)));
pf = p.parse();
args = getArgs(10 + generator.nextInt(20), 0, 2);
double answer = (double)(k1+k2) / (args[0]+1+k3-2);
testFunction(pf, args, n,
new MtasFunctionParserFunctionResponseDouble(answer, true));
} catch (ParseException e) {
e.printStackTrace();
}
}
private Object compute(int op, long v1, long v2) throws IOException {
if (op == 0) {
Long s;
s = (long) (v1 + v2);
if (s > Integer.MAX_VALUE || s < Integer.MIN_VALUE) {
throw new IOException("too big");
} else {
return s.intValue();
}
} else if (op == 1) {
Long s;
s = (long) (v1 - v2);
if (s > Integer.MAX_VALUE || s < Integer.MIN_VALUE) {
throw new IOException("too big");
} else {
return s.intValue();
}
} else if (op == 2) {
Long s;
s = (long) (v1 * v2);
if (s > Integer.MAX_VALUE || s < Integer.MIN_VALUE) {
throw new IOException("too big");
} else {
return s.intValue();
}
} else if (op == 3) {
if (v2 == 0) {
throw new IllegalArgumentException("division by zero");
} else {
return (double) v1 / v2;
}
} else if (op == 4) {
Long s;
s = (long) (v1 ^ v2);
if (s > Integer.MAX_VALUE || s < Integer.MIN_VALUE) {
throw new IOException("too big");
} else {
return s.intValue();
}
} else {
throw new IOException("unknown operator");
}
}
private Double compute(int op, long v1, double v2) throws IOException {
return compute(op, (double) v1, v2);
}
private Double compute(int op, double v1, long v2) throws IOException {
return compute(op, v1, (double) v2);
}
private Double compute(int op, double v1, double v2) throws IOException {
if (op == 0) {
return v1 + v2;
} else if (op == 1) {
return v1 - v2;
} else if (op == 2) {
return v1 * v2;
} else if (op == 3) {
if (v2 == 0) {
throw new IllegalArgumentException("division by zero");
} else {
return v1 / v2;
}
} else if (op == 4) {
return Math.pow(v1, v2);
} else {
throw new IOException("unknown operator");
}
}
private String getOperator(int op) {
if (op == 0) {
return "+";
} else if (op == 1) {
return "-";
} else if (op == 2) {
return "*";
} else if (op == 3) {
return "/";
} else if (op == 4) {
return "^";
} else {
return "?";
}
}
}