File: smoketest.php

package info (click to toggle)
xapian-bindings 1.4.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,436 kB
  • sloc: cpp: 379,853; python: 10,780; cs: 9,529; java: 6,949; sh: 4,629; perl: 4,435; makefile: 1,274; ruby: 1,028; php: 586; tcl: 246
file content (724 lines) | stat: -rw-r--r-- 21,891 bytes parent folder | download
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
<?php
// Run this PHP script using 'make check' in the build tree.

/* Simple test to ensure that we can load the xapian module and exercise basic
 * functionality successfully.
 *
 * Copyright (C) 2004-2022 Olly Betts
 * Copyright (C) 2010 Richard Boulton
 *
 * 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
 */

# Die on any error, warning, notice, etc.
function die_on_error($errno, $errstr, $file, $line) {
    if ($file !== Null) {
	print $file;
	if ($line !== Null) print ":$line";
	print ": ";
    }
    print "$errstr\n";
    exit(1);
}
set_error_handler("die_on_error", -1);

# Test the version number reporting functions give plausible results.
$v = Xapian::major_version().'.'.Xapian::minor_version().'.'.Xapian::revision();
$v2 = Xapian::version_string();
if ($v != $v2) {
    print "Unexpected version output ($v != $v2)\n";
    exit(1);
}

$db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
$db2 = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);

# Check handling of Xapian::DocNotFoundError
try {
    $doc2 = $db->get_document(2);
    print "Retrieved non-existent document\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "DocNotFoundError: Docid 2 not found") {
	print "DocNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
	exit(1);
    }
}

# Check QueryParser parsing error.
try {
    $qp = new XapianQueryParser;
    $qp->set_stemmer(new XapianStem("en"));
    $qp->parse_query("test AND");
    print "Successfully parsed bad query\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "QueryParserError: Syntax: <expression> AND <expression>") {
	print "QueryParserError Exception string not as expected, got: '$e->getMessage()'\n";
	exit(1);
    }
}

# Check that open_stub() is wrapped as expected.
try {
    $db = Xapian::auto_open_stub("nosuchdir/nosuchdb");
    print "Opened non-existent stub database\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
	print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
	exit(1);
    }
}

# Check that DB_BACKEND_STUB works as expected.
try {
    $db = new XapianDatabase("nosuchdir/nosuchdb", Xapian::DB_BACKEND_STUB);
    print "Opened non-existent stub database\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
	print "DatabaseNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
	exit(1);
    }
}

# Check that open_stub() writable form is wrapped as expected.
try {
    $db = Xapian::auto_open_stub("nosuchdir/nosuchdb", Xapian::DB_OPEN);
    print "Opened non-existent stub database\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
	print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
	exit(1);
    }
}

# Check that DB_BACKEND_STUB works as expected.
try {
    $db = new XapianWritableDatabase("nosuchdir/nosuchdb",
				     Xapian::DB_OPEN|Xapian::DB_BACKEND_STUB);
    print "Opened non-existent stub database\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== "DatabaseNotFoundError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
	print "DatabaseNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
	exit(1);
    }
}

# Regression test for bug#193, fixed in 1.0.3.
$vrp = new XapianNumberValueRangeProcessor(0, '$', true);
$a = '$10';
$b = '20';
$vrp->apply($a, $b);
if (Xapian::sortable_unserialise($a) != 10) {
    print Xapian::sortable_unserialise($a)." != 10\n";
    exit(1);
}
if (Xapian::sortable_unserialise($b) != 20) {
    print Xapian::sortable_unserialise($b)." != 20\n";
    exit(1);
}

$stem = new XapianStem("english");
if ($stem->get_description() != "Xapian::Stem(english)") {
    print "Unexpected \$stem->get_description()\n";
    exit(1);
}

$doc = new XapianDocument();
$doc->set_data("a\x00b");
if ($doc->get_data() === "a") {
    print "get_data+set_data truncates at a zero byte\n";
    exit(1);
}
if ($doc->get_data() !== "a\x00b") {
    print "get_data+set_data doesn't transparently handle a zero byte\n";
    exit(1);
}
$doc->set_data("is there anybody out there?");
$doc->add_term("XYzzy");
$doc->add_posting($stem->apply("is"), 1);
$doc->add_posting($stem->apply("there"), 2);
$doc->add_posting($stem->apply("anybody"), 3);
$doc->add_posting($stem->apply("out"), 4);
$doc->add_posting($stem->apply("there"), 5);

