~mixxxdevelopers/mixxx/features_search

« back to all changes in this revision

Viewing changes to mixxx/lib/clucene-2.3.3.4/src/test/search/TestBoolean.cpp

  • Committer: RJ Ryan
  • Date: 2011-05-27 23:54:08 UTC
  • Revision ID: rryan@mit.edu-20110527235408-kepzw0zdxdhngsqr
Add clucene-2.3.3.4 to lib/

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
#include "CLucene/search/_BooleanScorer2.h"
 
9
#include "CLucene/search/Similarity.h"
 
10
#include "MockScorer.h"
 
11
#include "MockHitCollector.h"
 
12
 
 
13
/// TestBooleanQuery.java, ported 5/9/2009
 
14
void testEquality(CuTest *tc) {
 
15
    BooleanQuery* bq1 = _CLNEW BooleanQuery();
 
16
    Term* t = _CLNEW Term(_T("field"), _T("value1"));
 
17
    bq1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
18
    _CLDECDELETE(t);
 
19
    t = _CLNEW Term(_T("field"), _T("value2"));
 
20
    bq1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
21
    _CLDECDELETE(t);
 
22
    BooleanQuery* nested1 = _CLNEW BooleanQuery();
 
23
    t = _CLNEW Term(_T("field"), _T("nestedvalue1"));
 
24
    nested1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
25
    _CLDECDELETE(t);
 
26
    t = _CLNEW Term(_T("field"), _T("nestedvalue2"));
 
27
    nested1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
28
    _CLDECDELETE(t);
 
29
    bq1->add(nested1, true, BooleanClause::SHOULD);
 
30
 
 
31
    BooleanQuery* bq2 = _CLNEW BooleanQuery();
 
32
    t = _CLNEW Term(_T("field"), _T("value1"));
 
33
    bq2->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
34
    _CLDECDELETE(t);
 
35
    t = _CLNEW Term(_T("field"), _T("value2"));
 
36
    bq2->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
37
    _CLDECDELETE(t);
 
38
    BooleanQuery* nested2 = _CLNEW BooleanQuery();
 
39
    t = _CLNEW Term(_T("field"), _T("nestedvalue1"));
 
40
    nested2->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
41
    _CLDECDELETE(t);
 
42
    t = _CLNEW Term(_T("field"), _T("nestedvalue2"));
 
43
    nested2->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
44
    _CLDECDELETE(t);
 
45
    bq2->add(nested2, true, BooleanClause::SHOULD);
 
46
 
 
47
    CLUCENE_ASSERT(bq1->equals(bq2));
 
48
 
 
49
    _CLLDELETE(bq1);
 
50
    _CLLDELETE(bq2);
 
51
}
 
52
void testException(CuTest *tc) {
 
53
    try {
 
54
        BooleanQuery::setMaxClauseCount(0);
 
55
        CuFail(tc, _T("setMaxClauseCount(0) did not throw an exception"));
 
56
    } catch (CLuceneError&) {
 
57
        // okay
 
58
    }
 
59
}
 
60
 
 
61
/// TestBooleanScorer.java, ported 5/9/2009
 
62
void testBooleanScorer(CuTest *tc) {
 
63
    const TCHAR* FIELD = _T("category");
 
64
    RAMDirectory directory;
 
65
 
 
66
    const TCHAR* values[] = { _T("1"), _T("2"), _T("3"), _T("4"), NULL};
 
67
 
 
68
    try {
 
69
        WhitespaceAnalyzer a;
 
70
        IndexWriter* writer = _CLNEW IndexWriter(&directory, &a, true);
 
71
        for (size_t i = 0; values[i]!=NULL; i++) {
 
72
            Document* doc = _CLNEW Document();
 
73
            doc->add(*_CLNEW Field(FIELD, values[i], Field::STORE_YES | Field::INDEX_TOKENIZED));
 
74
            writer->addDocument(doc);
 
75
            _CLLDELETE(doc);
 
76
        }
 
77
        writer->close();
 
78
        _CLLDELETE(writer);
 
79
 
 
80
        BooleanQuery* booleanQuery1 = _CLNEW BooleanQuery();
 
81
        Term *t = _CLNEW Term(FIELD, _T("1"));
 
82
        booleanQuery1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
83
        _CLDECDELETE(t);
 
84
        t = _CLNEW Term(FIELD, _T("2"));
 
85
        booleanQuery1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
 
86
        _CLDECDELETE(t);
 
87
 
 
88
        BooleanQuery* query = _CLNEW BooleanQuery();
 
89
        query->add(booleanQuery1, true, BooleanClause::MUST);
 
90
        t = _CLNEW Term(FIELD, _T("9"));
 
91
        query->add(_CLNEW TermQuery(t), true, BooleanClause::MUST_NOT);
 
92
        _CLDECDELETE(t);
 
93
 
 
94
        IndexSearcher *indexSearcher = _CLNEW IndexSearcher(&directory);
 
95
        Hits *hits = indexSearcher->search(query);
 
96
        CLUCENE_ASSERT(2 == hits->length()); // Number of matched documents
 
97
        _CLLDELETE(hits);
 
98
        _CLLDELETE(indexSearcher);
 
99
 
 
100
        _CLLDELETE(query);
 
101
    }
 
102
    catch (CLuceneError& e) {
 
103
        CuFail(tc, e.twhat());
 
104
    }
 
105
}
 
