LCOV - code coverage report
Current view: top level - tests/harness - testutils.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 65 87 74.7 %
Date: 2019-06-30 05:20:33 Functions: 5 7 71.4 %
Branches: 79 462 17.1 %

           Branch data     Line data    Source code
       1                 :            : /** @file testutils.cc
       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
       6                 :            :  *
       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.
      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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      20                 :            :  * USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <config.h>
      24                 :            : 
      25                 :            : #include "testutils.h"
      26                 :            : 
      27                 :            : #include "testsuite.h"
      28                 :            : 
      29                 :            : #include <fstream>
      30                 :            : #include <vector>
      31                 :            : 
      32                 :            : using namespace std;
      33                 :            : 
      34                 :            : ostream &
      35                 :          0 : operator<<(ostream &os, const vector<Xapian::docid> &ints)
      36                 :            : {
      37                 :            :     copy(ints.begin(), ints.end(),
      38         [ #  # ]:          0 :          ostream_iterator<Xapian::docid>(os, ", "));
      39                 :          0 :     return os;
      40                 :            : }
      41                 :            : 
      42                 :            : // ######################################################################
      43                 :            : // Useful comparison operators
      44                 :            : 
      45                 :            : bool
      46                 :        593 : mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1,
      47                 :            :                    const Xapian::MSet &mset2, unsigned int first2,
      48                 :            :                    unsigned int count)
      49                 :            : {
      50 [ +  - ][ -  + ]:        593 :     TEST_AND_EXPLAIN(mset1.size() >= first1 + count,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      51                 :            :                      "mset1 is too small: expected at least " <<
      52                 :            :                      (first1 + count) << " items, got " <<
      53                 :            :                      mset1.size());
      54                 :            : 
      55 [ +  - ][ -  + ]:        593 :     TEST_AND_EXPLAIN(mset2.size() >= first2 + count,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      56                 :            :                      "mset2 is too small: expected at least " <<
      57                 :            :                      (first2 + count) << " items, got " <<
      58                 :            :                      mset2.size());
      59                 :            : 
      60         [ +  - ]:        593 :     Xapian::MSetIterator i = mset1[first1];
      61         [ +  - ]:       1186 :     Xapian::MSetIterator j = mset2[first2];
      62                 :            : 
      63         [ +  + ]:       6373 :     for (unsigned int l = 0; l < count; ++l) {
      64 [ +  - ][ +  - ]:       5780 :         if (*i != *j) {
                 [ -  + ]
      65 [ #  # ][ #  # ]:          0 :             tout << "docids differ at item " << (l + 1) << " in range: "
                 [ #  # ]
      66 [ #  # ][ #  # ]:          0 :                     << *i << " != " << *j << "\n";
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      67                 :          0 :             return false;
      68                 :            :         }
      69                 :            :         // FIXME: don't use internal macro here...
      70 [ +  - ][ +  - ]:       5780 :         if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
         [ +  - ][ -  + ]
      71 [ #  # ][ #  # ]:          0 :             tout << "weights differ at item " << (l + 1) << " in range: "
                 [ #  # ]
      72 [ #  # ][ #  # ]:          0 :                     << i.get_weight() << " != " << j.get_weight() << "\n";
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      73                 :          0 :             return false;
      74                 :            :         }
      75                 :       5780 :         ++i;
      76                 :       5780 :         ++j;
      77                 :            :     }
      78                 :       1186 :     return true;
      79                 :            : }
      80                 :            : 
      81                 :            : bool
      82                 :         24 : mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1,
      83                 :            :                            const Xapian::MSet &mset2, unsigned int first2,
      84                 :            :                            unsigned int count)
      85                 :            : {
      86 [ +  - ][ -  + ]:         24 :     TEST_AND_EXPLAIN(mset1.size() >= first1 + count - 1,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      87                 :            :                      "mset1 is too small: expected at least " <<
      88                 :            :                      (first1 + count - 1) << " items, got " <<
      89                 :            :                      mset1.size() << ".");
      90                 :            : 
      91 [ +  - ][ -  + ]:         24 :     TEST_AND_EXPLAIN(mset2.size() >= first2 + count - 1,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      92                 :            :                      "mset2 is too small: expected at least " <<
      93                 :            :                      (first2 + count - 1) << " items, got " <<
      94                 :            :                      mset2.size() << ".");
      95                 :            : 
      96         [ +  - ]:         24 :     Xapian::MSetIterator i = mset1[first1];
      97         [ +  - ]:         48 :     Xapian::MSetIterator j = mset2[first2];
      98                 :            : 
      99         [ +  + ]:         72 :     for (unsigned int l = 0; l < count; ++l) {
     100                 :            :         // FIXME: don't use internal macro here...
     101 [ +  - ][ +  - ]:         48 :         if (!TEST_EQUAL_DOUBLE_(i.get_weight(), j.get_weight())) {
         [ +  - ][ -  + ]
     102 [ #  # ][ #  # ]:          0 :             tout << "weights differ at item " << (l + 1) << " in range: "
                 [ #  # ]
     103 [ #  # ][ #  # ]:          0 :                     << i.get_weight() << " != " << j.get_weight() << "\n";
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     104                 :          0 :             return false;
     105                 :            :         }
     106                 :         48 :         ++i;
     107                 :         48 :         ++j;
     108                 :            :     }
     109                 :         48 :     return true;
     110                 :            : }
     111                 :            : 
     112                 :         64 : bool operator==(const Xapian::MSet &first, const Xapian::MSet &second)
     113                 :            : {
     114 [ +  - ][ +  + ]:        192 :     if ((first.get_matches_lower_bound() != second.get_matches_lower_bound()) ||
     115         [ +  - ]:        128 :         (first.get_matches_upper_bound() != second.get_matches_upper_bound()) ||
     116         [ +  + ]:        128 :         (first.get_matches_estimated() != second.get_matches_estimated()) ||
     117 [ +  - ][ -  + ]:        192 :         (first.get_max_possible() != second.get_max_possible()) ||
     118                 :         40 :         (first.size() != second.size())) {
     119                 :         24 :         return false;
     120                 :            :     }
     121         [ -  + ]:         40 :     if (first.empty()) return true;
     122                 :         40 :     return mset_range_is_same(first, 0, second, 0, first.size());
     123                 :            : }
     124                 :            : 
     125                 :            : static void
     126                 :        743 : mset_expect_order_(const Xapian::MSet &A, bool beginning,
     127                 :            :                    Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
     128                 :            :                    Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
     129                 :            :                    Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
     130                 :            : {
     131                 :        743 :     vector<Xapian::docid> expect;
     132         [ +  + ]:        743 :     if (d1) {
     133         [ +  - ]:        687 :         expect.push_back(d1);
     134         [ +  + ]:        687 :         if (d2) {
     135         [ +  - ]:        535 :             expect.push_back(d2);
     136         [ +  + ]:        535 :             if (d3) {
     137         [ +  - ]:        312 :                 expect.push_back(d3);
     138         [ +  + ]:        312 :                 if (d4) {
     139         [ +  - ]:        260 :                     expect.push_back(d4);
     140         [ +  + ]:        260 :                     if (d5) {
     141         [ +  - ]:        231 :                         expect.push_back(d5);
     142         [ +  + ]:        231 :                         if (d6) {
     143         [ +  - ]:        135 :                             expect.push_back(d6);
     144         [ +  + ]:        135 :                             if (d7) {
     145         [ +  - ]:        114 :                                 expect.push_back(d7);
     146         [ +  + ]:        114 :                                 if (d8) {
     147         [ +  - ]:        107 :                                     expect.push_back(d8);
     148         [ +  + ]:        107 :                                     if (d9) {
     149         [ +  - ]:        100 :                                         expect.push_back(d9);
     150         [ +  + ]:        100 :                                         if (d10) {
     151         [ +  - ]:         30 :                                             expect.push_back(d10);
     152         [ +  + ]:         30 :                                             if (d11) {
     153         [ +  - ]:         19 :                                                 expect.push_back(d11);
     154         [ -  + ]:         19 :                                                 if (d12) {
     155         [ #  # ]:          0 :                                                     expect.push_back(d12);
     156                 :            :                                                 }
     157                 :            :                                             }
     158                 :            :                                         }
     159                 :            :                                     }
     160                 :            :                                 }
     161                 :            :                             }
     162                 :            :                         }
     163                 :            :                     }
     164                 :            :                 }
     165                 :            :             }
     166                 :            :         }
     167                 :            :     }
     168                 :            :     // Wheeee!
     169                 :            : 
     170         [ -  + ]:        743 :     if (beginning) {
     171 [ #  # ][ #  # ]:          0 :         TEST_AND_EXPLAIN(A.size() >= expect.size(),
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     172                 :            :                          "Mset is of wrong size (" << A.size()
     173                 :            :                          << " < " << expect.size() << "):\n"
     174                 :            :                          << "Full mset was: " << A << endl
     175                 :            :                          << "Expected order to start: {" << expect << "}");
     176                 :            :     } else {
     177 [ +  - ][ -  + ]:        743 :         TEST_AND_EXPLAIN(A.size() == expect.size(),
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     178                 :            :                          "Mset is of wrong size (" << A.size()
     179                 :            :                          << " != " << expect.size() << "):\n"
     180                 :            :                          << "Full mset was: " << A << endl
     181                 :            :                          << "Expected order: {" << expect << "}");
     182                 :            :     }
     183                 :            : 
     184         [ +  - ]:       1486 :     Xapian::MSetIterator j = A.begin();
     185 [ +  - ][ +  + ]:       3273 :     for (size_t i = 0; i < expect.size(); i++, j++) {
     186 [ +  - ][ -  + ]:       2530 :         TEST_AND_EXPLAIN(*j == expect[i],
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     187                 :            :                          "Mset didn't contain expected result:\n"
     188                 :            :                          << "Item " << i << " was " << *j
     189                 :            :                          << ", expected " << expect[i] << endl
     190                 :            :                          << "Full mset was: " << A << endl
     191                 :            :                          << "Expected: {" << expect << "}");
     192                 :        743 :     }
     193                 :        743 : }
     194                 :            : 
     195                 :            : void
     196                 :        743 : mset_expect_order(const Xapian::MSet &A,
     197                 :            :                   Xapian::docid d1, Xapian::docid d2, Xapian::docid d3, Xapian::docid d4,
     198                 :            :                   Xapian::docid d5, Xapian::docid d6, Xapian::docid d7, Xapian::docid d8,
     199                 :            :                   Xapian::docid d9, Xapian::docid d10, Xapian::docid d11, Xapian::docid d12)
     200                 :            : {
     201                 :        743 :     mset_expect_order_(A, false, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12);
     202                 :        743 : }
     203                 :            : 
     204                 :            : void
     205                 :          0 : test_mset_order_equal(const Xapian::MSet &mset1, const Xapian::MSet &mset2)
     206                 :            : {
     207 [ #  # ][ #  # ]:          0 :     TEST_AND_EXPLAIN(mset1.size() == mset2.size(),
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     208                 :            :                      "Msets not the same size - "
     209                 :            :                      << mset1.size() << " != " << mset2.size());
     210         [ #  # ]:          0 :     Xapian::MSetIterator i = mset1.begin();
     211         [ #  # ]:          0 :     Xapian::MSetIterator j = mset2.begin();
     212 [ #  # ][ #  # ]:          0 :     for (; i != mset1.end(); i++, j++) {
         [ #  # ][ #  # ]
     213 [ #  # ][ #  # ]:          0 :         TEST_AND_EXPLAIN(*i == *j,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     214                 :            :                          "Msets have different contents -\n" <<
     215                 :            :                          mset1 << "\n !=\n" << mset2);
     216                 :          0 :     }
     217                 :          0 : }

Generated by: LCOV version 1.11