e657050665f19c3104084253c1d1dec7d985c31b
[yaz-moved-to-github.git] / test / test_libstemmer.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <yaz/stemmer.h>
13 #include <yaz/test.h>
14
15 int test_stemmer_stem(yaz_stemmer_p stemmer, const char* to_stem, const char *expected) 
16 {
17     struct icu_buf_utf16 *src  = icu_buf_utf16_create(0);
18     struct icu_buf_utf16 *dst  = icu_buf_utf16_create(0);
19     struct icu_buf_utf8  *dst8 = icu_buf_utf8_create(0);
20
21     UErrorCode status; 
22     const char *result;
23     icu_utf16_from_utf8_cstr(src, to_stem, &status);
24     yaz_stemmer_stem(stemmer, dst, src, &status); 
25     /* Assume fail */
26     int rc = 0;
27     if (status == U_ZERO_ERROR) {
28         icu_utf16_to_utf8(dst8, dst, &status);
29         result = icu_buf_utf8_to_cstr(dst8);
30         rc = strcmp(result, expected) == 0;
31     }
32     icu_buf_utf8_destroy(dst8);
33     icu_buf_utf16_destroy(src);
34     icu_buf_utf16_destroy(dst);
35     return rc;
36 }
37
38
39
40 static void tst(void)
41 {
42     UErrorCode status;
43     //== U_ZERO_ERROR; 
44     yaz_stemmer_p stemmer = yaz_stemmer_create("en", "porter", &status);
45     YAZ_CHECK(stemmer); 
46
47     /* fail  */
48     YAZ_CHECK(test_stemmer_stem(stemmer, "beer", "water") == 0 ); 
49
50     /* Same */
51     YAZ_CHECK(test_stemmer_stem(stemmer, "adadwwr", "adadwwr")); 
52
53     /* Remove S */
54     YAZ_CHECK(test_stemmer_stem(stemmer, "beers", "beer")); 
55     YAZ_CHECK(test_stemmer_stem(stemmer, "persons", "person")); 
56
57     /* Remove s and ing  */
58     YAZ_CHECK(test_stemmer_stem(stemmer, "runs", "run")); 
59     YAZ_CHECK(test_stemmer_stem(stemmer, "running", "run")); 
60
61     yaz_stemmer_destroy(stemmer);
62 }
63
64 int main (int argc, char **argv)
65 {
66     YAZ_CHECK_INIT(argc, argv);
67     tst();
68     YAZ_CHECK_TERM;
69 }
70
71 /*
72  * Local variables:
73  * c-basic-offset: 4
74  * c-file-style: "Stroustrup"
75  * indent-tabs-mode: nil
76  * End:
77  * vim: shiftwidth=4 tabstop=8 expandtab
78  */
79