Stop forward declaring global QueryOptimiser in API header
[xapian.git] / xapian-bindings / tcl8 / smoketest.tcl
blob8261a400b2175a83f83a768cf6beb2539a990e5b
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
18 # USA
20 # We need at least Tcl version 8
21 package require Tcl 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] \
27 [xapian::revision]]
28 set v2 [xapian::version_string]
29 if { $v != $v2 } {
30 puts stderr "Unexpected version output ($v != $v2)"
31 exit 1
34 xapian::Stem stem "english"
35 if { [stem get_description] != "Xapian::Stem(english)" } {
36 puts stderr "Unexpected stem.get_description()"
37 exit 1
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.
43 xapian::Document doc
44 doc set_data "a\0b"
45 if { [doc get_data] == "a" } {
46 puts stderr "get_data+set_data truncates at a zero byte"
47 exit 1
49 if { [doc get_data] != "a\0b" } {
50 puts stderr "get_data+set_data doesn't transparently handle a zero byte"
51 exit 1
54 doc set_data "is there anybody out there?"
55 doc add_term "XYzzy"
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
63 db add_document doc
64 if { [db get_doccount] != 1 } {
65 puts stderr "Unexpected db.get_doccount()"
66 exit 1
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()"
73 exit 1
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()"
78 exit 1
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"
84 exit 1
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"
90 exit 1
93 if { [$xapian::Query_MatchAll get_description] != "Query(<alldocuments>)" } {
94 puts stderr "Unexpected Query_MatchAll get_description"
95 exit 1
98 if { [$xapian::Query_MatchNothing get_description] != "Query()" } {
99 puts stderr "Unexpected Query_MatchNothing get_description"
100 exit 1
103 xapian::Enquire enq db
104 xapian::Query q $xapian::Query_OP_OR "there" "is"
105 enq set_query q
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)"
109 exit 1
111 set terms [join [enq get_matching_terms [$mset get_hit 0]] " "]
112 if { $terms != "is there" } {
113 puts stderr "Unexpected terms"
114 exit 1
117 # Check exception handling for Xapian::DocNotFoundError
118 if [catch {
119 xapian::QueryParser qp
120 [qp parse_query "test AND"]
121 puts stderr "QueryParser doesn't report errors"
122 exit 1
123 } e] {
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"
129 exit 1
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"
135 exit 1
139 # Check exception handling for Xapian::DocNotFoundError
140 if [catch {
141 xapian::Document doc2 [db get_document 2]
142 puts stderr "Retrieved non-existent document"
143 exit 1
144 } e] {
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"
150 exit 1
154 if { $xapian::Query_OP_ELITE_SET != 10 } {
155 puts stderr "OP_ELITE_SET == $xapian::Query_OP_ELITE_SET not 10"
156 exit 1
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()"
162 exit 1
165 exit 0