LCOV - code coverage report
Current view: top level - harness - testutils.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 61 92 66.3 %
Date: 2011-08-21 Functions: 5 8 62.5 %
Branches: 48 102 47.1 %

           Branch data     Line data    Source code
       1                 :            : /* testutils.cc: Xapian-specific test helper functions.
       2                 :            :  *
       3                 :            :  * Copyright 1999,2000,2001 BrightStation PLC
       4                 :            :  * Copyright 2003,2004,2007,2008,2009 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "testutils.h"
      25                 :            : 
      26                 :            : #include "testsuite.h"
      27                 :            : 
      28                 :            : #include <fstream>
      29                 :            : #include <vector>
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33                 :            : ostream &
      34                 :          0 : operator<<(ostream &os, const vector<unsigned int> &ints)
      35                 :            : {
      36                 :            :     copy(ints.begin(), ints.end(),
      37                 :          0 :          ostream_iterator<unsigned int>(os, ", "));
      38                 :          0 :     return os;
      39                 :            : }
      40                 :            : 
      41                 :            : // ######################################################################
      42                 :            : // Useful comparison operators
      43                 :            : 
      44                 :            : bool
      45                 :        541 : mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1,
      46                 :            :                    const Xapian::MSet &mset2, unsigned int first2,
      47                 :            :                    unsigned int count)
      48                 :            : {
      49 [ -  + ][ #  # ]:        541 :     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
      50                 :            :                      "mset1 is too small: expected at least " <<
      51                 :            :                      (first1 + count - 1) << " items, got " <<
      52                 :            :                      mset1.size() << ".");
      53                 :            : 
      54 [ -  + ][ #  # ]:        541 :     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
      55                 :            :                      "mset2 is too small: expected at least " <<
      56                 :            :                      (first2 + count - 1) << " items, got " <<
      57                 :            :                      mset2.size() << ".");
      58                 :            : 
      59                 :        541 :     Xapian::MSetIterator i = mset1[first1];
      60                 :        541 :     Xapian::MSetIterator j = mset2[first2];
      61                 :            : 
      62         [ +  + ]:       4104 :     for (unsigned int l = 0; l < count; ++l) {
      63         [ -  + ]:       3563 :         if (*i != *j) {
      64                 :            :             tout << "docids differ at item " << (l + 1) << " in range: "
      65                 :          0 :                     << *i << " != " << *j << "\n";
      66                 :          0 :             return false;
      67                 :            :         }
      68                 :            :         // FIXME: don't use internal macro here...
      69         [ -  + ]:       3563 :         if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
      70                 :            :             tout << "weights differ at item " << (l + 1) << " in range: "
      71                 :          0 :                     << i.get_weight() << " != " << j.get_weight() << "\n";
      72                 :          0 :             return false;
      73                 :            :         }
      74                 :       3563 :         ++i;
      75                 :       3563 :         ++j;
      76                 :            :     }
      77                 :        541 :     return true;
      78                 :            : }
      79                 :            : 
      80                 :            : bool
      81                 :         63 : mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1,
      82                 :            :                            const Xapian::MSet &mset2, unsigned int first2,
      83                 :            :                            unsigned int count)
      84                 :            : {
      85 [ -  + ][ #  # ]:         63 :     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
      86                 :            :                      "mset1 is too small: expected at least " <<
      87                 :            :                      (first1 + count - 1) << " items, got " <<
      88                 :            :                      mset1.size() << ".");
      89                 :            : 
      90 [ -  + ][ #  # ]:         63 :     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
      91                 :            :                      "mset2 is too small: expected at least " <<
      92                 :            :                      (first2 + count - 1) << " items, got " <<
      93                 :            :                      mset2.size() << ".");
      94                 :            : 
      95                 :         63 :     Xapian::MSetIterator i = mset1[first1];
      96                 :         63 :     Xapian::MSetIterator j = mset2[first2];
      97                 :            : 
      98         [ +  + ]:        212 :     for (unsigned int l = 0; l < count; ++l) {
      99                 :            :         // FIXME: don't use internal macro here...
     100         [ -  + ]:        149 :         if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
     101                 :            :             tout << "weights differ at item " << (l + 1) << " in range: "
     102                 :          0 :                     << i.get_weight() << " != " << j.get_weight() << "\n";
     103                 :          0 :             return false;
     104                 :            :         }
     105                 :        149 :         ++i;
     106                 :        149 :         ++j;
     107                 :            :     }
     108                 :         63 :     return true;
     109                 :            : }
     110                 :            : 
     111                 :            : bool
     112                 :          0 : mset_range_is_same_percents(const Xapian::MSet &mset1, unsigned int first1,
     113                 :            :                             const Xapian::MSet &mset2, unsigned int first2,
     114                 :            :                             unsigned int count)
     115                 :            : {
     116 [ #  # ][ #  # ]:          0 :     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
     117                 :            :                      "mset1 is too small: expected at least " <<
     118                 :            :                      (first1 + count - 1) << " items, got " <<
     119                 :            :                      mset1.size() << ".");
     120                 :            : 
     121 [ #  # ][ #  # ]:          0 :     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
     122                 :            :                      "mset2 is too small: expected at least " <<
     123                 :            :                      (first2 + count - 1) << " items, got " <<
     124                 :            :                      mset2.size() << ".");
     125                 :            : 
     126                 :          0 :     Xapian::MSetIterator i = mset1[first1];
     127                 :          0 :     Xapian::MSetIterator j = mset2[first2];
     128                 :            : 
     129         [ #  # ]:          0 :     for (unsigned int l = 0; l < count; ++l) {
     130         [ #  # ]:          0 :         if (i.get_percent() != j.get_percent()) {
     131                 :          0 :             tout << i.get_percent() << "% != " << j.get_percent() << "%\n";
     132                 :          0 :             return false;
     133                 :            :         }
     134                 :          0 :         ++i;
     135                 :          0 :         ++j;
     136                 :            :     }
     137                 :          0 :     return true;
     138                 :            : }
     139                 :            : 
     140                 :        116 : bool operator==(const Xapian::MSet &first, const Xapian::MSet &second)
     141                 :            : {
     142 [ +  - ][ +  - ]:        116 :     if ((first.get_matches_lower_bound() != second.get_matches_lower_bound()) ||
         [ +  - ][ +  + ]
         [ -  + ][ +  + ]
     143                 :            :         (first.get_matches_upper_bound() != second.get_matches_upper_bound()) ||
     144                 :            :         (first.get_matches_estimated() != second.get_matches_estimated()) ||
     145                 :            :         (first.get_max_possible() != second.get_max_possible()) ||
     146                 :            :         (first.size() != second.size())) {
     147                 :         40 :         return false;
     148                 :            :     }
     149         [ -  + ]:         76 :     if (first.empty()) return true;
     150                 :        116 :     return mset_range_is_same(first, 0, second, 0, first.size());
     151                 :            : }
     152                 :            : 
     153                 :            : static void
     154                 :        941 : mset_expect_order_(const Xapian::MSet &A, bool beginning,
     155                 :            :                    Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
     156                 :            :                    Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
     157                 :            :                    Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
     158                 :            : {
     159                 :        941 :     vector<Xapian::docid> expect;
     160         [ +  + ]:        941 :     if (d1) {
     161                 :        837 :         expect.push_back(d1);
     162         [ +  + ]:        837 :         if (d2) {
     163                 :        596 :             expect.push_back(d2);
     164         [ +  + ]:        596 :             if (d3) {
     165                 :        412 :                 expect.push_back(d3);
     166         [ +  + ]:        412 :                 if (d4) {
     167                 :        379 :                     expect.push_back(d4);
     168         [ +  + ]:        379 :                     if (d5) {
     169                 :        339 :                         expect.push_back(d5);
     170         [ +  + ]:        339 :                         if (d6) {
     171                 :        187 :                             expect.push_back(d6);
     172         [ +  + ]:        187 :                             if (d7) {
     173                 :        174 :                                 expect.push_back(d7);
     174         [ +  + ]:        174 :                                 if (d8) {
     175                 :        161 :                                     expect.push_back(d8);
     176         [ +  + ]:        161 :                                     if (d9) {
     177                 :        148 :                                         expect.push_back(d9);
     178         [ +  + ]:        148 :                                         if (d10) {
     179                 :         50 :                                             expect.push_back(d10);
     180         [ +  + ]:         50 :                                             if (d11) {
     181                 :         33 :                                                 expect.push_back(d11);
     182         [ -  + ]:         33 :                                                 if (d12) {
     183                 :          0 :                                                     expect.push_back(d12);
     184                 :            :                                                 }
     185                 :            :                                             }
     186                 :            :                                         }
     187                 :            :                                     }
     188                 :            :                                 }
     189                 :            :                             }
     190                 :            :                         }
     191                 :            :                     }
     192                 :            :                 }
     193                 :            :             }
     194                 :            :         }
     195                 :            :     }
     196                 :            :     // Wheeee!
     197                 :            : 
     198         [ -  + ]:        941 :     if (beginning) {
     199 [ #  # ][ #  # ]:          0 :         TEST_AND_EXPLAIN(A.size() >= expect.size(),
     200                 :            :                          "Mset is of wrong size (" << A.size()
     201                 :            :                          << " < " << expect.size() << "):\n"
     202                 :            :                          << "Full mset was: " << A << endl
     203                 :            :                          << "Expected order to start: {" << expect << "}");
     204                 :            :     } else {
     205 [ -  + ][ #  # ]:        941 :         TEST_AND_EXPLAIN(A.size() == expect.size(),
     206                 :            :                          "Mset is of wrong size (" << A.size()
     207                 :            :                          << " != " << expect.size() << "):\n"
     208                 :            :                          << "Full mset was: " << A << endl
     209                 :            :                          << "Expected order: {" << expect << "}");
     210                 :            :     }
     211                 :            : 
     212                 :        941 :     Xapian::MSetIterator j = A.begin();
     213         [ +  + ]:       4257 :     for (size_t i = 0; i < expect.size(); i++, j++) {
     214 [ -  + ][ #  # ]:       3316 :         TEST_AND_EXPLAIN(*j == expect[i],
     215                 :            :                          "Mset didn't contain expected result:\n"
     216                 :            :                          << "Item " << i << " was " << *j
     217                 :            :                          << ", expected " << expect[i] << endl
     218                 :            :                          << "Full mset was: " << A << endl
     219                 :            :                          << "Expected: {" << expect << "}");      
     220                 :        941 :     }
     221                 :        941 : }
     222                 :            : 
     223                 :            : void
     224                 :        941 : mset_expect_order(const Xapian::MSet &A,
     225                 :            :                   Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
     226                 :            :                   Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
     227                 :            :                   Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
     228                 :            : {
     229                 :        941 :     mset_expect_order_(A, false, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12);
     230                 :        941 : }
     231                 :            : 
     232                 :            : void
     233                 :          0 : test_mset_order_equal(const Xapian::MSet &mset1, const Xapian::MSet &mset2)
     234                 :            : {
     235 [ #  # ][ #  # ]:          0 :     TEST_AND_EXPLAIN(mset1.size() == mset2.size(),
     236                 :            :                      "Msets not the same size - "
     237                 :            :                      << mset1.size() << " != " << mset2.size());
     238                 :          0 :     Xapian::MSetIterator i = mset1.begin();
     239                 :          0 :     Xapian::MSetIterator j = mset2.begin();
     240         [ #  # ]:          0 :     for (; i != mset1.end(); i++, j++) {
     241 [ #  # ][ #  # ]:          0 :         TEST_AND_EXPLAIN(*i == *j,
     242                 :            :                          "Msets have different contents -\n" <<
     243                 :            :                          mset1 << "\n !=\n" << mset2);
     244                 :          0 :     }
     245                 :          0 : }

Generated by: LCOV version 1.8