// Check virtual function dispatch.
if (substr($db->get_description(), 0, 17) !== "WritableDatabase(") {
    print "Unexpected \$db->get_description()\n";
    exit(1);
}
$db->add_document($doc);
if ($db->get_doccount() != 1) {
    print "Unexpected \$db->get_doccount()\n";
    exit(1);
}

$terms = array("smoke", "test", "terms");
$query = new XapianQuery(XapianQuery::OP_OR, $terms);
if ($query->get_description() != "Query((smoke OR test OR terms))") {
    print "Unexpected \$query->get_description()\n";
    exit(1);
}
$query1 = new XapianQuery(XapianQuery::OP_PHRASE, array("smoke", "test", "tuple"));
if ($query1->get_description() != "Query((smoke PHRASE 3 test PHRASE 3 tuple))") {
    print "Unexpected \$query1->get_description()\n";
    exit(1);
}
$query1b = new XapianQuery(XapianQuery::OP_NEAR, array("smoke", "test", "tuple"), 4);
if ($query1b->get_description() != "Query((smoke NEAR 4 test NEAR 4 tuple))") {
    print "Unexpected \$query1b->get_description()\n";
    exit(1);
}
$query2 = new XapianQuery(XapianQuery::OP_XOR, array(new XapianQuery("smoke"), $query1, "string"));
if ($query2->get_description() != "Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))") {
    print "Unexpected \$query2->get_description()\n";
    exit(1);
}
$subqs = array("a", "b");
$query3 = new XapianQuery(XapianQuery::OP_OR, $subqs);
if ($query3->get_description() != "Query((a OR b))") {
    print "Unexpected \$query3->get_description()\n";
    exit(1);
}
$enq = new XapianEnquire($db);

// Check Xapian::BAD_VALUENO is wrapped suitably.
$enq->set_collapse_key(Xapian::BAD_VALUENO);

// Test that the non-constant wrapping prior to 1.4.10 still works.
$enq->set_collapse_key(Xapian::BAD_VALUENO_get());

$enq->set_query(new XapianQuery(XapianQuery::OP_OR, "there", "is"));
$mset = $enq->get_mset(0, 10);
if ($mset->size() != 1) {
    print "Unexpected \$mset->size()\n";
    exit(1);
}
$terms = join(" ", $enq->get_matching_terms($mset->get_hit(0)));
if ($terms != "is there") {
    print "Unexpected matching terms: $terms\n";
    exit(1);
}

# Feature test for MatchDecider
$doc = new XapianDocument();
$doc->set_data("Two");
$doc->add_posting($stem->apply("out"), 1);
$doc->add_posting($stem->apply("outside"), 1);
$doc->add_posting($stem->apply("source"), 2);
$doc->add_value(0, "yes");
$db->add_document($doc);

class testmatchdecider extends XapianMatchDecider {
    function apply($doc) {
	return ($doc->get_value(0) == "yes");
    }
}

$query = new XapianQuery($stem->apply("out"));
$enquire = new XapianEnquire($db);
$enquire->set_query($query);
$mdecider = new testmatchdecider();
$mset = $enquire->get_mset(0, 10, null, $mdecider);
if ($mset->size() != 1) {
    print "Unexpected number of documents returned by match decider (".$mset->size().")\n";
    exit(1);
}
if ($mset->get_docid(0) != 2) {
    print "MatchDecider mset has wrong docid in\n";
    exit(1);
}

class testexpanddecider extends XapianExpandDecider {
    function apply($term) {
	return ($term[0] !== 'a');
    }
}

$enquire = new XapianEnquire($db);
$rset = new XapianRSet();
$rset->add_document(1);
$eset = $enquire->get_eset(10, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, new testexpanddecider());
foreach ($eset->begin() as $t) {
    if ($t[0] === 'a') {
	print "XapianExpandDecider was not used\n";
	exit(1);
    }
}

# Check min_wt argument to get_eset() works (new in 1.2.5).
$eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ);
$min_wt = 0;
foreach ($eset->begin() as $i => $dummy) {
    $min_wt = $i->get_weight();
}
if ($min_wt >= 1.9) {
    print "ESet min weight too high for testcase\n";
    exit(1);
}
$eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, NULL, 1.9);
$min_wt = 0;
foreach ($eset->begin() as $i => $dummy) {
    $min_wt = $i->get_weight();
}
if ($min_wt < 1.9) {
    print "ESet min_wt threshold not applied\n";
    exit(1);
}

