diff options
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/querytransform/BooleanAttributeParserTest.java')
-rw-r--r-- | container-search/src/test/java/com/yahoo/search/querytransform/BooleanAttributeParserTest.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/querytransform/BooleanAttributeParserTest.java b/container-search/src/test/java/com/yahoo/search/querytransform/BooleanAttributeParserTest.java new file mode 100644 index 00000000000..764a44a1bd6 --- /dev/null +++ b/container-search/src/test/java/com/yahoo/search/querytransform/BooleanAttributeParserTest.java @@ -0,0 +1,101 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.querytransform; + +import com.yahoo.prelude.query.PredicateQueryItem; +import org.junit.Test; + +import java.math.BigInteger; +import java.util.Iterator; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + +/** + * Created with IntelliJ IDEA. + * User: magnarn + * Date: 2/5/13 + * Time: 3:52 PM + */ +public class BooleanAttributeParserTest { + + @Test + public void requireThatParseHandlesAllFormats() throws Exception { + assertParse(null, 0); + assertParse("{}", 0); + assertParse("{foo:bar}", 1); + assertParse("{foo:[bar]}", 1); + assertParse("{foo:bar, baz:qux}", 2); + + assertParse("{foo:bar, foo:baz}", 2); + assertParse("{foo:[bar, baz, qux]}", 3); + assertParse("{foo:[bar, baz, qux], quux:corge}", 4); + assertParse("{foo:[bar, baz, qux], quux:[corge, grault]}", 5); + assertParse("{foo:bar, foo:bar, foo:bar}", 3); + + assertParse("{foo:bar:0x1, foo:baz:0xf}", 2); + assertParse("{foo:[bar:0xbabe, baz:0xbeef, qux:0xfee1], quux:corge:0x1234}", 4); + assertParse("{foo:bar:[1], foo:baz:[0,1,2,3]}", 2); + assertParse("{foo:bar:[ 1 ], foo:baz:[ 0 , 1 , 2 , 3 ]}", 2); + assertParse("{foo:[bar:[4,7],baz:[8,5],qux:[3,2]], quux:corge:[2, 5, 7, 58]}", 4); + } + + @Test + public void requireThatIllegalStringsFail() throws Exception { + assertException("{foo:[bar:[baz]}"); + assertException("{foo:[bar:baz}"); + assertException("{foo:bar:[0,1,2}"); + assertException("{foo:[bar:[0,1,2],baz:[0,,2]]}"); + assertException("{foo:[bar:[0,1,2],baz:[0,1,2]}"); + assertException("{foo:bar:[64]}"); + assertException("{foo:bar:[-1]}"); + assertException("{foo:bar:[a]}"); + assertException("{foo:bar:[0,1,[2]]}"); + assertException("{foo:bar}extrachars"); + } + + private void assertException(String s) { + try { + PredicateQueryItem item = new PredicateQueryItem(); + new BooleanSearcher.PredicateValueAttributeParser(item).parse(s); + fail("Expected an exception"); + } catch (IllegalArgumentException e) { + } + } + + @Test + public void requireThatTermsCanHaveBitmaps() throws Exception { + PredicateQueryItem q = assertParse("{foo:bar:0x1}", 1); + PredicateQueryItem.Entry[] features = new PredicateQueryItem.Entry[q.getFeatures().size()]; + q.getFeatures().toArray(features); + assertEquals(1l, q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:0x1, baz:qux:0xf}", 2); + Iterator<PredicateQueryItem.Entry> it = q.getFeatures().iterator(); + assertEquals(1l, it.next().getSubQueryBitmap()); + assertEquals(15l, it.next().getSubQueryBitmap()); + q = assertParse("{foo:bar:0xffffffffffffffff}", 1); + assertEquals(-1l, q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:[63]}", 1); + + assertEquals(new BigInteger("ffffffffffffffff", 16).shiftRight(1).add(BigInteger.ONE).longValue(), q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:0x7fffffffffffffff}", 1); + assertEquals(new BigInteger("ffffffffffffffff", 16).shiftRight(1).longValue(), q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:[0]}", 1); + assertEquals(1l, q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}", 1); + assertEquals(1l, q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:bar:[0,2,6,8]}", 1); + assertEquals(0x145l, q.getFeatures().iterator().next().getSubQueryBitmap()); + q = assertParse("{foo:[bar:[0,8,6,2],baz:[1,3,4,15]]}", 2); + it = q.getFeatures().iterator(); + assertEquals(0x145l, it.next().getSubQueryBitmap()); + assertEquals(0x801al, it.next().getSubQueryBitmap()); + } + + private PredicateQueryItem assertParse(String s, int numFeatures) { + PredicateQueryItem item = new PredicateQueryItem(); + BooleanAttributeParser parser = new BooleanSearcher.PredicateValueAttributeParser(item); + parser.parse(s); + assertEquals(numFeatures, item.getFeatures().size()); + return item; + } +} |