Eliminate broken PostListCursor for non-const HoneyTable
[xapian.git] / xapian-core / tests / api_stem.cc
blob4d0c4cd62b00e3f3cfe2a3612dfd17f0811b0623
1 /** @file api_stem.cc
2 * @brief Test the stemming API
3 */
4 /* Copyright (C) 2010,2012 Olly Betts
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.
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.
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
21 #include <config.h>
23 #include "api_stem.h"
25 #include <xapian.h>
27 #include "apitest.h"
28 #include "testsuite.h"
29 #include "testutils.h"
31 using namespace std;
33 class MyStemImpl : public Xapian::StemImplementation {
34 string operator()(const string & word) {
35 if (word == "vanish")
36 return string();
37 return word.substr(0, 3);
40 string get_description() const {
41 return "MyStem()";
45 /// Test user stemming algorithms.
46 DEFINE_TESTCASE(stem1, !backend) {
47 Xapian::Stem st(new MyStemImpl);
49 TEST_EQUAL(st.get_description(), "Xapian::Stem(MyStem())");
50 TEST_EQUAL(st("a"), "a");
51 TEST_EQUAL(st("foo"), "foo");
52 TEST_EQUAL(st("food"), "foo");
54 return true;
57 /// New feature in 1.0.21/1.2.1 - "nb" and "nn" select the Norwegian stemmer.
58 DEFINE_TESTCASE(stem2, !backend) {
59 Xapian::Stem st_norwegian("norwegian");
60 TEST_EQUAL(st_norwegian.get_description(),
61 Xapian::Stem("nb").get_description());
62 TEST_EQUAL(st_norwegian.get_description(),
63 Xapian::Stem("nn").get_description());
64 TEST_EQUAL(st_norwegian.get_description(),
65 Xapian::Stem("no").get_description());
66 TEST_NOT_EQUAL(st_norwegian.get_description(),
67 Xapian::Stem("en").get_description());
68 TEST_NOT_EQUAL(st_norwegian.get_description(),
69 Xapian::Stem("none").get_description());
70 return true;
73 /// Test add a stemmer test
74 DEFINE_TESTCASE(stem3, !backend) {
75 Xapian::Stem earlyenglish("earlyenglish");
76 TEST_EQUAL(earlyenglish("loved"), "love");
77 TEST_EQUAL(earlyenglish("loving"), "love");
78 TEST_EQUAL(earlyenglish("loveth"), "love");
79 TEST_EQUAL(earlyenglish("givest"), "give");
80 return true;
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 DEFINE_TESTCASE(stemempty1, !backend) {
86 Xapian::Stem st(new MyStemImpl);
88 Xapian::TermGenerator tg;
89 Xapian::Document doc;
90 tg.set_document(doc);
91 tg.set_stemmer(st);
92 tg.set_stemming_strategy(tg.STEM_ALL);
94 tg.index_text("watch me vanish now");
95 auto i = doc.termlist_begin();
96 TEST(i != doc.termlist_end());
97 TEST_EQUAL(*i, "me");
98 TEST(++i != doc.termlist_end());
99 TEST_EQUAL(*i, "now");
100 TEST(++i != doc.termlist_end());
101 TEST_EQUAL(*i, "wat");
102 TEST(++i == doc.termlist_end());
104 return true;
107 /// Test invalid language names with various characters in.
108 DEFINE_TESTCASE(stemlangs2, !backend) {
109 string lang("xdummy");
110 for (unsigned ch = 0; ch <= 255; ++ch) {
111 lang[0] = ch;
112 TEST_EXCEPTION(Xapian::InvalidArgumentError, Xapian::Stem stem(lang));
114 return true;