migration
This commit is contained in:
33
javascript/jstest/prefix/ParserTest.java
Normal file
33
javascript/jstest/prefix/ParserTest.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package jstest.prefix;
|
||||
|
||||
import base.Selector;
|
||||
import common.expression.Operation;
|
||||
|
||||
import static common.expression.Operations.*;
|
||||
|
||||
/**
|
||||
* Tests for
|
||||
* <a href="https://www.kgeorgiy.info/courses/paradigms/homeworks.html#js-expression-parsing">JavaScript Expression Parsing</a>
|
||||
* homework of <a href="https://www.kgeorgiy.info/courses/paradigms">Programming Paradigms</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class ParserTest {
|
||||
private static final Operation PARENTHESES = parentheses("(", ")", "{", "}", "[", "]", "<", ">");
|
||||
|
||||
public static final Selector SELECTOR = ParserTester.selector(
|
||||
ParserTest.class,
|
||||
"prefix", "parsePrefix", ParserTester.PREFIX
|
||||
)
|
||||
.variant("Base", ARITH)
|
||||
.variant("3637", PARENTHESES, any(3, SUM_EXP, LSE))
|
||||
.variant("3839", PARENTHESES, any(3, SUM_EXP, LME))
|
||||
.selector();
|
||||
|
||||
private ParserTest() {
|
||||
}
|
||||
|
||||
public static void main(final String... args) {
|
||||
SELECTOR.main(args);
|
||||
}
|
||||
}
|
||||
87
javascript/jstest/prefix/ParserTester.java
Normal file
87
javascript/jstest/prefix/ParserTester.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package jstest.prefix;
|
||||
|
||||
import base.Functional;
|
||||
import base.Selector;
|
||||
import common.expression.Dialect;
|
||||
import common.expression.ExprTester;
|
||||
import common.expression.Language;
|
||||
import common.expression.LanguageBuilder;
|
||||
import jstest.object.ObjectTester;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Tester for
|
||||
* <a href="https://www.kgeorgiy.info/courses/paradigms/homeworks.html#js-expression-parsing">JavaScript Expression Parsing</a>
|
||||
* homework of <a href="https://www.kgeorgiy.info/courses/paradigms">Programming Paradigms</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class ParserTester {
|
||||
public static final Dialect PREFIX = new Dialect("%s", "%s", "({op} {args})", " ");
|
||||
|
||||
private ParserTester() {
|
||||
}
|
||||
|
||||
public static Selector.Composite<LanguageBuilder> selector(
|
||||
final Class<?> owner,
|
||||
final String toString,
|
||||
final String parse,
|
||||
final Dialect unparsed,
|
||||
final String... parsingTests
|
||||
) {
|
||||
assert parsingTests.length % 2 == 0;
|
||||
|
||||
return LanguageBuilder.selector(owner, mode -> true, (builder, counter) -> {
|
||||
final String insertions = builder.variant().hasVarargs() ? "abc()+*/@ABC" : "xyz()+*/@ABC";
|
||||
final Language language = builder.language(ObjectTester.OBJECT, unparsed);
|
||||
final ExprTester<Object> tester = ObjectTester.tester(
|
||||
counter,
|
||||
language,
|
||||
toString,
|
||||
parse,
|
||||
(input, expr, random, build) -> build.add(removeSpaces(input)),
|
||||
corrupt(insertions)
|
||||
);
|
||||
tester.addStage(() -> Functional.forEachPair(
|
||||
parsingTests,
|
||||
(input, context) -> printParsingError(tester, input, context)
|
||||
));
|
||||
return tester;
|
||||
}, "", "easy", "hard");
|
||||
}
|
||||
|
||||
private static String removeSpaces(final String expression) {
|
||||
return expression.replace(" (", "(").replace(") ", ")");
|
||||
}
|
||||
|
||||
private static void printParsingError(final ExprTester<?> test, final String description, final String input) {
|
||||
final String message = new ExprTester.BadInput(input, "", "").assertError(test::parse);
|
||||
final int index = message.lastIndexOf("in <eval>");
|
||||
|
||||
System.err.format("%-15s | %-25s: %s%n", input, description, message.substring(0, index > 0 ? index : message.length()));
|
||||
}
|
||||
|
||||
private static ExprTester.Generator<ExprTester.BadInput> corrupt(final String insertions) {
|
||||
return (input, expr, random, builder) -> IntStream.range(0, 1 + Math.min(10, 200 / input.length())).boxed()
|
||||
.forEach(i -> {
|
||||
final int index = random.nextInt(input.length());
|
||||
final char c = input.charAt(index);
|
||||
if (!Character.isDigit(c) && !Character.isWhitespace(c) && "-hxyz".indexOf(c) == -1) {
|
||||
builder.add(new ExprTester.BadInput(
|
||||
input.substring(0, index),
|
||||
"<SYMBOL REMOVED>",
|
||||
input.substring(index + 1)
|
||||
));
|
||||
}
|
||||
final char newC = insertions.charAt(random.nextInt(insertions.length()));
|
||||
if (!Character.isDigit(c) && c != '-') {
|
||||
builder.add(new ExprTester.BadInput(
|
||||
input.substring(0, index),
|
||||
"<SYMBOL INSERTED -->",
|
||||
newC + input.substring(index)
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
54
javascript/jstest/prefix/PostfixTest.java
Normal file
54
javascript/jstest/prefix/PostfixTest.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package jstest.prefix;
|
||||
|
||||
import base.Selector;
|
||||
import common.expression.Dialect;
|
||||
import common.expression.Operation;
|
||||
|
||||
import static common.expression.Operations.*;
|
||||
|
||||
/**
|
||||
* Tests for Postfix* variants of
|
||||
* <a href="https://www.kgeorgiy.info/courses/paradigms/homeworks.html#js-expression-parsing">JavaScript Expression Parsing</a>
|
||||
* homework of <a href="https://www.kgeorgiy.info/courses/paradigms">Programming Paradigms</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class PostfixTest {
|
||||
private static final Operation PARENTHESES = parentheses("(", ")", "{", "}", "[", "]", "<", ">");
|
||||
|
||||
public static final Selector SELECTOR = ParserTester.selector(
|
||||
PostfixTest.class,
|
||||
"postfix", "parsePostfix", new Dialect("%s", "%s", "({args} {op})", " "),
|
||||
"Empty input", "",
|
||||
"Unknown variable", "a",
|
||||
"Invalid number", "-a",
|
||||
"Missing )", "(z (x y +) *",
|
||||
"Missing (", "z (x y +) *)",
|
||||
"Unknown operation", "( x y @@)",
|
||||
"Excessive info", "(x y +) x",
|
||||
"Empty op", "()",
|
||||
"Invalid unary (0 args)", "(negate)",
|
||||
"Invalid unary (2 args)", "(x y negate)",
|
||||
"Invalid binary (0 args)", "(+)",
|
||||
"Invalid binary (1 args)", "(x +)",
|
||||
"Invalid binary (3 args)", "(x y z +)",
|
||||
"Variable op (0 args)", "(x)",
|
||||
"Variable op (1 args)", "(1 x)",
|
||||
"Variable op (2 args)", "(1 2 x)",
|
||||
"Const op (0 args)", "(0)",
|
||||
"Const op (1 args)", "(0 1)",
|
||||
"Const op (2 args)", "(0 1 2)"
|
||||
)
|
||||
.variant("Base", ARITH)
|
||||
.variant("3637", PARENTHESES, any(3, SUM_EXP, LSE))
|
||||
.variant("3839", PARENTHESES, any(3, SUM_EXP, LME))
|
||||
.selector();
|
||||
|
||||
private PostfixTest() {
|
||||
}
|
||||
|
||||
public static void main(final String... args) {
|
||||
ParserTest.main(args);
|
||||
SELECTOR.main(args);
|
||||
}
|
||||
}
|
||||
51
javascript/jstest/prefix/PrefixTest.java
Normal file
51
javascript/jstest/prefix/PrefixTest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package jstest.prefix;
|
||||
|
||||
import base.Selector;
|
||||
import common.expression.Operation;
|
||||
|
||||
import static common.expression.Operations.*;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for Prefix* variants
|
||||
* <a href="https://www.kgeorgiy.info/courses/paradigms/homeworks.html#js-expression-parsing">JavaScript Expression Parsing</a>
|
||||
* homework of <a href="https://www.kgeorgiy.info/courses/paradigms">Programming Paradigms</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class PrefixTest {
|
||||
private static final Operation PARENTHESES = parentheses("(", ")", "{", "}", "[", "]", "<", ">", "«", "»");
|
||||
|
||||
public static final Selector SELECTOR = ParserTester.selector(
|
||||
PrefixTest.class,
|
||||
"prefix", "parsePrefix", ParserTester.PREFIX,
|
||||
"Empty input", "",
|
||||
"Unknown variable", "a",
|
||||
"Invalid number", "-a",
|
||||
"Missing )", "(* z (+ x y)",
|
||||
"Unknown operation", "(@@ x y)",
|
||||
"Excessive info", "(+ x y) x",
|
||||
"Empty op", "()",
|
||||
"Invalid unary (0 args)", "(negate)",
|
||||
"Invalid unary (2 args)", "(negate x y)",
|
||||
"Invalid binary (0 args)", "(+)",
|
||||
"Invalid binary (1 args)", "(+ x)",
|
||||
"Invalid binary (3 args)", "(+ x y z)",
|
||||
"Variable op (0 args)", "(x)",
|
||||
"Variable op (1 args)", "(x 1)",
|
||||
"Variable op (2 args)", "(x 1 2)",
|
||||
"Const op (0 args)", "(0)",
|
||||
"Const op (1 args)", "(0 1)",
|
||||
"Const op (2 args)", "(0 1 2)")
|
||||
.variant("Base", ARITH)
|
||||
.variant("3233", PARENTHESES, any(2, ATAN_12))
|
||||
.variant("3435", PARENTHESES, any(3, MEAN_EXP))
|
||||
.selector();
|
||||
|
||||
private PrefixTest() {
|
||||
}
|
||||
|
||||
public static void main(final String... args) {
|
||||
SELECTOR.main(args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user