if (XapianQuery::OP_ELITE_SET != 10) {
    print "OP_ELITE_SET is XapianQuery::OP_ELITE_SET not 10\n";
    exit(1);
}

# Regression test - overload resolution involving boolean types failed.
$enq->set_sort_by_value(1, TRUE);

# Regression test - fixed in 0.9.10.1.
$oqparser = new XapianQueryParser();
$oquery = $oqparser->parse_query("I like tea");

# Regression test for bug#192 - fixed in 1.0.3.
$enq->set_cutoff(100);

# Check DateRangeProcessor works.
function add_rp_date(&$qp) {
    $rpdate = new XapianDateRangeProcessor(1, Xapian::RP_DATE_PREFER_MDY, 1960);
    $qp->add_rangeprocessor($rpdate);
}
$qp = new XapianQueryParser();
add_rp_date($qp);
$query = $qp->parse_query('12/03/99..12/04/01');
if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
    print "XapianDateRangeProcessor didn't work - result was ".$query->get_description()."\n";
    exit(1);
}

# Check DateValueRangeProcessor works.
function add_vrp_date(&$qp) {
    $vrpdate = new XapianDateValueRangeProcessor(1, 1, 1960);
    $qp->add_valuerangeprocessor($vrpdate);
}
$qp = new XapianQueryParser();
add_vrp_date($qp);
$query = $qp->parse_query('12/03/99..12/04/01');
if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
    print "XapianDateValueRangeProcessor didn't work - result was ".$query->get_description()."\n";
    exit(1);
}

# Feature test for XapianFieldProcessor
class testfieldprocessor extends XapianFieldProcessor {
    static $count = 0;

    function __construct() {
        ++self::$count;
        parent::__construct();
    }

    function __destruct() {
        --self::$count;
    }

    function apply($str) {
	if ($str === 'spam') throw new Exception('already spam');
	return new XapianQuery("spam");
    }
}

$fp = new testfieldprocessor;
if (testfieldprocessor::$count !== 1) {
    print "testfieldprocessor counting not working\n";
    exit(1);
}
{
    # Check object is still usable after being assigned to an object that gets
    # deleted.  The initial development version of PHP8 bindings failed to
    # handle this case.
    $qptmp = new XapianQueryParser;
    $qptmp->add_prefix('spam', $fp);
    unset($qptmp);
}
if (testfieldprocessor::$count === 0) {
    print "testfieldprocessor object deleted early\n";
    exit(1);
}
$qp->add_prefix('spam', $fp);
unset($fp);
$qp->add_boolean_prefix('filter', new testfieldprocessor);
$query = $qp->parse_query('spam:ignored');
if ($query->get_description() !== 'Query(spam)') {
    print "testfieldprocessor didn't work - result was ".$query->get_description()."\n";
    exit(1);
}
$query = $qp->parse_query('filter:ignored');
if ($query->get_description() !== 'Query(0 * spam)') {
    print "Boolean testfieldprocessor didn't work - result was ".$query->get_description()."\n";
    exit(1);
}

try {
    $query = $qp->parse_query('spam:spam');
    print "testfieldprocessor exception not rethrown\n";
    exit(1);
} catch (Exception $e) {
    if ($e->getMessage() !== 'already spam') {
	print "Exception has wrong message\n";
	exit(1);
    }
}

if (testfieldprocessor::$count === 0) {
    print "testfieldprocessor deleted early\n";
    exit(1);
}
unset($qp);
if (testfieldprocessor::$count !== 0) {
    print "testfieldprocessor object not deleted\n";
}

# Test setting and getting metadata
if ($db->get_metadata('Foo') !== '') {
    print "Unexpected value for metadata associated with 'Foo' (expected ''): '".$db->get_metadata('Foo')."'\n";
    exit(1);
}
$db->set_metadata('Foo', 'Foo');
if ($db->get_metadata('Foo') !== 'Foo') {
    print "Unexpected value for metadata associated with 'Foo' (expected 'Foo'): '".$db->get_metadata('Foo')."'\n";
    exit(1);
}

# Test OP_SCALE_WEIGHT and corresponding constructor
$query4 = new XapianQuery(XapianQuery::OP_SCALE_WEIGHT, new XapianQuery('foo'), 5.0);
if ($query4->get_description() != "Query(5 * foo)") {
    print "Unexpected \$query4->get_description()\n";
    exit(1);
}

# Test MultiValueKeyMaker.

