LCOV - code coverage report
Current view: top level - tests - api_collapse.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 51 51 100.0 %
Date: 2019-06-30 05:20:33 Functions: 2 2 100.0 %
Branches: 89 252 35.3 %

           Branch data     Line data    Source code
       1                 :            : /** @file api_collapse.cc
       2                 :            :  * @brief Test collapsing during the match.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2009 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (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 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "api_collapse.h"
      24                 :            : 
      25                 :            : #include <xapian.h>
      26                 :            : 
      27                 :            : #include "apitest.h"
      28                 :            : #include "testutils.h"
      29                 :            : 
      30                 :            : using namespace std;
      31                 :            : 
      32                 :            : /// Simple test of collapsing with collapse_max > 1.
      33                 :          7 : DEFINE_TESTCASE(collapsekey5, backend) {
      34 [ +  - ][ +  - ]:          7 :     Xapian::Database db(get_database("apitest_simpledata"));
      35         [ +  - ]:         14 :     Xapian::Enquire enquire(db);
      36                 :            :     // "this" matches all documents.
      37 [ +  - ][ +  - ]:          7 :     enquire.set_query(Xapian::Query("this"));
                 [ +  - ]
      38                 :            : 
      39 [ +  - ][ +  - ]:         14 :     Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());
      40                 :            : 
      41         [ +  + ]:         77 :     for (Xapian::valueno slot = 0; slot < 10; ++slot) {
      42         [ +  - ]:         70 :         map<string, Xapian::doccount> tally;
      43 [ +  - ][ +  + ]:        490 :         for (Xapian::docid did = 1; did <= db.get_doccount(); ++did) {
      44 [ +  - ][ +  - ]:        420 :             ++tally[db.get_document(did).get_value(slot)];
                 [ +  - ]
      45                 :            :         }
      46                 :            : 
      47 [ +  - ][ +  + ]:        560 :         for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
      48 [ +  - ][ +  - ]:        490 :             tout << "Collapsing on slot " << slot << " max " << cmax << endl;
         [ +  - ][ +  - ]
                 [ +  - ]
      49         [ +  - ]:        490 :             enquire.set_collapse_key(slot, cmax);
      50 [ +  - ][ +  - ]:        490 :             Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
      51                 :            : 
      52                 :            :             // Check the collapse MSet size is as expected.
      53                 :        490 :             Xapian::doccount expect_size = 0;
      54                 :        490 :             map<string, Xapian::doccount>::const_iterator i;
      55         [ +  + ]:       2058 :             for (i = tally.begin(); i != tally.end(); ++i) {
      56 [ +  - ][ +  + ]:       1568 :                 if (i->first.empty() || i->second <= cmax) {
                 [ +  + ]
      57                 :       1372 :                     expect_size += i->second;
      58                 :            :                 } else {
      59                 :        196 :                     expect_size += cmax;
      60                 :            :                 }
      61                 :            :             }
      62 [ +  - ][ -  + ]:        490 :             TEST_EQUAL(mset.size(), expect_size);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      63                 :            : 
      64                 :            :             // Check that the right number of documents with each collapse key
      65                 :            :             // value are left after collapsing.
      66         [ +  - ]:        980 :             map<string, Xapian::doccount> seen;
      67 [ +  - ][ +  - ]:       3010 :             for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
                 [ +  + ]
      68         [ +  - ]:       2520 :                 const string & key = j.get_collapse_key();
      69 [ +  - ][ -  + ]:       2520 :                 TEST(tally.find(key) != tally.end());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      70         [ +  - ]:       2520 :                 ++seen[key];
      71                 :       3010 :             }
      72         [ +  + ]:       2058 :             for (i = tally.begin(); i != tally.end(); ++i) {
      73 [ +  - ][ +  + ]:       1568 :                 if (i->first.empty() || i->second <= cmax) {
                 [ +  + ]
      74 [ +  - ][ -  + ]:       1372 :                     TEST_EQUAL(seen[i->first], i->second);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      75                 :            :                 } else {
      76 [ +  - ][ -  + ]:        196 :                     TEST_EQUAL(seen[i->first], cmax);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      77                 :            :                 }
      78                 :            :             }
      79                 :        490 :         }
      80                 :         70 :     }
      81                 :            : 
      82                 :          7 :     return true;
      83                 :            : }
      84                 :            : 
      85                 :            : /// Test collapsing with a percentage cut-off.
      86                 :          7 : DEFINE_TESTCASE(collapsekey6, backend) {
      87 [ +  - ][ +  - ]:          7 :     Xapian::Database db(get_database("apitest_simpledata"));
      88         [ +  - ]:         14 :     Xapian::Enquire enquire(db);
      89                 :            :     // "this" matches all documents.
      90 [ +  - ][ +  - ]:          7 :     enquire.set_query(Xapian::Query("this"));
                 [ +  - ]
      91                 :            : 
      92 [ +  - ][ +  - ]:         14 :     Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());
      93                 :            : 
      94         [ +  + ]:         77 :     for (Xapian::valueno slot = 0; slot < 10; ++slot) {
      95 [ +  - ][ +  + ]:        560 :         for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
      96         [ +  + ]:      17640 :             for (int percent = 65; percent != 100; ++percent) {
      97 [ +  - ][ +  - ]:      17150 :                 tout << "Collapsing on slot " << slot << " max " << cmax
         [ +  - ][ +  - ]
      98 [ +  - ][ +  - ]:      17150 :                      << " cutoff " << percent << endl;
                 [ +  - ]
      99         [ +  - ]:      17150 :                 enquire.set_collapse_key(slot, cmax);
     100         [ +  - ]:      17150 :                 enquire.set_cutoff(percent);
     101 [ +  - ][ +  - ]:      17150 :                 Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
     102 [ +  - ][ +  - ]:      64554 :                 for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
                 [ +  + ]
     103 [ +  - ][ -  + ]:      47404 :                     TEST_REL(j.get_percent(), >=, percent);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     104                 :      17150 :                 }
     105                 :      17150 :             }
     106                 :            :         }
     107                 :            :     }
     108                 :            : 
     109                 :          7 :     return true;
     110                 :            : }

Generated by: LCOV version 1.11