~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/test/queryParser/TestMultiFieldQueryParser.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
*
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "test.h"
 
8
 
 
9
class MQPTestFilter: public TokenFilter {
 
10
public:
 
11
 
 
12
        bool inPhrase;
 
13
        int32_t savedStart, savedEnd;
 
14
 
 
15
        /**
 
16
        * Filter which discards the token 'stop' and which expands the
 
17
        * token 'phrase' into 'phrase1 phrase2'
 
18
        */
 
19
        MQPTestFilter(TokenStream* in):
 
20
        TokenFilter(in,true),
 
21
                inPhrase(false),
 
22
                savedStart(0),
 
23
                savedEnd(0)
 
24
        {
 
25
        }
 
26
 
 
27
        CL_NS(analysis)::Token* next(CL_NS(analysis)::Token* token) {
 
28
                if (inPhrase) {
 
29
                        inPhrase = false;
 
30
                        token->set( _T("phrase2"), savedStart, savedEnd);
 
31
                        return token;
 
32
                }else{
 
33
                        while( input->next(token) ){
 
34
                                if ( _tcscmp(token->termBuffer(), _T("phrase")) == 0 ) {
 
35
                                        inPhrase = true;
 
36
                                        savedStart = token->startOffset();
 
37
                                        savedEnd = token->endOffset();
 
38
                                        token->set( _T("phrase1"), savedStart, savedEnd);
 
39
                                        return token;
 
40
                                }else if ( _tcscmp(token->termBuffer(), _T("stop") ) !=0 ){
 
41
                                        return token;
 
42
                                }
 
43
                        }
 
44
                }
 
45
                return NULL;
 
46
        }
 
47
};
 
48
 
 
49
class MQPTestAnalyzer: public Analyzer {
 
50
public:
 
51
        MQPTestAnalyzer() {
 
52
        }
 
53
 
 
54
        /** Filters LowerCaseTokenizer with StopFilter. */
 
55
        TokenStream* tokenStream(const TCHAR* /*fieldName*/, Reader* reader) {
 
56
                return _CLNEW MQPTestFilter(_CLNEW LowerCaseTokenizer(reader));
 
57
        }
 
58
};
 
59
 
 
60
void assertQueryEquals(CuTest *tc,const TCHAR* result, Query* q) {
 
61
        if ( q == NULL )
 
62
                return;
 
63
 
 
64
        TCHAR* s = q->toString();
 
65
        int ret = _tcscmp(s,result);
 
66
        _CLDELETE(q);
 
67
        if ( ret != 0 ) {
 
68
                TCHAR buf[HUGE_STRING_LEN];
 
69
                _sntprintf(buf, HUGE_STRING_LEN, _T("FAILED Query yielded /%s/, expecting /%s/\n"), s, result);
 
70
                _CLDELETE_LCARRAY(s);
 
71
                CuFail(tc, buf);
 
72
    return;
 
73
        }
 
74
        _CLDELETE_LCARRAY(s);
 
75
}
 
76
 
 
77
// verify parsing of query using a stopping analyzer
 
78
void assertStopQueryEquals(CuTest *tc, const TCHAR* qtxt, const TCHAR* expectedRes) {
 
79
        const TCHAR* fields[] = {_T("b"), _T("t"), NULL };
 
80
        const uint8_t occur[] = {BooleanClause::SHOULD, BooleanClause::SHOULD, NULL};
 
81
        MQPTestAnalyzer *a = _CLNEW MQPTestAnalyzer();
 
82
        MultiFieldQueryParser mfqp(fields, a);
 
83
 
 
84
        Query *q = mfqp.parse(qtxt);
 
85
        assertQueryEquals(tc, expectedRes, q);
 
86
 
 
87
        q = MultiFieldQueryParser::parse(qtxt, reinterpret_cast<const TCHAR**>(&fields),
 
88
                reinterpret_cast<const uint8_t*>(&occur), a);
 
89
        assertQueryEquals(tc, expectedRes, q);
 
90
        _CLDELETE(a);
 
91
}
 
92
 
 
93
/** test stop words arsing for both the non static form, and for the
 
94
* corresponding static form (qtxt, fields[]). */
 
95
void tesStopwordsParsing(CuTest *tc) {
 
96
        assertStopQueryEquals(tc, _T("one"), _T("b:one t:one"));
 
97
        assertStopQueryEquals(tc, _T("one stop"), _T("b:one t:one"));
 
98
        assertStopQueryEquals(tc, _T("one (stop)"), _T("b:one t:one"));
 
99
        assertStopQueryEquals(tc, _T("one ((stop))"), _T("b:one t:one"));
 
100
        assertStopQueryEquals(tc, _T("stop"), _T(""));
 
101
        assertStopQueryEquals(tc, _T("(stop)"), _T(""));
 
102
        assertStopQueryEquals(tc, _T("((stop))"), _T(""));
 
103
}
 
