LCOV - code coverage report
Current view: top level - tests - api_stem.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 52 52 100.0 %
Date: 2019-02-17 14:59:59 Functions: 9 9 100.0 %
Branches: 102 678 15.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file api_stem.cc
       2                 :            :  * @brief Test the stemming API
       3                 :            :  */
       4                 :            : /* Copyright (C) 2010,2012 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_stem.h"
      24                 :            : 
      25                 :            : #include <xapian.h>
      26                 :            : 
      27                 :            : #include "apitest.h"
      28                 :            : #include "testsuite.h"
      29                 :            : #include "testutils.h"
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33         [ -  + ]:         12 : class MyStemImpl : public Xapian::StemImplementation {
      34                 :          7 :     string operator()(const string & word) {
      35         [ +  + ]:          7 :         if (word == "vanish")
      36                 :          1 :             return string();
      37                 :          6 :         return word.substr(0, 3);
      38                 :            :     }
      39                 :            : 
      40                 :          1 :     string get_description() const {
      41         [ +  - ]:          1 :         return "MyStem()";
      42                 :            :     }
      43                 :            : };
      44                 :            : 
      45                 :            : /// Test user stemming algorithms.
      46                 :          1 : DEFINE_TESTCASE(stem1, !backend) {
      47 [ +  - ][ +  - ]:          1 :     Xapian::Stem st(new MyStemImpl);
      48                 :            : 
      49 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st.get_description(), "Xapian::Stem(MyStem())");
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      50 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st("a"), "a");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      51 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st("foo"), "foo");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      52 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st("food"), "foo");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      53                 :            : 
      54                 :          1 :     return true;
      55                 :            : }
      56                 :            : 
      57                 :            : /// New feature in 1.0.21/1.2.1 - "nb" and "nn" select the Norwegian stemmer.
      58                 :          1 : DEFINE_TESTCASE(stem2, !backend) {
      59 [ +  - ][ +  - ]:          1 :     Xapian::Stem st_norwegian("norwegian");
      60 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st_norwegian.get_description(),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      61                 :            :                Xapian::Stem("nb").get_description());
      62 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st_norwegian.get_description(),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      63                 :            :                Xapian::Stem("nn").get_description());
      64 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(st_norwegian.get_description(),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      65                 :            :                Xapian::Stem("no").get_description());
      66 [ +  - ][ +  - ]:          1 :     TEST_NOT_EQUAL(st_norwegian.get_description(),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      67                 :            :                    Xapian::Stem("en").get_description());
      68 [ +  - ][ +  - ]:          1 :     TEST_NOT_EQUAL(st_norwegian.get_description(),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      69                 :            :                    Xapian::Stem("none").get_description());
      70                 :          1 :     return true;
      71                 :            : }
      72                 :            : 
      73                 :            : /// Test add a stemmer test
      74                 :          1 : DEFINE_TESTCASE(stem3, !backend) {
      75 [ +  - ][ +  - ]:          1 :     Xapian::Stem earlyenglish("earlyenglish");
      76 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(earlyenglish("loved"), "love");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      77 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(earlyenglish("loving"), "love");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      78 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(earlyenglish("loveth"), "love");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      79 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(earlyenglish("givest"), "give");
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      80                 :          1 :     return true;
      81                 :            : }
      82                 :            : 
      83                 :            : /// Test handling of a stemmer returning an empty string.
      84                 :            : // Regression test for https://trac.xapian.org/ticket/741 fixed in 1.4.2.
      85                 :          1 : DEFINE_TESTCASE(stemempty1, !backend) {
      86 [ +  - ][ +  - ]:          1 :     Xapian::Stem st(new MyStemImpl);
      87                 :            : 
      88         [ +  - ]:          2 :     Xapian::TermGenerator tg;
      89         [ +  - ]:          2 :     Xapian::Document doc;
      90         [ +  - ]:          1 :     tg.set_document(doc);
      91         [ +  - ]:          1 :     tg.set_stemmer(st);
      92         [ +  - ]:          1 :     tg.set_stemming_strategy(tg.STEM_ALL);
      93                 :            : 
      94 [ +  - ][ +  - ]:          1 :     tg.index_text("watch me vanish now");
                 [ +  - ]
      95         [ +  - ]:          2 :     auto i = doc.termlist_begin();
      96 [ -  + ][ #  # ]:          1 :     TEST(i != doc.termlist_end());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      97 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(*i, "me");
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      98 [ +  - ][ -  + ]:          1 :     TEST(++i != doc.termlist_end());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      99 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(*i, "now");
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     100 [ +  - ][ -  + ]:          1 :     TEST(++i != doc.termlist_end());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     101 [ +  - ][ +  - ]:          1 :     TEST_EQUAL(*i, "wat");
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     102 [ +  - ][ -  + ]:          1 :     TEST(++i == doc.termlist_end());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     103                 :            : 
     104                 :          1 :     return true;
     105                 :            : }
     106                 :            : 
     107                 :            : /// Test invalid language names with various characters in.
     108                 :          1 : DEFINE_TESTCASE(stemlangs2, !backend) {
     109         [ +  - ]:          1 :     string lang("xdummy");
     110         [ +  + ]:        257 :     for (unsigned ch = 0; ch <= 255; ++ch) {
     111         [ +  - ]:        256 :         lang[0] = char(ch);
     112 [ +  - ][ -  + ]:        256 :         TEST_EXCEPTION(Xapian::InvalidArgumentError, Xapian::Stem stem(lang));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ -  + ][ -  +  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
                 [ +  - ]
     113                 :            :     }
     114                 :          1 :     return true;
     115                 :            : }

Generated by: LCOV version 1.11