aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/juniper/matchobjectTest.cpp
blob: 260e1823e3404607a45331f2c625c239a0d22d30 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/*
 * Author: Knut Omang
 */
#include "matchobjectTest.h"
#include "fakerewriter.h"

// Comment out cerr below to ignore unimplemented tests
#define NOTEST(name) \
std::cerr << std::endl << __FILE__ << ':' << __LINE__ << ": " \
          << "No test for method '" << (name) << "'" << std::endl;

/*************************************************************************
 *                      Test methods
 *
 * This section contains boolean methods for testing each public method
 * in the class being tested
 *************************************************************************/

/**
 * Test of the Term method.
 */
void MatchObjectTest::testTerm() {
    // Test that two equal keywords are matched properly:
    TestQuery q("NEAR/2(word,PHRASE(near,word))");

    const char* content = "This is a small text with word appearing near word";
    size_t content_len = strlen(content);

    // Fetch a result descriptor:
    auto res = juniper::Analyse(*juniper::TestConfig, q._qhandle,
                                content, content_len,
                                0, 0);
    _test(static_cast<bool>(res));

    // Do the scanning manually. This calls accept several times
    res->Scan();
    Matcher& m = *res->_matcher;

    _test(m.TotalHits() == 3);// 3 occurrences
    match_candidate_set& ms = m.OrderedMatchSet();

    _test(ms.size() == 2);

    // printf("%d %d\n", m.TotalHits(),ms.size());
    TestQuery q1("t*t");
    TestQuery q2("*ea*");
    TestQuery q3("*d");
    TestQuery q4("*word");
    auto r1 = juniper::Analyse(*juniper::TestConfig, q1._qhandle, content, content_len, 0, 0);
    auto r2 = juniper::Analyse(*juniper::TestConfig, q2._qhandle, content, content_len, 0, 0);
    auto r3 = juniper::Analyse(*juniper::TestConfig, q3._qhandle, content, content_len, 0, 0);
    auto r4 = juniper::Analyse(*juniper::TestConfig, q4._qhandle, content, content_len, 0, 0);
    _test(static_cast<bool>(r1));
    if (r1) {
        r1->Scan();
        _test(r1->_matcher->TotalHits() == 1);
    }
    _test(static_cast<bool>(r2));
    if (r2) {
        r2->Scan();
        _test(r2->_matcher->TotalHits() == 2);
    }

    if (r3) {
        r3->Scan();
        _test(r3->_matcher->TotalHits() == 2);
    } else {
        _test(static_cast<bool>(r3));
    }

    if (r4) {
        r4->Scan();
        _test_equal(r4->_matcher->TotalHits(), 2);
    } else {
        _test(static_cast<bool>(r4));
    }
}

/**
 * Test of the Match method.
 */
void MatchObjectTest::testMatch() {
    // Check that we hit on the longest match first
    juniper::QueryParser p("AND(junipe,juniper)");
    juniper::QueryHandle qh(p, NULL, juniper::_Juniper->getModifier());

    MatchObject* mo = qh.MatchObj(0);
    juniper::Result res(*juniper::TestConfig, qh, "", 0, 0);
    unsigned opts = 0;
    match_iterator mi(mo, &res);
    ucs4_t ucs4_str[10];
    Fast_UnicodeUtil::ucs4copy(ucs4_str, "junipers");
    Token token;
    token.token = ucs4_str;
    token.curlen = 8;
    int idx = mo->Match(mi, token, opts);
    _test(strcmp(mo->Term(idx)->term(),"juniper") == 0);

    {
        // This test would loop in v.2.2.2
        TestQuery q("(word,");
        _test(q._qparser.ParseError());
    }

    {
        // Test to trigger ticket #5734 Dev Data Search
        std::string
            doc("A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit."
                "A simple document with an extremelylongwordhit in the middle of it that is"
                "long enough to allow the error to be triggered extremelylongwordhit.");
        TestQuery q("OR(OR(extremelylongwordhits,extremelylongwordhit,extremelylongwordhits,"
                    "extremelylongwordhit,extremelylongwordhits,extremelylongwordhit,"
                    "extremelylongwordhit,extremelylongwordhits,extremelylongwordhit,"
                    "extremelylongwordhit,extremelylongwordhits,extremelylongwordhit,"
                    "extremelylongwordhit,extremelylongwordhits,extremelylongwordhit,"
                    "extremelylongwordhit,extremelylongwordhits,extremelylongwordhit,"
                    "extremelylongwordhit))");
        QueryHandle& qh1(q._qhandle);
        juniper::Result res1(*juniper::TestConfig, qh1,
                             doc.c_str(), doc.size(), 0);
        juniper::Summary* sum = res1.GetTeaser(NULL);
        std::string s(sum->Text());
        _test_equal(s,
                    "A simple document with an <b>extremelylongwordhit</b> in the middle"
                    " of it that islong enough to allow...triggered "
                    "<b>extremelylongwordhit</b>.A simple document with an "
                    "<b>extremelylongwordhit</b> in the middle of it that islong enough to allow...");
    }
}

