libept
textsearch.test.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
2 /*
3  * popcon test
4  *
5  * Copyright (C) 2007 Enrico Zini <enrico@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 
22 #include <ept/test.h>
25 #include <ept/apt/apt.h>
26 #include <wibble/sys/fs.h>
27 #include <set>
28 
29 namespace ept {
30 namespace textsearch {
31 extern size_t max_index;
32 }
33 }
34 
35 using namespace std;
36 using namespace ept;
37 using namespace ept::textsearch;
38 using namespace ept::apt;
39 
40 struct DirMaker
41 {
42  DirMaker(const std::string& name)
43  {
44  wibble::sys::fs::mkdirIfMissing(name, 0755);
45  }
46 };
47 
49 {
54 
56  : md( TEST_ENV_DIR "xapian"), oid( TEST_ENV_DIR "xapian")
57  {
58  try {
60  textsearch.rebuildIfNeeded(apt);
61  } catch (Xapian::Error& e) {
62  cerr << e.get_type() << " " << e.get_msg() << " " << e.get_context() << endl;
63  throw;
64  }
65  }
66 
67 // Access an empty index
68  Test empty()
69  {
70  Path::OverrideIndexDir oid("./empty");
71  TextSearch empty;
72  assert_eq(empty.timestamp(), 0);
73  assert(!empty.hasData());
74  assert(empty.needsRebuild(apt));
75  /*
76  Xapian::Enquire enq(empty.db());
77  empty.search(enq, "apt");
78  Xapian::MSet matches = enq.get_mset(0, 100);
79  assert_eq(matches.size(), 0u);
80  */
81  }
82 
83 // Very basic access
84  Test basicAccess()
85  {
86  assert(textsearch.hasData());
87  assert(textsearch.timestamp() > 0);
88  assert(!textsearch.needsRebuild(apt));
89 
90  Xapian::Enquire enq(textsearch.db());
91  enq.set_query(textsearch.makeORQuery("sgml"));
92  Xapian::MSet matches = enq.get_mset(0, 100);
93  assert(matches.size() > 0);
94 
95  // See if the apt package is among the results
96  set<string> results;
97  for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
98  results.insert(i.get_document().get_data());
99  assert(results.find("sp") != results.end());
100  }
101 
102 // Alternate access using intermediate Xapian::Query objects
103  Test queryAccess()
104  {
105  Xapian::Enquire enq(textsearch.db());
106  enq.set_query(textsearch.makeORQuery("sgml"));
107  Xapian::MSet matches = enq.get_mset(0, 100);
108  assert(matches.size() > 0);
109 
110  // See if the apt package is among the results
111  set<string> results;
112  for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
113  results.insert(i.get_document().get_data());
114  assert(results.find("sp") != results.end());
115  }
116 
117 // Try makePartialORQuery
118  Test partialOrQuery()
119  {
120  Xapian::Enquire enq(textsearch.db());
121  enq.set_query(textsearch.makePartialORQuery("sgml"));
122  Xapian::MSet matches = enq.get_mset(0, 100);
123  assert(matches.size() > 0);
124 
125  // See if the apt package is among the results
126  set<string> results;
127  for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
128  results.insert(i.get_document().get_data());
129  assert(results.find("sp") != results.end());
130  }
131 
132 // Try docidByName
133  Test docidByName()
134  {
135  assert(textsearch.docidByName("sp") != 0);
136  assert_eq(textsearch.docidByName("thereisnopackagewiththisname"), 0u);
137  }
138 
139 // Access values
140  Test values()
141  {
142  assert(textsearch.hasData());
143  assert(textsearch.timestamp() > 0);
144  assert(!textsearch.needsRebuild(apt));
145 
146  double dval;
147  dval = textsearch.getDoubleValue("autoconf", VAL_APT_INSTALLED_SIZE);
148  assert(dval == 2408);
149  dval = textsearch.getDoubleValue("autoconf", VAL_APT_PACKAGE_SIZE);
150  assert(dval == 741486);
151  assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0.0);
152  assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0.0);
153 
154  int val;
155  val = textsearch.getIntValue("autoconf", VAL_APT_INSTALLED_SIZE);
156  assert(val == 2408);
157  val = textsearch.getIntValue("autoconf", VAL_APT_PACKAGE_SIZE);
158  assert(val == 741486);
159  cout << val;
160  assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0);
161  assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0);
162  }
163 
164 };
165 
166 // vim:set ts=4 sw=4: