• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

textsearch.test.h

Go to the documentation of this file.
00001 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
00002 /*
00003  * popcon test
00004  *
00005  * Copyright (C) 2007  Enrico Zini <enrico@debian.org>
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 #include <ept/test.h>
00023 #include <ept/textsearch/textsearch.h>
00024 #include <ept/textsearch/maint/path.h>
00025 #include <ept/apt/apt.h>
00026 #include <wibble/sys/fs.h>
00027 #include <set>
00028 
00029 namespace ept {
00030 namespace textsearch {
00031 extern size_t max_index;
00032 }
00033 }
00034 
00035 using namespace std;
00036 using namespace ept;
00037 using namespace ept::textsearch;
00038 using namespace ept::apt;
00039 
00040 struct DirMaker
00041 {
00042     DirMaker(const std::string& name)
00043     {
00044         wibble::sys::fs::mkdirIfMissing(name, 0755);
00045     }
00046 };
00047 
00048 struct TestTextsearch : AptTestEnvironment
00049 {
00050     DirMaker md;
00051     Path::OverrideIndexDir oid;
00052     Apt apt;
00053     TextSearch textsearch;
00054 
00055     TestTextsearch()
00056         : md( TEST_ENV_DIR "xapian"), oid( TEST_ENV_DIR "xapian")
00057     {
00058         try {
00059             ept::textsearch::max_index = 1000;
00060             textsearch.rebuildIfNeeded(apt);
00061         } catch (Xapian::Error& e) {
00062             cerr << e.get_type() << " " << e.get_msg() << " " << e.get_context() << endl;
00063             throw;
00064         }
00065     }
00066 
00067 // Access an empty index
00068     Test empty()
00069     {
00070         Path::OverrideIndexDir oid("./empty");
00071         TextSearch empty;
00072         assert_eq(empty.timestamp(), 0);
00073         assert(!empty.hasData());
00074         assert(empty.needsRebuild(apt));
00075         /*
00076           Xapian::Enquire enq(empty.db());
00077           empty.search(enq, "apt");
00078           Xapian::MSet matches = enq.get_mset(0, 100);
00079           assert_eq(matches.size(), 0u);
00080         */
00081     }
00082 
00083 // Very basic access
00084     Test basicAccess()
00085     {
00086         assert(textsearch.hasData());
00087         assert(textsearch.timestamp() > 0);
00088         assert(!textsearch.needsRebuild(apt));
00089 
00090         Xapian::Enquire enq(textsearch.db());
00091         enq.set_query(textsearch.makeORQuery("sgml"));
00092         Xapian::MSet matches = enq.get_mset(0, 100);
00093         assert(matches.size() > 0);
00094 
00095         // See if the apt package is among the results
00096         set<string> results;
00097         for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
00098             results.insert(i.get_document().get_data());
00099         assert(results.find("sp") != results.end());
00100     }
00101 
00102 // Alternate access using intermediate Xapian::Query objects
00103     Test queryAccess()
00104     {
00105         Xapian::Enquire enq(textsearch.db());
00106         enq.set_query(textsearch.makeORQuery("sgml"));
00107         Xapian::MSet matches = enq.get_mset(0, 100);
00108         assert(matches.size() > 0);
00109 
00110         // See if the apt package is among the results
00111         set<string> results;
00112         for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
00113             results.insert(i.get_document().get_data());
00114         assert(results.find("sp") != results.end());
00115     }
00116 
00117 // Try makePartialORQuery
00118     Test partialOrQuery()
00119     {
00120         Xapian::Enquire enq(textsearch.db());
00121         enq.set_query(textsearch.makePartialORQuery("sgml"));
00122         Xapian::MSet matches = enq.get_mset(0, 100);
00123         assert(matches.size() > 0);
00124 
00125         // See if the apt package is among the results
00126         set<string> results;
00127         for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
00128             results.insert(i.get_document().get_data());
00129         assert(results.find("sp") != results.end());
00130     }
00131 
00132 // Try docidByName
00133     Test docidByName()
00134     {
00135         assert(textsearch.docidByName("sp") != 0);
00136         assert_eq(textsearch.docidByName("thereisnopackagewiththisname"), 0u);
00137     }
00138 
00139 // Access values
00140     Test values()
00141     {
00142         assert(textsearch.hasData());
00143         assert(textsearch.timestamp() > 0);
00144         assert(!textsearch.needsRebuild(apt));
00145 
00146         double dval;
00147         dval = textsearch.getDoubleValue("autoconf", VAL_APT_INSTALLED_SIZE);
00148         assert(dval == 2408);
00149         dval = textsearch.getDoubleValue("autoconf", VAL_APT_PACKAGE_SIZE);
00150         assert(dval == 741486);
00151         assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0.0);
00152         assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0.0);
00153 
00154         int val;
00155         val = textsearch.getIntValue("autoconf", VAL_APT_INSTALLED_SIZE);
00156         assert(val == 2408);
00157         val = textsearch.getIntValue("autoconf", VAL_APT_PACKAGE_SIZE);
00158         assert(val == 741486);
00159         cout << val;
00160         assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0);
00161         assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0);
00162     }
00163 
00164 };
00165 
00166 // vim:set ts=4 sw=4:

Generated on Thu Aug 18 2016 17:48:34 for libept by  doxygen 1.7.1