1 // Simple test that we can load the xapian module and run a simple test
3 // Copyright (C) 2004,2005,2006,2007,2008,2011,2016 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 class TestMatchDecider
: Xapian
.MatchDecider
{
21 public override bool Apply(Xapian
.Document doc
) {
22 return (doc
.GetValue(0) == "yes");
27 public static void Main() {
29 // Test the version number reporting functions give plausible
32 v
+= Xapian
.Version
.Major();
34 v
+= Xapian
.Version
.Minor();
36 v
+= Xapian
.Version
.Revision();
37 string v2
= Xapian
.Version
.String();
39 System
.Console
.WriteLine("Unexpected version output (" + v
+ " != " + v2
+ ")");
40 System
.Environment
.Exit(1);
43 Xapian
.Stem stem
= new Xapian
.Stem("english");
44 Xapian
.Document doc
= new Xapian
.Document();
45 // Currently SWIG doesn't generate zero-byte clean code for
46 // transferring strings between C# and C++.
49 if (doc.GetData() == "a") {
50 System.Console.WriteLine("GetData+SetData truncates at a zero byte");
51 System.Environment.Exit(1);
53 if (doc.GetData() != "a\0b") {
54 System.Console.WriteLine("GetData+SetData doesn't transparently handle a zero byte");
55 System.Environment.Exit(1);
58 doc
.SetData("is there anybody out there?");
60 doc
.AddPosting(stem
.Apply("is"), 1);
61 doc
.AddPosting(stem
.Apply("there"), 2);
62 doc
.AddPosting(stem
.Apply("anybody"), 3);
63 doc
.AddPosting(stem
.Apply("out"), 4);
64 doc
.AddPosting(stem
.Apply("there"), 5);
66 Xapian
.WritableDatabase db
= new Xapian
.WritableDatabase("", Xapian
.Xapian
.DB_BACKEND_INMEMORY
);
68 if (db
.GetDocCount() != 1) {
69 System
.Environment
.Exit(1);
72 if (doc
.TermListCount() != 5) {
73 System
.Environment
.Exit(1);
76 Xapian
.TermIterator i
= doc
.TermListBegin();
77 while (i
!= doc
.TermListEnd()) {
82 System
.Environment
.Exit(1);
85 // Check exception handling for Xapian::DocNotFoundError.
87 Xapian
.Document doc2
= db
.GetDocument(2);
88 System
.Console
.WriteLine("Retrieved non-existent document: " + doc2
.ToString());
89 System
.Environment
.Exit(1);
90 } catch (System
.Exception e
) {
91 // We expect DocNotFoundError
92 if (e
.Message
.Substring(0, 16) != "DocNotFoundError") {
93 System
.Console
.WriteLine("Unexpected exception from accessing non-existent document: " + e
.Message
);
94 System
.Environment
.Exit(1);
98 // Check QueryParser parsing error.
100 Xapian
.QueryParser qp
= new Xapian
.QueryParser();
101 qp
.ParseQuery("test AND");
102 System
.Console
.WriteLine("Successfully parsed bad query");
103 System
.Environment
.Exit(1);
104 } catch (System
.Exception e
) {
105 if (e
.Message
!= "QueryParserError: Syntax: <expression> AND <expression>") {
106 System
.Console
.WriteLine("Exception string not as expected, got: '" + e
.Message
+ "'");
107 System
.Environment
.Exit(1);
112 Xapian
.QueryParser qp
= new Xapian
.QueryParser();
113 // FIXME: It would be better if the (uint) cast wasn't required
115 qp
.ParseQuery("hello world", (uint)Xapian
.QueryParser
.feature_flag
.FLAG_BOOLEAN
);
118 if (Xapian
.Query
.MatchAll
.GetDescription() != "Query(<alldocuments>)") {
119 System
.Console
.WriteLine("Unexpected Query.MatchAll.toString()");
120 System
.Environment
.Exit(1);
123 if (Xapian
.Query
.MatchNothing
.GetDescription() != "Query()") {
124 System
.Console
.WriteLine("Unexpected Query.MatchNothing.toString()");
125 System
.Environment
.Exit(1);
128 // Check that OP_ELITE_SET works (in 0.9.6 and earlier it had the
129 // wrong value in C#).
131 Xapian
.Query foo
= new Xapian
.Query(Xapian
.Query
.op
.OP_OR
, "hello", "world");
132 Xapian
.Query foo2
= new Xapian
.Query(Xapian
.Query
.op
.OP_ELITE_SET
, foo
, foo
);
133 foo
= foo2
; // Avoid "unused variable" warning.
134 } catch (System
.Exception e
) {
135 System
.Console
.WriteLine("Using OP_ELITE_SET cause exception '" + e
.Message
+ "'");
136 System
.Environment
.Exit(1);
139 // Feature test for MatchDecider.
140 doc
= new Xapian
.Document();
142 doc
.AddPosting(stem
.Apply("out"), 1);
143 doc
.AddPosting(stem
.Apply("source"), 2);
144 doc
.AddValue(0, "yes");
147 Xapian
.Query query
= new Xapian
.Query(stem
.Apply("out"));
148 Xapian
.Enquire enquire
= new Xapian
.Enquire(db
);
149 enquire
.SetQuery(query
);
150 Xapian
.MSet mset
= enquire
.GetMSet(0, 10, null, new TestMatchDecider());
151 if (mset
.Size() != 1) {
152 System
.Console
.WriteLine("MatchDecider found " + mset
.Size().ToString() + " documents, expected 1");
153 System
.Environment
.Exit(1);
155 if (mset
.GetDocId(0) != 2) {
156 System
.Console
.WriteLine("MatchDecider mset has wrong docid in");
157 System
.Environment
.Exit(1);
160 mset
= enquire
.GetMSet(0, 10);
161 for (Xapian
.MSetIterator m
= mset
.Begin(); m
!= mset
.End(); m
++) {
162 // In Xapian 1.2.6 and earlier, the iterator would become
163 // eligible for garbage collection after being advanced.
164 // It didn't actually get garbage collected often, but when
165 // it did, it caused a crash. Here we force a GC run to make
166 // this issue manifest if it is present.
168 System
.GC
.WaitForPendingFinalizers();
171 // Test setting and getting metadata
172 if (db
.GetMetadata("Foo") != "") {
173 System
.Console
.WriteLine("db.GetMetadata(\"Foo\") returned wrong value \"" + db
.GetMetadata("Foo") + "\" - expected \"\"");
174 System
.Environment
.Exit(1);
176 db
.SetMetadata("Foo", "Foo");
177 if (db
.GetMetadata("Foo") != "Foo") {
178 System
.Console
.WriteLine("db.GetMetadata(\"Foo\") returned wrong value \"" + db
.GetMetadata("Foo") + "\" - expected \"Foo\"");
179 System
.Environment
.Exit(1);
182 // Test OP_SCALE_WEIGHT and corresponding constructor
183 Xapian
.Query query4
= new Xapian
.Query(Xapian
.Query
.op
.OP_SCALE_WEIGHT
, new Xapian
.Query("foo"), 5.0);
184 if (query4
.GetDescription() != "Query(5 * foo)") {
185 System
.Console
.WriteLine("Unexpected query4.GetDescription()");
186 System
.Environment
.Exit(1);
189 } catch (System
.Exception e
) {
190 System
.Console
.WriteLine("Exception: " + e
.ToString());
191 System
.Environment
.Exit(1);