1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
}
}
|