$doc = new XapianDocument();
$doc->add_term("foo");
$doc->add_value(0, "ABB");
$db2->add_document($doc);
$doc->add_value(0, "ABC");
$db2->add_document($doc);
$doc->add_value(0, "ABC\0");
$db2->add_document($doc);
$doc->add_value(0, "ABCD");
$db2->add_document($doc);
$doc->add_value(0, "ABC\xff");
$db2->add_document($doc);

$enquire = new XapianEnquire($db2);
$enquire->set_query(new XapianQuery("foo"));

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(5, 4, 3, 2, 1));
}

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0, true);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(1, 2, 3, 4, 5));
}

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0);
    $sorter->add_value(1);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(5, 4, 3, 2, 1));
}

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0, true);
    $sorter->add_value(1);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(1, 2, 3, 4, 5));
}

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0);
    $sorter->add_value(1, true);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(5, 4, 3, 2, 1));
}

{
    $sorter = new XapianMultiValueKeyMaker();
    $sorter->add_value(0, true);
    $sorter->add_value(1, true);
    $enquire->set_sort_by_key($sorter, true);
    $mset = $enquire->get_mset(0, 10);
    mset_expect_order($mset, array(1, 2, 3, 4, 5));
}

# Feature test for ValueSetMatchDecider:
{
    $md = new XapianValueSetMatchDecider(0, true);
    $md->add_value("ABC");
    $doc = new XapianDocument();
    $doc->add_value(0, "ABCD");
    if ($md->apply($doc)) {
	print "Unexpected result from ValueSetMatchDecider->apply(); expected false\n";
	exit(1);
    }

    $doc = new XapianDocument();
    $doc->add_value(0, "ABC");
    if (!$md->apply($doc)) {
	print "Unexpected result from ValueSetMatchDecider->apply(); expected true\n";
	exit(1);
    }

    $mset = $enquire->get_mset(0, 10, 0, null, $md);
    mset_expect_order($mset, array(2));

    $md = new XapianValueSetMatchDecider(0, false);
    $md->add_value("ABC");
    $mset = $enquire->get_mset(0, 10, 0, null, $md);
    mset_expect_order($mset, array(1, 3, 4, 5));
}

function mset_expect_order($mset, $a) {
    if ($mset->size() != sizeof($a)) {
	print "MSet has ".$mset->size()." entries, expected ".sizeof($a)."\n";
	exit(1);
    }
    for ($j = 0; $j < sizeof($a); ++$j) {
	$docid = $mset->get_hit($j)->get_docid();
	if ($docid != $a[$j]) {
	    print "Expected MSet[$j] to be $a[$j], got $docid\n";
	    exit(1);
	}
    }
}

# Feature tests for Query "term" constructor optional arguments:
$query_wqf = new XapianQuery('wqf', 3);
if ($query_wqf->get_description() != 'Query(wqf#3)') {
    print "Unexpected \$query_wqf->get_description():\n";
    print $query_wqf->get_description() . "\n";
    exit(1);
}

$query = new XapianQuery(XapianQuery::OP_VALUE_GE, 0, "100");
if ($query->get_description() != 'Query(VALUE_GE 0 100)') {
    print "Unexpected \$query->get_description():\n";
    print $query->get_description() . "\n";
    exit(1);
}

$query = XapianQuery::MatchAll();
if ($query->get_description() != 'Query(<alldocuments>)') {
    print "Unexpected \$query->get_description():\n";
    print $query->get_description() . "\n";
    exit(1);
}

$query = XapianQuery::MatchNothing();
if ($query->get_description() != 'Query()') {
    print "Unexpected \$query->get_description():\n";
    print $query->get_description() . "\n";
    exit(1);
}

# Test access to matchspy values:
{
    $matchspy = new XapianValueCountMatchSpy(0);
    $enquire->add_matchspy($matchspy);
    $enquire->get_mset(0, 10);
    $values = array();
    foreach ($matchspy->values_begin() as $k => $term) {
	$values[$term] = $k->get_termfreq();
    }
    $expected = array(
        "ABB" => 1,
	"ABC" => 1,
	"ABC\0" => 1,
	"ABCD" => 1,
	"ABC\xff" => 1,
    );
    if ($values != $expected) {
        print "Unexpected matchspy values():\n";
	var_dump($values);
	var_dump($expected);
	print "\n";
	exit(1);
    }
}