/**
 * Test matching in annotated buffers
 */
void MatchObjectTest::testMatchAnnotated() {
  const char *doc = "A big and ugly teaser about "
    "\xEF\xBF\xB9"
    "buying"
    "\xEF\xBF\xBA"
    "buy"
    "\xEF\xBF\xBB"
    " stuff";
  TestQuery q("AND(big,buy)");
  QueryHandle &qh1(q._qhandle);
  juniper::Result res1(*juniper::TestConfig, qh1,
                       doc, strlen(doc), 0);
  juniper::Summary *sum = res1.GetTeaser(NULL);
  std::string s(sum->Text());

  _test_equal(s,
              "A <b>big</b> and ugly teaser about <b>"
              "\xEF\xBF\xB9"
              "buying"
              "\xEF\xBF\xBA"
              "buy"
              "\xEF\xBF\xBB"
              "</b> stuff");
}


/**
 * Test of the the expansion based (langid) constructor
 */
void MatchObjectTest::testLangid()
{
    FakeRewriter frew;
    juniper::_Juniper->AddRewriter("exp", &frew, true, false);
    juniper::_Juniper->AddRewriter("red", &frew, false, true);
    juniper::_Juniper->AddRewriter("expred", &frew, true, true);

    TestQuery q("AND(exp:a,red:b1,expred:c)");
    QueryHandle& qh(q._qhandle);

    {
        {
            std::string stk;
            qh.MatchObj(0)->Query()->Dump(stk);
            _test_equal(stk,
                        "Node<a:3>[Node<a:4>[a0:100,a1:100,a2:100,a3:100],"
                        "b1:100,Node<a:4>[c0:100,c1:100,c2:100,c3:100]]");
        }

        std::string doc("see if we can match b or c somewhere in this a3 doc. "
                        "Note that we should not match b1 or c1 or a somewhere..");
        juniper::Result res(*juniper::TestConfig, qh, doc.c_str(), doc.size(),0);

        juniper::Summary* sum = res.GetTeaser(NULL);
        std::string s(sum->Text());
        _test_equal(s,
                    "see if we can match <b>b</b> or <b>c</b> somewhere in this"
                    " <b>a3</b> doc. Note that we should not match b1 or c1 or a somewhere..");
    }

    {
        // Do another test with the same query handle (testing reuse of qh with rewriters)
        std::string doc("Try to run this on another doc just to see if b or c still can be"
                        " matched with the same query handle");
        juniper::Result res(*juniper::TestConfig, qh,
                            doc.c_str(), doc.size(), 0);

        juniper::Summary* sum = res.GetTeaser(NULL);
        std::string s(sum->Text());
        _test_equal(s,
                    "Try to run this on another doc just to see if <b>b</b> or <b>c</b>"
                    " still can be matched with the same query handle");
    }
    juniper::_Juniper->FlushRewriters();
}


/**
 * Test of the the expansion based (langid) constructor in
 * combination with a normal search
 */
void MatchObjectTest::testCombined()
{
    FakeRewriter frew;
    juniper::_Juniper->AddRewriter("exp", &frew, true, false);
    juniper::_Juniper->AddRewriter("red", &frew, false, true);

    TestQuery q("OR(OR(AND(exp:a,b)))");
    QueryHandle& qh(q._qhandle);

    {
        std::string doc("see if we can match a3 or c somewhere in this b doc. "
                        "Note that we should not match b1 or c1 or a somewhere..");
        juniper::Result res(*juniper::TestConfig, qh, doc.c_str(), doc.size(), 0);

        juniper::Summary* sum = res.GetTeaser(NULL);
        std::string s(sum->Text());
        _test_equal(s,
                    "see if we can match <b>a3</b> or c somewhere in this <b>b</b> doc."
                    " Note that we should not match b1 or c1 or a somewhere..");
    }
    juniper::_Juniper->FlushRewriters();
}

