omegatest: Use test_scriptindex_error in another case
[xapian.git] / xapian-core / tests / harness / testutils.cc
blobd78af46db7b6ad0c92d0ca80d39604f04a35c156
1 /** @file
2 * @brief Xapian-specific test helper functions.
3 */
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2003,2004,2007,2008,2009,2015 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <config.h>
25 #include "testutils.h"
27 #include "testsuite.h"
29 #include <fstream>
30 #include <vector>
32 using namespace std;
34 ostream &
35 operator<<(ostream &os, const vector<Xapian::docid> &ints)
37 copy(ints.begin(), ints.end(),
38 ostream_iterator<Xapian::docid>(os, ", "));
39 return os;
42 // ######################################################################
43 // Useful comparison operators
45 bool
46 mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1,
47 const Xapian::MSet &mset2, unsigned int first2,
48 unsigned int count)
50 TEST_AND_EXPLAIN(mset1.size() >= first1 + count,
51 "mset1 is too small: expected at least " <<
52 (first1 + count) << " items, got " <<
53 mset1.size());
55 TEST_AND_EXPLAIN(mset2.size() >= first2 + count,
56 "mset2 is too small: expected at least " <<
57 (first2 + count) << " items, got " <<
58 mset2.size());
60 Xapian::MSetIterator i = mset1[first1];
61 Xapian::MSetIterator j = mset2[first2];
63 for (unsigned int l = 0; l < count; ++l) {
64 if (*i != *j) {
65 tout << "docids differ at item " << (l + 1) << " in range: "
66 << *i << " != " << *j << "\n";
67 return false;
69 // FIXME: don't use internal macro here...
70 if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
71 tout << "weights differ at item " << (l + 1) << " in range: "
72 << i.get_weight() << " != " << j.get_weight() << "\n";
73 return false;
75 ++i;
76 ++j;
78 return true;
81 bool
82 mset_range_is_same(const Xapian::MSet& mset, unsigned int first,
83 const pair<Xapian::docid, double> to_compare[],
84 unsigned int count)
86 TEST_AND_EXPLAIN(mset.size() >= first + count - 1,
87 "mset is too small: expected at least " <<
88 (first + count - 1) << " items, got " <<
89 mset.size() << ".");
91 Xapian::MSetIterator i = mset[first];
93 for (unsigned int l = 0; l < count; ++l) {
94 if (*i != to_compare[l].first) {
95 tout << "docids differ at item " << (l + 1) << " in range: "
96 << *i << " != " << to_compare[l].first << "\n";
97 return false;
99 // FIXME: don't use internal macro here...
100 if (!TEST_EQUAL_DOUBLE_(i.get_weight(), to_compare[l].second)) {
101 tout << "weights differ at item " << (l + 1) << " in range: "
102 << i.get_weight() << " != " << to_compare[l].second << "\n";
103 return false;
105 ++i;
107 return true;
110 bool
111 mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1,
112 const Xapian::MSet &mset2, unsigned int first2,
113 unsigned int count)
115 TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
116 "mset1 is too small: expected at least " <<
117 (first1 + count - 1) << " items, got " <<
118 mset1.size() << ".");
120 TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
121 "mset2 is too small: expected at least " <<
122 (first2 + count - 1) << " items, got " <<
123 mset2.size() << ".");
125 Xapian::MSetIterator i = mset1[first1];
126 Xapian::MSetIterator j = mset2[first2];
128 for (unsigned int l = 0; l < count; ++l) {
129 // FIXME: don't use internal macro here...
130 if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
131 tout << "weights differ at item " << (l + 1) << " in range: "
132 << i.get_weight() << " != " << j.get_weight() << "\n";
133 return false;
135 ++i;
136 ++j;
138 return true;
141 bool operator==(const Xapian::MSet &first, const Xapian::MSet &second)
143 if ((first.get_matches_lower_bound() != second.get_matches_lower_bound()) ||
144 (first.get_matches_upper_bound() != second.get_matches_upper_bound()) ||
145 (first.get_matches_estimated() != second.get_matches_estimated()) ||
146 (first.get_max_possible() != second.get_max_possible()) ||
147 (first.size() != second.size())) {
148 return false;
150 if (first.empty()) return true;
151 return mset_range_is_same(first, 0, second, 0, first.size());
154 static void
155 mset_expect_order_(const Xapian::MSet &A, bool beginning,
156 Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
157 Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
158 Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
160 vector<Xapian::docid> expect;
161 if (d1) {
162 expect.push_back(d1);
163 if (d2) {
164 expect.push_back(d2);
165 if (d3) {
166 expect.push_back(d3);
167 if (d4) {
168 expect.push_back(d4);
169 if (d5) {
170 expect.push_back(d5);
171 if (d6) {
172 expect.push_back(d6);
173 if (d7) {
174 expect.push_back(d7);
175 if (d8) {
176 expect.push_back(d8);
177 if (d9) {
178 expect.push_back(d9);
179 if (d10) {
180 expect.push_back(d10);
181 if (d11) {
182 expect.push_back(d11);
183 if (d12) {
184 expect.push_back(d12);
197 // Wheeee!
199 if (beginning) {
200 TEST_AND_EXPLAIN(A.size() >= expect.size(),
201 "Mset is of wrong size (" << A.size()
202 << " < " << expect.size() << "):\n"
203 << "Full mset was: " << A << endl
204 << "Expected order to start: {" << expect << "}");
205 } else {
206 TEST_AND_EXPLAIN(A.size() == expect.size(),
207 "Mset is of wrong size (" << A.size()
208 << " != " << expect.size() << "):\n"
209 << "Full mset was: " << A << endl
210 << "Expected order: {" << expect << "}");
213 Xapian::MSetIterator j = A.begin();
214 for (size_t i = 0; i < expect.size(); ++i, ++j) {
215 TEST_AND_EXPLAIN(*j == expect[i],
216 "Mset didn't contain expected result:\n"
217 << "Item " << i << " was " << *j
218 << ", expected " << expect[i] << endl
219 << "Full mset was: " << A << endl
220 << "Expected: {" << expect << "}");
224 void
225 mset_expect_order(const Xapian::MSet &A,
226 Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
227 Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
228 Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
230 mset_expect_order_(A, false, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12);
233 void
234 test_mset_order_equal(const Xapian::MSet &mset1, const Xapian::MSet &mset2)
236 TEST_AND_EXPLAIN(mset1.size() == mset2.size(),
237 "Msets not the same size - "
238 << mset1.size() << " != " << mset2.size());
239 Xapian::MSetIterator i = mset1.begin();
240 Xapian::MSetIterator j = mset2.begin();
241 for (; i != mset1.end(); ++i, ++j) {
242 TEST_AND_EXPLAIN(*i == *j,
243 "Msets have different contents -\n" <<
244 mset1 << "\n !=\n" << mset2);