{
    class testspy extends XapianMatchSpy {
        static $count = 0;

        function __construct() {
            ++self::$count;
            parent::__construct();
        }

        function __destruct() {
            --self::$count;
        }

	public $matchspy_count = 0;

	function apply($doc, $wt) {
	    if (substr($doc->get_value(0), 0, 3) == "ABC") ++$this->matchspy_count;
	}
    }

    $matchspy = new testspy();
    if (testspy::$count !== 1) {
        print "testspy counting not working\n";
        exit(1);
    }
    $enquire->clear_matchspies();
    $enquire->add_matchspy($matchspy);
    $enquire->get_mset(0, 10);
    if ($matchspy->matchspy_count != 4) {
	print "Unexpected matchspy count of {$matchspy->matchspy_count}\n";
	exit(1);
    }
    unset($matchspy);
    if (testspy::$count === 0) {
        print "testspy object deleted early\n";
        exit(1);
    }
    unset($enquire);
    if (testspy::$count !== 0) {
        print "testspy object not deleted\n";
        exit(1);
    }
}

# Regression test for SWIG bug - it was generating "return $r;" in wrapper
# functions which didn't set $r.
$indexer = new XapianTermGenerator();
$doc = new XapianDocument();

$indexer->set_document($doc);
$indexer->index_text("I ask nothing in return");
$indexer->index_text_without_positions("Tea time");
$indexer->index_text("Return in time");

$s = '';
foreach ($doc->termlist_begin() as $term) {
    $s .= $term . ' ';
}
if ($s !== 'ask i in nothing return tea time ') {
    print "PHP Iterator wrapping of TermIterator doesn't work ($s)\n";
    exit(1);
}

$s = '';
foreach ($doc->termlist_begin() as $k => $term) {
    $s .= $term . ':' . $k->get_wdf() . ' ';
}
if ($s !== 'ask:1 i:1 in:2 nothing:1 return:2 tea:1 time:2 ') {
    print "PHP Iterator wrapping of TermIterator keys doesn't work ($s)\n";
    exit(1);
}

// Test that XapianTermGenerator keeps a reference to XapianStopper.
$indexer->set_stopper_strategy(XapianTermGenerator::STOP_ALL);
{
    $stop = new XapianSimpleStopper();
    $stop->add('a');
    $indexer->set_stopper($stop);
    $stop = null;
}
$indexer->index_text("a b");

# Test GeoSpatial API
$coord = new XapianLatLongCoord();
$coord = new XapianLatLongCoord(-41.288889, 174.777222);

define('COORD_SLOT', 2);
$metric = new XapianGreatCircleMetric();
$range = 42.0;

$centre = new XapianLatLongCoords($coord);
$query = new XapianQuery(new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range));

$db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
$coords = new XapianLatLongCoords();
$coords->append(new XapianLatLongCoord(40.6048, -74.4427));
$doc = new XapianDocument();
$doc->add_term("coffee");
$doc->add_value(COORD_SLOT, $coords->serialise());
$db->add_document($doc);

$centre = new XapianLatLongCoords();
$centre->append(new XapianLatLongCoord(40.6048, -74.4427));

$ps = new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range);
$q = new XapianQuery("coffee");
$q = new XapianQuery(XapianQuery::OP_AND, $q, new XapianQuery($ps));
$q = new XapianQuery(XapianQuery::OP_OR, [$q, XapianQuery::MatchNothing()]);
// Check that we keep a reference via XapianQuery.
$ps = null;

$enq = new XapianEnquire($db);
$enq->set_query($q);
$mset = $enq->get_mset(0, 10);
if ($mset->size() != 1) {
    print "Expected one result with XapianLatLongDistancePostingSource, got ";
    print $mset->size() . "\n";
    exit(1);
}

$s = '';
foreach ($db->allterms_begin() as $k => $term) {
    $s .= "($term:{$k->get_termfreq()})";
}
if ($s !== '(coffee:1)') {
    print "PHP Iterator iteration of allterms doesn't work ($s)\n";
    exit(1);
}

# Test reference tracking and regression test for #659.
$qp = new XapianQueryParser();
{
    $stop = new XapianSimpleStopper();
    $stop->add('a');
    $qp->set_stopper($stop);
    // Test that XapianQueryParser keeps a reference to XapianStopper.
    $stop = null;
}
$query = $qp->parse_query('a b');
if ($query->get_description() !== 'Query(b@2)') {
    print "XapianQueryParser::set_stopper() didn't work as expected - result was ".$query->get_description()."\n";
    exit(1);
}

?>