104
 
 
105
void testMFQPSimple(CuTest *tc) {
 
106
        const TCHAR* fields[] = {_T("b"), _T("t"), NULL};
 
107
        Analyzer* a = _CLNEW StandardAnalyzer();
 
108
        MultiFieldQueryParser mfqp(fields, a);
 
109
 
 
110
        Query *q = mfqp.parse(_T("one"));
 
111
        assertQueryEquals(tc, _T("b:one t:one"), q);
 
112
 
 
113
        q = mfqp.parse(_T("one two"));
 
114
        assertQueryEquals(tc, _T("(b:one t:one) (b:two t:two)"),q);
 
115
 
 
116
        q = mfqp.parse(_T("+one +two"));
 
117
        assertQueryEquals(tc, _T("+(b:one t:one) +(b:two t:two)"), q);
 
118
 
 
119
        q = mfqp.parse(_T("+one -two -three"));
 
120
        assertQueryEquals(tc, _T("+(b:one t:one) -(b:two t:two) -(b:three t:three)"), q);
 
121
 
 
122
        q = mfqp.parse(_T("one^2 two"));
 
123
        assertQueryEquals(tc, _T("((b:one t:one)^2.0) (b:two t:two)"), q);
 
124
 
 
125
        q = mfqp.parse(_T("one~ two"));
 
126
        assertQueryEquals(tc, _T("(b:one~0.5 t:one~0.5) (b:two t:two)"), q);
 
127
 
 
128
        q = mfqp.parse(_T("one~0.8 two^2"));
 
129
        assertQueryEquals(tc, _T("(b:one~0.8 t:one~0.8) ((b:two t:two)^2.0)"), q);
 
130
 
 
131
        q = mfqp.parse(_T("one* two*"));
 
132
        assertQueryEquals(tc, _T("(b:one* t:one*) (b:two* t:two*)"), q);
 
133
 
 
134
        q = mfqp.parse(_T("[a TO c] two"));
 
135
        assertQueryEquals(tc, _T("(b:[a TO c] t:[a TO c]) (b:two t:two)"), q);
 
136
 
 
137
        q = mfqp.parse(_T("w?ldcard"));
 
138
        assertQueryEquals(tc, _T("b:w?ldcard t:w?ldcard"), q);
 
139
 
 
140
        q = mfqp.parse(_T("\"foo bar\""));
 
141
        assertQueryEquals(tc, _T("b:\"foo bar\" t:\"foo bar\""), q);
 
142
 
 
143
        q = mfqp.parse(_T("\"aa bb cc\" \"dd ee\""));
 
144
        assertQueryEquals(tc, _T("(b:\"aa bb cc\" t:\"aa bb cc\") (b:\"dd ee\" t:\"dd ee\")"), q);
 
145
 
 
146
        q = mfqp.parse(_T("\"foo bar\"~4"));
 
147
        assertQueryEquals(tc, _T("b:\"foo bar\"~4 t:\"foo bar\"~4"), q);
 
148
 
 
149
        // make sure that terms which have a field are not touched:
 
150
        q = mfqp.parse(_T("one f:two"));
 
151
        assertQueryEquals(tc, _T("(b:one t:one) f:two"), q);
 
152
 
 
153
        // AND mode:
 
154
        mfqp.setDefaultOperator(QueryParser::AND_OPERATOR);
 
155
        q = mfqp.parse(_T("one two"));
 
156
        assertQueryEquals(tc, _T("+(b:one t:one) +(b:two t:two)"), q);
 
157
        q = mfqp.parse(_T("\"aa bb cc\" \"dd ee\""));
 
158
        assertQueryEquals(tc, _T("+(b:\"aa bb cc\" t:\"aa bb cc\") +(b:\"dd ee\" t:\"dd ee\")"), q);
 
159
 
 
160
        _CLDELETE(a);
 
161
}
 
162
 
 
163
CuSuite *testMultiFieldQueryParser(void)
 
164
{
 
165
        CuSuite *suite = CuSuiteNew(_T("CLucene Multi-Field QP Test"));
 
166
 
 
167
        SUITE_ADD_TEST(suite, tesStopwordsParsing);
 
168
        SUITE_ADD_TEST(suite, testMFQPSimple);
 
169
 
 
170
        return suite;
 
171
}