106
 
 
107
/// TestBooleanPrefixQuery.java, ported 5/9/2009
 
108
void testBooleanPrefixQuery(CuTest* tc) {
 
109
    RAMDirectory directory;
 
110
    WhitespaceAnalyzer a;
 
111
 
 
112
    const TCHAR* categories[] = {_T("food"), _T("foodanddrink"),
 
113
        _T("foodanddrinkandgoodtimes"), _T("food and drink"), NULL};
 
114
 
 
115
    Query* rw1 = NULL;
 
116
    Query* rw2 = NULL;
 
117
    try {
 
118
        IndexWriter* writer = _CLNEW IndexWriter(&directory, &a, true);
 
119
        for (size_t i = 0; categories[i]!=NULL; i++) {
 
120
            Document* doc = new Document();
 
121
            doc->add(*_CLNEW Field(_T("category"), categories[i], Field::STORE_YES | Field::INDEX_UNTOKENIZED));
 
122
            writer->addDocument(doc);
 
123
            _CLLDELETE(doc);
 
124
        }
 
125
        writer->close();
 
126
        _CLLDELETE(writer);
 
127
 
 
128
        IndexReader* reader = IndexReader::open(&directory);
 
129
        Term* t = _CLNEW Term(_T("category"), _T("foo"));
 
130
        PrefixQuery* query = _CLNEW PrefixQuery(t);
 
131
        _CLDECDELETE(t);
 
132
 
 
133
        rw1 = query->rewrite(reader);
 
134
 
 
135
        BooleanQuery* bq = _CLNEW BooleanQuery();
 
136
        bq->add(query, true, BooleanClause::MUST);
 
137
 
 
138
        rw2 = bq->rewrite(reader);
 
139
 
 
140
        reader->close(); // TODO: check necessity (_CLLDELETE(reader) alone will not do the same cleanup)
 
141
 
 
142
        _CLLDELETE(reader);
 
143
        _CLLDELETE(bq);
 
144
    } catch (CLuceneError& e) {
 
145
        CuFail(tc, e.twhat());
 
146
    }
 
147
 
 
148
    BooleanQuery* bq1 = NULL;
 
149
    if (rw1->instanceOf(BooleanQuery::getClassName())) {
 
150
        bq1 = (BooleanQuery*) rw1;
 
151
    }
 
152
 
 
153
    BooleanQuery* bq2 = NULL;
 
154
    if (rw2->instanceOf(BooleanQuery::getClassName())) {
 
155
        bq2 = (BooleanQuery*) rw2;
 
156
    } else {
 
157
        CuFail(tc, _T("Rewrite"));
 
158
    }
 
159
 
 
160
    bool bClausesMatch = bq1->getClauseCount() == bq2->getClauseCount();
 
161
 
 
162
    _CLLDELETE(rw1);
 
163
    _CLLDELETE(rw2);
 
164
 
 
165
    if (!bClausesMatch) {
 
166
        CuFail(tc, _T("Number of Clauses Mismatch"));
 
167
    }
 
168
}
 
169
 
 
170
void testBooleanScorer2WithProhibitedScorer(CuTest* tc) {
 
171
    CL_NS(search)::DefaultSimilarity similarity;
 
172
    BooleanScorer2 scorer(&similarity, 0, true);
 
173
    MockScorer prohibitedScorer(&similarity);
 
174
    scorer.add(&prohibitedScorer, false, true);
 
175
    CL_NS(search)::MockHitCollector collector;
 
176
    scorer.score(&collector);
 
177
 
 
178
    CuAssertIntEquals(tc, _T("Unexpected calls of next()!"), 1, prohibitedScorer.getNextCalls());
 
179
}
 
180
 
 
181
CuSuite *testBoolean(void)
 
182
{
 
183
    CuSuite *suite = CuSuiteNew(_T("CLucene Boolean Tests"));
 
184
 
 
185
    SUITE_ADD_TEST(suite, testEquality);
 
186
    SUITE_ADD_TEST(suite, testException);
 
187
 
 
188
    SUITE_ADD_TEST(suite, testBooleanScorer);
 
189
 
 
190
    SUITE_ADD_TEST(suite, testBooleanPrefixQuery);
 
191
    SUITE_ADD_TEST(suite, testBooleanScorer2WithProhibitedScorer);
 
192
 
 
193
    //_CrtSetBreakAlloc(1179);
 
194
 
 
195
        return suite; 
 
196
}
 
197
// EOF