Ticket #90: smoketest.tcl

File smoketest.tcl, 3.9 KB (added by Michael Schlenker, 18 years ago)

Improved smoketest.tcl file

Line 
1# Simple test that we can load the xapian module and run a simple test
2#
3# Copyright (C) 2004,2006 Olly Betts
4#
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.
9#
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.
14#
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
19
20# We need at least Tcl version 8
21package require Tcl 8
22
23load [file join ".libs" xapian.so]
24
25# Test the version number reporting functions give plausible results.
26set v [format {%d.%d.%d} [xapian::major_version] \
27 [xapian::minor_version] \
28 [xapian::revision]]
29set v2 [xapian::version_string]
30if { $v != $v2 } {
31 puts stderr "Unexpected version output ($v != $v2)"
32 exit 1
33}
34
35# Check if the xapian version matches the provided tcl package version
36if {$v != [package provide xapian]} {
37 puts stderr "Unexpected package version output ($v != [package provide xapian])"
38 exit 1
39}
40
41xapian::Stem stem "english"
42if { [stem get_description] != "Xapian::Stem(english)" } {
43 puts stderr "Unexpected stem.get_description()"
44 exit 1
45}
46
47xapian::Document doc
48doc set_data "a\u0000b"
49if { [doc get_data] == "a" } {
50 puts stderr "get_data+set_data truncates at a zero byte"
51 exit 1
52}
53if { [doc get_data] != "a\u0000b" } {
54 puts stderr "get_data+set_data doesn't transparently handle a zero byte"
55 exit 1
56}
57doc set_data "is there anybody out there?"
58doc add_term "XYzzy"
59doc add_posting [stem stem_word "is"] 1
60doc add_posting [stem stem_word "there"] 2
61doc add_posting [stem stem_word "anybody"] 3
62doc add_posting [stem stem_word "out"] 4
63doc add_posting [stem stem_word "there"] 5
64
65xapian::WritableDatabase db [xapian::inmemory_open]
66db add_document doc
67if { [db get_doccount] != 1 } {
68 puts stderr "Unexpected db.get_doccount()"
69 exit 1
70}
71
72set terms [list "smoke" "test" "terms"]
73xapian::Query query $xapian::Query_OP_OR $terms
74if { [query get_description] != "Xapian::Query((smoke OR test OR terms))" } {
75 puts stderr "Unexpected query.get_description()"
76 exit 1
77}
78xapian::Query query1 $xapian::Query_OP_PHRASE [list "smoke" "test" "tuple"]
79if { [query1 get_description] != "Xapian::Query((smoke PHRASE 3 test PHRASE 3 tuple))" } {
80 puts stderr "Unexpected query1.get_description()"
81 exit 1
82}
83xapian::Query smoke "smoke"
84xapian::Query query2 $xapian::Query_OP_XOR [list smoke query1 "string" ]
85if { [query2 get_description] != "Xapian::Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))" } {
86 puts stderr "Unexpected query2.get_description()"
87 exit 1
88}
89set subqs [list "a" "b"]
90xapian::Query query3 $xapian::Query_OP_OR $subqs
91if { [query3 get_description] != "Xapian::Query((a OR b))" } {
92 puts stderr "Unexpected query3.get_description()"
93 exit 1
94}
95
96xapian::Enquire enq db
97xapian::Query q $xapian::Query_OP_OR "there" "is"
98enq set_query q
99set mset [enq get_mset 0 10]
100if { [$mset size] != 1 } {
101 puts stderr "Unexpected number of entries in mset ([$mset size] != 1)"
102 exit 1
103}
104set terms [join [enq get_matching_terms [$mset get_hit 0]] " "]
105if { $terms != "is there" } {
106 puts stderr "Unexpected terms"
107 exit 1
108}
109
110# Check exception handling for Xapian::DocNotFoundError
111if [catch {
112 xapian::Document doc2 [db get_document 2]
113 puts stderr "Retrieved non-existent document"
114 exit 1
115} e] {
116 # We expect DocNotFoundError
117 if { $errorCode != "XAPIAN DocNotFoundError" } {
118 puts stderr "Unexpected exception from accessing non-existent document:"
119 puts stderr "errorCode: $errorCode"
120 puts stderr "message: $e"
121 exit 1
122 }
123}