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>
23 : #include <ept/textsearch/textsearch.h>
24 : #include <ept/textsearch/maint/path.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 6 : DirMaker(const std::string& name)
43 : {
44 6 : wibble::sys::fs::mkdirIfMissing(name, 0755);
45 6 : }
46 : };
47 :
48 : struct TestTextsearch : AptTestEnvironment
49 6 : {
50 : DirMaker md;
51 : Path::OverrideIndexDir oid;
52 : Apt apt;
53 : TextSearch textsearch;
54 :
55 6 : TestTextsearch()
56 6 : : md( TEST_ENV_DIR "xapian"), oid( TEST_ENV_DIR "xapian")
57 : {
58 : try {
59 6 : ept::textsearch::max_index = 1000;
60 6 : textsearch.rebuildIfNeeded(apt);
61 0 : } catch (Xapian::Error& e) {
62 0 : cerr << e.get_type() << " " << e.get_msg() << " " << e.get_context() << endl;
63 0 : throw;
64 : }
65 6 : }
66 :
67 : // Access an empty index
68 1 : Test empty()
69 : {
70 1 : Path::OverrideIndexDir oid("./empty");
71 1 : TextSearch empty;
72 1 : assert_eq(empty.timestamp(), 0);
73 2 : assert(!empty.hasData());
74 2 : 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 1 : }
82 :
83 : // Very basic access
84 1 : Test basicAccess()
85 : {
86 1 : assert(textsearch.hasData());
87 2 : assert(textsearch.timestamp() > 0);
88 2 : assert(!textsearch.needsRebuild(apt));
89 :
90 1 : Xapian::Enquire enq(textsearch.db());
91 2 : enq.set_query(textsearch.makeORQuery("sgml"));
92 1 : Xapian::MSet matches = enq.get_mset(0, 100);
93 1 : assert(matches.size() > 0);
94 :
95 : // See if the apt package is among the results
96 1 : set<string> results;
97 10 : for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
98 10 : results.insert(i.get_document().get_data());
99 1 : assert(results.find("sp") != results.end());
100 1 : }
101 :
102 : // Alternate access using intermediate Xapian::Query objects
103 1 : Test queryAccess()
104 : {
105 1 : Xapian::Enquire enq(textsearch.db());
106 1 : enq.set_query(textsearch.makeORQuery("sgml"));
107 1 : Xapian::MSet matches = enq.get_mset(0, 100);
108 1 : assert(matches.size() > 0);
109 :
110 : // See if the apt package is among the results
111 1 : set<string> results;
112 10 : for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
113 10 : results.insert(i.get_document().get_data());
114 1 : assert(results.find("sp") != results.end());
115 1 : }
116 :
117 : // Try makePartialORQuery
118 1 : Test partialOrQuery()
119 : {
120 1 : Xapian::Enquire enq(textsearch.db());
121 1 : enq.set_query(textsearch.makePartialORQuery("sgml"));
122 1 : Xapian::MSet matches = enq.get_mset(0, 100);
123 1 : assert(matches.size() > 0);
124 :
125 : // See if the apt package is among the results
126 1 : set<string> results;
127 10 : for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
128 10 : results.insert(i.get_document().get_data());
129 1 : assert(results.find("sp") != results.end());
130 1 : }
131 :
132 : // Try docidByName
133 1 : Test docidByName()
134 : {
135 1 : assert(textsearch.docidByName("sp") != 0);
136 2 : assert_eq(textsearch.docidByName("thereisnopackagewiththisname"), 0u);
137 1 : }
138 :
139 : // Access values
140 1 : Test values()
141 : {
142 1 : assert(textsearch.hasData());
143 2 : assert(textsearch.timestamp() > 0);
144 2 : assert(!textsearch.needsRebuild(apt));
145 :
146 : double dval;
147 2 : dval = textsearch.getDoubleValue("autoconf", VAL_APT_INSTALLED_SIZE);
148 2 : assert(dval == 2408);
149 2 : dval = textsearch.getDoubleValue("autoconf", VAL_APT_PACKAGE_SIZE);
150 2 : assert(dval == 741486);
151 2 : assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0.0);
152 2 : assert_eq(textsearch.getDoubleValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0.0);
153 :
154 : int val;
155 2 : val = textsearch.getIntValue("autoconf", VAL_APT_INSTALLED_SIZE);
156 2 : assert(val == 2408);
157 2 : val = textsearch.getIntValue("autoconf", VAL_APT_PACKAGE_SIZE);
158 2 : assert(val == 741486);
159 1 : cout << val;
160 2 : assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_INSTALLED_SIZE), 0);
161 2 : assert_eq(textsearch.getIntValue("thereisnopackagewiththisname", VAL_APT_PACKAGE_SIZE), 0);
162 1 : }
163 :
164 : };
165 :
166 : // vim:set ts=4 sw=4:
|