tests/api_posdb.cc

Go to the documentation of this file.
00001 /* api_posdb.cc: tests which need a backend with positional information
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2002,2003,2004,2005,2006,2007 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include <config.h>
00024 
00025 #include "api_posdb.h"
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 using namespace std;
00031 
00032 #include <xapian.h>
00033 #include "testsuite.h"
00034 #include "testutils.h"
00035 
00036 #include "apitest.h"
00037 
00039 DEFINE_TESTCASE(near1, positional) {
00040     Xapian::Database mydb(get_database("apitest_phrase"));
00041     Xapian::Enquire enquire(mydb);
00042     Xapian::Stem stemmer("english");
00043     enquire.set_weighting_scheme(Xapian::BoolWeight());
00044 
00045     // make a query
00046     vector<Xapian::Query> subqs;
00047     Xapian::Query q;
00048     subqs.push_back(Xapian::Query(stemmer("phrase")));
00049     subqs.push_back(Xapian::Query(stemmer("fridge")));
00050     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00051     enquire.set_query(q);
00052 
00053     // retrieve the top ten results
00054     Xapian::MSet mymset = enquire.get_mset(0, 10);
00055     mset_expect_order(mymset);
00056 
00057     subqs.clear();
00058     subqs.push_back(Xapian::Query(stemmer("phrase")));
00059     subqs.push_back(Xapian::Query(stemmer("near")));
00060     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 2);
00061     enquire.set_query(q);
00062 
00063     // retrieve the top ten results
00064     mymset = enquire.get_mset(0, 10);
00065     mset_expect_order(mymset, 3);
00066 
00067     subqs.clear();
00068     subqs.push_back(Xapian::Query(stemmer("phrase")));
00069     subqs.push_back(Xapian::Query(stemmer("near")));
00070     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 3);
00071     enquire.set_query(q);
00072 
00073     // retrieve the top ten results
00074     mymset = enquire.get_mset(0, 10);
00075     mset_expect_order(mymset, 1, 3);
00076 
00077     subqs.clear();
00078     subqs.push_back(Xapian::Query(stemmer("phrase")));
00079     subqs.push_back(Xapian::Query(stemmer("near")));
00080     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 5);
00081     enquire.set_query(q);
00082 
00083     // retrieve the top ten results
00084     mymset = enquire.get_mset(0, 10);
00085     mset_expect_order(mymset, 1, 3);
00086 
00087     subqs.clear();
00088     subqs.push_back(Xapian::Query(stemmer("phrase")));
00089     subqs.push_back(Xapian::Query(stemmer("near")));
00090     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 6);
00091     enquire.set_query(q);
00092 
00093     // retrieve the top ten results
00094     mymset = enquire.get_mset(0, 10);
00095     mset_expect_order(mymset, 1, 2, 3);
00096 
00097     subqs.clear();
00098     subqs.push_back(Xapian::Query(stemmer("leave")));
00099     subqs.push_back(Xapian::Query(stemmer("fridge")));
00100     subqs.push_back(Xapian::Query(stemmer("on")));
00101     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 3);
00102     enquire.set_query(q);
00103 
00104     // retrieve the top ten results
00105     mymset = enquire.get_mset(0, 10);
00106     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9);
00107 
00108     subqs.clear();
00109     subqs.push_back(Xapian::Query(stemmer("leave")));
00110     subqs.push_back(Xapian::Query(stemmer("fridge")));
00111     subqs.push_back(Xapian::Query(stemmer("on")));
00112     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 4);
00113     enquire.set_query(q);
00114 
00115     // retrieve the top ten results
00116     mymset = enquire.get_mset(0, 10);
00117     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10);
00118 
00119     subqs.clear();
00120     subqs.push_back(Xapian::Query(stemmer("leave")));
00121     subqs.push_back(Xapian::Query(stemmer("fridge")));
00122     subqs.push_back(Xapian::Query(stemmer("on")));
00123     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 5);
00124     enquire.set_query(q);
00125 
00126     // retrieve the top ten results
00127     mymset = enquire.get_mset(0, 10);
00128     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10, 11);
00129 
00130     subqs.clear();
00131     subqs.push_back(Xapian::Query(stemmer("leave")));
00132     subqs.push_back(Xapian::Query(stemmer("fridge")));
00133     subqs.push_back(Xapian::Query(stemmer("on")));
00134     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 6);
00135     enquire.set_query(q);
00136 
00137     // retrieve the top ten results
00138     mymset = enquire.get_mset(0, 10);
00139     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10, 11, 12);
00140 
00141     subqs.clear();
00142     subqs.push_back(Xapian::Query(stemmer("leave")));
00143     subqs.push_back(Xapian::Query(stemmer("fridge")));
00144     subqs.push_back(Xapian::Query(stemmer("on")));
00145     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 7);
00146     enquire.set_query(q);
00147 
00148     // retrieve the top twenty results
00149     mymset = enquire.get_mset(0, 20);
00150     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
00151 
00152     subqs.clear();
00153     subqs.push_back(Xapian::Query(stemmer("leave")));
00154     subqs.push_back(Xapian::Query(stemmer("fridge")));
00155     subqs.push_back(Xapian::Query(stemmer("on")));
00156     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 8);
00157     enquire.set_query(q);
00158 
00159     // retrieve the top ten results
00160     mymset = enquire.get_mset(0, 20);
00161     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
00162 
00163     subqs.clear();
00164     subqs.push_back(Xapian::Query(stemmer("leave")));
00165     subqs.push_back(Xapian::Query(stemmer("fridge")));
00166     subqs.push_back(Xapian::Query(stemmer("on")));
00167     // test really large window size
00168     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 999999999);
00169     enquire.set_query(q);
00170 
00171     // retrieve the top ten results
00172     mymset = enquire.get_mset(0, 20);
00173     mset_expect_order(mymset, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
00174 
00175     return true;
00176 }
00177 
00179 DEFINE_TESTCASE(near2, positional) {
00180     Xapian::Database mydb(get_database("apitest_phrase"));
00181     Xapian::Enquire enquire(mydb);
00182     Xapian::Stem stemmer("english");
00183     enquire.set_weighting_scheme(Xapian::BoolWeight());
00184 
00185     // make a query
00186     vector<Xapian::Query> subqs;
00187     Xapian::Query q;
00188     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00189                             Xapian::Query(stemmer("phrase")),
00190                             Xapian::Query(stemmer("near"))));
00191     subqs.push_back(Xapian::Query(stemmer("and")));
00192     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 2);
00193     enquire.set_query(q);
00194 
00195     // retrieve the top ten results
00196     Xapian::MSet mymset = enquire.get_mset(0, 10);
00197     mset_expect_order(mymset, 1);
00198 
00199     subqs.clear();
00200     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00201                             Xapian::Query(stemmer("phrase")),
00202                             Xapian::Query(stemmer("near"))));
00203     subqs.push_back(Xapian::Query(stemmer("operator")));
00204     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 2);
00205     enquire.set_query(q);
00206 
00207     // retrieve the top ten results
00208     mymset = enquire.get_mset(0, 10);
00209     mset_expect_order(mymset, 2);
00210 
00211     subqs.clear();
00212     subqs.push_back(Xapian::Query(stemmer("operator")));
00213     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00214                             Xapian::Query(stemmer("phrase")),
00215                             Xapian::Query(stemmer("near"))));
00216     q = Xapian::Query(Xapian::Query::OP_NEAR, subqs.begin(), subqs.end(), 2);
00217     enquire.set_query(q);
00218 
00219     // retrieve the top ten results
00220     mymset = enquire.get_mset(0, 10);
00221     mset_expect_order(mymset, 2);
00222 
00223     return true;
00224 }
00225 
00227 DEFINE_TESTCASE(phrase1, positional) {
00228     Xapian::Database mydb(get_database("apitest_phrase"));
00229     Xapian::Enquire enquire(mydb);
00230     Xapian::Stem stemmer("english");
00231     enquire.set_weighting_scheme(Xapian::BoolWeight());
00232 
00233     // make a query
00234     vector<Xapian::Query> subqs;
00235     Xapian::Query q;
00236     subqs.push_back(Xapian::Query(stemmer("phrase")));
00237     subqs.push_back(Xapian::Query(stemmer("fridge")));
00238     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00239     enquire.set_query(q);
00240 
00241     // retrieve the top ten results
00242     Xapian::MSet mymset = enquire.get_mset(0, 10);
00243     mset_expect_order(mymset);
00244 
00245     subqs.clear();
00246     subqs.push_back(Xapian::Query(stemmer("phrase")));
00247     subqs.push_back(Xapian::Query(stemmer("near")));
00248     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00249     enquire.set_query(q);
00250 
00251     // retrieve the top ten results
00252     mymset = enquire.get_mset(0, 10);
00253     mset_expect_order(mymset);
00254 
00255     subqs.clear();
00256     subqs.push_back(Xapian::Query(stemmer("phrase")));
00257     subqs.push_back(Xapian::Query(stemmer("near")));
00258     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 3);
00259     enquire.set_query(q);
00260 
00261     // retrieve the top ten results
00262     mymset = enquire.get_mset(0, 10);
00263     mset_expect_order(mymset, 1);
00264 
00265     subqs.clear();
00266     subqs.push_back(Xapian::Query(stemmer("phrase")));
00267     subqs.push_back(Xapian::Query(stemmer("near")));
00268     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 5);
00269     enquire.set_query(q);
00270 
00271     // retrieve the top ten results
00272     mymset = enquire.get_mset(0, 10);
00273     mset_expect_order(mymset, 1);
00274 
00275     subqs.clear();
00276     subqs.push_back(Xapian::Query(stemmer("phrase")));
00277     subqs.push_back(Xapian::Query(stemmer("near")));
00278     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 6);
00279     enquire.set_query(q);
00280 
00281     // retrieve the top ten results
00282     mymset = enquire.get_mset(0, 10);
00283     mset_expect_order(mymset, 1, 2);
00284 
00285     subqs.clear();
00286     subqs.push_back(Xapian::Query(stemmer("leave")));
00287     subqs.push_back(Xapian::Query(stemmer("fridge")));
00288     subqs.push_back(Xapian::Query(stemmer("on")));
00289     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 3);
00290     enquire.set_query(q);
00291 
00292     // retrieve the top ten results
00293     mymset = enquire.get_mset(0, 10);
00294     mset_expect_order(mymset, 4);
00295 
00296     subqs.clear();
00297     subqs.push_back(Xapian::Query(stemmer("leave")));
00298     subqs.push_back(Xapian::Query(stemmer("fridge")));
00299     subqs.push_back(Xapian::Query(stemmer("on")));
00300     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 4);
00301     enquire.set_query(q);
00302 
00303     // retrieve the top ten results
00304     mymset = enquire.get_mset(0, 10);
00305     mset_expect_order(mymset, 4);
00306 
00307     subqs.clear();
00308     subqs.push_back(Xapian::Query(stemmer("leave")));
00309     subqs.push_back(Xapian::Query(stemmer("fridge")));
00310     subqs.push_back(Xapian::Query(stemmer("on")));
00311     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 5);
00312     enquire.set_query(q);
00313 
00314     // retrieve the top ten results
00315     mymset = enquire.get_mset(0, 10);
00316     mset_expect_order(mymset, 4);
00317 
00318     subqs.clear();
00319     subqs.push_back(Xapian::Query(stemmer("leave")));
00320     subqs.push_back(Xapian::Query(stemmer("fridge")));
00321     subqs.push_back(Xapian::Query(stemmer("on")));
00322     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 6);
00323     enquire.set_query(q);
00324 
00325     // retrieve the top ten results
00326     mymset = enquire.get_mset(0, 10);
00327     mset_expect_order(mymset, 4);
00328 
00329     subqs.clear();
00330     subqs.push_back(Xapian::Query(stemmer("leave")));
00331     subqs.push_back(Xapian::Query(stemmer("fridge")));
00332     subqs.push_back(Xapian::Query(stemmer("on")));
00333     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 7);
00334     enquire.set_query(q);
00335 
00336     // retrieve the top twenty results
00337     mymset = enquire.get_mset(0, 20);
00338     mset_expect_order(mymset, 4);
00339 
00340     subqs.clear();
00341     subqs.push_back(Xapian::Query(stemmer("leave")));
00342     subqs.push_back(Xapian::Query(stemmer("fridge")));
00343     subqs.push_back(Xapian::Query(stemmer("on")));
00344     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 8);
00345     enquire.set_query(q);
00346 
00347     // retrieve the top 20 results
00348     mymset = enquire.get_mset(0, 20);
00349     mset_expect_order(mymset, 4);
00350 
00351     // test really large window size
00352     subqs.clear();
00353     subqs.push_back(Xapian::Query(stemmer("leave")));
00354     subqs.push_back(Xapian::Query(stemmer("fridge")));
00355     subqs.push_back(Xapian::Query(stemmer("on")));
00356     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 999999999);
00357     enquire.set_query(q);
00358 
00359     // retrieve the top 20 results
00360     mymset = enquire.get_mset(0, 20);
00361     mset_expect_order(mymset, 4);
00362 
00363     // regression test (was matching doc 15, should fail)
00364     subqs.clear();
00365     subqs.push_back(Xapian::Query(stemmer("first")));
00366     subqs.push_back(Xapian::Query(stemmer("second")));
00367     subqs.push_back(Xapian::Query(stemmer("third")));
00368     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 9);
00369     enquire.set_query(q);
00370 
00371     // retrieve the top ten results
00372     mymset = enquire.get_mset(0, 10);
00373     mset_expect_order(mymset);
00374 
00375     // regression test (should match doc 15, make sure still does with fix)
00376     subqs.clear();
00377     subqs.push_back(Xapian::Query(stemmer("first")));
00378     subqs.push_back(Xapian::Query(stemmer("second")));
00379     subqs.push_back(Xapian::Query(stemmer("third")));
00380     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 10);
00381     enquire.set_query(q);
00382 
00383     // retrieve the top ten results
00384     mymset = enquire.get_mset(0, 10);
00385     mset_expect_order(mymset, 15);
00386 
00387     // regression test (phrase matching was getting order wrong when
00388     // build_and_tree reordered vector of PostLists)
00389     subqs.clear();
00390     subqs.push_back(Xapian::Query(stemmer("milk")));
00391     subqs.push_back(Xapian::Query(stemmer("rare")));
00392     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00393     enquire.set_query(q);
00394 
00395     // retrieve the top ten results
00396     mymset = enquire.get_mset(0, 10);
00397     mset_expect_order(mymset, 16);
00398 
00399     // regression test (phrase matching was getting order wrong when
00400     // build_and_tree reordered vector of PostLists)
00401     subqs.clear();
00402     subqs.push_back(Xapian::Query(stemmer("rare")));
00403     subqs.push_back(Xapian::Query(stemmer("milk")));
00404     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00405     enquire.set_query(q);
00406 
00407     // retrieve the top ten results
00408     mymset = enquire.get_mset(0, 10);
00409     mset_expect_order(mymset, 17);
00410 
00411     return true;
00412 }
00413 
00415 DEFINE_TESTCASE(phrase2, positional) {
00416     Xapian::Database mydb(get_database("apitest_phrase"));
00417     Xapian::Enquire enquire(mydb);
00418     Xapian::Stem stemmer("english");
00419     enquire.set_weighting_scheme(Xapian::BoolWeight());
00420 
00421     // make a query
00422     vector<Xapian::Query> subqs;
00423     Xapian::Query q;
00424     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00425                             Xapian::Query(stemmer("phrase")),
00426                             Xapian::Query(stemmer("near"))));
00427     subqs.push_back(Xapian::Query(stemmer("and")));
00428     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00429     enquire.set_query(q);
00430 
00431     // retrieve the top ten results
00432     Xapian::MSet mymset = enquire.get_mset(0, 10);
00433     mset_expect_order(mymset);
00434 
00435     subqs.clear();
00436     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00437                             Xapian::Query(stemmer("phrase")),
00438                             Xapian::Query(stemmer("near"))));
00439     subqs.push_back(Xapian::Query(stemmer("operator")));
00440     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00441     enquire.set_query(q);
00442 
00443     // retrieve the top ten results
00444     mymset = enquire.get_mset(0, 10);
00445     mset_expect_order(mymset, 2);
00446 
00447     subqs.clear();
00448     subqs.push_back(Xapian::Query(stemmer("operator")));
00449     subqs.push_back(Xapian::Query(Xapian::Query::OP_AND,
00450                             Xapian::Query(stemmer("phrase")),
00451                             Xapian::Query(stemmer("near"))));
00452     q = Xapian::Query(Xapian::Query::OP_PHRASE, subqs.begin(), subqs.end(), 2);
00453     enquire.set_query(q);
00454 
00455     // retrieve the top ten results
00456     mymset = enquire.get_mset(0, 10);
00457     mset_expect_order(mymset);
00458 
00459     return true;
00460 }
00461 
00463 DEFINE_TESTCASE(poslist1, positional) {
00464     Xapian::Database mydb(get_database("apitest_poslist"));
00465 
00466     Xapian::Stem stemmer("english");
00467     string term = stemmer("sponge");
00468 
00469     Xapian::PositionIterator pli = mydb.positionlist_begin(2, term);
00470 
00471     TEST(pli != mydb.positionlist_end(2, term));
00472     TEST(*pli == 1);
00473     pli++;
00474     TEST(pli != mydb.positionlist_end(2, term));
00475     TEST(*pli == 2);
00476     pli++;
00477     TEST(pli != mydb.positionlist_end(2, term));
00478     TEST(*pli == 3);
00479     pli++;
00480     TEST(pli != mydb.positionlist_end(2, term));
00481     TEST(*pli == 5);
00482     pli++;
00483     TEST(pli != mydb.positionlist_end(2, term));
00484     TEST(*pli == 8);
00485     pli++;
00486     TEST(pli != mydb.positionlist_end(2, term));
00487     TEST(*pli == 13);
00488     pli++;
00489     TEST(pli != mydb.positionlist_end(2, term));
00490     TEST(*pli == 21);
00491     pli++;
00492     TEST(pli != mydb.positionlist_end(2, term));
00493     TEST(*pli == 34);
00494     pli++;
00495     TEST(pli == mydb.positionlist_end(2, term));
00496 
00497     TEST_EXCEPTION(Xapian::DocNotFoundError, mydb.positionlist_begin(55, term));
00498 
00499     /* FIXME: what exception should be thrown here?  Quartz throws
00500      * Xapian::DocNotFoundError, and InMemory throws Xapian::RangeError.
00501      */
00502     TEST_EXCEPTION(Xapian::RuntimeError, mydb.positionlist_begin(2, "adskfjadsfa"));
00503     TEST_EXCEPTION(Xapian::DocNotFoundError, mydb.positionlist_begin(55, "adskfjadsfa"));
00504 
00505     return true;
00506 }
00507 
00508 DEFINE_TESTCASE(poslist2, positional && writable) {
00509     Xapian::WritableDatabase db = get_writable_database();
00510 
00511     Xapian::Document doc;
00512     doc.add_term("nopos");
00513     Xapian::docid did = db.add_document(doc);
00514 
00515     TEST_EXCEPTION(Xapian::RangeError,
00516         // Check what happens when term doesn't exist
00517         Xapian::PositionIterator i = db.positionlist_begin(did, "nosuchterm");
00518     );
00519 
00520     TEST_EXCEPTION(Xapian::DocNotFoundError,
00521         // Check what happens when the document doesn't even exist
00522         Xapian::PositionIterator i = db.positionlist_begin(123, "nosuchterm");
00523     );
00524 
00525     {
00526         Xapian::PositionIterator i = db.positionlist_begin(did, "nopos");
00527         TEST_EQUAL(i, db.positionlist_end(did, "nopos"));
00528     }
00529 
00530     Xapian::Document doc2 = db.get_document(did);
00531 
00532     Xapian::TermIterator term = doc2.termlist_begin();
00533 
00534     {
00535         Xapian::PositionIterator i = term.positionlist_begin();
00536         TEST_EQUAL(i, term.positionlist_end());
00537     }
00538 
00539     Xapian::Document doc3;
00540     doc3.add_posting("hadpos", 1);
00541     Xapian::docid did2 = db.add_document(doc3);
00542 
00543     Xapian::Document doc4 = db.get_document(did2);
00544     doc4.remove_posting("hadpos", 1);
00545     db.replace_document(did2, doc4);
00546 
00547     {
00548         Xapian::PositionIterator i = db.positionlist_begin(did2, "hadpos");
00549         TEST_EQUAL(i, db.positionlist_end(did2, "hadpos"));
00550     }
00551 
00552     db.delete_document(did);
00553     TEST_EXCEPTION(Xapian::DocNotFoundError,
00554         // Check what happens when the document doesn't even exist
00555         // (but once did)
00556         Xapian::PositionIterator i = db.positionlist_begin(did, "nosuchterm");
00557     );
00558 
00559     return true;
00560 }
00561 
00564 DEFINE_TESTCASE(poslist3, positional && writable) {
00565     Xapian::WritableDatabase db = get_writable_database();
00566 
00567     vector<Xapian::termpos> positions;
00568 
00569     Xapian::Document document;
00570     document.add_posting("foo", 5);
00571     document.add_posting("foo", 8);
00572     document.add_posting("foo", 10);
00573     document.add_posting("foo", 12);
00574     db.add_document(document);
00575 
00576     TEST_EXCEPTION(Xapian::RangeError, db.positionlist_begin(1, "foobar"));
00577     TEST_EXCEPTION(Xapian::DocNotFoundError, db.positionlist_begin(2, "foo"));
00578     TEST_EXCEPTION(Xapian::DocNotFoundError, db.positionlist_begin(2, "foobar"));
00579     Xapian::PositionIterator pl = db.positionlist_begin(1, "foo");
00580     Xapian::PositionIterator pl_end = db.positionlist_end(1, "foo");
00581 
00582     TEST(pl != pl_end);
00583     TEST_EQUAL(*pl, 5);
00584     ++pl;
00585     TEST(pl != pl_end);
00586     TEST_EQUAL(*pl, 8);
00587     ++pl;
00588     TEST(pl != pl_end);
00589     TEST_EQUAL(*pl, 10);
00590     ++pl;
00591     TEST(pl != pl_end);
00592     TEST_EQUAL(*pl, 12);
00593     ++pl;
00594     TEST(pl == pl_end);
00595 
00596     pl = db.positionlist_begin(1, "foo");
00597     pl.skip_to(5);
00598     TEST(pl != pl_end);
00599     TEST_EQUAL(*pl, 5);
00600 
00601     pl.skip_to(9);
00602     TEST(pl != pl_end);
00603     TEST_EQUAL(*pl, 10);
00604 
00605     ++pl;
00606     TEST(pl != pl_end);
00607     TEST_EQUAL(*pl, 12);
00608 
00609     pl.skip_to(12);
00610     TEST(pl != pl_end);
00611     TEST_EQUAL(*pl, 12);
00612 
00613     pl.skip_to(13);
00614     TEST(pl == pl_end);
00615 
00616     return true;
00617 }
00618 
00619 // Regression test - in 0.9.4 (and many previous versions) you couldn't get a
00620 // PositionIterator from a TermIterator from Database::termlist_begin().
00621 //
00622 // Also test that positionlist_count() is implemented for this case, which it
00623 // wasn't in 1.0.2 and earlier.
00624 DEFINE_TESTCASE(positfromtermit1, positional) {
00625     Xapian::Database db(get_database("apitest_phrase"));
00626     Xapian::TermIterator t(db.termlist_begin(7));
00627     TEST_NOT_EQUAL(t, db.termlist_end(7));
00628     Xapian::PositionIterator p = t.positionlist_begin();
00629     TEST_NOT_EQUAL(p, t.positionlist_end());
00630 
00631     try {
00632         TEST_EQUAL(t.positionlist_count(), 1);
00633         t.skip_to("on");
00634         TEST_NOT_EQUAL(t, db.termlist_end(7));
00635         TEST_EQUAL(t.positionlist_count(), 2);
00636     } catch (const Xapian::UnimplementedError &) {
00637         SKIP_TEST("TermList::positionlist_count() not yet implemented for this backend");
00638     }
00639 
00640     return true;
00641 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.