File: api_none.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (834 lines) | stat: -rw-r--r-- 22,165 bytes parent folder | download | duplicates (2)
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
/** @file api_none.cc
 * @brief tests which don't need a backend
 */
/* Copyright (C) 2009 Richard Boulton
 * Copyright (C) 2009,2010,2011,2013,2014,2015,2016 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#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"

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);
    return true;
}

// 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));
    return true;
}

/// 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());
    return true;
}

/// 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);
    return true;
}

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());
    return true;
}

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);
    return true;
}

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);
    return true;
}

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);
    return true;
}

/// 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))");
    return true;
}

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&)
    {
	return Xapian::Query::MatchAll;
    }
};

/// Check reference counting of user-subclassable classes.
DEFINE_TESTCASE(subclassablerefcount1, !backend) {
    bool gone_auto, gone;

    // 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 VRP 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_instrusive_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.
    }

    return true;
}

class TestFieldProcessor : public Xapian::FieldProcessor {
    DestroyedFlag destroyed;

  public:
    TestFieldProcessor(bool & destroyed_) : destroyed(destroyed_) { }

    Xapian::Query operator()(const string &str) {
	return Xapian::Query(str);
    }
};

/// Check reference counting of user-subclassable classes.
DEFINE_TESTCASE(subclassablerefcount2, !backend) {
    bool gone_auto, gone;

    // 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);

    return true;
}

class TestMatchSpy : public Xapian::MatchSpy {
    DestroyedFlag destroyed;

  public:
    TestMatchSpy(bool & destroyed_) : destroyed(destroyed_) { }

    void operator()(const Xapian::Document &, double) { }
};

/// Check reference counting of MatchSpy.
DEFINE_TESTCASE(subclassablerefcount3, backend) {
    Xapian::Database db = get_database("apitest_simpledata");

    bool gone_auto, gone;

    // 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);

    return true;
}

class TestStopper : public Xapian::Stopper {
    DestroyedFlag destroyed;

  public:
    TestStopper(bool & destroyed_) : destroyed(destroyed_) { }

    bool operator()(const std::string&) const { return true; }
};

/// Check reference counting of Stopper with QueryParser.
DEFINE_TESTCASE(subclassablerefcount4, !backend) {
    bool gone_auto, gone;

    // 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);

    return true;
}

/// Check reference counting of Stopper with TermGenerator.
DEFINE_TESTCASE(subclassablerefcount5, !backend) {
    bool gone_auto, gone;

    // 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);

    return true;
}

class TestKeyMaker : public Xapian::KeyMaker {
    DestroyedFlag destroyed;

  public:
    TestKeyMaker(bool & destroyed_) : destroyed(destroyed_) { }

    string operator()(const Xapian::Document&) const { return string(); }
};

/// Check reference counting of KeyMaker.
DEFINE_TESTCASE(subclassablerefcount6, backend) {
    Xapian::Database db = get_database("apitest_simpledata");

    bool gone_auto, gone;

    // 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);

    return true;
}

class TestExpandDecider : public Xapian::ExpandDecider {
    DestroyedFlag destroyed;

  public:
    TestExpandDecider(bool & destroyed_) : destroyed(destroyed_) { }

    bool operator()(const string&) const { 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;

    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);

    return true;
}

class TestValueRangeProcessor : public Xapian::ValueRangeProcessor {
    DestroyedFlag destroyed;

  public:
    TestValueRangeProcessor(bool & destroyed_) : destroyed(destroyed_) { }

    Xapian::valueno operator()(std::string &, std::string &) {
	return 42;
    }
};

/// Check reference counting of user-subclassable classes.
DEFINE_TESTCASE(subclassablerefcount8, !backend) {
    bool gone_auto, gone;

    // Simple test of release().
    {
	Xapian::ValueRangeProcessor * vrp = new TestValueRangeProcessor(gone);
	TEST(!gone);
	Xapian::QueryParser qp;
	qp.add_valuerangeprocessor(vrp->release());
	TEST(!gone);
    }
    TEST(gone);

    // Check a second call to release() has no effect.
    {
	Xapian::ValueRangeProcessor * vrp = new TestValueRangeProcessor(gone);
	TEST(!gone);
	Xapian::QueryParser qp;
	qp.add_valuerangeprocessor(vrp->release());
	vrp->release();
	TEST(!gone);
    }
    TEST(gone);

    // Test reference counting works, and that a VRP with automatic storage
    // works OK.
    {
	TestValueRangeProcessor vrp_auto(gone_auto);
	TEST(!gone_auto);
	{
	    Xapian::QueryParser qp1;
	    {
		Xapian::QueryParser qp2;
		Xapian::ValueRangeProcessor * vrp;
		vrp = new TestValueRangeProcessor(gone);
		TEST(!gone);
		qp1.add_valuerangeprocessor(vrp->release());
		TEST(!gone);
		qp2.add_valuerangeprocessor(vrp);
		TEST(!gone);
		qp2.add_valuerangeprocessor(&vrp_auto);
		TEST(!gone);
		TEST(!gone_auto);
	    }
	    TEST(!gone);
	}
	TEST(gone);
	TEST(!gone_auto);
    }
    TEST(gone_auto);

    // Regression test for initial implementation, where ~opt_instrusive_ptr()
    // checked the reference of the object, which may have already been deleted
    // if it wasn't been reference counted.
    {
	Xapian::QueryParser qp;
	{
	    Xapian::ValueRangeProcessor * vrp =
		new TestValueRangeProcessor(gone);
	    TEST(!gone);
	    qp.add_valuerangeprocessor(vrp);
	    delete vrp;
	    TEST(gone);
	}
	// At the end of this block, qp is destroyed, but mustn't dereference
	// the pointer it has to vrp.  If it does, that should get caught
	// when tests are run under valgrind.
    }

    return true;
}

/// 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(data='\\xc0\\x80\\xf5\\x80\\x80\\x80\\xfe\\xff')");
    doc.set_data(string("\x00\x1f", 2));
    TEST_EQUAL(doc.get_description(),
	      "Document(data='\\x00\\x1f')");
    // Check that backslashes are encoded so output isn't ambiguous.
    doc.set_data("back\\slash");
    TEST_EQUAL(doc.get_description(),
	      "Document(data='back\\x5cslash')");
    return true;
}