1 # Simple test that we can load the xapian module and run a simple test
3 # Copyright (C) 2004,2006,2009,2011,2017 Olly Betts
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 # We need at least Tcl version 8
22 package require xapian
1.0.0
24 # Test the version number reporting functions give plausible results.
25 set v
[format {%d.
%d.
%d
} [xapian
::major_version] \
26 [xapian
::minor_version] \
28 set v2
[xapian
::version_string]
30 puts stderr
"Unexpected version output ($v != $v2)"
34 xapian
::Stem stem
"english"
35 if { [stem get_description
] != "Xapian::Stem(english)" } {
36 puts stderr
"Unexpected stem.get_description()"
40 # Tcl stores zero bytes as \xc0\x80 so this doesn't actually exactly test that
41 # strings containing zero bytes can be passed from Tcl to C++ and back. But
42 # it is still a useful test to perform.
45 if { [doc get_data
] == "a" } {
46 puts stderr
"get_data+set_data truncates at a zero byte"
49 if { [doc get_data
] != "a\0b" } {
50 puts stderr
"get_data+set_data doesn't transparently handle a zero byte"
54 doc set_data
"is there anybody out there?"
56 doc add_posting
[stem apply
"is"] 1
57 doc add_posting
[stem apply
"there"] 2
58 doc add_posting
[stem apply
"anybody"] 3
59 doc add_posting
[stem apply
"out"] 4
60 doc add_posting
[stem apply
"there"] 5
62 xapian
::WritableDatabase db
"" $xapian::DB_BACKEND_INMEMORY
64 if { [db get_doccount
] != 1 } {
65 puts stderr
"Unexpected db.get_doccount()"
69 set terms
[list "smoke" "test" "terms"]
70 xapian
::Query query
$xapian::Query_OP_OR $terms
71 if { [query get_description
] != "Query((smoke OR test OR terms))" } {
72 puts stderr
"Unexpected query.get_description()"
75 xapian
::Query query1
$xapian::Query_OP_PHRASE [list "smoke" "test" "tuple"]
76 if { [query1 get_description
] != "Query((smoke PHRASE 3 test PHRASE 3 tuple))" } {
77 puts stderr
"Unexpected query1.get_description()"
80 xapian
::Query smoke
"smoke"
81 xapian
::Query query2
$xapian::Query_OP_XOR [list smoke query1
"string" ]
82 if { [query2 get_description
] != "Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))" } {
83 puts stderr
"Unexpected query2 get_description"
86 set subqs
[list "a" "b"]
87 xapian
::Query query3
$xapian::Query_OP_OR $subqs
88 if { [query3 get_description
] != "Query((a OR b))" } {
89 puts stderr
"Unexpected query3 get_description"
93 if { [$xapian::Query_MatchAll get_description
] != "Query(<alldocuments>)" } {
94 puts stderr
"Unexpected Query_MatchAll get_description"
98 if { [$xapian::Query_MatchNothing get_description
] != "Query()" } {
99 puts stderr
"Unexpected Query_MatchNothing get_description"
103 xapian
::Enquire enq db
104 xapian
::Query q
$xapian::Query_OP_OR "there" "is"
106 set mset
[enq get_mset
0 10]
107 if { [$mset size
] != 1 } {
108 puts stderr
"Unexpected number of entries in mset ([$mset size] != 1)"
111 set terms
[join [enq get_matching_terms
[$mset get_hit
0]] " "]
112 if { $terms != "is there" } {
113 puts stderr
"Unexpected terms"
117 # Check exception handling for Xapian::DocNotFoundError
119 xapian
::QueryParser qp
120 [qp parse_query
"test AND"]
121 puts stderr
"QueryParser doesn't report errors"
124 # We expect QueryParserError
125 if { $errorCode != "XAPIAN QueryParserError" } {
126 puts stderr
"Unexpected errorCode from parsing bad query"
127 puts stderr
"errorCode: $errorCode"
128 puts stderr
"message: $e"
131 if { $e != "Syntax: <expression> AND <expression>" } {
132 puts stderr
"Unexpected exception message from parsing bad query"
133 puts stderr
"errorCode: $errorCode"
134 puts stderr
"message: $e"
139 # Check exception handling for Xapian::DocNotFoundError
141 xapian
::Document doc2
[db get_document
2]
142 puts stderr
"Retrieved non-existent document"
145 # We expect DocNotFoundError
146 if { $errorCode != "XAPIAN DocNotFoundError" } {
147 puts stderr
"Unexpected exception from accessing non-existent document:"
148 puts stderr
"errorCode: $errorCode"
149 puts stderr
"message: $e"
154 if { $xapian::Query_OP_ELITE_SET != 10 } {
155 puts stderr
"OP_ELITE_SET == $xapian::Query_OP_ELITE_SET not 10"
159 xapian
::Query query4
$xapian::Query_OP_SCALE_WEIGHT smoke
5
160 if { [query4 get_description
] != "Query(5 * smoke)" } {
161 puts stderr
"Unexpected query4.get_description()"