tests/harness/testutils.cc

Go to the documentation of this file.
00001 /* testutils.cc: Xapian-specific test helper functions.
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2003,2004,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (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
00019  * USA
00020  */
00021 
00022 #include <config.h>
00023 
00024 #include "testutils.h"
00025 
00026 #include <fstream>
00027 #include <vector>
00028 
00029 using namespace std;
00030 
00031 ostream &
00032 operator<<(ostream &os, const vector<unsigned int> &ints)
00033 {
00034     copy(ints.begin(), ints.end(),
00035          ostream_iterator<unsigned int>(os, ", "));
00036     return os;
00037 }
00038 
00039 // ######################################################################
00040 // Useful comparison operators
00041 
00042 bool
00043 mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1,
00044                    const Xapian::MSet &mset2, unsigned int first2,
00045                    unsigned int count)
00046 {
00047     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
00048                      "mset1 is too small: expected at least " <<
00049                      (first1 + count - 1) << " items, got " <<
00050                      mset1.size() << ".");
00051 
00052     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
00053                      "mset2 is too small: expected at least " <<
00054                      (first2 + count - 1) << " items, got " <<
00055                      mset2.size() << ".");
00056 
00057     Xapian::MSetIterator i = mset1[first1];
00058     Xapian::MSetIterator j = mset2[first2];
00059 
00060     for (unsigned int l = 0; l < count; ++l) {
00061         if (*i != *j || i.get_weight() != j.get_weight()) {
00062             return false;
00063         }
00064         ++i;
00065         ++j;
00066     }
00067     return true;
00068 }
00069 
00070 bool
00071 mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1,
00072                            const Xapian::MSet &mset2, unsigned int first2,
00073                            unsigned int count)
00074 {
00075     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
00076                      "mset1 is too small: expected at least " <<
00077                      (first1 + count - 1) << " items, got " <<
00078                      mset1.size() << ".");
00079 
00080     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
00081                      "mset2 is too small: expected at least " <<
00082                      (first2 + count - 1) << " items, got " <<
00083                      mset2.size() << ".");
00084 
00085     Xapian::MSetIterator i = mset1[first1];
00086     Xapian::MSetIterator j = mset2[first2];
00087 
00088     for (unsigned int l = 0; l < count; ++l) {
00089         if (i.get_weight() != j.get_weight()) {
00090             return false;
00091         }
00092         ++i;
00093         ++j;
00094     }
00095     return true;
00096 }
00097 
00098 bool
00099 mset_range_is_same_percents(const Xapian::MSet &mset1, unsigned int first1,
00100                             const Xapian::MSet &mset2, unsigned int first2,
00101                             unsigned int count)
00102 {
00103     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
00104                      "mset1 is too small: expected at least " <<
00105                      (first1 + count - 1) << " items, got " <<
00106                      mset1.size() << ".");
00107 
00108     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
00109                      "mset2 is too small: expected at least " <<
00110                      (first2 + count - 1) << " items, got " <<
00111                      mset2.size() << ".");
00112 
00113     Xapian::MSetIterator i = mset1[first1];
00114     Xapian::MSetIterator j = mset2[first2];
00115 
00116     for (unsigned int l = 0; l < count; ++l) {
00117         if (i.get_percent() != j.get_percent()) {
00118             return false;
00119         }
00120         ++i;
00121         ++j;
00122     }
00123     return true;
00124 }
00125 
00126 bool operator==(const Xapian::MSet &first, const Xapian::MSet &second)
00127 {
00128     if ((first.get_matches_lower_bound() != second.get_matches_lower_bound()) ||
00129         (first.get_matches_upper_bound() != second.get_matches_upper_bound()) ||
00130         (first.get_matches_estimated() != second.get_matches_estimated()) ||
00131         (first.get_max_possible() != second.get_max_possible()) ||
00132         (first.size() != second.size())) {
00133         return false;
00134     }
00135     if (first.empty()) return true;
00136     return mset_range_is_same(first, 0, second, 0, first.size());
00137 }
00138 
00139 static void
00140 mset_expect_order_(const Xapian::MSet &A, bool beginning,
00141                    Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
00142                    Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
00143                    Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
00144 {
00145     vector<Xapian::docid> expect;
00146     if (d1) {
00147         expect.push_back(d1);
00148         if (d2) {
00149             expect.push_back(d2);
00150             if (d3) {
00151                 expect.push_back(d3);
00152                 if (d4) {
00153                     expect.push_back(d4);
00154                     if (d5) {
00155                         expect.push_back(d5);
00156                         if (d6) {
00157                             expect.push_back(d6);
00158                             if (d7) {
00159                                 expect.push_back(d7);
00160                                 if (d8) {
00161                                     expect.push_back(d8);
00162                                     if (d9) {
00163                                         expect.push_back(d9);
00164                                         if (d10) {
00165                                             expect.push_back(d10);
00166                                             if (d11) {
00167                                                 expect.push_back(d11);
00168                                                 if (d12) {
00169                                                     expect.push_back(d12);
00170                                                 }
00171                                             }
00172                                         }
00173                                     }
00174                                 }
00175                             }
00176                         }
00177                     }
00178                 }
00179             }
00180         }
00181     }
00182     // Wheeee!
00183 
00184     if (beginning) {
00185         TEST_AND_EXPLAIN(A.size() >= expect.size(),
00186                          "Mset is of wrong size (" << A.size()
00187                          << " < " << expect.size() << "):\n"
00188                          << "Full mset was: " << A << endl
00189                          << "Expected order to start: {" << expect << "}");
00190     } else {
00191         TEST_AND_EXPLAIN(A.size() == expect.size(),
00192                          "Mset is of wrong size (" << A.size()
00193                          << " != " << expect.size() << "):\n"
00194                          << "Full mset was: " << A << endl
00195                          << "Expected order: {" << expect << "}");
00196     }
00197 
00198     Xapian::MSetIterator j = A.begin();
00199     for (size_t i = 0; i < expect.size(); i++, j++) {
00200         TEST_AND_EXPLAIN(*j == expect[i],
00201                          "Mset didn't contain expected result:\n"
00202                          << "Item " << i << " was " << *j
00203                          << ", expected " << expect[i] << endl
00204                          << "Full mset was: " << A << endl
00205                          << "Expected: {" << expect << "}");    
00206     }
00207 }
00208 
00209 void
00210 mset_expect_order(const Xapian::MSet &A,
00211                   Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
00212                   Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
00213                   Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
00214 {
00215     mset_expect_order_(A, false, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12);
00216 }
00217 
00218 void
00219 test_mset_order_equal(const Xapian::MSet &mset1, const Xapian::MSet &mset2)
00220 {
00221     TEST_AND_EXPLAIN(mset1.size() == mset2.size(),
00222                      "Msets not the same size - "
00223                      << mset1.size() << " != " << mset2.size());
00224     Xapian::MSetIterator i = mset1.begin();
00225     Xapian::MSetIterator j = mset2.begin();
00226     for (; i != mset1.end(); i++, j++) {
00227         TEST_AND_EXPLAIN(*i == *j,
00228                          "Msets have different contents -\n" <<
00229                          mset1 << "\n !=\n" << mset2);
00230     }
00231 }

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