Add regression test for expression performance (closes #192)

This commit is contained in:
Sam Harwell 2014-01-17 08:54:43 -06:00
parent ef0b38e2bb
commit 14107cc40a
1 changed files with 54 additions and 0 deletions

View File

@ -60,6 +60,7 @@ import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeListener;
@ -1963,4 +1964,57 @@ public class TestPerformance extends BaseTest {
return referent;
}
}
/**
* This is a regression test for antlr/antlr4#192 "Poor performance of
* expression parsing".
* https://github.com/antlr/antlr4/issues/192
*/
@Test(timeout = 60000)
public void testExpressionGrammar() {
String grammar =
"grammar Expr;\n" +
"\n" +
"program: expr EOF;\n" +
"\n" +
"expr: ID\n" +
" | 'not' expr\n" +
" | expr 'and' expr\n" +
" | expr 'or' expr\n" +
" ;\n" +
"\n" +
"ID: [a-zA-Z_][a-zA-Z_0-9]*;\n" +
"WS: [ \\t\\n\\r\\f]+ -> skip;\n" +
"ERROR: .;\n";
String input =
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12\n";
String found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "program",
input, false);
Assert.assertEquals("", found);
Assert.assertEquals(null, stderrDuringParse);
List<String> inputs = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
inputs.add(input);
}
input = Utils.join(inputs.iterator(), " or\n");
found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "program",
input, false);
Assert.assertEquals("", found);
Assert.assertEquals(null, stderrDuringParse);
}
}