-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionalExpressionTest.java
73 lines (62 loc) · 2.64 KB
/
FunctionalExpressionTest.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
package jstest;
import expression.BaseTest;
import java.util.function.BiFunction;
/**
* @author Georgiy Korneev ([email protected])
*/
public class FunctionalExpressionTest extends BaseJavascriptTest<Engine> {
public static final Dialect PURE_FUNCTIONS = dialect(
"variable('%s')",
"cnst(%s)",
(op, args) -> op + "(" + String.join(", ", args) + ")"
);
public static final Dialect ARITHMETIC_FUNCTIONS = PURE_FUNCTIONS.clone()
.rename("+", "add")
.rename("-", "subtract")
.rename("/", "divide")
.rename("*", "multiply");
public static final Dialect POLISH = dialect(
"%s",
"%s",
(op, args) -> String.join(" ", args) + " " + op
);
protected FunctionalExpressionTest(final Language language, final boolean testParsing) {
super(new JSEngine("functionalExpression.js", ""), language, testParsing);
}
@Override
protected String parse(final String expression) {
return "parse('" + expression + "')";
}
protected static <T extends BaseTest> void test(final Class<T> type, final BiFunction<Language, Boolean, T> cons, final String[] args, final AbstractTests tests) {
test(type, cons, tests, args, ARITHMETIC_FUNCTIONS);
}
protected static <T extends BaseTest> void test(final Class<T> type, final BiFunction<Language, Boolean, T> cons, final AbstractTests tests, final String[] args, final Dialect parsed) {
cons.apply(
new Language(parsed, POLISH, tests),
mode(args, type, "easy", "hard") == 1
).run();
System.out.println("Mode: " + args[0]);
}
public static void main(final String... args) {
test(FunctionalExpressionTest.class, FunctionalExpressionTest::new, args, new AbstractTests() {
protected final AbstractExpression vx = variable("x", 0);
{
binary("+", (a, b) -> a + b);
binary("-", (a, b) -> a - b);
binary("*", (a, b) -> a * b);
binary("/", (a, b) -> a / b);
tests(
c(10),
vx,
f("+", vx, c(2)),
f("-", c(3), vx),
f("*", c(4), vx),
f("/", c(5), vx),
f("/", vx, f("*", f("+", vx, c(1)), vx)),
f("+", f("+", f("*", vx, vx), f("*", vx, vx)), f("*", vx, vx)),
f("-", f("+", f("*", vx, vx), f("*", c(5), f("*", vx, f("*", vx, vx)))), f("*", vx, c(8)))
);
}
});
}
}