Skip to content

Commit

Permalink
Add regression test for expression performance (closes #192)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jan 17, 2014
1 parent ef0b38e commit 14107cc
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tool/test/org/antlr/v4/test/TestPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
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;
Expand Down Expand Up @@ -1963,4 +1964,57 @@ public T get() {
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);
}
}

0 comments on commit 14107cc

Please sign in to comment.