From 176334efcb53debcd0b1ec159273a7703b714a52 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sat, 14 Sep 2013 22:14:21 +0000 Subject: [PATCH] LUCENE-5207: add test for huge amounts of variables git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5207@1523339 13f79535-47bb-0310-9956-ffa450edef68 --- .../expressions/TestDemoExpressions.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/TestDemoExpressions.java b/lucene/expressions/src/test/org/apache/lucene/expressions/TestDemoExpressions.java index 96535ec311f..190b38e6484 100644 --- a/lucene/expressions/src/test/org/apache/lucene/expressions/TestDemoExpressions.java +++ b/lucene/expressions/src/test/org/apache/lucene/expressions/TestDemoExpressions.java @@ -110,4 +110,36 @@ public class TestDemoExpressions extends LuceneTestCase { assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual)); } } + + /** tests huge amounts of variables in the expression */ + public void testLotsOfBindings() throws Exception { + doTestLotsOfBindings(Byte.MAX_VALUE-1); + doTestLotsOfBindings(Byte.MAX_VALUE); + doTestLotsOfBindings(Byte.MAX_VALUE+1); + // TODO: ideally we'd test > Short.MAX_VALUE too, but compilation is currently recursive. + // so if we want to test such huge expressions, we need to instead change parser to use an explicit Stack + } + + private void doTestLotsOfBindings(int n) throws Exception { + SimpleBindings bindings = new SimpleBindings(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n; i++) { + if (i > 0) { + sb.append("+"); + } + sb.append("x" + i); + bindings.add(new SortField("x" + i, SortField.Type.SCORE)); + } + + Expression expr = JavascriptCompiler.compile(sb.toString()); + Sort sort = new Sort(expr.getSortField(bindings, true)); + Query query = new TermQuery(new Term("body", "contents")); + TopFieldDocs td = searcher.search(query, null, 3, sort, true, true); + for (int i = 0; i < 3; i++) { + FieldDoc d = (FieldDoc) td.scoreDocs[i]; + float expected = n*d.score; + float actual = ((Double)d.fields[0]).floatValue(); + assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual)); + } + } }