-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTest.java
259 lines (222 loc) · 9.12 KB
/
ParserTest.java
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
package expression.parser;
import expression.BaseTest;
import expression.CommonExpression;
import expression.TripleExpression;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.LongBinaryOperator;
import java.util.function.LongSupplier;
import java.util.function.LongUnaryOperator;
/**
* @author Niyaz Nigmatullin
* @author Georgiy Korneev ([email protected])
*/
public class ParserTest extends BaseTest {
private final static int D = 5;
private final static List<Integer> TEST_VALUES = new ArrayList<>();
static {
addRange(TEST_VALUES, D, D);
addRange(TEST_VALUES, D, -D);
}
protected final List<Op<LongUnaryOperator>> unary = new ArrayList<>();
protected final List<List<Op<LongBinaryOperator>>> levels = new ArrayList<>();
protected List<Op<TExpression>> tests;
protected ParserTest() {
unary.add(op("-", a -> -a));
levels.add(list(
op("+", (a, b) -> a + b),
op("-", (a, b) -> a - b)
));
levels.add(list(
op("*", (a, b) -> a * b),
op("/", (a, b) -> b == 0 ? error(DBZ) : a / b)
));
tests = list(
op("10", (x, y, z) -> 10L),
op("x", (x, y, z) -> x),
op("y", (x, y, z) -> y),
op("z", (x, y, z) -> z),
op("x+2", (x, y, z) -> x + 2),
op("2-y", (x, y, z) -> 2 - y),
op(" 3* z ", (x, y, z) -> 3 * z),
op("x/ - 2", (x, y, z) -> -x / 2),
op("x*y+(z-1 )/10", (x, y, z) -> x * y + (int) (z - 1) / 10),
op("-(-(-\t\t-5 + 16 *x*y) + 1 * z) -(((-11)))", (x, y, z) -> -(-(5 + 16 * x * y) + z) + 11),
op("" + Integer.MAX_VALUE, (x, y, z) -> (long) Integer.MAX_VALUE),
op("" + Integer.MIN_VALUE, (x, y, z) -> (long) Integer.MIN_VALUE),
op("x--y--z", (x, y, z) -> x + y + z),
op("((2+2))-0/(--2)*555", (x, y, z) -> 4L),
op("x-x+y-y+z-(z)", (x, y, z) -> 0L),
op(repeat("(", 500) + "x + y + (-10*-z)" + repeat(")", 500), (x, y, z) -> x + y + 10 * z),
op("x / y / z", (x, y, z) -> y == 0 || z == 0 ? error(DBZ) : (int) x / (int) y / z)
);
}
public static void main(final String[] args) {
new ParserTest().run();
}
@Override
protected void test() {
for (final Op<TExpression> test : tests) {
System.out.println("Testing: " + test.name);
final TripleExpression expression = parse(test.name, true);
for (final Integer x : TEST_VALUES) {
for (final Integer y : TEST_VALUES) {
for (final Integer z : TEST_VALUES) {
check(new int[]{x, y, z}, expression, eval(() -> test.f.evaluate(x, y, z)));
}
}
}
}
testRandom(1, 2000, (v, i) -> generate(v, i / 5 + 2));
testRandom(2, 777, (v, i) -> genExpression(1, i / 25 / levels.size() + 1, v, 0));
}
private Either<Reason, Integer> eval(final LongSupplier supplier) {
try {
return lift(supplier.getAsLong());
} catch (final ExpException e) {
return Either.left(e.reason);
}
}
protected TripleExpression parse(final String expression, final boolean reparse) {
try {
final ExpressionParser parser = new ExpressionParser();
if (reparse) {
counter.nextTest();
parser.parse(expression);
counter.passed();
}
counter.nextTest();
final CommonExpression result = parser.parse(expression);
counter.passed();
return result;
} catch (final Exception e) {
throw new AssertionError("Parser failed", e);
}
}
protected void testRandom(final int seq, final int n, final BiFunction<int[], Integer, Test> f) {
System.out.println("Testing random tests #" + seq);
for (int i = 0; i < n; i++) {
if (i % 100 == 0) {
System.out.println("Completed " + i + " out of " + n);
}
final int[] vars = new int[]{random.nextInt(), random.nextInt(), random.nextInt()};
final Test test = f.apply(vars, i);
try {
check(vars, parse(test.expr, false), test.answer);
} catch (final Throwable e) {
System.out.println("Failed test: " + test.expr);
throw e;
}
}
}
private void check(final int[] vars, final TripleExpression expression, final Either<Reason, Integer> answer) {
counter.nextTest();
try {
final int actual = expression.evaluate(vars[0], vars[1], vars[2]);
assert answer.isRight() : String.format("Error expected for x=%d, y=%d, z=%d", vars[0], vars[1], vars[2]);
assertEquals(String.format("f(%d, %d, %d)\n%s", vars[0], vars[1], vars[2], expression), actual, (int) answer.getRight());
} catch (final Exception e) {
if (answer.isRight()) {
throw new AssertionError(String.format("No error expected for x=%d, y=%d, z=%d", vars[0], vars[1], vars[2]), e);
}
}
counter.passed();
}
private Test generate(final int[] vars, final int depth) {
if (depth == 0) {
return constOrVariable(vars);
}
final int operator = randomInt(6);
if (operator <= 0) {
return genP(vars, depth);
} else if (operator <= 1) {
return unary(genP(vars, depth));
} else {
return binary(random(levels), genP(vars, depth), genP(vars, depth));
}
}
private Test genP(final int[] vars, final int depth) {
return p(generate(vars, randomInt(depth)));
}
private Test constOrVariable(final int[] vars) {
if (random.nextBoolean()) {
final int id = randomInt(3);
return new Test("xyz".charAt(id) + "", Either.right(vars[id]));
} else {
final int value = random.nextInt();
return new Test(value + "", Either.right(value));
}
}
private Test genExpression(final int depth, final int coefficient, final int[] vars, final int level) {
if (level == levels.size()) {
return genFactor(depth, coefficient, vars);
} else if (makeNewBranch(depth, coefficient)) {
return binary(levels.get(level), genExpression(depth + 1, coefficient, vars, level), genExpression(depth, coefficient, vars, level + 1));
} else {
return genExpression(depth, coefficient, vars, level + 1);
}
}
private Test genFactor(final int depth, final int coefficient, final int[] vars) {
if (makeNewBranch(depth, coefficient)) {
return unary(genFactor(depth + 1, coefficient, vars));
} else {
return genValue(depth, coefficient, vars);
}
}
private static Test p(final Test t) {
return new Test("(" + t.expr + ")", t.answer);
}
private Test binary(final List<Op<LongBinaryOperator>> ops, final Test t1, final Test t2) {
final Op<LongBinaryOperator> op = random(ops);
return new Test(
t1.expr + " " + op.name + " " + t2.expr,
t1.answer.flatMapRight(a -> t2.answer.flatMapRight(b -> eval(() -> op.f.applyAsLong(a, b))))
);
}
private Test unary(final Test arg) {
final Op<LongUnaryOperator> op = random(unary);
return new Test(op.name + " " + arg.expr, arg.answer.flatMapRight(a -> eval(() -> op.f.applyAsLong(a))));
}
private Test genValue(final int depth, final int coefficient, final int[] vars) {
if (makeNewBranch(depth, coefficient)) {
return p(genExpression(depth + 1, coefficient, vars, 0));
} else {
return constOrVariable(vars);
}
}
private boolean makeNewBranch(final int depth, final int coefficient) {
return randomInt(depth + coefficient) < coefficient;
}
protected Either<Reason, Integer> lift(final long value) {
return Either.right((int) value);
}
protected static class Test {
final String expr;
final Either<Reason, Integer> answer;
Test(final String expr, final Either<Reason, Integer> answer) {
this.expr = expr;
this.answer = answer;
}
}
protected static class Reason {
private final String description;
public Reason(final String description) {
this.description = description;
}
}
public static final Reason DBZ = new Reason("Division by zero");
public interface TExpression {
long evaluate(long x, long y, long z);
}
public static class ExpException extends RuntimeException {
private final Reason reason;
public ExpException(final Reason reason) {
super(reason.description);
this.reason = reason;
}
}
public static long error(final Reason reason) {
throw new ExpException(reason);
}
}