xapian-core  1.4.27
api_db.cc
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002 Ananova Ltd
6  * Copyright 2002-2023 Olly Betts
7  * Copyright 2006,2007,2008,2009 Lemur Consulting Ltd
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24 
25 #include <config.h>
26 
27 #include "api_db.h"
28 
29 #include <algorithm>
30 #include <fstream>
31 #include <map>
32 #include <string>
33 #include <vector>
34 #include "safenetdb.h" // For gai_strerror().
35 #include "safesysstat.h" // For mkdir().
36 #include "safeunistd.h" // For sleep().
37 
38 #include <xapian.h>
39 
40 #include "backendmanager.h"
41 #include "testsuite.h"
42 #include "testutils.h"
43 #include "unixcmds.h"
44 
45 #include "apitest.h"
46 
47 using namespace std;
48 
49 static Xapian::Query
50 query(const string &t)
51 {
52  return Xapian::Query(Xapian::Stem("english")(t));
53 }
54 
55 // #######################################################################
56 // # Tests start here
57 
58 // tests Xapian::Database::get_termfreq() and Xapian::Database::term_exists()
59 DEFINE_TESTCASE(termstats, backend) {
60  Xapian::Database db(get_database("apitest_simpledata"));
61 
62  TEST(!db.term_exists("corn"));
63  TEST_EQUAL(db.get_termfreq("corn"), 0);
64  TEST(db.term_exists("banana"));
65  TEST_EQUAL(db.get_termfreq("banana"), 1);
66  TEST(db.term_exists("paragraph"));
67  TEST_EQUAL(db.get_termfreq("paragraph"), 5);
68 }
69 
70 // Check that stub databases work.
71 DEFINE_TESTCASE(stubdb1, check && path) {
72  mkdir(".stub", 0755);
73  const char * dbpath = ".stub/stubdb1";
74  ofstream out(dbpath);
75  TEST(out.is_open());
76  out << "auto ../" << get_database_path("apitest_simpledata") << '\n';
77  out.close();
78 
79  {
81  Xapian::Enquire enquire(db);
82  enquire.set_query(Xapian::Query("word"));
83  enquire.get_mset(0, 10);
84  }
85  {
86  Xapian::Database db(dbpath);
87  Xapian::Enquire enquire(db);
88  enquire.set_query(Xapian::Query("word"));
89  enquire.get_mset(0, 10);
90  }
91 
93 }
94 
95 // Check that stub databases work remotely.
96 DEFINE_TESTCASE(stubdb2, path) {
97  mkdir(".stub", 0755);
98  const char * dbpath = ".stub/stubdb2";
99  ofstream out(dbpath);
100  TEST(out.is_open());
101  out << "remote :" << BackendManager::get_xapian_progsrv_command()
102  << ' ' << get_database_path("apitest_simpledata") << '\n';
103  out.close();
104 
105  try {
107  Xapian::Enquire enquire(db);
108  enquire.set_query(Xapian::Query("word"));
109  enquire.get_mset(0, 10);
111 #ifdef XAPIAN_HAS_REMOTE_BACKEND
112  throw;
113 #endif
114  }
115 
116  try {
117  Xapian::Database db(dbpath);
118  Xapian::Enquire enquire(db);
119  enquire.set_query(Xapian::Query("word"));
120  enquire.get_mset(0, 10);
122 #ifdef XAPIAN_HAS_REMOTE_BACKEND
123  throw;
124 #endif
125  }
126 
127  out.open(dbpath);
128  TEST(out.is_open());
129  out << "remote\n";
130  out.close();
131 
132  // Quietly ignored prior to 1.4.1.
135  );
136 
137  // Quietly ignored prior to 1.4.1.
140  );
141 
142 #ifdef XAPIAN_HAS_REMOTE_BACKEND
143 # define EXPECTED_EXCEPTION Xapian::DatabaseOpeningError
144 #else
145 # define EXPECTED_EXCEPTION Xapian::FeatureUnavailableError
146 #endif
147 
148  out.open(dbpath);
149  TEST(out.is_open());
150  out << "remote foo\n";
151  out.close();
152 
153  // Quietly ignored prior to 1.4.1.
156  );
157 
158  // Quietly ignored prior to 1.4.1.
161  );
162 
163 #ifdef XAPIAN_HAS_REMOTE_BACKEND
164  out.open(dbpath);
165  TEST(out.is_open());
166  out << "remote [::1]:65535\n";
167  out.close();
168 
169  try {
171  } catch (const Xapian::NetworkError& e) {
172  // 1.4.0 threw (on Linux) the confusing message:
173  // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname)
174  // 1.4.1 throws (because we don't actually support IPv6 yet) on Linux (EAI_ADDRFAMILY):
175  // NetworkError: Couldn't resolve host ::1 (context: remote:tcp(::1:65535)) (nodename nor servname provided, or not known)
176  // or on macOS (EAI_NONAME):
177  // NetworkError: Couldn't resolve host ::1 (context: remote:tcp(::1:65535)) (Address family for hostname not supported)
178  //
179  // But NetBSD seems to resolve ::1 to an IPv4 address and then tries
180  // to connect to it (which hopefully fails), so just test the message
181  // doesn't match the bad 1.4.0 result.
182  TEST(e.get_msg().find("host [") == string::npos);
183  }
184 
185  try {
187  } catch (const Xapian::NetworkError& e) {
188  // 1.4.0 threw (Linux):
189  // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname)
190  // 1.4.1 throws (because we don't actually support IPv6 yet) on Linux (EAI_ADDRFAMILY):
191  // NetworkError: Couldn't resolve host ::1 (context: remote:tcp(::1:65535)) (nodename nor servname provided, or not known)
192  // or on macOS (EAI_NONAME):
193  // NetworkError: Couldn't resolve host ::1 (context: remote:tcp(::1:65535)) (Address family for hostname not supported)
194  // So we test the message instead of the error string for portability.
195  //
196  // But NetBSD seems to resolve ::1 to an IPv4 address and then tries
197  // to connect to it (which hopefully fails), so just test the message
198  // doesn't match the bad 1.4.0 result.
199  TEST(e.get_msg().find("host [") == string::npos);
200  }
201 #endif
202 
203  out.open(dbpath);
204  TEST(out.is_open());
205  // Invalid - the port number is required.
206  out << "remote [::1]\n";
207  out.close();
208 
209  // 1.4.0 threw:
210  // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname)
213  );
214 
215  // 1.4.0 threw:
216  // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname)
219  );
220 }
221 
222 // Regression test - bad entries were ignored after a good entry prior to 1.0.8.
223 DEFINE_TESTCASE(stubdb3, path) {
224  mkdir(".stub", 0755);
225  const char * dbpath = ".stub/stubdb3";
226  ofstream out(dbpath);
227  TEST(out.is_open());
228  out << "auto ../" << get_database_path("apitest_simpledata") << "\n"
229  "bad line here\n";
230  out.close();
231 
234 
236  Xapian::Database db(dbpath));
237 }
238 
239 // Test a stub database with just a bad entry.
240 DEFINE_TESTCASE(stubdb4, !backend) {
241  mkdir(".stub", 0755);
242  const char * dbpath = ".stub/stubdb4";
243  ofstream out(dbpath);
244  TEST(out.is_open());
245  out << "bad line here\n";
246  out.close();
247 
250 
252  Xapian::Database db(dbpath));
253 }
254 
255 // Test a stub database with a bad entry with no spaces (prior to 1.1.0 this
256 // was deliberately allowed, though not documented.
257 DEFINE_TESTCASE(stubdb5, path) {
258  mkdir(".stub", 0755);
259  const char * dbpath = ".stub/stubdb5";
260  ofstream out(dbpath);
261  TEST(out.is_open());
262  out << "bad\n"
263  "auto ../" << get_database_path("apitest_simpledata") << '\n';
264  out.close();
265 
268 
270  Xapian::Database db(dbpath));
271 }
272 
273 // Test a stub database with an inmemory database (new feature in 1.1.0).
274 DEFINE_TESTCASE(stubdb6, inmemory) {
275  mkdir(".stub", 0755);
276  const char * dbpath = ".stub/stubdb6";
277  ofstream out(dbpath);
278  TEST(out.is_open());
279  out << "inmemory\n";
280  out.close();
281 
282  // Read-only tests:
283  {
285  TEST_EQUAL(db.get_doccount(), 0);
286  Xapian::Enquire enquire(db);
287  enquire.set_query(Xapian::Query("word"));
288  Xapian::MSet mset = enquire.get_mset(0, 10);
289  TEST(mset.empty());
290  }
291  {
292  Xapian::Database db(dbpath);
293  TEST_EQUAL(db.get_doccount(), 0);
294  Xapian::Enquire enquire(db);
295  enquire.set_query(Xapian::Query("word"));
296  Xapian::MSet mset = enquire.get_mset(0, 10);
297  TEST(mset.empty());
298  }
299 
300  // Writable tests:
301  {
302  Xapian::WritableDatabase db(dbpath,
304  TEST_EQUAL(db.get_doccount(), 0);
306  TEST_EQUAL(db.get_doccount(), 1);
307  }
308  {
309  Xapian::WritableDatabase db(dbpath,
311  TEST_EQUAL(db.get_doccount(), 0);
313  TEST_EQUAL(db.get_doccount(), 1);
314  }
315 }
316 
318 // Regression test - in 1.4.3 and earlier this threw
319 // Xapian::DatabaseError.
320 DEFINE_TESTCASE(stubdb8, inmemory) {
321  mkdir(".stub", 0755);
322  const char * dbpath = ".stub/stubdb8";
323  ofstream out(dbpath);
324  TEST(out.is_open());
325  out << "inmemory\n";
326  out.close();
327 
328  try {
329  Xapian::Database::check(dbpath);
330  FAIL_TEST("Managed to check inmemory stub");
331  } catch (const Xapian::UnimplementedError& e) {
332  // Check the message is appropriate.
334  "InMemory database checking not implemented");
335  }
336 }
337 
338 #if 0 // the "force error" mechanism is no longer in place...
339 class MyErrorHandler : public Xapian::ErrorHandler {
340  public:
341  int count;
342 
343  bool handle_error(Xapian::Error & error) {
344  ++count;
345  tout << "Error handling caught: " << error.get_description()
346  << ", count is now " << count << "\n";
347  return true;
348  }
349 
350  MyErrorHandler() : count (0) {}
351 };
352 
353 // tests error handler in multimatch().
354 DEFINE_TESTCASE(multierrhandler1, backend) {
355  MyErrorHandler myhandler;
356 
357  Xapian::Database mydb2(get_database("apitest_simpledata"));
358  Xapian::Database mydb3(get_database("apitest_simpledata2"));
359  int errcount = 1;
360  for (int testcount = 0; testcount < 14; testcount ++) {
361  tout << "testcount=" << testcount << "\n";
362  Xapian::Database mydb4(get_database("-e", "apitest_termorder"));
363  Xapian::Database mydb5(get_network_database("apitest_termorder", 1));
364  Xapian::Database mydb6(get_database("-e2", "apitest_termorder"));
365  Xapian::Database mydb7(get_database("-e3", "apitest_simpledata"));
366 
367  Xapian::Database dbs;
368  switch (testcount) {
369  case 0:
370  dbs.add_database(mydb2);
371  dbs.add_database(mydb3);
372  dbs.add_database(mydb4);
373  break;
374  case 1:
375  dbs.add_database(mydb4);
376  dbs.add_database(mydb2);
377  dbs.add_database(mydb3);
378  break;
379  case 2:
380  dbs.add_database(mydb3);
381  dbs.add_database(mydb4);
382  dbs.add_database(mydb2);
383  break;
384  case 3:
385  dbs.add_database(mydb2);
386  dbs.add_database(mydb3);
387  dbs.add_database(mydb5);
388  sleep(1);
389  break;
390  case 4:
391  dbs.add_database(mydb5);
392  dbs.add_database(mydb2);
393  dbs.add_database(mydb3);
394  sleep(1);
395  break;
396  case 5:
397  dbs.add_database(mydb3);
398  dbs.add_database(mydb5);
399  dbs.add_database(mydb2);
400  sleep(1);
401  break;
402  case 6:
403  dbs.add_database(mydb2);
404  dbs.add_database(mydb3);
405  dbs.add_database(mydb6);
406  break;
407  case 7:
408  dbs.add_database(mydb6);
409  dbs.add_database(mydb2);
410  dbs.add_database(mydb3);
411  break;
412  case 8:
413  dbs.add_database(mydb3);
414  dbs.add_database(mydb6);
415  dbs.add_database(mydb2);
416  break;
417  case 9:
418  dbs.add_database(mydb2);
419  dbs.add_database(mydb3);
420  dbs.add_database(mydb7);
421  break;
422  case 10:
423  dbs.add_database(mydb7);
424  dbs.add_database(mydb2);
425  dbs.add_database(mydb3);
426  break;
427  case 11:
428  dbs.add_database(mydb3);
429  dbs.add_database(mydb7);
430  dbs.add_database(mydb2);
431  break;
432  case 12:
433  dbs.add_database(mydb2);
434  dbs.add_database(mydb6);
435  dbs.add_database(mydb7);
436  break;
437  case 13:
438  dbs.add_database(mydb2);
439  dbs.add_database(mydb7);
440  dbs.add_database(mydb6);
441  break;
442  }
443  tout << "db=" << dbs << "\n";
444  Xapian::Enquire enquire(dbs, &myhandler);
445 
446  // make a query
447  Xapian::Query myquery = query(Xapian::Query::OP_OR, "inmemory", "word");
449  enquire.set_query(myquery);
450 
451  tout << "query=" << myquery << "\n";
452  // retrieve the top ten results
453  Xapian::MSet mymset = enquire.get_mset(0, 10);
454 
455  switch (testcount) {
456  case 0: case 3: case 6: case 9:
457  mset_expect_order(mymset, 2, 4, 10);
458  break;
459  case 1: case 4: case 7: case 10:
460  mset_expect_order(mymset, 3, 5, 11);
461  break;
462  case 2: case 5: case 8: case 11:
463  mset_expect_order(mymset, 1, 6, 12);
464  break;
465  case 12:
466  case 13:
467  mset_expect_order(mymset, 4, 10);
468  errcount += 1;
469  break;
470  }
471  TEST_EQUAL(myhandler.count, errcount);
472  errcount += 1;
473  }
474 }
475 #endif
476 
478  string needle;
479  public:
480  explicit GrepMatchDecider(const string& needle_)
481  : needle(needle_) {}
482 
483  bool operator()(const Xapian::Document& doc) const override {
484  // Note that this is not recommended usage of get_data()
485  return doc.get_data().find(needle) != string::npos;
486  }
487 };
488 
489 // Test Xapian::MatchDecider functor.
490 DEFINE_TESTCASE(matchdecider1, backend && !remote) {
491  Xapian::Database db(get_database("apitest_simpledata"));
492  Xapian::Enquire enquire(db);
493  enquire.set_query(Xapian::Query("this"));
494 
495  GrepMatchDecider myfunctor("This is");
496 
497  Xapian::MSet mymset = enquire.get_mset(0, 100, 0, &myfunctor);
498 
499  vector<bool> docid_checked(db.get_lastdocid());
500 
501  // Check that we get the expected number of matches, and that they
502  // satisfy the condition.
503  Xapian::MSetIterator i = mymset.begin();
504  TEST(i != mymset.end());
505  TEST_EQUAL(mymset.size(), 3);
506  TEST_EQUAL(mymset.get_matches_lower_bound(), 3);
507  TEST_EQUAL(mymset.get_matches_upper_bound(), 3);
508  TEST_EQUAL(mymset.get_matches_estimated(), 3);
512  for ( ; i != mymset.end(); ++i) {
513  const Xapian::Document doc(i.get_document());
514  TEST(myfunctor(doc));
515  docid_checked[*i] = true;
516  }
517 
518  // Check that there are some documents which aren't accepted by the match
519  // decider.
520  mymset = enquire.get_mset(0, 100);
521  TEST(mymset.size() > 3);
522 
523  // Check that the bounds are appropriate even if we don't ask for any
524  // actual matches.
525  mymset = enquire.get_mset(0, 0, 0, &myfunctor);
526  TEST_EQUAL(mymset.size(), 0);
527  TEST_EQUAL(mymset.get_matches_lower_bound(), 0);
528  TEST_EQUAL(mymset.get_matches_upper_bound(), 6);
529  TEST_REL(mymset.get_matches_estimated(),>,0);
530  TEST_REL(mymset.get_matches_estimated(),<=,6);
531  TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 0);
532  TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 6);
533  TEST_REL(mymset.get_uncollapsed_matches_estimated(),>,0);
534  TEST_REL(mymset.get_uncollapsed_matches_estimated(),<=,6);
535 
536  // Check that the bounds are appropriate if we ask for only one hit.
537  // (Regression test - until SVN 10256, we didn't reduce the lower_bound
538  // appropriately, and returned 6 here.)
539  mymset = enquire.get_mset(0, 1, 0, &myfunctor);
540  TEST_EQUAL(mymset.size(), 1);
541  TEST_REL(mymset.get_matches_lower_bound(),>=,1);
542  TEST_REL(mymset.get_matches_lower_bound(),<=,3);
543  TEST_REL(mymset.get_matches_upper_bound(),>=,3);
544  TEST_REL(mymset.get_matches_upper_bound(),<=,6);
545  TEST_REL(mymset.get_matches_estimated(),>,0);
546  TEST_REL(mymset.get_matches_estimated(),<=,6);
547  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,1);
548  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),<=,3);
549  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),>=,3);
550  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),<=,6);
551  TEST_REL(mymset.get_uncollapsed_matches_estimated(),>,0);
552  TEST_REL(mymset.get_uncollapsed_matches_estimated(),<=,6);
553 
554  // Check that the other documents don't satisfy the condition.
555  for (Xapian::docid did = 1; did < docid_checked.size(); ++did) {
556  if (!docid_checked[did]) {
557  TEST(!myfunctor(db.get_document(did)));
558  }
559  }
560 
561  // Check that the bounds are appropriate if a collapse key is used.
562  // Use a value which is never set so we don't actually discard anything.
563  enquire.set_collapse_key(99);
564  mymset = enquire.get_mset(0, 1, 0, &myfunctor);
565  TEST_EQUAL(mymset.size(), 1);
566  TEST_REL(mymset.get_matches_lower_bound(),>=,1);
567  TEST_REL(mymset.get_matches_lower_bound(),<=,3);
568  TEST_REL(mymset.get_matches_upper_bound(),>=,3);
569  TEST_REL(mymset.get_matches_upper_bound(),<=,6);
570  TEST_REL(mymset.get_matches_estimated(),>,0);
571  TEST_REL(mymset.get_matches_estimated(),<=,6);
572  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,1);
573  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),<=,3);
574  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),>=,3);
575  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),<=,6);
576  TEST_REL(mymset.get_uncollapsed_matches_estimated(),>,0);
577  TEST_REL(mymset.get_uncollapsed_matches_estimated(),<=,6);
578 
579  // Check that the bounds are appropriate if a percentage cutoff is in
580  // use. Set a 1% threshold so we don't actually discard anything.
582  enquire.set_cutoff(1);
583  mymset = enquire.get_mset(0, 1, 0, &myfunctor);
584  TEST_EQUAL(mymset.size(), 1);
585  TEST_REL(mymset.get_matches_lower_bound(),>=,1);
586  TEST_REL(mymset.get_matches_lower_bound(),<=,3);
587  TEST_REL(mymset.get_matches_upper_bound(),>=,3);
588  TEST_REL(mymset.get_matches_upper_bound(),<=,6);
589  TEST_REL(mymset.get_matches_estimated(),>,0);
590  TEST_REL(mymset.get_matches_estimated(),<=,6);
591  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,1);
592  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),<=,3);
593  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),>=,3);
594  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),<=,6);
595  TEST_REL(mymset.get_uncollapsed_matches_estimated(),>,0);
596  TEST_REL(mymset.get_uncollapsed_matches_estimated(),<=,6);
597 
598  // And now with both a collapse key and percentage cutoff.
599  enquire.set_collapse_key(99);
600  mymset = enquire.get_mset(0, 1, 0, &myfunctor);
601  TEST_EQUAL(mymset.size(), 1);
602  TEST_REL(mymset.get_matches_lower_bound(),>=,1);
603  TEST_REL(mymset.get_matches_lower_bound(),<=,3);
604  TEST_REL(mymset.get_matches_upper_bound(),>=,3);
605  TEST_REL(mymset.get_matches_upper_bound(),<=,6);
606  TEST_REL(mymset.get_matches_estimated(),>,0);
607  TEST_REL(mymset.get_matches_estimated(),<=,6);
608  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,1);
609  TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),<=,3);
610  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),>=,3);
611  TEST_REL(mymset.get_uncollapsed_matches_upper_bound(),<=,6);
612  TEST_REL(mymset.get_uncollapsed_matches_estimated(),>,0);
613  TEST_REL(mymset.get_uncollapsed_matches_estimated(),<=,6);
614 }
615 
616 // Test Xapian::MatchDecider functor used as a match spy.
617 DEFINE_TESTCASE(matchdecider2, backend && !remote) {
618  Xapian::Database db(get_database("apitest_simpledata"));
619  Xapian::Enquire enquire(db);
620  enquire.set_query(Xapian::Query("this"));
621 
622  GrepMatchDecider myfunctor("This is");
623 
624  Xapian::MSet mymset = enquire.get_mset(0, 100, 0, NULL, &myfunctor);
625 
626  vector<bool> docid_checked(db.get_lastdocid());
627 
628  // Check that we get the expected number of matches, and that they
629  // satisfy the condition.
630  Xapian::MSetIterator i = mymset.begin();
631  TEST(i != mymset.end());
632  TEST_EQUAL(mymset.size(), 3);
633  for ( ; i != mymset.end(); ++i) {
634  const Xapian::Document doc(i.get_document());
635  TEST(myfunctor(doc));
636  docid_checked[*i] = true;
637  }
638 
639  // Check that the other documents don't satisfy the condition.
640  for (Xapian::docid did = 1; did < docid_checked.size(); ++did) {
641  if (!docid_checked[did]) {
642  TEST(!myfunctor(db.get_document(did)));
643  }
644  }
645 }
646 
647 // Regression test for lower bound using functor, sorting and collapsing.
648 DEFINE_TESTCASE(matchdecider3, backend && !remote) {
649  Xapian::Database db(get_database("etext"));
650  Xapian::Enquire enquire(db);
651  enquire.set_query(Xapian::Query(""));
652  enquire.set_collapse_key(12);
653  enquire.set_sort_by_value(11, true);
654 
655  GrepMatchDecider myfunctor("We produce");
656 
657  Xapian::MSet mset1 = enquire.get_mset(0, 2, 0, NULL, &myfunctor);
658  Xapian::MSet mset2 = enquire.get_mset(0, 1000, 0, NULL, &myfunctor);
659 
660  // mset2 should contain all the hits, so the statistics should be exact.
661  TEST_EQUAL(mset2.get_matches_estimated(), mset2.size());
664 
667 
668  // Check that the lower bound in mset1 is not greater than the known
669  // number of hits. This failed until revision 10811.
670  TEST_REL(mset1.get_matches_lower_bound(),<=,mset2.size());
671 
672  // Check that the bounds for mset1 make sense.
675  TEST_REL(mset1.size(),<=,mset1.get_matches_upper_bound());
676 
679 
680  // The uncollapsed match would match all documents but the one the
681  // matchdecider rejects.
686 }
687 
688 // tests that mset iterators on msets compare correctly.
689 DEFINE_TESTCASE(msetiterator1, backend) {
690  Xapian::Enquire enquire(get_database("apitest_simpledata"));
691  enquire.set_query(Xapian::Query("this"));
692  Xapian::MSet mymset = enquire.get_mset(0, 2);
693 
695  j = mymset.begin();
696  Xapian::MSetIterator k = mymset.end();
699  Xapian::MSetIterator n = mymset.begin();
700  Xapian::MSetIterator o = mymset.begin();
701  TEST_NOT_EQUAL(j, k);
702  TEST_NOT_EQUAL(l, m);
703  TEST_EQUAL(k, m);
704  TEST_EQUAL(j, l);
705  TEST_EQUAL(j, j);
706  TEST_EQUAL(k, k);
707 
708  k = j;
709  TEST_EQUAL(j, k);
710  TEST_EQUAL(j, o);
711  k++;
712  TEST_NOT_EQUAL(j, k);
713  TEST_NOT_EQUAL(k, l);
714  TEST_NOT_EQUAL(k, m);
715  TEST_NOT_EQUAL(k, o);
716  o++;
717  TEST_EQUAL(k, o);
718  k++;
719  TEST_NOT_EQUAL(j, k);
720  TEST_NOT_EQUAL(k, l);
721  TEST_EQUAL(k, m);
722  TEST_EQUAL(n, l);
723 
724  n = m;
725  TEST_NOT_EQUAL(n, l);
726  TEST_EQUAL(n, m);
727  TEST_NOT_EQUAL(n, mymset.begin());
728  TEST_EQUAL(n, mymset.end());
729 }
730 
731 // tests that mset iterators on empty msets compare equal.
732 DEFINE_TESTCASE(msetiterator2, backend) {
733  Xapian::Enquire enquire(get_database("apitest_simpledata"));
734  enquire.set_query(Xapian::Query("this"));
735  Xapian::MSet mymset = enquire.get_mset(0, 0);
736 
737  Xapian::MSetIterator j = mymset.begin();
738  Xapian::MSetIterator k = mymset.end();
741  TEST_EQUAL(j, k);
742  TEST_EQUAL(l, m);
743  TEST_EQUAL(k, m);
744  TEST_EQUAL(j, l);
745  TEST_EQUAL(j, j);
746  TEST_EQUAL(k, k);
747 }
748 
749 // tests that begin().get_document() works when first != 0
750 DEFINE_TESTCASE(msetiterator3, backend) {
751  Xapian::Database mydb(get_database("apitest_simpledata"));
752  Xapian::Enquire enquire(mydb);
753  enquire.set_query(Xapian::Query("this"));
754 
755  Xapian::MSet mymset = enquire.get_mset(2, 10);
756 
757  TEST(!mymset.empty());
758  Xapian::Document doc(mymset.begin().get_document());
759  TEST(!doc.get_data().empty());
760 }
761 
762 // tests that eset iterators on empty esets compare equal.
763 DEFINE_TESTCASE(esetiterator1, backend) {
764  Xapian::Enquire enquire(get_database("apitest_simpledata"));
765  enquire.set_query(Xapian::Query("this"));
766 
767  Xapian::MSet mymset = enquire.get_mset(0, 10);
768  TEST(mymset.size() >= 2);
769 
770  Xapian::RSet myrset;
771  Xapian::MSetIterator i = mymset.begin();
772  myrset.add_document(*i);
773  myrset.add_document(*(++i));
774 
775  Xapian::ESet myeset = enquire.get_eset(2, myrset);
777  j = myeset.begin();
778  Xapian::ESetIterator k = myeset.end();
781  Xapian::ESetIterator n = myeset.begin();
782 
783  TEST_NOT_EQUAL(j, k);
784  TEST_NOT_EQUAL(l, m);
785  TEST_EQUAL(k, m);
786  TEST_EQUAL(j, l);
787  TEST_EQUAL(j, j);
788  TEST_EQUAL(k, k);
789 
790  k = j;
791  TEST_EQUAL(j, k);
792  k++;
793  TEST_NOT_EQUAL(j, k);
794  TEST_NOT_EQUAL(k, l);
795  TEST_NOT_EQUAL(k, m);
796  k++;
797  TEST_NOT_EQUAL(j, k);
798  TEST_NOT_EQUAL(k, l);
799  TEST_EQUAL(k, m);
800  TEST_EQUAL(n, l);
801 
802  n = m;
803  TEST_NOT_EQUAL(n, l);
804  TEST_EQUAL(n, m);
805  TEST_NOT_EQUAL(n, myeset.begin());
806  TEST_EQUAL(n, myeset.end());
807 }
808 
809 // tests that eset iterators on empty esets compare equal.
810 DEFINE_TESTCASE(esetiterator2, backend) {
811  Xapian::Enquire enquire(get_database("apitest_simpledata"));
812  enquire.set_query(Xapian::Query("this"));
813 
814  Xapian::MSet mymset = enquire.get_mset(0, 10);
815  TEST(mymset.size() >= 2);
816 
817  Xapian::RSet myrset;
818  Xapian::MSetIterator i = mymset.begin();
819  myrset.add_document(*i);
820  myrset.add_document(*(++i));
821 
822  Xapian::ESet myeset = enquire.get_eset(0, myrset);
823  Xapian::ESetIterator j = myeset.begin();
824  Xapian::ESetIterator k = myeset.end();
827  TEST_EQUAL(j, k);
828  TEST_EQUAL(l, m);
829  TEST_EQUAL(k, m);
830  TEST_EQUAL(j, l);
831  TEST_EQUAL(j, j);
832  TEST_EQUAL(k, k);
833 }
834 
835 // tests the collapse-on-key
836 DEFINE_TESTCASE(collapsekey1, backend) {
837  Xapian::Enquire enquire(get_database("apitest_simpledata"));
838  enquire.set_query(Xapian::Query("this"));
839 
840  Xapian::MSet mymset1 = enquire.get_mset(0, 100);
841  Xapian::doccount mymsize1 = mymset1.size();
842 
843  for (Xapian::valueno value_no = 1; value_no < 7; ++value_no) {
844  enquire.set_collapse_key(value_no);
845  Xapian::MSet mymset = enquire.get_mset(0, 100);
846 
847  TEST_AND_EXPLAIN(mymsize1 > mymset.size(),
848  "Had no fewer items when performing collapse: don't know whether it worked.");
849 
850  map<string, Xapian::docid> values;
851  Xapian::MSetIterator i = mymset.begin();
852  for ( ; i != mymset.end(); ++i) {
853  string value = i.get_document().get_value(value_no);
854  TEST(values[value] == 0 || value.empty());
855  values[value] = *i;
856  }
857  }
858 }
859 
860 // tests that collapse-on-key modifies the predicted bounds for the number of
861 // matches appropriately.
862 DEFINE_TESTCASE(collapsekey2, backend) {
863  SKIP_TEST("Don't have a suitable database currently");
864  // FIXME: this needs an appropriate database creating, but that's quite
865  // subtle to do it seems.
866  Xapian::Enquire enquire(get_database("apitest_simpledata2"));
867  enquire.set_query(Xapian::Query("this"));
868 
869  Xapian::MSet mset1 = enquire.get_mset(0, 1);
870 
871  // Test that if no duplicates are found, then the upper bound remains
872  // unchanged and the lower bound drops.
873  {
874  enquire.set_query(Xapian::Query("this"));
875  Xapian::valueno value_no = 3;
876  enquire.set_collapse_key(value_no);
877  Xapian::MSet mset = enquire.get_mset(0, 1);
878 
881  }
882 }
883 
884 // tests that collapse-on-key modifies the predicted bounds for the number of
885 // matches appropriately.
886 DEFINE_TESTCASE(collapsekey3, backend) {
887  Xapian::Enquire enquire(get_database("apitest_simpledata"));
888  enquire.set_query(Xapian::Query("this"));
889 
890  Xapian::MSet mymset1 = enquire.get_mset(0, 3);
891 
892  for (Xapian::valueno value_no = 1; value_no < 7; ++value_no) {
893  enquire.set_collapse_key(value_no);
894  Xapian::MSet mymset = enquire.get_mset(0, 3);
895 
897  "Lower bound was not lower when performing collapse: don't know whether it worked.");
899  "Upper bound was not lower when performing collapse: don't know whether it worked.");
900 
901  map<string, Xapian::docid> values;
902  Xapian::MSetIterator i = mymset.begin();
903  for ( ; i != mymset.end(); ++i) {
904  string value = i.get_document().get_value(value_no);
905  TEST(values[value] == 0 || value.empty());
906  values[value] = *i;
907  }
908  }
909 
910  // Test that if the collapse value is always empty, then the upper bound
911  // remains unchanged, and the lower bound is the same or lower (it can be
912  // lower because the matcher counts the number of documents with empty
913  // collapse keys, but may have rejected a document because its weight is
914  // too low for the proto-MSet before it even looks at its collapse key).
915  {
916  Xapian::valueno value_no = 1000;
917  enquire.set_collapse_key(value_no);
918  Xapian::MSet mymset = enquire.get_mset(0, 3);
919 
922 
923  map<string, Xapian::docid> values;
924  Xapian::MSetIterator i = mymset.begin();
925  for ( ; i != mymset.end(); ++i) {
926  string value = i.get_document().get_value(value_no);
927  TEST(values[value] == 0 || value.empty());
928  values[value] = *i;
929  }
930  }
931 }
932 
933 // tests that collapse-on-key modifies the predicted bounds for the number of
934 // matches appropriately even when no results are requested.
935 DEFINE_TESTCASE(collapsekey4, backend) {
936  Xapian::Enquire enquire(get_database("apitest_simpledata"));
937  enquire.set_query(Xapian::Query("this"));
938 
939  Xapian::MSet mymset1 = enquire.get_mset(0, 0);
940 
941  for (Xapian::valueno value_no = 1; value_no < 7; ++value_no) {
942  enquire.set_collapse_key(value_no);
943  Xapian::MSet mymset = enquire.get_mset(0, 0);
944 
946  "Lower bound was not 1 when performing collapse but not asking for any results.");
948  "Upper bound was changed when performing collapse but not asking for any results.");
949 
950  map<string, Xapian::docid> values;
951  Xapian::MSetIterator i = mymset.begin();
952  for ( ; i != mymset.end(); ++i) {
953  string value = i.get_document().get_value(value_no);
954  TEST(values[value] == 0 || value.empty());
955  values[value] = *i;
956  }
957  }
958 }
959 
960 // test for keepalives
961 DEFINE_TESTCASE(keepalive1, remote) {
962  Xapian::Database db(get_remote_database("apitest_simpledata", 5000));
963 
964  /* Test that keep-alives work */
965  for (int i = 0; i < 10; ++i) {
966  sleep(2);
967  db.keep_alive();
968  }
969  Xapian::Enquire enquire(db);
970  enquire.set_query(Xapian::Query("word"));
971  enquire.get_mset(0, 10);
972 
973  /* Test that things break without keepalives */
974  sleep(10);
975  enquire.set_query(Xapian::Query("word"));
976  /* Currently this can throw NetworkError or NetworkTimeoutError (which is
977  * a subclass of NetworkError).
978  */
980  enquire.get_mset(0, 10));
981 }
982 
983 // test that iterating through all terms in a database works.
984 DEFINE_TESTCASE(allterms1, backend) {
985  Xapian::Database db(get_database("apitest_allterms"));
987  TEST(ati != db.allterms_end());
988  TEST_EQUAL(*ati, "one");
989  TEST_EQUAL(ati.get_termfreq(), 1);
990 
991  Xapian::TermIterator ati2 = ati;
992 
993  ati++;
994  TEST(ati != db.allterms_end());
995  if (verbose) {
996  tout << "*ati = '" << *ati << "'\n";
997  tout << "*ati.length = '" << (*ati).length() << "'\n";
998  tout << "*ati == \"one\" = " << (*ati == "one") << "\n";
999  tout << "*ati[3] = " << ((*ati)[3]) << "\n";
1000  tout << "*ati = '" << *ati << "'\n";
1001  }
1002  TEST(*ati == "three");
1003  TEST(ati.get_termfreq() == 3);
1004 
1005 #if 0
1006  TEST(ati2 != db.allterms_end());
1007  TEST(*ati2 == "one");
1008  TEST(ati2.get_termfreq() == 1);
1009 #endif
1010 
1011  ++ati;
1012 #if 0
1013  ++ati2;
1014 #endif
1015  TEST(ati != db.allterms_end());
1016  TEST(*ati == "two");
1017  TEST(ati.get_termfreq() == 2);
1018 
1019 #if 0
1020  TEST(ati2 != db.allterms_end());
1021  TEST(*ati2 == "three");
1022  TEST(ati2.get_termfreq() == 3);
1023 #endif
1024 
1025  ati++;
1026  TEST(ati == db.allterms_end());
1027 }
1028 
1029 // test that iterating through all terms in two databases works.
1030 DEFINE_TESTCASE(allterms2, backend) {
1031  Xapian::Database db;
1032  db.add_database(get_database("apitest_allterms"));
1033  db.add_database(get_database("apitest_allterms2"));
1035 
1036  TEST(ati != db.allterms_end());
1037  TEST(*ati == "five");
1038  TEST(ati.get_termfreq() == 2);
1039  ati++;
1040 
1041  TEST(ati != db.allterms_end());
1042  TEST(*ati == "four");
1043  TEST(ati.get_termfreq() == 1);
1044 
1045  ati++;
1046  TEST(ati != db.allterms_end());
1047  TEST(*ati == "one");
1048  TEST(ati.get_termfreq() == 1);
1049 
1050  ++ati;
1051  TEST(ati != db.allterms_end());
1052  TEST(*ati == "six");
1053  TEST(ati.get_termfreq() == 3);
1054 
1055  ati++;
1056  TEST(ati != db.allterms_end());
1057  TEST(*ati == "three");
1058  TEST(ati.get_termfreq() == 3);
1059 
1060  ati++;
1061  TEST(ati != db.allterms_end());
1062  TEST(*ati == "two");
1063  TEST(ati.get_termfreq() == 2);
1064 
1065  ati++;
1066  TEST(ati == db.allterms_end());
1067 }
1068 
1069 // test that skip_to sets at_end (regression test)
1070 DEFINE_TESTCASE(allterms3, backend) {
1071  Xapian::Database db;
1072  db.add_database(get_database("apitest_allterms"));
1074 
1075  ati.skip_to(string("zzzzzz"));
1076  TEST(ati == db.allterms_end());
1077 }
1078 
1079 // test that next ignores extra entries due to long posting lists being
1080 // chunked (regression test for quartz)
1081 DEFINE_TESTCASE(allterms4, backend) {
1082  // apitest_allterms4 contains 682 documents each containing just the word
1083  // "foo". 682 was the magic number which started to cause Quartz problems.
1084  Xapian::Database db = get_database("apitest_allterms4");
1085 
1087  TEST(i != db.allterms_end());
1088  TEST(*i == "foo");
1089  TEST(i.get_termfreq() == 682);
1090  ++i;
1091  TEST(i == db.allterms_end());
1092 }
1093 
1094 // test that skip_to with an exact match sets the current term (regression test
1095 // for quartz)
1096 DEFINE_TESTCASE(allterms5, backend) {
1097  Xapian::Database db;
1098  db.add_database(get_database("apitest_allterms"));
1100  ati.skip_to("three");
1101  TEST(ati != db.allterms_end());
1102  TEST_EQUAL(*ati, "three");
1103 }
1104 
1105 // test allterms iterators with prefixes
1106 DEFINE_TESTCASE(allterms6, backend) {
1107  Xapian::Database db;
1108  db.add_database(get_database("apitest_allterms"));
1109  db.add_database(get_database("apitest_allterms2"));
1110 
1111  Xapian::TermIterator ati = db.allterms_begin("three");
1112  TEST(ati != db.allterms_end("three"));
1113  TEST_EQUAL(*ati, "three");
1114  ati.skip_to("three");
1115  TEST(ati != db.allterms_end("three"));
1116  TEST_EQUAL(*ati, "three");
1117  ati++;
1118  TEST(ati == db.allterms_end("three"));
1119 
1120  ati = db.allterms_begin("thre");
1121  TEST(ati != db.allterms_end("thre"));
1122  TEST_EQUAL(*ati, "three");
1123  ati.skip_to("three");
1124  TEST(ati != db.allterms_end("thre"));
1125  TEST_EQUAL(*ati, "three");
1126  ati++;
1127  TEST(ati == db.allterms_end("thre"));
1128 
1129  ati = db.allterms_begin("f");
1130  TEST(ati != db.allterms_end("f"));
1131  TEST_EQUAL(*ati, "five");
1132  TEST(ati != db.allterms_end("f"));
1133  ati.skip_to("three");
1134  TEST(ati == db.allterms_end("f"));
1135 
1136  ati = db.allterms_begin("f");
1137  TEST(ati != db.allterms_end("f"));
1138  TEST_EQUAL(*ati, "five");
1139  ati++;
1140  TEST(ati != db.allterms_end("f"));
1141  TEST_EQUAL(*ati, "four");
1142  ati++;
1143  TEST(ati == db.allterms_end("f"));
1144 
1145  ati = db.allterms_begin("absent");
1146  TEST(ati == db.allterms_end("absent"));
1147 }
1148 
1149 // test that searching for a term with a special characters in it works
1150 DEFINE_TESTCASE(specialterms1, backend) {
1151  Xapian::Enquire enquire(get_database("apitest_space"));
1152  Xapian::MSet mymset;
1153  Xapian::doccount count;
1155  Xapian::Stem stemmer("english");
1156 
1157  enquire.set_query(stemmer("new\nline"));
1158  mymset = enquire.get_mset(0, 10);
1159  TEST_MSET_SIZE(mymset, 1);
1160  count = 0;
1161  for (m = mymset.begin(); m != mymset.end(); ++m) ++count;
1162  TEST_EQUAL(count, 1);
1163 
1164  for (Xapian::valueno value_no = 0; value_no < 7; ++value_no) {
1165  string value = mymset.begin().get_document().get_value(value_no);
1166  TEST_NOT_EQUAL(value, "");
1167  if (value_no == 0) {
1168  TEST(value.size() > 263);
1169  TEST_EQUAL(static_cast<unsigned char>(value[262]), 255);
1170  for (int k = 0; k < 256; ++k) {
1171  TEST_EQUAL(static_cast<unsigned char>(value[k + 7]), k);
1172  }
1173  }
1174  }
1175 
1176  enquire.set_query(stemmer(string("big\0zero", 8)));
1177  mymset = enquire.get_mset(0, 10);
1178  TEST_MSET_SIZE(mymset, 1);
1179  count = 0;
1180  for (m = mymset.begin(); m != mymset.end(); ++m) ++count;
1181  TEST_EQUAL(count, 1);
1182 }
1183 
1184 // test that terms with a special characters in appear correctly when iterating
1185 // allterms
1186 DEFINE_TESTCASE(specialterms2, backend) {
1187  Xapian::Database db(get_database("apitest_space"));
1188 
1189  // Check the terms are all as expected (after stemming) and that allterms
1190  // copes with iterating over them.
1192  t = db.allterms_begin();
1193  TEST_EQUAL(*t, "back\\slash"); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1194  TEST_EQUAL(*t, string("big\0zero", 8)); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1195  TEST_EQUAL(*t, "new\nlin"); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1196  TEST_EQUAL(*t, "one\x01on"); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1197  TEST_EQUAL(*t, "space man"); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1198  TEST_EQUAL(*t, "tab\tbi"); ++t; TEST_NOT_EQUAL(t, db.allterms_end());
1199  TEST_EQUAL(*t, "tu\x02tu"); ++t; TEST_EQUAL(t, db.allterms_end());
1200 
1201  // Now check that skip_to exactly a term containing a zero byte works.
1202  // This is a regression test for flint and quartz - an Assert() used to
1203  // fire in debug builds (the Assert was wrong - the actual code handled
1204  // this OK).
1205  t = db.allterms_begin();
1206  t.skip_to(string("big\0zero", 8));
1207  TEST_NOT_EQUAL(t, db.allterms_end());
1208  TEST_EQUAL(*t, string("big\0zero", 8));
1209 }
1210 
1211 // test that rsets behave correctly with multiDBs
1212 DEFINE_TESTCASE(rsetmultidb2, backend && !multi) {
1213  Xapian::Database mydb1(get_database("apitest_rset", "apitest_simpledata2"));
1214  Xapian::Database mydb2(get_database("apitest_rset"));
1215  mydb2.add_database(get_database("apitest_simpledata2"));
1216 
1217  Xapian::Enquire enquire1(mydb1);
1218  Xapian::Enquire enquire2(mydb2);
1219 
1220  Xapian::Query myquery = query("is");
1221 
1222  enquire1.set_query(myquery);
1223  enquire2.set_query(myquery);
1224 
1225  Xapian::RSet myrset1;
1226  Xapian::RSet myrset2;
1227  myrset1.add_document(4);
1228  myrset2.add_document(2);
1229 
1230  Xapian::MSet mymset1a = enquire1.get_mset(0, 10);
1231  Xapian::MSet mymset1b = enquire1.get_mset(0, 10, &myrset1);
1232  Xapian::MSet mymset2a = enquire2.get_mset(0, 10);
1233  Xapian::MSet mymset2b = enquire2.get_mset(0, 10, &myrset2);
1234 
1235  mset_expect_order(mymset1a, 4, 3);
1236  mset_expect_order(mymset1b, 4, 3);
1237  mset_expect_order(mymset2a, 2, 5);
1238  mset_expect_order(mymset2b, 2, 5);
1239 
1240  TEST(mset_range_is_same_weights(mymset1a, 0, mymset2a, 0, 2));
1241  TEST(mset_range_is_same_weights(mymset1b, 0, mymset2b, 0, 2));
1242  TEST_NOT_EQUAL(mymset1a, mymset1b);
1243  TEST_NOT_EQUAL(mymset2a, mymset2b);
1244 }
1245 
1246 // tests an expand across multiple databases
1247 DEFINE_TESTCASE(multiexpand1, backend && !multi) {
1248  Xapian::Database mydb1(get_database("apitest_simpledata", "apitest_simpledata2"));
1249  Xapian::Enquire enquire1(mydb1);
1250 
1251  Xapian::Database mydb2(get_database("apitest_simpledata"));
1252  mydb2.add_database(get_database("apitest_simpledata2"));
1253  Xapian::Enquire enquire2(mydb2);
1254 
1255  // make simple equivalent rsets, with a document from each database in each.
1256  Xapian::RSet rset1;
1257  Xapian::RSet rset2;
1258  rset1.add_document(1);
1259  rset1.add_document(7);
1260  rset2.add_document(1);
1261  rset2.add_document(2);
1262 
1263  // Retrieve all the ESet results in each of the three setups:
1264 
1265  // This is the single database one.
1266  Xapian::ESet eset1 = enquire1.get_eset(1000, rset1);
1267 
1268  // This is the multi database with approximation
1269  Xapian::ESet eset2 = enquire2.get_eset(1000, rset2);
1270 
1271  // This is the multi database without approximation
1272  Xapian::ESet eset3 = enquire2.get_eset(1000, rset2, Xapian::Enquire::USE_EXACT_TERMFREQ);
1273 
1274  TEST_EQUAL(eset1.size(), eset3.size());
1275 
1276  Xapian::ESetIterator i = eset1.begin();
1277  Xapian::ESetIterator j = eset3.begin();
1278  while (i != eset1.end() && j != eset3.end()) {
1279  TEST_EQUAL(*i, *j);
1280  TEST_EQUAL(i.get_weight(), j.get_weight());
1281  ++i;
1282  ++j;
1283  }
1284  TEST(i == eset1.end());
1285  TEST(j == eset3.end());
1286 
1287  bool eset1_eq_eset2 = true;
1288  i = eset1.begin();
1289  j = eset2.begin();
1290  while (i != eset1.end() && j != eset2.end()) {
1291  if (i.get_weight() != j.get_weight()) {
1292  eset1_eq_eset2 = false;
1293  break;
1294  }
1295  ++i;
1296  ++j;
1297  }
1298  TEST(!eset1_eq_eset2);
1299 }
1300 
1301 // tests that opening a non-existent postlist returns an empty list
1302 DEFINE_TESTCASE(postlist1, backend) {
1303  Xapian::Database db(get_database("apitest_simpledata"));
1304 
1305  TEST_EQUAL(db.postlist_begin("rosebud"), db.postlist_end("rosebud"));
1306 
1307  string s = "let_us_see_if_we_can_break_it_with_a_really_really_long_term.";
1308  for (int i = 0; i < 8; ++i) {
1309  s += s;
1310  TEST_EQUAL(db.postlist_begin(s), db.postlist_end(s));
1311  }
1312 
1313  // A regression test (no, really!)
1314  TEST_NOT_EQUAL(db.postlist_begin("a"), db.postlist_end("a"));
1315 }
1316 
1317 // tests that a Xapian::PostingIterator works as an STL iterator
1318 DEFINE_TESTCASE(postlist2, backend) {
1319  Xapian::Database db(get_database("apitest_simpledata"));
1321  p = db.postlist_begin("this");
1322  Xapian::PostingIterator pend = db.postlist_end("this");
1323 
1324  TEST(p.get_description() != "PostingIterator()");
1325 
1326  // test operator= creates a copy which compares equal
1327  Xapian::PostingIterator p_copy = p;
1328  TEST_EQUAL(p, p_copy);
1329 
1330  TEST(p_copy.get_description() != "PostingIterator()");
1331 
1332  // test copy constructor creates a copy which compares equal
1333  Xapian::PostingIterator p_clone(p);
1334  TEST_EQUAL(p, p_clone);
1335 
1336  TEST(p_clone.get_description() != "PostingIterator()");
1337 
1338  vector<Xapian::docid> v(p, pend);
1339 
1340  p = db.postlist_begin("this");
1341  pend = db.postlist_end("this");
1342  vector<Xapian::docid>::const_iterator i;
1343  for (i = v.begin(); i != v.end(); ++i) {
1344  TEST_NOT_EQUAL(p, pend);
1345  TEST_EQUAL(*i, *p);
1346  p++;
1347  }
1348  TEST_EQUAL(p, pend);
1349 
1350  TEST_STRINGS_EQUAL(p.get_description(), "PostingIterator()");
1351  TEST_STRINGS_EQUAL(pend.get_description(), "PostingIterator()");
1352 }
1353 
1354 // tests that a Xapian::PostingIterator still works when the DB is deleted
1355 DEFINE_TESTCASE(postlist3, backend) {
1357  {
1358  Xapian::Database db_temp(get_database("apitest_simpledata"));
1359  u = db_temp.postlist_begin("this");
1360  }
1361 
1362  Xapian::Database db(get_database("apitest_simpledata"));
1363  Xapian::PostingIterator p = db.postlist_begin("this");
1364  Xapian::PostingIterator pend = db.postlist_end("this");
1365 
1366  while (p != pend) {
1367  TEST_EQUAL(*p, *u);
1368  p++;
1369  u++;
1370  }
1371 }
1372 
1373 // tests skip_to
1374 DEFINE_TESTCASE(postlist4, backend) {
1375  Xapian::Database db(get_database("apitest_simpledata"));
1376  Xapian::PostingIterator i = db.postlist_begin("this");
1377  i.skip_to(1);
1378  i.skip_to(999999999);
1379  TEST(i == db.postlist_end("this"));
1380 }
1381 
1382 // tests long postlists
1383 DEFINE_TESTCASE(postlist5, backend) {
1384  Xapian::Database db(get_database("apitest_manydocs"));
1386  Xapian::PostingIterator i = db.postlist_begin("this");
1387  unsigned int j = 1;
1388  while (i != db.postlist_end("this")) {
1389  TEST_EQUAL(*i, j);
1390  i++;
1391  j++;
1392  }
1393  TEST_EQUAL(j, 513);
1394 }
1395 
1396 // tests document length in postlists
1397 DEFINE_TESTCASE(postlist6, backend) {
1398  Xapian::Database db(get_database("apitest_simpledata"));
1399  Xapian::PostingIterator i = db.postlist_begin("this");
1400  TEST(i != db.postlist_end("this"));
1401  while (i != db.postlist_end("this")) {
1402  TEST_EQUAL(i.get_doclength(), db.get_doclength(*i));
1404  TEST_REL(i.get_wdf(),<=,i.get_doclength());
1405  TEST_REL(1,<=,i.get_unique_terms());
1406  // The next two aren't necessarily true if there are terms with wdf=0
1407  // in the document, but that isn't the case here.
1409  TEST_REL(i.get_wdf() + i.get_unique_terms() - 1,<=,i.get_doclength());
1410  ++i;
1411  }
1412 }
1413 
1414 // tests collection frequency
1415 DEFINE_TESTCASE(collfreq1, backend) {
1416  Xapian::Database db(get_database("apitest_simpledata"));
1417 
1418  TEST_EQUAL(db.get_collection_freq("this"), 11);
1419  TEST_EQUAL(db.get_collection_freq("first"), 1);
1420  TEST_EQUAL(db.get_collection_freq("last"), 0);
1421  TEST_EQUAL(db.get_collection_freq("word"), 9);
1422 
1423  Xapian::Database db1(get_database("apitest_simpledata", "apitest_simpledata2"));
1424  Xapian::Database db2(get_database("apitest_simpledata"));
1425  db2.add_database(get_database("apitest_simpledata2"));
1426 
1427  TEST_EQUAL(db1.get_collection_freq("this"), 15);
1428  TEST_EQUAL(db1.get_collection_freq("first"), 1);
1429  TEST_EQUAL(db1.get_collection_freq("last"), 0);
1430  TEST_EQUAL(db1.get_collection_freq("word"), 11);
1431  TEST_EQUAL(db2.get_collection_freq("this"), 15);
1432  TEST_EQUAL(db2.get_collection_freq("first"), 1);
1433  TEST_EQUAL(db2.get_collection_freq("last"), 0);
1434  TEST_EQUAL(db2.get_collection_freq("word"), 11);
1435 }
1436 
1437 // Regression test for split msets being incorrect when sorting
1438 DEFINE_TESTCASE(sortvalue1, backend) {
1439  Xapian::Enquire enquire(get_database("apitest_simpledata"));
1440  enquire.set_query(Xapian::Query("this"));
1441 
1442  for (int pass = 1; pass <= 2; ++pass) {
1443  for (Xapian::valueno value_no = 1; value_no < 7; ++value_no) {
1444  tout << "Sorting on value " << value_no << '\n';
1445  enquire.set_sort_by_value(value_no, true);
1446  Xapian::MSet allbset = enquire.get_mset(0, 100);
1447  Xapian::MSet partbset1 = enquire.get_mset(0, 3);
1448  Xapian::MSet partbset2 = enquire.get_mset(3, 97);
1449  TEST_EQUAL(allbset.size(), partbset1.size() + partbset2.size());
1450 
1451  bool ok = true;
1452  int n = 0;
1453  Xapian::MSetIterator i, j;
1454  j = allbset.begin();
1455  for (i = partbset1.begin(); i != partbset1.end(); ++i) {
1456  tout << "Entry " << n << ": " << *i << " | " << *j << '\n';
1457  TEST(j != allbset.end());
1458  if (*i != *j) ok = false;
1459  ++j;
1460  ++n;
1461  }
1462  tout << "===\n";
1463  for (i = partbset2.begin(); i != partbset2.end(); ++i) {
1464  tout << "Entry " << n << ": " << *i << " | " << *j << '\n';
1465  TEST(j != allbset.end());
1466  if (*i != *j) ok = false;
1467  ++j;
1468  ++n;
1469  }
1470  TEST(j == allbset.end());
1471  if (!ok)
1472  FAIL_TEST("Split msets aren't consistent with unsplit");
1473  }
1475  }
1476 }
1477 
1478 // consistency check match - vary mset size and check results agree.
1479 // consistency1 will run on the remote backend, but it's particularly slow
1480 // with that, and testing it there doesn't actually improve the test
1481 // coverage really.
1482 DEFINE_TESTCASE(consistency1, backend && !remote) {
1483  Xapian::Database db(get_database("etext"));
1484  Xapian::Enquire enquire(db);
1486  Xapian::doccount lots = 214;
1487  Xapian::MSet bigmset = enquire.get_mset(0, lots);
1488  TEST_EQUAL(bigmset.size(), lots);
1489  try {
1490  for (Xapian::doccount start = 0; start < lots; ++start) {
1491  for (Xapian::doccount size = 0; size < lots - start; ++size) {
1492  Xapian::MSet mset = enquire.get_mset(start, size);
1493  if (mset.size()) {
1494  TEST_EQUAL(start + mset.size(),
1495  min(start + size, bigmset.size()));
1496  } else if (size) {
1497 // tout << start << mset.size() << bigmset.size() << '\n';
1498  TEST(start >= bigmset.size());
1499  }
1500  for (Xapian::doccount i = 0; i < mset.size(); ++i) {
1501  TEST_EQUAL(*mset[i], *bigmset[start + i]);
1502  TEST_EQUAL_DOUBLE(mset[i].get_weight(),
1503  bigmset[start + i].get_weight());
1504  }
1505  }
1506  }
1507  } catch (const Xapian::NetworkTimeoutError &) {
1508  // consistency1 is a long test - may timeout with the remote backend...
1509  SKIP_TEST("Test taking too long");
1510  }
1511 }
1512 
1513 // Test that specifying a nonexistent input file throws an exception
1514 // (chert-specific cases).
1515 DEFINE_TESTCASE(chertdatabasenotfounderror1, chert) {
1516 #ifdef XAPIAN_HAS_CHERT_BACKEND
1517  mkdir(".chert", 0755);
1518 
1520  Xapian::Database(".chert/nosuchdirectory",
1523  Xapian::WritableDatabase(".chert/nosuchdirectory",
1525 
1526  mkdir(".chert/emptydirectory", 0700);
1528  Xapian::Database(".chert/emptydirectory",
1530 
1531  touch(".chert/somefile");
1533  Xapian::Database(".chert/somefile",
1536  Xapian::WritableDatabase(".chert/somefile",
1539  Xapian::WritableDatabase(".chert/somefile",
1542  Xapian::WritableDatabase(".chert/somefile",
1545  Xapian::WritableDatabase(".chert/somefile",
1547 #endif
1548 
1549 }
1550 
1551 // Test that specifying a nonexistent input file throws an exception
1552 // (glass-specific cases).
1553 DEFINE_TESTCASE(glassdatabasenotfounderror1, glass) {
1554 #ifdef XAPIAN_HAS_GLASS_BACKEND
1555  mkdir(".glass", 0755);
1556 
1558  Xapian::Database(".glass/nosuchdirectory",
1561  Xapian::WritableDatabase(".glass/nosuchdirectory",
1563 
1564  mkdir(".glass/emptydirectory", 0700);
1566  Xapian::Database(".glass/emptydirectory",
1568 
1569  touch(".glass/somefile");
1571  Xapian::Database(".glass/somefile",
1574  Xapian::WritableDatabase(".glass/somefile",
1577  Xapian::WritableDatabase(".glass/somefile",
1580  Xapian::WritableDatabase(".glass/somefile",
1583  Xapian::WritableDatabase(".glass/somefile",
1585 #endif
1586 }
1587 
1588 // Test that specifying a nonexistent input file throws an exception
1589 // (non-backend-specific cases).
1590 DEFINE_TESTCASE(databasenotfounderror2, !backend) {
1592  Xapian::Database("nosuchdirectory"));
1594  Xapian::Database("no/such/directory"));
1595 
1597  Xapian::WritableDatabase("nosuchdirectory", Xapian::DB_OPEN));
1599  Xapian::WritableDatabase("no/such/directory", Xapian::DB_OPEN));
1600 
1601  string empty_dir = "emptydirectory";
1602  mkdir(empty_dir.c_str(), 0700);
1604  Xapian::Database{empty_dir});
1605 }
1606 
1608 DEFINE_TESTCASE(chertdatabaseopen1, chert) {
1609 #ifdef XAPIAN_HAS_CHERT_BACKEND
1610  const string dbdir = ".chert/test_chertdatabaseopen1";
1611  mkdir(".chert", 0755);
1612 
1613  {
1614  rm_rf(dbdir);
1615  Xapian::WritableDatabase wdb(dbdir,
1621  }
1622 
1623  {
1624  rm_rf(dbdir);
1625  Xapian::WritableDatabase wdb(dbdir,
1631  }
1632 
1633  {
1634  rm_rf(dbdir);
1635  Xapian::WritableDatabase wdb(dbdir,
1641  }
1642 
1643  {
1647  Xapian::WritableDatabase wdb(dbdir,
1650  }
1651 
1652  {
1653  Xapian::WritableDatabase wdb(dbdir,
1656  }
1657 
1658  {
1659  Xapian::WritableDatabase wdb(dbdir,
1662  }
1663 #endif
1664 }
1665 
1666 // feature test for Enquire:
1667 // set_sort_by_value
1668 // set_sort_by_value_then_relevance
1669 // set_sort_by_relevance_then_value
1670 // Prior to 1.2.17 and 1.3.2, order8 and order9 were swapped, and
1671 // set_sort_by_relevance_then_value was buggy, so this testcase now serves as
1672 // a regression test for that bug.
1673 DEFINE_TESTCASE(sortrel1, backend) {
1674  Xapian::Enquire enquire(get_database("apitest_sortrel"));
1675  enquire.set_sort_by_value(1, true);
1676  enquire.set_query(Xapian::Query("woman"));
1677 
1678  static const Xapian::docid order1[] = { 1,2,3,4,5,6,7,8,9 };
1679  static const Xapian::docid order2[] = { 2,1,3,6,5,4,7,9,8 };
1680  static const Xapian::docid order3[] = { 3,2,1,6,5,4,9,8,7 };
1681  static const Xapian::docid order4[] = { 7,8,9,4,5,6,1,2,3 };
1682  static const Xapian::docid order5[] = { 9,8,7,6,5,4,3,2,1 };
1683  static const Xapian::docid order6[] = { 7,9,8,6,5,4,2,1,3 };
1684  static const Xapian::docid order7[] = { 7,9,8,6,5,4,2,1,3 };
1685  static const Xapian::docid order8[] = { 2,6,7,1,5,9,3,4,8 };
1686  static const Xapian::docid order9[] = { 7,6,2,9,5,1,8,4,3 };
1687 
1688  Xapian::MSet mset;
1689  Xapian::doccount i;
1690 
1691  mset = enquire.get_mset(0, 10);
1692  TEST_EQUAL(mset.size(), sizeof(order1) / sizeof(Xapian::docid));
1693  for (i = 0; i < sizeof(order1) / sizeof(Xapian::docid); ++i) {
1694  TEST_EQUAL(*mset[i], order1[i]);
1695  }
1696 
1697  enquire.set_sort_by_value_then_relevance(1, true);
1698 
1699  mset = enquire.get_mset(0, 10);
1700  TEST_EQUAL(mset.size(), sizeof(order2) / sizeof(Xapian::docid));
1701  for (i = 0; i < sizeof(order2) / sizeof(Xapian::docid); ++i) {
1702  TEST_EQUAL(*mset[i], order2[i]);
1703  }
1704 
1705  enquire.set_sort_by_value(1, true);
1706 
1707  mset = enquire.get_mset(0, 10);
1708  TEST_EQUAL(mset.size(), sizeof(order1) / sizeof(Xapian::docid));
1709  for (i = 0; i < sizeof(order1) / sizeof(Xapian::docid); ++i) {
1710  TEST_EQUAL(*mset[i], order1[i]);
1711  }
1712 
1713  enquire.set_sort_by_value_then_relevance(1, true);
1715 
1716  mset = enquire.get_mset(0, 10);
1717  TEST_EQUAL(mset.size(), sizeof(order2) / sizeof(Xapian::docid));
1718  for (i = 0; i < sizeof(order2) / sizeof(Xapian::docid); ++i) {
1719  TEST_EQUAL(*mset[i], order2[i]);
1720  }
1721 
1722  enquire.set_sort_by_value(1, true);
1724 
1725  mset = enquire.get_mset(0, 10);
1726  TEST_EQUAL(mset.size(), sizeof(order3) / sizeof(Xapian::docid));
1727  for (i = 0; i < sizeof(order3) / sizeof(Xapian::docid); ++i) {
1728  TEST_EQUAL(*mset[i], order3[i]);
1729  }
1730 
1731  enquire.set_sort_by_value(1, false);
1733  mset = enquire.get_mset(0, 10);
1734  TEST_EQUAL(mset.size(), sizeof(order4) / sizeof(Xapian::docid));
1735  for (i = 0; i < sizeof(order4) / sizeof(Xapian::docid); ++i) {
1736  TEST_EQUAL(*mset[i], order4[i]);
1737  }
1738 
1739  enquire.set_sort_by_value(1, false);
1741  mset = enquire.get_mset(0, 10);
1742  TEST_EQUAL(mset.size(), sizeof(order5) / sizeof(Xapian::docid));
1743  for (i = 0; i < sizeof(order5) / sizeof(Xapian::docid); ++i) {
1744  TEST_EQUAL(*mset[i], order5[i]);
1745  }
1746 
1747  enquire.set_sort_by_value_then_relevance(1, false);
1749  mset = enquire.get_mset(0, 10);
1750  TEST_EQUAL(mset.size(), sizeof(order6) / sizeof(Xapian::docid));
1751  for (i = 0; i < sizeof(order6) / sizeof(Xapian::docid); ++i) {
1752  TEST_EQUAL(*mset[i], order6[i]);
1753  }
1754 
1755  enquire.set_sort_by_value_then_relevance(1, false);
1757  mset = enquire.get_mset(0, 10);
1758  TEST_EQUAL(mset.size(), sizeof(order7) / sizeof(Xapian::docid));
1759  for (i = 0; i < sizeof(order7) / sizeof(Xapian::docid); ++i) {
1760  TEST_EQUAL(*mset[i], order7[i]);
1761  }
1762 
1763  enquire.set_sort_by_relevance_then_value(1, true);
1765  mset = enquire.get_mset(0, 10);
1766  TEST_EQUAL(mset.size(), sizeof(order8) / sizeof(Xapian::docid));
1767  for (i = 0; i < sizeof(order8) / sizeof(Xapian::docid); ++i) {
1768  TEST_EQUAL(*mset[i], order8[i]);
1769  }
1770 
1771  enquire.set_sort_by_relevance_then_value(1, true);
1773  mset = enquire.get_mset(0, 10);
1774  TEST_EQUAL(mset.size(), sizeof(order8) / sizeof(Xapian::docid));
1775  for (i = 0; i < sizeof(order8) / sizeof(Xapian::docid); ++i) {
1776  TEST_EQUAL(*mset[i], order8[i]);
1777  }
1778 
1779  enquire.set_sort_by_relevance_then_value(1, false);
1781  mset = enquire.get_mset(0, 10);
1782  TEST_EQUAL(mset.size(), sizeof(order9) / sizeof(Xapian::docid));
1783  for (i = 0; i < sizeof(order9) / sizeof(Xapian::docid); ++i) {
1784  TEST_EQUAL(*mset[i], order9[i]);
1785  }
1786 
1787  enquire.set_sort_by_relevance_then_value(1, false);
1789  mset = enquire.get_mset(0, 10);
1790  TEST_EQUAL(mset.size(), sizeof(order9) / sizeof(Xapian::docid));
1791  for (i = 0; i < sizeof(order9) / sizeof(Xapian::docid); ++i) {
1792  TEST_EQUAL(*mset[i], order9[i]);
1793  }
1794 }
1795 
1796 static void
1798 {
1799  static const struct { Xapian::docid did; const char* text; } content[] = {
1800  {1, "This is a test document used with the API test. This paragraph "
1801  "must be at least three lines (including the blank line) to be "
1802  "counted as a \"paragraph\"."},
1803  {2, "This is a second simple data test, used to test multiple "
1804  "(inmemory anyway) databases. The text in this file is "
1805  "unimportant, although I suppose it ought to include the "
1806  "standard word \"word\" in a few places."},
1807  {3, "This file will be indexed by paragraph, and the simple query will "
1808  "search for the word \"word\". Well expect the mset to contain "
1809  "two documents, including this paragraph and the fourth, below. "
1810  "Since this paragraph uses the word \"word\" so much, this "
1811  "should be the first one in the match set. Ill just say the word "
1812  "a few more times (word!) to make sure of that. If this doesnt "
1813  "word (typo, I meant work), then there may be fourletter words "
1814  "spoken."},
1815  {4, "Ill leave this at two paragraphs. This one hasnt got any useful "
1816  "information in it either."},
1817  {5, "This paragraph only has a load of absolute rubbish, and nothing "
1818  "of any use whatsoever."},
1819  {7, "This is the other paragraph with the word in the simple query "
1820  "in it. For simplicity, all paragraphs are at least two lines, "
1821  "due to how the hacked up indexer works."},
1822  {9, "This is another paragraph which wont be returned. Well, not "
1823  "with the simple query, anyway."},
1824  {11, "And yet another. This one does mention banana splits, though, "
1825  "so cant be that bad."}
1826  };
1827 
1828  Xapian::TermGenerator indexer;
1829  indexer.set_stemmer(Xapian::Stem("english"));
1830  indexer.set_stemming_strategy(indexer.STEM_ALL);
1831 
1832  for (auto& i : content) {
1833  Xapian::Document doc;
1834  indexer.set_document(doc);
1835  indexer.index_text(i.text);
1836  db.replace_document(i.did, doc);
1837  }
1838 
1839  db.commit();
1840 }
1841 
1842 // Test network stats and local stats give the same results.
1843 DEFINE_TESTCASE(netstats1, backend) {
1844  static const char * const words[] = { "paragraph", "word" };
1845  Xapian::Query query(Xapian::Query::OP_OR, words, words + 2);
1846  const size_t MSET_SIZE = 10;
1847 
1848  Xapian::RSet rset;
1849  rset.add_document(4);
1850  rset.add_document(9);
1851 
1852  {
1853  Xapian::Database db = get_database("netstats1", make_netstats1_db);
1854 
1855  Xapian::Enquire enq(db);
1856  enq.set_query(query);
1857  Xapian::MSet mset = enq.get_mset(0, MSET_SIZE, &rset);
1860  TEST_EQUAL(mset.get_matches_estimated(), 7);
1861  TEST_EQUAL_DOUBLE(mset.get_max_attained(), 1.445962071042388164);
1862  TEST_EQUAL(mset.size(), 7);
1863 
1864  static const pair<Xapian::docid, double> to_compare[] = {
1865  {7, 1.445962071042388164},
1866  {3, 1.4140112748017070743},
1867  {1, 1.3747698831232337824},
1868  {5, 1.1654938419498412916},
1869  {9, 1.1654938419498412916},
1870  {4, 1.1543806706320836053},
1871  {2, 0.12268031290495594321}
1872  };
1873 
1874  TEST(mset_range_is_same(mset, 0, to_compare, mset.size()));
1875  }
1876 }
1877 
1878 // Coordinate matching - scores 1 for each matching term
1879 class MyWeight : public Xapian::Weight {
1881 
1882  public:
1883  MyWeight* clone() const override {
1884  return new MyWeight;
1885  }
1886  void init(double factor) override {
1887  scale_factor = factor;
1888  }
1889  MyWeight() { }
1891  std::string name() const override { return "MyWeight"; }
1892  string serialise() const override { return string(); }
1893  MyWeight* unserialise(const string&) const override {
1894  return new MyWeight;
1895  }
1898  Xapian::termcount) const override {
1899  return scale_factor;
1900  }
1901  double get_maxpart() const override { return scale_factor; }
1902 
1904  Xapian::termcount) const override {
1905  return 0;
1906  }
1907  double get_maxextra() const override { return 0; }
1908 };
1909 
1910 // tests user weighting scheme.
1911 // Would work with remote if we registered the weighting scheme.
1912 // FIXME: do this so we also test that functionality...
1913 DEFINE_TESTCASE(userweight1, backend && !remote) {
1914  Xapian::Enquire enquire(get_database("apitest_simpledata"));
1915  enquire.set_weighting_scheme(MyWeight());
1916  static const char * const query[] = {
1917  "this", "line", "paragraph", "rubbish"
1918  };
1920  query + sizeof(query) / sizeof(query[0])));
1921  Xapian::MSet mymset1 = enquire.get_mset(0, 100);
1922  // MyWeight scores 1 for each matching term, so the weight should equal
1923  // the number of matching terms.
1924  for (Xapian::MSetIterator i = mymset1.begin(); i != mymset1.end(); ++i) {
1925  Xapian::termcount matching_terms = 0;
1927  while (t != enquire.get_matching_terms_end(i)) {
1928  ++matching_terms;
1929  ++t;
1930  }
1931  TEST_EQUAL(i.get_weight(), matching_terms);
1932  }
1933 }
1934 
1935 // tests MatchAll queries
1936 // This is a regression test, which failed with assertion failures in
1937 // revision 9094. Also check that the results aren't ranked by relevance
1938 // (regression test for bug fixed in 1.0.9).
1939 DEFINE_TESTCASE(matchall1, backend) {
1940  Xapian::Database db(get_database("apitest_simpledata"));
1941  Xapian::Enquire enquire(db);
1943  Xapian::MSet mset = enquire.get_mset(0, 10);
1946 
1948  Xapian::Query("nosuchterm"),
1950  mset = enquire.get_mset(0, 10);
1953 
1954  // Check that the results aren't ranked by relevance (fixed in 1.0.9).
1955  TEST(mset.size() > 1);
1956  TEST_EQUAL(mset[mset.size() - 1].get_weight(), 0);
1957  TEST_EQUAL(*mset[0], 1);
1958  TEST_EQUAL(*mset[mset.size() - 1], mset.size());
1959 }
1960 
1961 // Test using a ValueSetMatchDecider
1962 DEFINE_TESTCASE(valuesetmatchdecider2, backend && !remote) {
1963  Xapian::Database db(get_database("apitest_phrase"));
1964  Xapian::Enquire enq(db);
1965  enq.set_query(Xapian::Query("leav"));
1966 
1967  Xapian::ValueSetMatchDecider vsmd1(1, true);
1968  vsmd1.add_value("n");
1969  Xapian::ValueSetMatchDecider vsmd2(1, false);
1970  vsmd2.add_value("n");
1971 
1972  Xapian::MSet mymset = enq.get_mset(0, 20);
1973  mset_expect_order(mymset, 8, 6, 4, 5, 7, 10, 12, 11, 13, 9, 14);
1974  mymset = enq.get_mset(0, 20, 0, NULL, &vsmd1);
1975  mset_expect_order(mymset, 6, 12);
1976  mymset = enq.get_mset(0, 20, 0, NULL, &vsmd2);
1977  mset_expect_order(mymset, 8, 4, 5, 7, 10, 11, 13, 9, 14);
1978 }
#define TEST_MSET_SIZE(M, S)
Check MSet M has size S.
Definition: testutils.h:78
static void make_netstats1_db(Xapian::WritableDatabase &db, const string &)
Definition: api_db.cc:1797
static const char * get_xapian_progsrv_command()
Get the command line required to run xapian-progsrv.
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: omenquire.cc:318
Xapian::Document get_document(Xapian::docid did) const
Get a document from the database, given its document id.
Definition: omdatabase.cc:490
Xapian::docid add_document(const Xapian::Document &document)
Add a new document to the database.
Definition: omdatabase.cc:902
MatchDecider filtering results based on whether document values are in a user-defined set...
void set_sort_by_value_then_relevance(Xapian::valueno sort_key, bool reverse)
Set the sorting to be by value, then by relevance for documents with the same value.
Definition: omenquire.cc:884
static size_t check(const std::string &path, int opts=0, std::ostream *out=NULL)
Check the integrity of a database or database table.
Definition: database.h:564
void init(double factor) override
Allow the subclass to perform any initialisation it needs to.
Definition: api_db.cc:1886
void set_docid_order(docid_order order)
Set sort order for document IDs.
Definition: omenquire.cc:856
double get_sumpart(Xapian::termcount, Xapian::termcount, Xapian::termcount) const override
Calculate the weight contribution for this object&#39;s term to a document.
Definition: api_db.cc:1896
#define TEST(a)
Test a condition, without an additional explanation for failure.
Definition: testsuite.h:275
~MyWeight()
Definition: api_db.cc:1890
#define TEST_EXCEPTION_BASE_CLASS(TYPE, CODE)
Check that CODE throws Xapian exception derived from TYPE.
Definition: testutils.h:106
This class is used to access a database, or a group of databases.
Definition: database.h:68
void set_sort_by_value(Xapian::valueno sort_key, bool reverse)
Set the sorting to be by value only.
Definition: omenquire.cc:875
double get_sumextra(Xapian::termcount, Xapian::termcount) const override
Calculate the term-independent weight component for a document.
Definition: api_db.cc:1903
Xapian::termcount get_wdf() const
Return the wdf for the document at the current position.
TermIterator get_matching_terms_end(Xapian::docid) const
End iterator corresponding to get_matching_terms_begin()
Definition: enquire.h:717
static const Xapian::Query MatchAll
A query matching all documents.
Definition: query.h:75
void set_cutoff(int percent_cutoff, double weight_cutoff=0)
Set the percentage and/or weight cutoffs.
Definition: omenquire.cc:862
const int DB_CREATE
Create a new database.
Definition: constants.h:44
DatabaseOpeningError indicates failure to open a database.
Definition: error.h:581
Class representing a stemming algorithm.
Definition: stem.h:62
void set_document(const Xapian::Document &doc)
Set the current document.
bool mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1, const Xapian::MSet &mset2, unsigned int first2, unsigned int count)
Definition: testutils.cc:46
Parses a piece of text and generate terms.
Definition: termgenerator.h:48
Indicates a timeout expired while communicating with a remote database.
Definition: error.h:845
#define TEST_AND_EXPLAIN(a, b)
Test a condition, and display the test with an extra explanation if the condition fails...
Definition: testsuite.h:267
bool empty() const
Return true if this MSet object is empty.
Definition: mset.h:300
void set_stemming_strategy(stem_strategy strategy)
Set the stemming strategy.
Xapian::doccount get_matches_lower_bound() const
Lower bound on the total number of matching documents.
Definition: omenquire.cc:246
bool operator()(const Xapian::Document &doc) const override
Decide whether we want this document to be in the MSet.
Definition: api_db.cc:483
#define EXPECTED_EXCEPTION
TermIterator allterms_end(const std::string &=std::string()) const
Corresponding end iterator to allterms_begin(prefix).
Definition: database.h:269
Xapian::docid get_lastdocid() const
Get the highest document id which has been used in the database.
Definition: omdatabase.cc:279
double get_max_attained() const
The maximum weight attained by any document.
Definition: omenquire.cc:297
const std::string & get_msg() const
Message giving details of the error, intended for human consumption.
Definition: error.h:122
string needle
Definition: api_db.cc:478
void sleep(double t)
Sleep until the time represented by this object.
Definition: realtime.h:127
a generic test suite engine
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
static const int USE_EXACT_TERMFREQ
Calculate exact term frequencies in get_eset().
Definition: enquire.h:605
void add_value(const std::string &value)
Add a value to the test set.
C++ function versions of useful Unix commands.
Class representing a list of search results.
Definition: mset.h:44
void skip_to(const std::string &term)
Advance the iterator to term term.
STL namespace.
MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems, Xapian::doccount checkatleast=0, const RSet *omrset=0, const MatchDecider *mdecider=0) const
Get (a portion of) the match set for the current query.
Definition: omenquire.cc:938
const int DB_CREATE_OR_OPEN
Create database if it doesn&#39;t already exist.
Definition: constants.h:35
include <netdb.h>, with portability workarounds.
void replace_document(Xapian::docid did, const Xapian::Document &document)
Replace a given document in the database.
Definition: omdatabase.cc:952
static Xapian::Stem stemmer
Definition: stemtest.cc:41
Xapian::doccount get_doccount() const
Get the number of documents in the database.
Definition: omdatabase.cc:267
include <sys/stat.h> with portability enhancements
double get_weight() const
Get the weight for the current position.
void index_text(const Xapian::Utf8Iterator &itor, Xapian::termcount wdf_inc=1, const std::string &prefix=std::string())
Index some text.
MyWeight()
Definition: api_db.cc:1889
const int DB_BACKEND_GLASS
Use the glass backend.
Definition: constants.h:158
TermIterator get_matching_terms_begin(Xapian::docid did) const
Get terms which match a given document, by document id.
Definition: omenquire.cc:962
test functionality of the Xapian API
void rm_rf(const string &filename)
Remove a directory and contents, just like the Unix "rm -rf" command.
Definition: unixcmds.cc:111
Xapian::doccount get_matches_upper_bound() const
Upper bound on the total number of matching documents.
Definition: omenquire.cc:262
Xapian::doclength get_avlength() const
Get the average length of the documents in the database.
Definition: omdatabase.cc:293
Class for iterating over a list of terms.
Definition: termiterator.h:41
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
#define TEST_REL(A, REL, B)
Test a relation holds,e.g. TEST_REL(a,>,b);.
Definition: testmacros.h:32
ESetIterator begin() const
Return iterator pointing to the first item in this ESet.
Definition: eset.h:345
Class for iterating over a list of terms.
ESet get_eset(Xapian::termcount maxitems, const RSet &omrset, int flags=0, const Xapian::ExpandDecider *edecider=0, double min_wt=0.0) const
Get the expand set for the given rset.
Definition: omenquire.cc:947
Decide if a Xapian::Error exception should be ignored.
Definition: errorhandler.h:50
#define TEST_NOT_EQUAL(a, b)
Test for non-equality of two things.
Definition: testsuite.h:305
Xapian::doccount size() const
Return number of items in this ESet object.
Xapian::doccount get_uncollapsed_matches_estimated() const
Estimate of the total number of matching documents before collapsing.
Definition: omenquire.cc:276
Base class for backend handling in test harness.
string get_database_path(const string &dbname)
Definition: apitest.cc:72
Class implementing a "boolean" weighting scheme.
Definition: weight.h:433
static int verbose
Definition: xapian-delve.cc:47
DatabaseLockError indicates failure to lock a database.
Definition: error.h:493
const int DB_OPEN
Open an existing database.
Definition: constants.h:50
This class provides read/write access to a database.
Definition: database.h:789
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:104
Iterator over a Xapian::MSet.
Definition: mset.h:368
Indicates an attempt to use a feature which is unavailable.
Definition: error.h:719
DatabaseCreateError indicates a failure to create a database.
Definition: error.h:451
Public interfaces for the Xapian library.
void set_sort_by_relevance_then_value(Xapian::valueno sort_key, bool reverse)
Set the sorting to be by relevance then value.
Definition: omenquire.cc:893
double get_maxpart() const override
Return an upper bound on what get_sumpart() can return for any document.
Definition: api_db.cc:1901
docids sort in ascending order (default)
Definition: enquire.h:328
#define TEST_EXCEPTION(TYPE, CODE)
Check that CODE throws exactly Xapian exception TYPE.
Definition: testutils.h:109
double scale_factor
Definition: api_db.cc:1880
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:624
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:629
void set_stemmer(const Xapian::Stem &stemmer)
Set the Xapian::Stem object to be used for generating stemmed terms.
Xapian::termcount get_doclength(Xapian::docid did) const
Get the length of a document.
Definition: omdatabase.cc:461
void commit()
Commit any pending modifications made to the database.
Definition: omdatabase.cc:857
std::string name() const override
Return the name of this weighting scheme.
Definition: api_db.cc:1891
Indicates an attempt to access a database not present.
Definition: error.h:1055
TermIterator allterms_begin(const std::string &prefix=std::string()) const
An iterator which runs across all terms with a given prefix.
Definition: omdatabase.cc:223
Iterator over a Xapian::ESet.
Definition: eset.h:160
#define TEST_EQUAL_DOUBLE(a, b)
Test two doubles for near equality.
Definition: testsuite.h:295
double get_maxextra() const override
Return an upper bound on what get_sumextra() can return for any document.
Definition: api_db.cc:1907
void add_database(const Database &database)
Add an existing database (or group of databases) to those accessed by this object.
Definition: omdatabase.cc:148
void set_query(const Xapian::Query &query, Xapian::termcount qlen=0)
Set the query to run.
Definition: omenquire.cc:793
bool term_exists(const std::string &tname) const
Check if a given term exists in the database.
Definition: omdatabase.cc:524
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
Base class for matcher decision functor.
Definition: enquire.h:118
void add_document(Xapian::docid did)
Add a document to the relevance set.
Definition: omenquire.cc:104
#define FAIL_TEST(MSG)
Fail the current testcase with message MSG.
Definition: testsuite.h:68
Xapian::Database get_database(const string &dbname)
Definition: apitest.cc:48
Xapian::doccount get_matches_estimated() const
Estimate of the total number of matching documents.
Definition: omenquire.cc:253
MyWeight * unserialise(const string &) const override
Unserialise parameters.
Definition: api_db.cc:1893
const int DB_BACKEND_STUB
Open a stub database file.
Definition: constants.h:179
#define SKIP_TEST(MSG)
Skip the current testcase with message MSG.
Definition: testsuite.h:74
This class provides an interface to the information retrieval system for the purpose of searching...
Definition: enquire.h:152
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
string serialise() const override
Return this object&#39;s parameters serialised as a single string.
Definition: api_db.cc:1892
Xapian::termcount get_doclength() const
Return the length of the document at the current position.
Xapian::doccount get_uncollapsed_matches_upper_bound() const
Upper bound on the total number of matching documents before collapsing.
Definition: omenquire.cc:283
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
Indicates a problem communicating with a remote database.
Definition: error.h:803
Match documents which at least one subquery matches.
Definition: query.h:92
void skip_to(Xapian::docid did)
Advance the iterator to document did.
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
Xapian-specific test helper functions and macros.
DEFINE_TESTCASE(termstats, backend)
Definition: api_db.cc:59
#define TEST_STRINGS_EQUAL(a, b)
Test for equality of two strings.
Definition: testsuite.h:287
bool mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1, const Xapian::MSet &mset2, unsigned int first2, unsigned int count)
Definition: testutils.cc:111
void keep_alive()
Send a "keep-alive" to remote databases to stop them timing out.
Definition: omdatabase.cc:538
<unistd.h>, but with compat.
void mset_expect_order(const Xapian::MSet &A, Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4, Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8, Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
Definition: testutils.cc:225
Class representing a list of search results.
Definition: eset.h:43
Xapian::Document get_document() const
Get the Document object for the current position.
Definition: omenquire.cc:450
const int DB_CREATE_OR_OVERWRITE
Create database if it doesn&#39;t already exist, or overwrite if it does.
Definition: constants.h:38
void set_weighting_scheme(const Weight &weight_)
Set the weighting scheme to use for queries.
Definition: omenquire.cc:819
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
Class representing a query.
Definition: query.h:46
std::string get_data() const
Get data stored in the document.
Definition: omdocument.cc:71
const valueno BAD_VALUENO
Reserved value to indicate "no valueno".
Definition: types.h:125
#define TEST_EQUAL(a, b)
Test for equality of two things.
Definition: testsuite.h:278
Xapian::termcount get_unique_terms(Xapian::docid did) const
Get the number of unique terms in document.
Definition: omdatabase.cc:476
PostingIterator postlist_end(const std::string &) const
Corresponding end iterator to postlist_begin().
Definition: database.h:225
Xapian::Database get_remote_database(const string &dbname, unsigned int timeout)
Definition: apitest.cc:111
void set_collapse_key(Xapian::valueno collapse_key, Xapian::doccount collapse_max=1)
Set the collapse key to use for queries.
Definition: omenquire.cc:848
std::string get_value(Xapian::valueno slot) const
Get value by number.
Definition: omdocument.cc:64
ESetIterator end() const
Return iterator pointing to just after the last item in this ESet.
Definition: eset.h:350
Xapian::doccount get_termfreq(const std::string &tname) const
Get the number of documents in the database indexed by a given term.
Definition: omdatabase.cc:323
A handle representing a document in a Xapian database.
Definition: document.h:61
MyWeight * clone() const override
Clone this object.
Definition: api_db.cc:1883
const int DB_BACKEND_CHERT
Use the chert backend.
Definition: constants.h:170
GrepMatchDecider(const string &needle_)
Definition: api_db.cc:480
Xapian::termcount get_unique_terms() const
Return the number of unique terms in the current document.
static Xapian::Query query(const string &t)
Definition: api_db.cc:50
A relevance set (R-Set).
Definition: enquire.h:60
UnimplementedError indicates an attempt to use an unimplemented feature.
Definition: error.h:325
PostingIterator postlist_begin(const std::string &tname) const
An iterator pointing to the start of the postlist for a given term.
Definition: omdatabase.cc:162
void touch(const string &filename)
Touch a file, just like the Unix "touch" command.
Definition: unixcmds.cc:155
Xapian::doccount get_uncollapsed_matches_lower_bound() const
Lower bound on the total number of matching documents before collapsing.
Definition: omenquire.cc:269
Abstract base class for weighting schemes.
Definition: weight.h:35
Xapian::termcount get_collection_freq(const std::string &tname) const
Return the total number of occurrences of the given term.
Definition: omdatabase.cc:339
docids sort in descending order.
Definition: enquire.h:330
std::string get_description() const
Return a string describing this object.