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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
/** @file
* @brief tests which don't need a backend
*/
/* Copyright (C) 2009 Richard Boulton
* Copyright (C) 2009-2026 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see
* <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "api_none.h"
#define XAPIAN_DEPRECATED(D) D
#include <xapian.h>
#include "apitest.h"
#include "str.h"
#include "testsuite.h"
#include "testutils.h"
#include <string_view>
#include <vector>
using namespace std;
// Check the version functions give consistent results.
DEFINE_TESTCASE(version1, !backend) {
string version = str(Xapian::major_version());
version += '.';
version += str(Xapian::minor_version());
version += '.';
version += str(Xapian::revision());
TEST_EQUAL(Xapian::version_string(), version);
}
// Regression test: various methods on Database() used to segfault or cause
// division by 0. Fixed in 1.1.4 and 1.0.18. Ticket#415.
DEFINE_TESTCASE(nosubdatabases1, !backend) {
Xapian::Database db;
TEST(db.get_metadata("foo").empty());
TEST_EQUAL(db.metadata_keys_begin(), db.metadata_keys_end());
TEST_EXCEPTION(Xapian::InvalidOperationError, db.termlist_begin(1));
TEST_EQUAL(db.allterms_begin(), db.allterms_end());
TEST_EQUAL(db.allterms_begin("foo"), db.allterms_end("foo"));
TEST_EXCEPTION(Xapian::InvalidOperationError, db.positionlist_begin(1, "foo"));
TEST_EQUAL(db.get_lastdocid(), 0);
TEST_EQUAL(db.valuestream_begin(7), db.valuestream_end(7));
TEST_EXCEPTION(Xapian::InvalidOperationError, db.get_doclength(1));
TEST_EXCEPTION(Xapian::InvalidOperationError, db.get_unique_terms(1));
TEST_EXCEPTION(Xapian::InvalidOperationError, db.get_document(1));
Xapian::WritableDatabase wdb;
TEST_EXCEPTION(Xapian::InvalidOperationError, wdb.begin_transaction());
TEST_EXCEPTION(Xapian::InvalidOperationError, wdb.commit_transaction());
TEST_EXCEPTION(Xapian::InvalidOperationError, wdb.cancel_transaction());
}
/// Feature test for Document::add_boolean_term(), new in 1.0.18/1.1.4.
DEFINE_TESTCASE(document1, !backend) {
Xapian::Document doc;
doc.add_boolean_term("Hxapian.org");
TEST_EQUAL(doc.termlist_count(), 1);
Xapian::TermIterator t = doc.termlist_begin();
TEST(t != doc.termlist_end());
TEST_EQUAL(*t, "Hxapian.org");
TEST_EQUAL(t.get_wdf(), 0);
TEST(++t == doc.termlist_end());
doc.remove_term("Hxapian.org");
TEST_EQUAL(doc.termlist_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
}
/// Regression test - the docid wasn't initialised prior to 1.0.22/1.2.4.
DEFINE_TESTCASE(document2, !backend) {
Xapian::Document doc;
// The return value is uninitialised, so running under valgrind this
// will fail reliably prior to the fix.
TEST_EQUAL(doc.get_docid(), 0);
}
/// Feature tests for Document::clear_terms().
DEFINE_TESTCASE(documentclearterms1, !backend) {
{
Xapian::Document doc;
doc.add_boolean_term("Hlocalhost");
doc.add_term("hello");
doc.add_term("there", 2);
doc.add_posting("positional", 1);
doc.add_posting("information", 2, 3);
TEST_EQUAL(doc.termlist_count(), 5);
TEST(doc.termlist_begin() != doc.termlist_end());
doc.clear_terms();
TEST_EQUAL(doc.termlist_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
// Test clear_terms() when there are no terms.
doc.clear_terms();
TEST_EQUAL(doc.termlist_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
}
{
// Test clear_terms() when there have never been any terms.
Xapian::Document doc;
doc.clear_terms();
TEST_EQUAL(doc.termlist_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
}
}
/// Feature tests for Document::clear_values().
DEFINE_TESTCASE(documentclearvalues1, !backend) {
{
Xapian::Document doc;
doc.add_value(37, "hello");
doc.add_value(42, "world");
TEST_EQUAL(doc.values_count(), 2);
TEST(doc.values_begin() != doc.values_end());
doc.clear_values();
TEST_EQUAL(doc.values_count(), 0);
TEST(doc.values_begin() == doc.values_end());
// Test clear_values() when there are no values.
doc.clear_values();
TEST_EQUAL(doc.values_count(), 0);
TEST(doc.values_begin() == doc.values_end());
}
{
// Test clear_values() when there have never been any values.
Xapian::Document doc;
doc.clear_values();
TEST_EQUAL(doc.values_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
}
}
/// Feature tests for errors for empty terms.
DEFINE_TESTCASE(documentemptyterm1, !backend) {
Xapian::Document doc;
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.add_boolean_term(string()));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.add_term(string()));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.add_posting(string(), 1));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.add_posting(string(), 2, 3));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.remove_term(string()));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.remove_posting(string(), 1));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.remove_posting(string(), 2, 3));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.remove_postings(string(), 2, 3));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
doc.remove_postings(string(), 2, 3, 4));
}
DEFINE_TESTCASE(emptyquery4, !backend) {
// Test we get an empty query from applying any of the following ops to
// an empty list of subqueries.
Xapian::Query q;
TEST(Xapian::Query(q.OP_AND, &q, &q).empty());
TEST(Xapian::Query(q.OP_OR, &q, &q).empty());
TEST(Xapian::Query(q.OP_AND_NOT, &q, &q).empty());
TEST(Xapian::Query(q.OP_XOR, &q, &q).empty());
TEST(Xapian::Query(q.OP_AND_MAYBE, &q, &q).empty());
TEST(Xapian::Query(q.OP_FILTER, &q, &q).empty());
TEST(Xapian::Query(q.OP_NEAR, &q, &q).empty());
TEST(Xapian::Query(q.OP_PHRASE, &q, &q).empty());
TEST(Xapian::Query(q.OP_ELITE_SET, &q, &q).empty());
TEST(Xapian::Query(q.OP_SYNONYM, &q, &q).empty());
TEST(Xapian::Query(q.OP_MAX, &q, &q).empty());
}
DEFINE_TESTCASE(singlesubquery1, !backend) {
// Test that we get just the subquery if we apply any of the following
// ops to just that subquery.
#define singlesubquery1_(OP) \
TEST_STRINGS_EQUAL(Xapian::Query(q->OP, q, q + 1).get_description(),\
"Query(test)")
Xapian::Query q[1] = { Xapian::Query("test") };
singlesubquery1_(OP_AND);
singlesubquery1_(OP_OR);
singlesubquery1_(OP_AND_NOT);
singlesubquery1_(OP_XOR);
singlesubquery1_(OP_AND_MAYBE);
singlesubquery1_(OP_FILTER);
singlesubquery1_(OP_NEAR);
singlesubquery1_(OP_PHRASE);
singlesubquery1_(OP_ELITE_SET);
singlesubquery1_(OP_SYNONYM);
singlesubquery1_(OP_MAX);
}
DEFINE_TESTCASE(singlesubquery2, !backend) {
// Like the previous test, but using MatchNothing as the subquery.
#define singlesubquery2_(OP) \
TEST_STRINGS_EQUAL(Xapian::Query(q->OP, q, q + 1).get_description(),\
"Query()")
Xapian::Query q[1] = { Xapian::Query::MatchNothing };
singlesubquery2_(OP_AND);
singlesubquery2_(OP_OR);
singlesubquery2_(OP_AND_NOT);
singlesubquery2_(OP_XOR);
singlesubquery2_(OP_AND_MAYBE);
singlesubquery2_(OP_FILTER);
singlesubquery2_(OP_NEAR);
singlesubquery2_(OP_PHRASE);
singlesubquery2_(OP_ELITE_SET);
singlesubquery2_(OP_SYNONYM);
singlesubquery2_(OP_MAX);
}
DEFINE_TESTCASE(singlesubquery3, !backend) {
// Like the previous test, but using MatchAll as the subquery.
#define singlesubquery3_(OP) \
TEST_STRINGS_EQUAL(Xapian::Query(q->OP, q, q + 1).get_description(),\
"Query(<alldocuments>)")
Xapian::Query q[1] = { Xapian::Query::MatchAll };
singlesubquery3_(OP_AND);
singlesubquery3_(OP_OR);
singlesubquery3_(OP_AND_NOT);
singlesubquery3_(OP_XOR);
singlesubquery3_(OP_AND_MAYBE);
singlesubquery3_(OP_FILTER);
// OP_NEAR and OP_PHRASE over MatchAll doesn't really make sense.
singlesubquery3_(OP_ELITE_SET);
singlesubquery3_(OP_SYNONYM);
singlesubquery3_(OP_MAX);
}
DEFINE_TESTCASE(pairwisequery1, !backend) {
// Test that constructing from char* and const char* gives the same result
// as explicitly constructing Query objects for the subqueries. This
// serves as a regression test for using char*/const char* not compiling
// with Xapian <= 1.4.30.
#define lit_a "a"
#define lit_b "b"
char aa[] = lit_a;
char ab[] = lit_b;
const char* ca = aa;
const char* cb = ab;
Xapian::Query qa(aa);
Xapian::Query qb(ab);
#define pairwisequery1_(OP) do {\
auto expect = Xapian::Query(qa.OP, lit_a, lit_b).get_description(); \
TEST_STRINGS_EQUAL(expect, \
Xapian::Query(qa.OP, aa, ab).get_description()); \
TEST_STRINGS_EQUAL(expect, \
Xapian::Query(qa.OP, ca, cb).get_description()); \
TEST_STRINGS_EQUAL(expect, \
Xapian::Query(qa.OP, qa, qb).get_description()); \
} while (false)
pairwisequery1_(OP_AND);
pairwisequery1_(OP_OR);
pairwisequery1_(OP_AND_NOT);
pairwisequery1_(OP_XOR);
pairwisequery1_(OP_AND_MAYBE);
pairwisequery1_(OP_FILTER);
pairwisequery1_(OP_NEAR);
pairwisequery1_(OP_PHRASE);
pairwisequery1_(OP_ELITE_SET);
pairwisequery1_(OP_SYNONYM);
pairwisequery1_(OP_MAX);
}
/// Check we no longer combine wqf for same term at the same position.
DEFINE_TESTCASE(combinewqfnomore1, !backend) {
Xapian::Query q(Xapian::Query::OP_OR,
Xapian::Query("beer", 1, 1),
Xapian::Query("beer", 1, 1));
// Prior to 1.3.0, we would have given beer@2, but we decided that wasn't
// really useful or helpful.
TEST_EQUAL(q.get_description(), "Query((beer@1 OR beer@1))");
}
class DestroyedFlag {
bool & destroyed;
public:
DestroyedFlag(bool & destroyed_) : destroyed(destroyed_) {
destroyed = false;
}
~DestroyedFlag() {
destroyed = true;
}
};
class TestRangeProcessor : public Xapian::RangeProcessor {
DestroyedFlag destroyed;
public:
TestRangeProcessor(bool & destroyed_)
: Xapian::RangeProcessor(0), destroyed(destroyed_) { }
Xapian::Query operator()(const std::string&, const std::string&) override {
return Xapian::Query::MatchAll;
}
};
/// Check reference counting of user-subclassable classes.
DEFINE_TESTCASE(subclassablerefcount1, !backend) {
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::RangeProcessor * rp = new TestRangeProcessor(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.add_rangeprocessor(rp->release());
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::RangeProcessor * rp = new TestRangeProcessor(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.add_rangeprocessor(rp->release());
rp->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a RangeProcessor with automatic
// storage works OK.
{
TestRangeProcessor rp_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::QueryParser qp1;
{
Xapian::QueryParser qp2;
Xapian::RangeProcessor * rp;
rp = new TestRangeProcessor(gone);
TEST(!gone);
qp1.add_rangeprocessor(rp->release());
TEST(!gone);
qp2.add_rangeprocessor(rp);
TEST(!gone);
qp2.add_rangeprocessor(&rp_auto);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
// Regression test for initial implementation, where ~opt_intrusive_ptr()
// checked the reference of the object, which may have already been deleted
// if it wasn't been reference counted.
{
Xapian::QueryParser qp;
{
Xapian::RangeProcessor * rp = new TestRangeProcessor(gone);
TEST(!gone);
qp.add_rangeprocessor(rp);
delete rp;
TEST(gone);
}
// At the end of this block, qp is destroyed, but mustn't dereference
// the pointer it has to rp. If it does, that should get caught
// when tests are run under valgrind.
}
}
class TestFieldProcessor : public Xapian::FieldProcessor {
DestroyedFlag destroyed;
public:
TestFieldProcessor(bool & destroyed_) : destroyed(destroyed_) { }
Xapian::Query operator()(const string& str) override {
return Xapian::Query(str);
}
};
/// Check reference counting of user-subclassable classes.
DEFINE_TESTCASE(subclassablerefcount2, !backend) {
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::FieldProcessor * proc = new TestFieldProcessor(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.add_prefix("foo", proc->release());
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::FieldProcessor * proc = new TestFieldProcessor(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.add_prefix("foo", proc->release());
proc->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a FieldProcessor with automatic
// storage works OK.
{
TestFieldProcessor proc_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::QueryParser qp1;
{
Xapian::QueryParser qp2;
Xapian::FieldProcessor * proc;
proc = new TestFieldProcessor(gone);
TEST(!gone);
qp1.add_prefix("foo", proc->release());
TEST(!gone);
qp2.add_prefix("foo", proc);
TEST(!gone);
qp2.add_prefix("bar", &proc_auto);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
}
class TestMatchSpy : public Xapian::MatchSpy {
DestroyedFlag destroyed;
public:
TestMatchSpy(bool & destroyed_) : destroyed(destroyed_) { }
void operator()(const Xapian::Document&, double) override { }
};
/// Check reference counting of MatchSpy.
DEFINE_TESTCASE(subclassablerefcount3, backend) {
Xapian::Database db = get_database("apitest_simpledata");
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::MatchSpy * spy = new TestMatchSpy(gone);
TEST(!gone);
Xapian::Enquire enquire(db);
enquire.add_matchspy(spy->release());
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::MatchSpy * spy = new TestMatchSpy(gone);
TEST(!gone);
Xapian::Enquire enquire(db);
enquire.add_matchspy(spy->release());
spy->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a MatchSpy with automatic
// storage works OK.
{
TestMatchSpy spy_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::Enquire enq1(db);
{
Xapian::Enquire enq2(db);
Xapian::MatchSpy * spy;
spy = new TestMatchSpy(gone);
TEST(!gone);
enq1.add_matchspy(spy->release());
TEST(!gone);
enq2.add_matchspy(spy);
TEST(!gone);
enq2.add_matchspy(&spy_auto);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
}
class TestStopper : public Xapian::Stopper {
DestroyedFlag destroyed;
public:
TestStopper(bool & destroyed_) : destroyed(destroyed_) { }
bool operator()(const std::string&) const override { return true; }
};
/// Check reference counting of Stopper with QueryParser.
DEFINE_TESTCASE(subclassablerefcount4, !backend) {
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.set_stopper(stopper->release());
TEST(!gone);
}
TEST(gone);
// Test that setting a new stopper causes the previous one to be released.
{
bool gone0;
Xapian::Stopper * stopper0 = new TestStopper(gone0);
TEST(!gone0);
Xapian::QueryParser qp;
qp.set_stopper(stopper0->release());
TEST(!gone0);
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
qp.set_stopper(stopper->release());
TEST(gone0);
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
Xapian::QueryParser qp;
qp.set_stopper(stopper->release());
stopper->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a Stopper with automatic
// storage works OK.
{
TestStopper stopper_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::QueryParser qp1;
{
Xapian::QueryParser qp2;
Xapian::Stopper * stopper;
stopper = new TestStopper(gone);
TEST(!gone);
qp1.set_stopper(stopper->release());
TEST(!gone);
qp2.set_stopper(stopper);
TEST(!gone);
qp2.set_stopper(&stopper_auto);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
}
/// Check reference counting of Stopper with TermGenerator.
DEFINE_TESTCASE(subclassablerefcount5, !backend) {
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
Xapian::TermGenerator indexer;
indexer.set_stopper(stopper->release());
TEST(!gone);
}
TEST(gone);
// Test that setting a new stopper causes the previous one to be released.
{
bool gone0;
Xapian::Stopper * stopper0 = new TestStopper(gone0);
TEST(!gone0);
Xapian::TermGenerator indexer;
indexer.set_stopper(stopper0->release());
TEST(!gone0);
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
indexer.set_stopper(stopper->release());
TEST(gone0);
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::Stopper * stopper = new TestStopper(gone);
TEST(!gone);
Xapian::TermGenerator indexer;
indexer.set_stopper(stopper->release());
stopper->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a Stopper with automatic
// storage works OK.
{
TestStopper stopper_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::TermGenerator indexer1;
{
Xapian::TermGenerator indexer2;
Xapian::Stopper * stopper;
stopper = new TestStopper(gone);
TEST(!gone);
indexer1.set_stopper(stopper->release());
TEST(!gone);
indexer2.set_stopper(stopper);
TEST(!gone);
indexer2.set_stopper(&stopper_auto);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
}
class TestKeyMaker : public Xapian::KeyMaker {
DestroyedFlag destroyed;
public:
TestKeyMaker(bool & destroyed_) : destroyed(destroyed_) { }
string operator()(const Xapian::Document&) const override {
return string();
}
};
/// Check reference counting of KeyMaker.
DEFINE_TESTCASE(subclassablerefcount6, backend) {
Xapian::Database db = get_database("apitest_simpledata");
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
// Simple test of release().
{
Xapian::KeyMaker * keymaker = new TestKeyMaker(gone);
TEST(!gone);
Xapian::Enquire enq(db);
enq.set_sort_by_key(keymaker->release(), false);
TEST(!gone);
}
TEST(gone);
// Test that setting a new keymaker causes the previous one to be released.
{
bool gone0;
Xapian::KeyMaker * keymaker0 = new TestKeyMaker(gone0);
TEST(!gone0);
Xapian::Enquire enq(db);
enq.set_sort_by_key(keymaker0->release(), false);
TEST(!gone0);
Xapian::KeyMaker * keymaker = new TestKeyMaker(gone);
TEST(!gone);
enq.set_sort_by_key_then_relevance(keymaker->release(), false);
TEST(gone0);
TEST(!gone);
}
TEST(gone);
// Check a second call to release() has no effect.
{
Xapian::KeyMaker * keymaker = new TestKeyMaker(gone);
TEST(!gone);
Xapian::Enquire enq(db);
enq.set_sort_by_key(keymaker->release(), false);
keymaker->release();
TEST(!gone);
}
TEST(gone);
// Test reference counting works, and that a KeyMaker with automatic
// storage works OK.
{
TestKeyMaker keymaker_auto(gone_auto);
TEST(!gone_auto);
{
Xapian::Enquire enq1(db);
{
Xapian::Enquire enq2(db);
Xapian::KeyMaker * keymaker;
keymaker = new TestKeyMaker(gone);
TEST(!gone);
enq1.set_sort_by_key(keymaker->release(), false);
TEST(!gone);
enq2.set_sort_by_relevance_then_key(keymaker, false);
TEST(!gone);
enq2.set_sort_by_key_then_relevance(&keymaker_auto, false);
TEST(!gone);
TEST(!gone_auto);
}
TEST(!gone);
}
TEST(gone);
TEST(!gone_auto);
}
TEST(gone_auto);
}
class TestExpandDecider : public Xapian::ExpandDecider {
DestroyedFlag destroyed;
public:
TestExpandDecider(bool & destroyed_) : destroyed(destroyed_) { }
bool operator()(const string&) const override { return true; }
};
/// Check reference counting of ExpandDecider.
DEFINE_TESTCASE(subclassablerefcount7, backend) {
Xapian::Database db = get_database("apitest_simpledata");
Xapian::Enquire enq(db);
Xapian::RSet rset;
rset.add_document(1);
bool gone_auto, gone;
#ifdef _MSC_VER
// MSVC incorrectly warns these are potentially uninitialised. It's
// unhelpful to always initialise these as that could mask if a genuine bug
// were introduced (which currently would likely be caught by a warning
// from a smarter compiler).
gone_auto = gone = false;
#endif
for (int flags = 0;
flags <= Xapian::Enquire::INCLUDE_QUERY_TERMS;
flags += Xapian::Enquire::INCLUDE_QUERY_TERMS) {
// Test of auto lifetime ExpandDecider.
{
TestExpandDecider edecider_auto(gone_auto);
TEST(!gone_auto);
(void)enq.get_eset(5, rset, 0, &edecider_auto);
TEST(!gone_auto);
}
TEST(gone_auto);
// Simple test of release().
{
Xapian::ExpandDecider * edecider = new TestExpandDecider(gone);
TEST(!gone);
(void)enq.get_eset(5, rset, 0, edecider);
TEST(!gone);
delete edecider;
TEST(gone);
}
// Test that a released ExpandDecider gets cleaned up by get_eset().
{
Xapian::ExpandDecider * edecider = new TestExpandDecider(gone);
TEST(!gone);
(void)enq.get_eset(5, rset, 0, edecider->release());
TEST(gone);
}
// Check a second call to release() has no effect.
{
Xapian::ExpandDecider * edecider = new TestExpandDecider(gone);
TEST(!gone);
edecider->release();
TEST(!gone);
(void)enq.get_eset(5, rset, 0, edecider->release());
TEST(gone);
}
}
// Test combinations of released/non-released with ExpandDeciderAnd.
{
TestExpandDecider edecider_auto(gone_auto);
TEST(!gone_auto);
Xapian::ExpandDecider * edecider = new TestExpandDecider(gone);
TEST(!gone);
(void)enq.get_eset(5, rset, 0,
(new Xapian::ExpandDeciderAnd(
&edecider_auto,
edecider->release()))->release());
TEST(!gone_auto);
TEST(gone);
}
TEST(gone_auto);
{
TestExpandDecider edecider_auto(gone_auto);
TEST(!gone_auto);
Xapian::ExpandDecider * edecider = new TestExpandDecider(gone);
TEST(!gone);
(void)enq.get_eset(5, rset, 0,
(new Xapian::ExpandDeciderAnd(
edecider->release(),
&edecider_auto))->release());
TEST(!gone_auto);
TEST(gone);
}
TEST(gone_auto);
}
/// Check encoding of non-UTF8 document data.
DEFINE_TESTCASE(nonutf8docdesc1, !backend) {
Xapian::Document doc;
doc.set_data("\xc0\x80\xf5\x80\x80\x80\xfe\xff");
TEST_EQUAL(doc.get_description(),
"Document(docid=0, data=\\xc0\\x80\\xf5\\x80\\x80\\x80\\xfe\\xff)");
doc.set_data(string("\x00\x1f", 2));
TEST_EQUAL(doc.get_description(),
"Document(docid=0, data=\\x00\\x1f)");
// Check that backslashes are encoded so output isn't ambiguous.
doc.set_data("back\\slash");
TEST_EQUAL(doc.get_description(),
"Document(docid=0, data=back\\x5cslash)");
}
DEFINE_TESTCASE(orphaneddoctermitor1, !backend) {
Xapian::TermIterator t;
{
Xapian::Document doc;
doc.add_term("foo");
t = doc.termlist_begin();
}
TEST_EQUAL(*t, "foo");
}
/** Test removal of terms from a document while iterating over them.
*
* Prior to 1.4.6 the underlying iterator was invalidated when
* preinc == false, leading to undefined behaviour (typically a segmentation
* fault).
*/
DEFINE_TESTCASE(deletewhileiterating1, !backend) {
for (bool preinc : { false, true }) {
Xapian::Document doc;
Xapian::TermGenerator indexer;
indexer.set_document(doc);
indexer.index_text("Pull the rug out from under ourselves", 1, "S");
Xapian::TermIterator term_iterator = doc.termlist_begin();
term_iterator.skip_to("S");
while (term_iterator != doc.termlist_end()) {
const string& term = *term_iterator;
if (!startswith(term, "S")) {
break;
}
if (preinc) ++term_iterator;
doc.remove_term(term);
if (!preinc) ++term_iterator;
}
TEST_EQUAL(doc.termlist_count(), 0);
TEST(doc.termlist_begin() == doc.termlist_end());
}
}
/// Feature test for Document::remove_postings().
DEFINE_TESTCASE(removepostings, !backend) {
Xapian::Document doc;
// Add Fibonacci sequence as positions.
Xapian::termpos prev_pos = 1;
Xapian::termpos pos = 1;
while (pos < 1000) {
doc.add_posting("foo", pos);
auto new_pos = prev_pos + pos;
prev_pos = pos;
pos = new_pos;
}
// Check we added exactly one term.
TEST_EQUAL(doc.termlist_count(), 1);
Xapian::TermIterator t = doc.termlist_begin();
auto num_pos = t.positionlist_count();
TEST_EQUAL(t.get_wdf(), num_pos);
Xapian::PositionIterator pi = t.positionlist_begin();
// Out of order is a no-op.
TEST_EQUAL(doc.remove_postings("foo", 2, 1), 0);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos);
TEST_EQUAL(t.get_wdf(), num_pos);
// 6 and 7 aren't in the sequence.
TEST_EQUAL(doc.remove_postings("foo", 6, 7), 0);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos);
TEST_EQUAL(t.get_wdf(), num_pos);
// Beyond the end of the positions.
TEST_EQUAL(doc.remove_postings("foo", 1000, 2000), 0);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos);
TEST_EQUAL(t.get_wdf(), num_pos);
// 1, 2, 3 are in the sequence, 4 isn't.
TEST_EQUAL(doc.remove_postings("foo", 1, 4), 3);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos - 3);
TEST_EQUAL(t.get_wdf(), num_pos - 3);
// Test an existing PositionIterator isn't affected.
TEST_EQUAL(*pi, 1);
TEST_EQUAL(*++pi, 2);
TEST_EQUAL(*++pi, 3);
TEST_EQUAL(*++pi, 5);
// Remove the end position.
TEST_EQUAL(doc.remove_postings("foo", 876, 987), 1);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos - 4);
TEST_EQUAL(t.get_wdf(), num_pos - 4);
// Remove a range in the middle.
TEST_EQUAL(doc.remove_postings("foo", 33, 233), 5);
t = doc.termlist_begin();
TEST_EQUAL(t.positionlist_count(), num_pos - 9);
TEST_EQUAL(t.get_wdf(), num_pos - 9);
// Check the expected positions are left.
t = doc.termlist_begin();
static const Xapian::termpos expected[] = { 5, 8, 13, 21, 377, 610, 9999 };
const Xapian::termpos* expect = expected;
for (auto p = t.positionlist_begin(); p != t.positionlist_end(); ++p) {
TEST_EQUAL(*p, *expect);
++expect;
}
TEST_EQUAL(*expect, 9999);
TEST_EQUAL(t.get_wdf(), 6);
pi = t.positionlist_begin();
// Test remove_position().
doc.remove_posting("foo", 8);
// Test an existing PositionIterator isn't affected.
TEST_EQUAL(*pi, 5);
TEST_EQUAL(*++pi, 8);
// Check removing all positions removes the term too if the wdf reaches 0
// (since 2.0.0).
TEST_EQUAL(doc.remove_postings("foo", 5, 1000), 5);
t = doc.termlist_begin();
TEST(t == doc.termlist_end());
// Test the removing all positions doesn't remove the term if the wdf is
// still non-zero.
doc.add_posting("foo", 123, 2);
TEST_EQUAL(doc.remove_postings("foo", 1, 200), 1);
t = doc.termlist_begin();
TEST(t != doc.termlist_end());
TEST(t.positionlist_begin() == t.positionlist_end());
TEST_EQUAL(t.get_wdf(), 1);
// Test removing the last posting is handled correctly (this case is
// special-cased internally to be O(1)).
t = doc.termlist_begin();
doc.add_posting("foo", 12);
doc.add_posting("foo", 23);
pi = t.positionlist_begin();
doc.remove_posting("foo", 23);
TEST_EQUAL(*pi, 12);
++pi;
TEST_EQUAL(*pi, 23);
++pi;
TEST(pi == t.positionlist_end());
}
[[noreturn]]
static void
errorcopyctor_helper(Xapian::Error& error)
{
// GCC 9 was giving a warning on the next line with -Wdeprecated-copy
// (which is enabled by -Wextra).
throw error;
}
/// Regression test for warning with GCC 9.
DEFINE_TESTCASE(errorcopyctor, !backend) {
Xapian::RangeError e("test");
try {
errorcopyctor_helper(e);
} catch (Xapian::Error&) {
return;
}
FAIL_TEST("Expected exception to be thrown");
}
DEFINE_TESTCASE(emptydbbounds, !backend) {
Xapian::Database db;
TEST_EQUAL(db.get_doclength_lower_bound(), 0);
TEST_EQUAL(db.get_doclength_upper_bound(), 0);
// We always returned 1 here in the initial implementation.
TEST_EQUAL(db.get_unique_terms_lower_bound(), 0);
TEST_EQUAL(db.get_unique_terms_upper_bound(), 0);
}
// Test ESetIterator iterator_traits.
DEFINE_TESTCASE(stlesetiterator, !backend) {
Xapian::ESet eset;
vector<string> v;
// This gave a compile error with stdc++ and -DGLIBCXX_DEBUG in 1.4.30:
v.insert(v.begin(), eset.begin(), eset.end());
}
// Test MSetIterator iterator_traits.
DEFINE_TESTCASE(stlmsetiterator, !backend) {
Xapian::MSet mset;
vector<Xapian::docid> v;
// In Xapian <= 1.4.30 this gave a compile error with libc++, or
// with stdc++ and -DGLIBCXX_DEBUG:
v.insert(v.begin(), mset.begin(), mset.end());
}
// Test PositionIterator iterator_traits.
DEFINE_TESTCASE(stlpositioniterator, !backend) {
Xapian::Database db;
vector<Xapian::termpos> v;
if (db.get_doccount() > 0) {
// In Xapian <= 1.4.30 this gave a compile error with stdc++ and
// -DGLIBCXX_DEBUG:
v.insert(v.begin(),
db.positionlist_begin(1, ""),
db.positionlist_end(1, ""));
}
}
// Test PostingIterator iterator_traits.
DEFINE_TESTCASE(stlpostingiterator, !backend) {
Xapian::Database db;
vector<Xapian::docid> v;
// In Xapian <= 1.4.30 this gave a compile error with stdc++ and
// -DGLIBCXX_DEBUG:
v.insert(v.begin(), db.postlist_begin(""), db.postlist_end(""));
}
// Test TermIterator iterator_traits.
DEFINE_TESTCASE(stltermiterator, !backend) {
Xapian::Document doc;
vector<string> v;
// In Xapian <= 1.4.30 this gave a compile error with stdc++ and
// -DGLIBCXX_DEBUG:
v.insert(v.begin(), doc.termlist_begin(), doc.termlist_end());
}
// Test Utf8Iterator iterator_traits.
DEFINE_TESTCASE(stlutf8iterator, !backend) {
vector<unsigned> v;
// In Xapian <= 1.4.30 this gave a compile error with stdc++ and
// -DGLIBCXX_DEBUG:
v.insert(v.begin(), Xapian::Utf8Iterator(""), Xapian::Utf8Iterator());
}
// Test ValueIterator iterator_traits.
DEFINE_TESTCASE(stlvalueiterator, !backend) {
Xapian::Document doc;
vector<string> v;
// In Xapian <= 1.4.30 this gave a compile error with stdc++ and
// -DGLIBCXX_DEBUG:
v.insert(v.begin(), doc.values_begin(), doc.values_end());
}
|