tests/api_sorting.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include "api_sorting.h"
00024 
00025 #include <xapian.h>
00026 
00027 #include "apitest.h"
00028 #include "testutils.h"
00029 
00030 using namespace std;
00031 
00032 DEFINE_TESTCASE(sortfunctor1,backend && !remote) {
00033     Xapian::Enquire enquire(get_database("apitest_sortrel"));
00034     enquire.set_query(Xapian::Query("woman"));
00035 
00036     {
00037         const int keys[] = { 3, 1 };
00038         Xapian::MultiValueSorter sorter(keys, keys + 2);
00039 
00040         enquire.set_sort_by_key(&sorter);
00041         Xapian::MSet mset = enquire.get_mset(0, 10);
00042         mset_expect_order(mset, 2, 6, 7, 1, 3, 4, 5, 8, 9);
00043     }
00044 
00045     {
00046         Xapian::MultiValueSorter sorter;
00047         sorter.add(3);
00048         sorter.add(1, false);
00049 
00050         enquire.set_sort_by_key(&sorter);
00051         Xapian::MSet mset = enquire.get_mset(0, 10);
00052         mset_expect_order(mset, 7, 6, 2, 8, 9, 4, 5, 1, 3);
00053     }
00054 
00055     {
00056         Xapian::MultiValueSorter sorter;
00057         sorter.add(13); // Value 13 isn't set.
00058         sorter.add(3);
00059         sorter.add(1, false);
00060 
00061         enquire.set_sort_by_key(&sorter);
00062         Xapian::MSet mset = enquire.get_mset(0, 10);
00063         mset_expect_order(mset, 7, 6, 2, 8, 9, 4, 5, 1, 3);
00064     }
00065 
00066     {
00067         Xapian::MultiValueSorter sorter;
00068         sorter.add(10); // Value 10 isn't always set.
00069         sorter.add(1, false);
00070 
00071         enquire.set_sort_by_key(&sorter);
00072         Xapian::MSet mset = enquire.get_mset(0, 10);
00073         mset_expect_order(mset, 8, 9, 4, 5, 1, 3, 7, 6, 2);
00074     }
00075 
00076     return true;
00077 }
00078 
00080 DEFINE_TESTCASE(sortfunctor2,writable && !remote) {
00081     Xapian::WritableDatabase db = get_writable_database();
00082     Xapian::Document doc;
00083     doc.add_term("foo");
00084     doc.add_value(0, "ABB");
00085     db.add_document(doc);
00086     doc.add_value(0, "ABC");
00087     db.add_document(doc);
00088     doc.add_value(0, string("ABC", 4));
00089     db.add_document(doc);
00090     doc.add_value(0, "ABCD");
00091     db.add_document(doc);
00092     doc.add_value(0, "ABC\xff");
00093     db.add_document(doc);
00094 
00095     Xapian::Enquire enquire(db);
00096     enquire.set_query(Xapian::Query("foo"));
00097 
00098     {
00099         Xapian::MultiValueSorter sorter;
00100         sorter.add(0);
00101         enquire.set_sort_by_key(&sorter);
00102         Xapian::MSet mset = enquire.get_mset(0, 10);
00103         mset_expect_order(mset, 5, 4, 3, 2, 1);
00104     }
00105 
00106     {
00107         Xapian::MultiValueSorter sorter;
00108         sorter.add(0, false);
00109         enquire.set_sort_by_key(&sorter);
00110         Xapian::MSet mset = enquire.get_mset(0, 10);
00111         mset_expect_order(mset, 1, 2, 3, 4, 5);
00112     }
00113 
00114     {
00115         Xapian::MultiValueSorter sorter;
00116         sorter.add(0);
00117         sorter.add(1);
00118         enquire.set_sort_by_key(&sorter);
00119         Xapian::MSet mset = enquire.get_mset(0, 10);
00120         mset_expect_order(mset, 5, 4, 3, 2, 1);
00121     }
00122 
00123     {
00124         Xapian::MultiValueSorter sorter;
00125         sorter.add(0, false);
00126         sorter.add(1);
00127         enquire.set_sort_by_key(&sorter);
00128         Xapian::MSet mset = enquire.get_mset(0, 10);
00129         mset_expect_order(mset, 1, 2, 3, 4, 5);
00130     }
00131 
00132     {
00133         Xapian::MultiValueSorter sorter;
00134         sorter.add(0);
00135         sorter.add(1, false);
00136         enquire.set_sort_by_key(&sorter);
00137         Xapian::MSet mset = enquire.get_mset(0, 10);
00138         mset_expect_order(mset, 5, 4, 3, 2, 1);
00139     }
00140 
00141     {
00142         Xapian::MultiValueSorter sorter;
00143         sorter.add(0, false);
00144         sorter.add(1, false);
00145         enquire.set_sort_by_key(&sorter);
00146         Xapian::MSet mset = enquire.get_mset(0, 10);
00147         mset_expect_order(mset, 1, 2, 3, 4, 5);
00148     }
00149 
00150     return true;
00151 }
00152 
00153 class NeverUseMeSorter : public Xapian::Sorter {
00154   public:
00155     std::string operator() (const Xapian::Document &) const
00156     {
00157         FAIL_TEST("NeverUseMeSorter was called");
00158     }
00159 };
00160 
00162 DEFINE_TESTCASE(changesorter1, backend) {
00163     Xapian::Enquire enquire(get_database("apitest_simpledata"));
00164     enquire.set_query(Xapian::Query("word"));
00165     NeverUseMeSorter sorter;
00166 
00167     enquire.set_sort_by_key(&sorter);
00168     enquire.set_sort_by_value(0);
00169     Xapian::MSet mset = enquire.get_mset(0, 25);
00170     TEST_EQUAL(mset.size(), 2); // Check that search is still doing something.
00171 
00172     enquire.set_sort_by_key(&sorter);
00173     enquire.set_sort_by_value_then_relevance(0);
00174     mset = enquire.get_mset(0, 25);
00175     TEST_EQUAL(mset.size(), 2); // Check that search is still doing something.
00176 
00177     enquire.set_sort_by_key(&sorter);
00178     enquire.set_sort_by_relevance_then_value(0);
00179     mset = enquire.get_mset(0, 25);
00180     TEST_EQUAL(mset.size(), 2); // Check that search is still doing something.
00181 
00182     enquire.set_sort_by_key(&sorter);
00183     enquire.set_sort_by_relevance();
00184     mset = enquire.get_mset(0, 25);
00185     TEST_EQUAL(mset.size(), 2); // Check that search is still doing something.
00186 
00187     return true;
00188 }
00189 
00191 DEFINE_TESTCASE(sortfunctorempty1,backend && !remote) {
00192     Xapian::Enquire enquire(get_database("apitest_sortrel"));
00193     enquire.set_query(Xapian::Query("woman"));
00194 
00195     {
00196         int i;
00197         Xapian::MultiValueSorter sorter(&i, &i);
00198 
00199         enquire.set_sort_by_key(&sorter);
00200         Xapian::MSet mset = enquire.get_mset(0, 10);
00201         mset_expect_order(mset, 1, 2, 3, 4, 5, 6, 7, 8, 9);
00202     }
00203 
00204     return true;
00205 }

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