/** Test parameter input via options
 */

void MatchObjectTest::testParams()
{
    {
        TestQuery q("AND(a,b)", "near.1");
        QueryHandle& qh = q._qhandle;
        std::string stk;
        qh.MatchObj(0)->Query()->Dump(stk);
        // Expect l:1 == limit:1 v: Validity check of keywords needed, c: Completeness req'ed
        _test_equal(stk, "Node<a:2,l:1,v,c>[a:100,b:100]");
    }

    {
        TestQuery q("AND(a,b)", "onear.1");
        QueryHandle& qh = q._qhandle;
        std::string stk;
        qh.MatchObj(0)->Query()->Dump(stk);
        // Expect l:1 == limit:1 o: ordered, v: Validity check of keywords needed,
        //        c: Completeness req'ed
        _test_equal(stk, "Node<a:2,o,l:1,v,c>[a:100,b:100]");
    }

    {
        TestQuery q("AND(a,b)", "within.1");
        QueryHandle& qh = q._qhandle;
        std::string stk;
        qh.MatchObj(0)->Query()->Dump(stk);
        // Expect l:1 == limit:1 o: ordered, v: Validity check of keywords needed,
        //        c: Completeness req'ed
        _test_equal(stk, "Node<a:2,o,l:1,v,c>[a:100,b:100]");
    }

    {
        // Check that query option replaces orig.query
        TestQuery q("OR(a,b)", "query.ONEAR/1(a,b)");
        QueryHandle& qh = q._qhandle;
        std::string stk;
        qh.MatchObj(0)->Query()->Dump(stk);
        // Expect l:1 == limit:1 o: ordered, v: Validity check of keywords needed,
        //        c: Completeness req'ed
        _test_equal(stk, "Node<a:2,o,l:1,v,c>[a:100,b:100]");
    }

    {
        // Check that query option replaces orig.query, and check that ANY works..
        TestQuery q("OR(a,b,c)", "query.ANY(a,b)");
        QueryHandle& qh = q._qhandle;
        std::string stk;
        qh.MatchObj(0)->Query()->Dump(stk);
        // Expect l:1 == limit:1 o: ordered, v: Validity check of keywords needed,
        //        c: Completeness req'ed
        _test_equal(stk, "Node<a:2>[a:100,b:100]");
    }
}


/*************************************************************************
 *                      Test administration methods
 *************************************************************************/

/**
 * Set up common stuff for all test methods.
 * This method is called immediately before each test method is called
 */
bool MatchObjectTest::setUp() {
    return true;
}

/**
   test_methods_["testCombined"] =
   &MatchObjectTest::testCombined;
   * Tear down common stuff for all test methods.
   * This method is called immediately after each test method is called
   */
void MatchObjectTest::tearDown() {
}

/**
 * Build up a map with all test methods
 */
void MatchObjectTest::init() {
    test_methods_["testTerm"] =
        &MatchObjectTest::testTerm;
    test_methods_["testMatch"] =
        &MatchObjectTest::testMatch;
    test_methods_["testMatchAnnotated"] =
      &MatchObjectTest::testMatchAnnotated;
    test_methods_["testLangid"] =
        &MatchObjectTest::testLangid;
    test_methods_["testCombined"] =
        &MatchObjectTest::testCombined;
    test_methods_["testParams"] =
        &MatchObjectTest::testParams;
}

/*************************************************************************
 *                         main entry points
 *************************************************************************/


void MatchObjectTest::Run(MethodContainer::iterator &itr) {
    try {
        if (setUp()) {
            (this->*itr->second)();
            tearDown();
        }
    } catch (...) {
        _fail("Got unknown exception in test method " + itr->first);
    }
}

void MatchObjectTest::Run(const char* method) {
    MethodContainer::iterator pos(test_methods_.find(method));
    if (pos != test_methods_.end()) {
        Run(pos);
    } else {
        std::cerr << "ERROR: No test method named \""
                  << method << "\"" << std::endl;
        _fail("No such method");
    }
}

void MatchObjectTest::Run() {
    for (MethodContainer::iterator itr(test_methods_.begin());
         itr != test_methods_.end();
         ++itr)
        Run(itr);
}

/*
 * Parse runtime arguments before running.
 * If the -m METHOD parameter is given, run only that method
 */
void MatchObjectTest::Run(int argc, char* argv[]) {
    for (int i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "-m") == 0 && argc > i + 1) {
            Run(argv[++i]);
            return;
        }
    }
    Run();
}